blob: cdd24399ec4980a135a7f142bc1facb9559e1379 [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;
126 mapper.populateDeviceInfo(&info);
127
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;
135 mapper.populateDeviceInfo(&info);
136
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
Chris Yea52ade12020-08-27 16:49:20 -0700258 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 InputMapper::populateDeviceInfo(deviceInfo);
260
261 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
262 deviceInfo->setKeyboardType(mKeyboardType);
263 }
264 }
265
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700266 std::list<NotifyArgs> configure(nsecs_t, const InputReaderConfiguration* config,
267 uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700268 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800270
271 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800272 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +0800273 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
274 mViewport = config->getDisplayViewportByPort(*displayPort);
275 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700276
277 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700278 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279 }
280
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700281 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700282 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700284 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700285 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800286 }
287
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700288 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700289 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800290 mLastEvent = *rawEvent;
291 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700292 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700293 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800294 }
295
Chris Yea52ade12020-08-27 16:49:20 -0700296 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800297 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
298 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
299 }
300
Philip Junker4af3b3d2021-12-14 10:36:55 +0100301 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
302 auto it = mKeyCodeMapping.find(locationKeyCode);
303 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
304 }
305
Chris Yea52ade12020-08-27 16:49:20 -0700306 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800307 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
308 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
309 }
310
Chris Yea52ade12020-08-27 16:49:20 -0700311 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800312 ssize_t index = mSwitchStates.indexOfKey(switchCode);
313 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
314 }
315
Chris Yea52ade12020-08-27 16:49:20 -0700316 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700317 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700318 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700319 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800320 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
321 if (keyCodes[i] == mSupportedKeyCodes[j]) {
322 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800323 }
324 }
325 }
Chris Yea52ade12020-08-27 16:49:20 -0700326 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 return result;
328 }
329
330 virtual int32_t getMetaState() {
331 return mMetaState;
332 }
333
334 virtual void fadePointer() {
335 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800336
337 virtual std::optional<int32_t> getAssociatedDisplay() {
338 if (mViewport) {
339 return std::make_optional(mViewport->displayId);
340 }
341 return std::nullopt;
342 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800343};
344
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700345// --- InputReaderPolicyTest ---
346class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700347protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700348 sp<FakeInputReaderPolicy> mFakePolicy;
349
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700350 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700351 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700352};
353
354/**
355 * Check that empty set of viewports is an acceptable configuration.
356 * Also try to get internal viewport two different ways - by type and by uniqueId.
357 *
358 * There will be confusion if two viewports with empty uniqueId and identical type are present.
359 * Such configuration is not currently allowed.
360 */
361TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700362 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700363
364 // We didn't add any viewports yet, so there shouldn't be any.
365 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100366 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700367 ASSERT_FALSE(internalViewport);
368
369 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000370 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000371 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700372
373 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700374 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700375 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100376 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700377
378 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100379 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700380 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700381 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700382
383 mFakePolicy->clearViewports();
384 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700385 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700386 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100387 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700388 ASSERT_FALSE(internalViewport);
389}
390
391TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
392 const std::string internalUniqueId = "local:0";
393 const std::string externalUniqueId = "local:1";
394 const std::string virtualUniqueId1 = "virtual:2";
395 const std::string virtualUniqueId2 = "virtual:3";
396 constexpr int32_t virtualDisplayId1 = 2;
397 constexpr int32_t virtualDisplayId2 = 3;
398
399 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000400 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000401 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000402 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700403 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000404 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000405 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000406 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700407 // Add an virtual viewport
408 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000409 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000410 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700411 // Add another virtual viewport
412 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000413 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000414 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700415
416 // Check matching by type for internal
417 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100418 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700419 ASSERT_TRUE(internalViewport);
420 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
421
422 // Check matching by type for external
423 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100424 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700425 ASSERT_TRUE(externalViewport);
426 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
427
428 // Check matching by uniqueId for virtual viewport #1
429 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700430 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700431 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100432 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700433 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
434 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
435
436 // Check matching by uniqueId for virtual viewport #2
437 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700438 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700439 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100440 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700441 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
442 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
443}
444
445
446/**
447 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
448 * that lookup works by checking display id.
449 * Check that 2 viewports of each kind is possible, for all existing viewport types.
450 */
451TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
452 const std::string uniqueId1 = "uniqueId1";
453 const std::string uniqueId2 = "uniqueId2";
454 constexpr int32_t displayId1 = 2;
455 constexpr int32_t displayId2 = 3;
456
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100457 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
458 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700459 for (const ViewportType& type : types) {
460 mFakePolicy->clearViewports();
461 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000462 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000463 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700464 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000465 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000466 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700467
468 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700469 std::optional<DisplayViewport> viewport1 =
470 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700471 ASSERT_TRUE(viewport1);
472 ASSERT_EQ(displayId1, viewport1->displayId);
473 ASSERT_EQ(type, viewport1->type);
474
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700475 std::optional<DisplayViewport> viewport2 =
476 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700477 ASSERT_TRUE(viewport2);
478 ASSERT_EQ(displayId2, viewport2->displayId);
479 ASSERT_EQ(type, viewport2->type);
480
481 // When there are multiple viewports of the same kind, and uniqueId is not specified
482 // in the call to getDisplayViewport, then that situation is not supported.
483 // The viewports can be stored in any order, so we cannot rely on the order, since that
484 // is just implementation detail.
485 // However, we can check that it still returns *a* viewport, we just cannot assert
486 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700487 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700488 ASSERT_TRUE(someViewport);
489 }
490}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800491
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700492/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000493 * When we have multiple internal displays make sure we always return the default display when
494 * querying by type.
495 */
496TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
497 const std::string uniqueId1 = "uniqueId1";
498 const std::string uniqueId2 = "uniqueId2";
499 constexpr int32_t nonDefaultDisplayId = 2;
500 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
501 "Test display ID should not be ADISPLAY_ID_DEFAULT");
502
503 // Add the default display first and ensure it gets returned.
504 mFakePolicy->clearViewports();
505 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000506 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000507 ViewportType::INTERNAL);
508 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000509 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000510 ViewportType::INTERNAL);
511
512 std::optional<DisplayViewport> viewport =
513 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
514 ASSERT_TRUE(viewport);
515 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
516 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
517
518 // Add the default display second to make sure order doesn't matter.
519 mFakePolicy->clearViewports();
520 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000521 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000522 ViewportType::INTERNAL);
523 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000524 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000525 ViewportType::INTERNAL);
526
527 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
528 ASSERT_TRUE(viewport);
529 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
530 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
531}
532
533/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700534 * Check getDisplayViewportByPort
535 */
536TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100537 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700538 const std::string uniqueId1 = "uniqueId1";
539 const std::string uniqueId2 = "uniqueId2";
540 constexpr int32_t displayId1 = 1;
541 constexpr int32_t displayId2 = 2;
542 const uint8_t hdmi1 = 0;
543 const uint8_t hdmi2 = 1;
544 const uint8_t hdmi3 = 2;
545
546 mFakePolicy->clearViewports();
547 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000548 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000549 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700550 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000551 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000552 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700553
554 // Check that correct display viewport was returned by comparing the display ports.
555 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
556 ASSERT_TRUE(hdmi1Viewport);
557 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
558 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
559
560 // Check that we can still get the same viewport using the uniqueId
561 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
562 ASSERT_TRUE(hdmi1Viewport);
563 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
564 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
565 ASSERT_EQ(type, hdmi1Viewport->type);
566
567 // Check that we cannot find a port with "HDMI2", because we never added one
568 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
569 ASSERT_FALSE(hdmi2Viewport);
570}
571
Michael Wrightd02c5b62014-02-10 15:10:22 -0800572// --- InputReaderTest ---
573
574class InputReaderTest : public testing::Test {
575protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700576 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700578 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000579 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800580
Chris Yea52ade12020-08-27 16:49:20 -0700581 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700582 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700583 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700584 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800585
Prabir Pradhan28efc192019-11-05 01:10:04 +0000586 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700587 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588 }
589
Chris Yea52ade12020-08-27 16:49:20 -0700590 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700591 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 }
594
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700595 void addDevice(int32_t eventHubId, const std::string& name,
596 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800597 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800598
599 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800600 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800601 }
602 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000603 mReader->loopOnce();
604 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700605 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
606 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800607 }
608
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800609 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700610 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000611 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700612 }
613
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800614 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700615 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000616 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700617 }
618
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800619 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700620 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700621 ftl::Flags<InputDeviceClass> classes,
622 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800623 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800624 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
625 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800626 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800627 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800628 return mapper;
629 }
630};
631
Chris Ye98d3f532020-10-01 21:48:59 -0700632TEST_F(InputReaderTest, PolicyGetInputDevices) {
633 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700634 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700635 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800636
637 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700638 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800639 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800640 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100641 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800642 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
643 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000644 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800645}
646
Chris Yee7310032020-09-22 15:36:28 -0700647TEST_F(InputReaderTest, GetMergedInputDevices) {
648 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
649 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
650 // Add two subdevices to device
651 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
652 // Must add at least one mapper or the device will be ignored!
653 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
654 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
655
656 // Push same device instance for next device to be added, so they'll have same identifier.
657 mReader->pushNextDevice(device);
658 mReader->pushNextDevice(device);
659 ASSERT_NO_FATAL_FAILURE(
660 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
661 ASSERT_NO_FATAL_FAILURE(
662 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
663
664 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000665 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700666}
667
Chris Yee14523a2020-12-19 13:46:00 -0800668TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
669 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
670 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
671 // Add two subdevices to device
672 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
673 // Must add at least one mapper or the device will be ignored!
674 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
675 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
676
677 // Push same device instance for next device to be added, so they'll have same identifier.
678 mReader->pushNextDevice(device);
679 mReader->pushNextDevice(device);
680 // Sensor device is initially disabled
681 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
682 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
683 nullptr));
684 // Device is disabled because the only sub device is a sensor device and disabled initially.
685 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
686 ASSERT_FALSE(device->isEnabled());
687 ASSERT_NO_FATAL_FAILURE(
688 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
689 // The merged device is enabled if any sub device is enabled
690 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
691 ASSERT_TRUE(device->isEnabled());
692}
693
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700694TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800695 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700696 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800697 constexpr int32_t eventHubId = 1;
698 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700699 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800700 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800701 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800702 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700703
Yi Kong9b14ac62018-07-17 13:48:38 -0700704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700705
706 NotifyDeviceResetArgs resetArgs;
707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700708 ASSERT_EQ(deviceId, resetArgs.deviceId);
709
710 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800711 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000712 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700713
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700715 ASSERT_EQ(deviceId, resetArgs.deviceId);
716 ASSERT_EQ(device->isEnabled(), false);
717
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800718 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000719 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700722 ASSERT_EQ(device->isEnabled(), false);
723
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800724 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000725 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700727 ASSERT_EQ(deviceId, resetArgs.deviceId);
728 ASSERT_EQ(device->isEnabled(), true);
729}
730
Michael Wrightd02c5b62014-02-10 15:10:22 -0800731TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800732 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700733 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800734 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800735 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800736 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800737 AINPUT_SOURCE_KEYBOARD, nullptr);
738 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800739
740 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
741 AINPUT_SOURCE_ANY, AKEYCODE_A))
742 << "Should return unknown when the device id is >= 0 but unknown.";
743
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800744 ASSERT_EQ(AKEY_STATE_UNKNOWN,
745 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
746 << "Should return unknown when the device id is valid but the sources are not "
747 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800748
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800749 ASSERT_EQ(AKEY_STATE_DOWN,
750 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
751 AKEYCODE_A))
752 << "Should return value provided by mapper when device id is valid and the device "
753 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800754
755 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
756 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
757 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
758
759 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
760 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
761 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
762}
763
Philip Junker4af3b3d2021-12-14 10:36:55 +0100764TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
765 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
766 constexpr int32_t eventHubId = 1;
767 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
768 InputDeviceClass::KEYBOARD,
769 AINPUT_SOURCE_KEYBOARD, nullptr);
770 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
771
772 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
773 << "Should return unknown when the device with the specified id is not found.";
774
775 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
776 << "Should return correct mapping when device id is valid and mapping exists.";
777
778 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
779 << "Should return the location key code when device id is valid and there's no "
780 "mapping.";
781}
782
783TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
784 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
785 constexpr int32_t eventHubId = 1;
786 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
787 InputDeviceClass::JOYSTICK,
788 AINPUT_SOURCE_GAMEPAD, nullptr);
789 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
790
791 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
792 << "Should return unknown when the device id is valid but there is no keyboard mapper";
793}
794
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800796 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700797 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800798 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800799 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800800 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800801 AINPUT_SOURCE_KEYBOARD, nullptr);
802 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800803
804 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
805 AINPUT_SOURCE_ANY, KEY_A))
806 << "Should return unknown when the device id is >= 0 but unknown.";
807
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800808 ASSERT_EQ(AKEY_STATE_UNKNOWN,
809 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
810 << "Should return unknown when the device id is valid but the sources are not "
811 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800812
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800813 ASSERT_EQ(AKEY_STATE_DOWN,
814 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
815 KEY_A))
816 << "Should return value provided by mapper when device id is valid and the device "
817 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818
819 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
820 AINPUT_SOURCE_TRACKBALL, KEY_A))
821 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
822
823 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
824 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
825 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
826}
827
828TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800829 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700830 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800831 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800832 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800833 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800834 AINPUT_SOURCE_KEYBOARD, nullptr);
835 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836
837 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
838 AINPUT_SOURCE_ANY, SW_LID))
839 << "Should return unknown when the device id is >= 0 but unknown.";
840
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800841 ASSERT_EQ(AKEY_STATE_UNKNOWN,
842 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
843 << "Should return unknown when the device id is valid but the sources are not "
844 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800846 ASSERT_EQ(AKEY_STATE_DOWN,
847 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
848 SW_LID))
849 << "Should return value provided by mapper when device id is valid and the device "
850 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851
852 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
853 AINPUT_SOURCE_TRACKBALL, SW_LID))
854 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
855
856 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
857 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
858 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
859}
860
861TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800862 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700863 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800864 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800865 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800866 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800867 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100868
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800869 mapper.addSupportedKeyCode(AKEYCODE_A);
870 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800871
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700872 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800873 uint8_t flags[4] = { 0, 0, 0, 1 };
874
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700875 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800876 << "Should return false when device id is >= 0 but unknown.";
877 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
878
879 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700880 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800881 << "Should return false when device id is valid but the sources are not supported by "
882 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800883 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
884
885 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700886 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800887 keyCodes, flags))
888 << "Should return value provided by mapper when device id is valid and the device "
889 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800890 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
891
892 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700893 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
894 << "Should return false when the device id is < 0 but the sources are not supported by "
895 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
897
898 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700899 ASSERT_TRUE(
900 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
901 << "Should return value provided by mapper when device id is < 0 and one of the "
902 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800903 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
904}
905
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000906TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800907 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700908 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909
910 NotifyConfigurationChangedArgs args;
911
912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
913 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
914}
915
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000916TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800917 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700918 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000919 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800920 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000921 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800922 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800923 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800924 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800925
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000926 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000927 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800928 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
929
930 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800931 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000932 ASSERT_EQ(when, event.when);
933 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800934 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800935 ASSERT_EQ(EV_KEY, event.type);
936 ASSERT_EQ(KEY_A, event.code);
937 ASSERT_EQ(1, event.value);
938}
939
Garfield Tan1c7bc862020-01-28 13:24:04 -0800940TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800941 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700942 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800943 constexpr int32_t eventHubId = 1;
944 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800945 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800946 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800947 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800948 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800949
950 NotifyDeviceResetArgs resetArgs;
951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800952 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800953
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800954 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000955 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800957 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800958 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800959
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800960 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000961 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800963 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800964 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800965
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800966 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000967 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800969 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800970 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800971}
972
Garfield Tan1c7bc862020-01-28 13:24:04 -0800973TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
974 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700975 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800976 constexpr int32_t eventHubId = 1;
977 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
978 // Must add at least one mapper or the device will be ignored!
979 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800980 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -0800981 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
982
983 NotifyDeviceResetArgs resetArgs;
984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
985 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
986}
987
Arthur Hungc23540e2018-11-29 20:42:11 +0800988TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800989 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700990 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800991 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +0800992 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800993 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
994 FakeInputMapper& mapper =
995 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800996 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +0800997
998 const uint8_t hdmi1 = 1;
999
1000 // Associated touch screen with second display.
1001 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1002
1003 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001004 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001005 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001006 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001007 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001008 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001009 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001010 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001011 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001012
1013 // Add the device, and make sure all of the callbacks are triggered.
1014 // The device is added after the input port associations are processed since
1015 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001016 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001019 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001020
Arthur Hung2c9a3342019-07-23 14:18:59 +08001021 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001022 ASSERT_EQ(deviceId, device->getId());
1023 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1024 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001025
1026 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001027 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001028 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001029 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001030}
1031
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001032TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1033 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001034 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001035 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1036 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1037 // Must add at least one mapper or the device will be ignored!
1038 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1039 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1040 mReader->pushNextDevice(device);
1041 mReader->pushNextDevice(device);
1042 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1043 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1044
1045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1046
1047 NotifyDeviceResetArgs resetArgs;
1048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1049 ASSERT_EQ(deviceId, resetArgs.deviceId);
1050 ASSERT_TRUE(device->isEnabled());
1051 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1052 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1053
1054 disableDevice(deviceId);
1055 mReader->loopOnce();
1056
1057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1058 ASSERT_EQ(deviceId, resetArgs.deviceId);
1059 ASSERT_FALSE(device->isEnabled());
1060 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1061 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1062
1063 enableDevice(deviceId);
1064 mReader->loopOnce();
1065
1066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1067 ASSERT_EQ(deviceId, resetArgs.deviceId);
1068 ASSERT_TRUE(device->isEnabled());
1069 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1070 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1071}
1072
1073TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1074 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001075 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001076 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1077 // Add two subdevices to device
1078 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1079 FakeInputMapper& mapperDevice1 =
1080 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1081 FakeInputMapper& mapperDevice2 =
1082 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1083 mReader->pushNextDevice(device);
1084 mReader->pushNextDevice(device);
1085 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1086 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1087
1088 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1089 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1090
1091 ASSERT_EQ(AKEY_STATE_DOWN,
1092 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1093 ASSERT_EQ(AKEY_STATE_DOWN,
1094 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1095 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1096 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1097}
1098
Prabir Pradhan7e186182020-11-10 13:56:45 -08001099TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1100 NotifyPointerCaptureChangedArgs args;
1101
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001102 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001103 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1104 mReader->loopOnce();
1105 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001106 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1107 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001108
1109 mFakePolicy->setPointerCapture(false);
1110 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1111 mReader->loopOnce();
1112 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001113 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001114
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001115 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001116 // does not change.
1117 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1118 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001119 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001120}
1121
Chris Ye87143712020-11-10 05:05:58 +00001122class FakeVibratorInputMapper : public FakeInputMapper {
1123public:
1124 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1125 : FakeInputMapper(deviceContext, sources) {}
1126
1127 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1128};
1129
1130TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1131 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001132 ftl::Flags<InputDeviceClass> deviceClass =
1133 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001134 constexpr int32_t eventHubId = 1;
1135 const char* DEVICE_LOCATION = "BLUETOOTH";
1136 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1137 FakeVibratorInputMapper& mapper =
1138 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1139 mReader->pushNextDevice(device);
1140
1141 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1142 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1143
1144 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1145 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1146}
1147
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001148// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001149
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001150class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001151public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001152 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001153
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001154 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001155
Andy Chenf9f1a022022-08-29 20:07:10 -04001156 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1157
Chris Yee2b1e5c2021-03-10 22:45:12 -08001158 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1159
1160 void dump(std::string& dump) override {}
1161
1162 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1163 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001164 }
1165
Chris Yee2b1e5c2021-03-10 22:45:12 -08001166 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1167 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001168 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001169
1170 bool setLightColor(int32_t lightId, int32_t color) override {
1171 getDeviceContext().setLightBrightness(lightId, color >> 24);
1172 return true;
1173 }
1174
1175 std::optional<int32_t> getLightColor(int32_t lightId) override {
1176 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1177 if (!result.has_value()) {
1178 return std::nullopt;
1179 }
1180 return result.value() << 24;
1181 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001182
1183 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1184
1185 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1186
1187private:
1188 InputDeviceContext& mDeviceContext;
1189 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1190 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001191 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001192};
1193
Chris Yee2b1e5c2021-03-10 22:45:12 -08001194TEST_F(InputReaderTest, BatteryGetCapacity) {
1195 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001196 ftl::Flags<InputDeviceClass> deviceClass =
1197 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001198 constexpr int32_t eventHubId = 1;
1199 const char* DEVICE_LOCATION = "BLUETOOTH";
1200 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001201 FakePeripheralController& controller =
1202 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001203 mReader->pushNextDevice(device);
1204
1205 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1206
Harry Cuttsa5b71292022-11-28 12:56:17 +00001207 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1208 FakeEventHub::BATTERY_CAPACITY);
1209 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001210}
1211
1212TEST_F(InputReaderTest, BatteryGetStatus) {
1213 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001214 ftl::Flags<InputDeviceClass> deviceClass =
1215 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001216 constexpr int32_t eventHubId = 1;
1217 const char* DEVICE_LOCATION = "BLUETOOTH";
1218 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001219 FakePeripheralController& controller =
1220 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001221 mReader->pushNextDevice(device);
1222
1223 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1224
Harry Cuttsa5b71292022-11-28 12:56:17 +00001225 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1226 FakeEventHub::BATTERY_STATUS);
1227 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001228}
1229
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001230TEST_F(InputReaderTest, BatteryGetDevicePath) {
1231 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1232 ftl::Flags<InputDeviceClass> deviceClass =
1233 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1234 constexpr int32_t eventHubId = 1;
1235 const char* DEVICE_LOCATION = "BLUETOOTH";
1236 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1237 device->addController<FakePeripheralController>(eventHubId);
1238 mReader->pushNextDevice(device);
1239
1240 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1241
Harry Cuttsa5b71292022-11-28 12:56:17 +00001242 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001243}
1244
Chris Ye3fdbfef2021-01-06 18:45:18 -08001245TEST_F(InputReaderTest, LightGetColor) {
1246 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001247 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001248 constexpr int32_t eventHubId = 1;
1249 const char* DEVICE_LOCATION = "BLUETOOTH";
1250 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001251 FakePeripheralController& controller =
1252 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001253 mReader->pushNextDevice(device);
1254 RawLightInfo info = {.id = 1,
1255 .name = "Mono",
1256 .maxBrightness = 255,
1257 .flags = InputLightClass::BRIGHTNESS,
1258 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001259 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1260 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001261
1262 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001263
Harry Cutts33476232023-01-30 19:57:29 +00001264 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1265 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1266 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1267 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001268}
1269
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001270// --- InputReaderIntegrationTest ---
1271
1272// These tests create and interact with the InputReader only through its interface.
1273// The InputReader is started during SetUp(), which starts its processing in its own
1274// thread. The tests use linux uinput to emulate input devices.
1275// NOTE: Interacting with the physical device while these tests are running may cause
1276// the tests to fail.
1277class InputReaderIntegrationTest : public testing::Test {
1278protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001279 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001280 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001281 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001282
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001283 std::shared_ptr<FakePointerController> mFakePointerController;
1284
Chris Yea52ade12020-08-27 16:49:20 -07001285 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001286#if !defined(__ANDROID__)
1287 GTEST_SKIP();
1288#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001289 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001290 mFakePointerController = std::make_shared<FakePointerController>();
1291 mFakePolicy->setPointerController(mFakePointerController);
Harry Cutts33476232023-01-30 19:57:29 +00001292 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1293 /*eventDidNotHappenTimeout=*/30ms);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001294
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001295 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1296 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001297 ASSERT_EQ(mReader->start(), OK);
1298
1299 // Since this test is run on a real device, all the input devices connected
1300 // to the test device will show up in mReader. We wait for those input devices to
1301 // show up before beginning the tests.
1302 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1303 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1304 }
1305
Chris Yea52ade12020-08-27 16:49:20 -07001306 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001307#if !defined(__ANDROID__)
1308 return;
1309#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001310 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001311 mReader.reset();
1312 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001313 mFakePolicy.clear();
1314 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001315
1316 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1317 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1318 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1319 [&name](const InputDeviceInfo& info) {
1320 return info.getIdentifier().name == name;
1321 });
1322 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1323 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001324};
1325
1326TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1327 // An invalid input device that is only used for this test.
1328 class InvalidUinputDevice : public UinputDevice {
1329 public:
Harry Cutts33476232023-01-30 19:57:29 +00001330 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001331
1332 private:
1333 void configureDevice(int fd, uinput_user_dev* device) override {}
1334 };
1335
1336 const size_t numDevices = mFakePolicy->getInputDevices().size();
1337
1338 // UinputDevice does not set any event or key bits, so InputReader should not
1339 // consider it as a valid device.
1340 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1341 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1342 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1343 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1344
1345 invalidDevice.reset();
1346 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1347 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1348 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1349}
1350
1351TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1352 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1353
1354 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1355 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1356 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1357 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1358
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001359 const auto device = findDeviceByName(keyboard->getName());
1360 ASSERT_TRUE(device.has_value());
1361 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1362 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1363 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001364
1365 keyboard.reset();
1366 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1367 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1368 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1369}
1370
1371TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1372 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1373 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1374
1375 NotifyConfigurationChangedArgs configChangedArgs;
1376 ASSERT_NO_FATAL_FAILURE(
1377 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001378 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001379 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1380
1381 NotifyKeyArgs keyArgs;
1382 keyboard->pressAndReleaseHomeKey();
1383 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1384 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001385 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001386 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001387 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001388 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001389 prevTimestamp = keyArgs.eventTime;
1390
1391 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1392 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001393 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001394 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001395 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001396}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001397
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001398TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1399 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1400 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1401
1402 const auto device = findDeviceByName(stylus->getName());
1403 ASSERT_TRUE(device.has_value());
1404
Prabir Pradhana3621852022-10-14 18:57:23 +00001405 // An external stylus with buttons should also be recognized as a keyboard.
1406 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001407 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1408 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1409
1410 const auto DOWN =
1411 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1412 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1413
1414 stylus->pressAndReleaseKey(BTN_STYLUS);
1415 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1416 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1417 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1418 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1419
1420 stylus->pressAndReleaseKey(BTN_STYLUS2);
1421 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1422 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1423 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1424 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1425
1426 stylus->pressAndReleaseKey(BTN_STYLUS3);
1427 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1428 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1429 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1430 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1431}
1432
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001433/**
1434 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1435 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1436 * are passed to the listener.
1437 */
1438static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1439TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1440 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1441 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1442 NotifyKeyArgs keyArgs;
1443
1444 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1445 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1446 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1447 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1448
1449 controller->pressAndReleaseKey(BTN_GEAR_UP);
1450 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1451 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1452 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1453}
1454
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001455// --- TouchIntegrationTest ---
1456
Arthur Hungaab25622020-01-16 11:22:11 +08001457class TouchIntegrationTest : public InputReaderIntegrationTest {
1458protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001459 const std::string UNIQUE_ID = "local:0";
1460
Chris Yea52ade12020-08-27 16:49:20 -07001461 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001462#if !defined(__ANDROID__)
1463 GTEST_SKIP();
1464#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001465 InputReaderIntegrationTest::SetUp();
1466 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001467 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1468 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001469
1470 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1471 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1472 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001473 const auto info = findDeviceByName(mDevice->getName());
1474 ASSERT_TRUE(info);
1475 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001476 }
1477
1478 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001479 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001480 std::optional<uint8_t> physicalPort,
1481 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001482 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001483 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08001484 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1485 }
1486
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001487 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1488 NotifyMotionArgs args;
1489 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1490 EXPECT_EQ(action, args.action);
1491 ASSERT_EQ(points.size(), args.pointerCount);
1492 for (size_t i = 0; i < args.pointerCount; i++) {
1493 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1494 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1495 }
1496 }
1497
Arthur Hungaab25622020-01-16 11:22:11 +08001498 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001499 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001500};
1501
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001502TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
1503 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1504 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1505 // presses).
1506 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1507 mDeviceInfo.getSources());
1508}
1509
Arthur Hungaab25622020-01-16 11:22:11 +08001510TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1511 NotifyMotionArgs args;
1512 const Point centerPoint = mDevice->getCenterPoint();
1513
1514 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001515 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001516 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001517 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001518 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1519 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1520
1521 // ACTION_MOVE
1522 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001523 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001524 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1525 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1526
1527 // ACTION_UP
1528 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001529 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001530 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1531 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1532}
1533
1534TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1535 NotifyMotionArgs args;
1536 const Point centerPoint = mDevice->getCenterPoint();
1537
1538 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001539 mDevice->sendSlot(FIRST_SLOT);
1540 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001541 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001542 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001543 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1544 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1545
1546 // ACTION_POINTER_DOWN (Second slot)
1547 const Point secondPoint = centerPoint + Point(100, 100);
1548 mDevice->sendSlot(SECOND_SLOT);
1549 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001550 mDevice->sendDown(secondPoint);
1551 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001552 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001553 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001554
1555 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001556 mDevice->sendMove(secondPoint + Point(1, 1));
1557 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001558 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1560
1561 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001562 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001563 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001564 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001565 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001566
1567 // ACTION_UP
1568 mDevice->sendSlot(FIRST_SLOT);
1569 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001570 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001571 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1572 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1573}
1574
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001575/**
1576 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1577 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1578 * data?
1579 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1580 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1581 * for Pointer 0 only is generated after.
1582 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1583 * events, we will not miss any information.
1584 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1585 * event generated afterwards that contains the newest movement of pointer 0.
1586 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1587 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1588 * losing information about non-palm pointers.
1589 */
1590TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1591 NotifyMotionArgs args;
1592 const Point centerPoint = mDevice->getCenterPoint();
1593
1594 // ACTION_DOWN
1595 mDevice->sendSlot(FIRST_SLOT);
1596 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1597 mDevice->sendDown(centerPoint);
1598 mDevice->sendSync();
1599 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1600
1601 // ACTION_POINTER_DOWN (Second slot)
1602 const Point secondPoint = centerPoint + Point(100, 100);
1603 mDevice->sendSlot(SECOND_SLOT);
1604 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1605 mDevice->sendDown(secondPoint);
1606 mDevice->sendSync();
1607 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1608
1609 // ACTION_MOVE (First slot)
1610 mDevice->sendSlot(FIRST_SLOT);
1611 mDevice->sendMove(centerPoint + Point(5, 5));
1612 // ACTION_POINTER_UP (Second slot)
1613 mDevice->sendSlot(SECOND_SLOT);
1614 mDevice->sendPointerUp();
1615 // Send a single sync for the above 2 pointer updates
1616 mDevice->sendSync();
1617
1618 // First, we should get POINTER_UP for the second pointer
1619 assertReceivedMotion(ACTION_POINTER_1_UP,
1620 {/*first pointer */ centerPoint + Point(5, 5),
1621 /*second pointer*/ secondPoint});
1622
1623 // Next, the MOVE event for the first pointer
1624 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1625}
1626
1627/**
1628 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1629 * move, and then it will go up, all in the same frame.
1630 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1631 * gets sent to the listener.
1632 */
1633TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1634 NotifyMotionArgs args;
1635 const Point centerPoint = mDevice->getCenterPoint();
1636
1637 // ACTION_DOWN
1638 mDevice->sendSlot(FIRST_SLOT);
1639 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1640 mDevice->sendDown(centerPoint);
1641 mDevice->sendSync();
1642 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1643
1644 // ACTION_POINTER_DOWN (Second slot)
1645 const Point secondPoint = centerPoint + Point(100, 100);
1646 mDevice->sendSlot(SECOND_SLOT);
1647 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1648 mDevice->sendDown(secondPoint);
1649 mDevice->sendSync();
1650 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1651
1652 // ACTION_MOVE (First slot)
1653 mDevice->sendSlot(FIRST_SLOT);
1654 mDevice->sendMove(centerPoint + Point(5, 5));
1655 // ACTION_POINTER_UP (Second slot)
1656 mDevice->sendSlot(SECOND_SLOT);
1657 mDevice->sendMove(secondPoint + Point(6, 6));
1658 mDevice->sendPointerUp();
1659 // Send a single sync for the above 2 pointer updates
1660 mDevice->sendSync();
1661
1662 // First, we should get POINTER_UP for the second pointer
1663 // The movement of the second pointer during the liftoff frame is ignored.
1664 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1665 assertReceivedMotion(ACTION_POINTER_1_UP,
1666 {/*first pointer */ centerPoint + Point(5, 5),
1667 /*second pointer*/ secondPoint});
1668
1669 // Next, the MOVE event for the first pointer
1670 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1671}
1672
Arthur Hungaab25622020-01-16 11:22:11 +08001673TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1674 NotifyMotionArgs args;
1675 const Point centerPoint = mDevice->getCenterPoint();
1676
1677 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001678 mDevice->sendSlot(FIRST_SLOT);
1679 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001680 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001681 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001682 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1683 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1684
arthurhungcc7f9802020-04-30 17:55:40 +08001685 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001686 const Point secondPoint = centerPoint + Point(100, 100);
1687 mDevice->sendSlot(SECOND_SLOT);
1688 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1689 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001690 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001691 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001692 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001693
arthurhungcc7f9802020-04-30 17:55:40 +08001694 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001695 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001696 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001697 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1698 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1699
arthurhungcc7f9802020-04-30 17:55:40 +08001700 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1701 // a palm event.
1702 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001703 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001704 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001705 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001706 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001707 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001708
arthurhungcc7f9802020-04-30 17:55:40 +08001709 // Send up to second slot, expect first slot send moving.
1710 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001711 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001712 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1713 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001714
arthurhungcc7f9802020-04-30 17:55:40 +08001715 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001716 mDevice->sendSlot(FIRST_SLOT);
1717 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001718 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001719
arthurhungcc7f9802020-04-30 17:55:40 +08001720 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1721 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001722}
1723
Prabir Pradhanda20b172022-09-26 17:01:18 +00001724TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
1725 const Point centerPoint = mDevice->getCenterPoint();
1726
1727 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1728 mDevice->sendSlot(FIRST_SLOT);
1729 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1730 mDevice->sendToolType(MT_TOOL_PEN);
1731 mDevice->sendDown(centerPoint);
1732 mDevice->sendSync();
1733 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1734 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1735 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
1736
1737 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1738
1739 // Release the stylus touch.
1740 mDevice->sendUp();
1741 mDevice->sendSync();
1742 ASSERT_NO_FATAL_FAILURE(
1743 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1744
1745 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1746
1747 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1748 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1749 mDevice->sendToolType(MT_TOOL_FINGER);
1750 mDevice->sendDown(centerPoint);
1751 mDevice->sendSync();
1752 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1753 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1754 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
1755
1756 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1757
1758 mDevice->sendUp();
1759 mDevice->sendSync();
1760 ASSERT_NO_FATAL_FAILURE(
1761 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1762
1763 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1764 // The policy should be notified of the stylus presence.
1765 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1766 mDevice->sendToolType(MT_TOOL_PEN);
1767 mDevice->sendMove(centerPoint);
1768 mDevice->sendSync();
1769 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1770 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1771 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
1772
1773 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1774}
1775
Prabir Pradhan124ea442022-10-28 20:27:44 +00001776// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001777
Prabir Pradhan124ea442022-10-28 20:27:44 +00001778// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1779// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1780// stylus.
1781template <typename UinputStylusDevice>
1782class StylusButtonIntegrationTest : public TouchIntegrationTest {
1783protected:
1784 void SetUp() override {
1785#if !defined(__ANDROID__)
1786 GTEST_SKIP();
1787#endif
1788 TouchIntegrationTest::SetUp();
1789 mTouchscreen = mDevice.get();
1790 mTouchscreenInfo = mDeviceInfo;
1791
1792 setUpStylusDevice();
1793 }
1794
1795 UinputStylusDevice* mStylus{nullptr};
1796 InputDeviceInfo mStylusInfo{};
1797
1798 UinputTouchScreen* mTouchscreen{nullptr};
1799 InputDeviceInfo mTouchscreenInfo{};
1800
1801private:
1802 // When we are attempting to test stylus button events that are sent from the touchscreen,
1803 // use the same Uinput device for the touchscreen and the stylus.
1804 template <typename T = UinputStylusDevice>
1805 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1806 mStylus = mDevice.get();
1807 mStylusInfo = mDeviceInfo;
1808 }
1809
1810 // When we are attempting to stylus buttons from an external stylus being merged with touches
1811 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1812 template <typename T = UinputStylusDevice>
1813 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1814 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1815 mStylus = mStylusDeviceLifecycleTracker.get();
1816 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1817 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1818 const auto info = findDeviceByName(mStylus->getName());
1819 ASSERT_TRUE(info);
1820 mStylusInfo = *info;
1821 }
1822
1823 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1824
1825 // Hide the base class's device to expose it with a different name for readability.
1826 using TouchIntegrationTest::mDevice;
1827 using TouchIntegrationTest::mDeviceInfo;
1828};
1829
1830using StylusButtonIntegrationTestTypes =
1831 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1832TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1833
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001834TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001835 const auto stylusId = TestFixture::mStylusInfo.getId();
1836
1837 TestFixture::mStylus->pressKey(BTN_STYLUS);
1838 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1839 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1840 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1841
1842 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1843 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001844 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001845 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001846}
1847
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001848TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001849 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1850 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1851 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001852
1853 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001854 TestFixture::mStylus->pressKey(BTN_STYLUS);
1855 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001856 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001857 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001858
1859 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001860 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1861 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1862 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1863 TestFixture::mTouchscreen->sendDown(centerPoint);
1864 TestFixture::mTouchscreen->sendSync();
1865 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001866 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1867 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001868 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1869 WithDeviceId(touchscreenId))));
1870 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001871 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1872 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001873 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1874 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001875
Prabir Pradhan124ea442022-10-28 20:27:44 +00001876 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1877 TestFixture::mTouchscreen->sendSync();
1878 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001879 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001880 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1881 WithDeviceId(touchscreenId))));
1882 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001883 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001884 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1885 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001886
1887 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001888 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1889 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001890 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001891 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001892}
1893
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001894TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001895 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1896 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1897 const auto stylusId = TestFixture::mStylusInfo.getId();
1898 auto toolTypeDevice =
1899 AllOf(WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithDeviceId(touchscreenId));
1900
1901 // Press the stylus button.
1902 TestFixture::mStylus->pressKey(BTN_STYLUS);
1903 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1904 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1905 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1906
1907 // Start hovering with the stylus.
1908 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1909 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1910 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1911 TestFixture::mTouchscreen->sendMove(centerPoint);
1912 TestFixture::mTouchscreen->sendSync();
1913 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1914 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1915 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1916 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1917 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1918 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1919 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1920 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1921 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1922
1923 // Touch down with the stylus.
1924 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1925 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1926 TestFixture::mTouchscreen->sendDown(centerPoint);
1927 TestFixture::mTouchscreen->sendSync();
1928 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1929 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1930 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1931
1932 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1933 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1934 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1935
1936 // Stop touching with the stylus, and start hovering.
1937 TestFixture::mTouchscreen->sendUp();
1938 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1939 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1940 TestFixture::mTouchscreen->sendMove(centerPoint);
1941 TestFixture::mTouchscreen->sendSync();
1942 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1943 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
1944 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1945 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1946 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1947 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1948 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1949 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1950 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1951
1952 // Stop hovering.
1953 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1954 TestFixture::mTouchscreen->sendSync();
1955 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1956 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1957 WithButtonState(0))));
1958 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
1959 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1960 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1961 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1962
1963 // Release the stylus button.
1964 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1965 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1966 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
1967 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1968}
1969
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001970TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001971 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1972 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1973 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001974
1975 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001976 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1977 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1978 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1979 TestFixture::mTouchscreen->sendDown(centerPoint);
1980 TestFixture::mTouchscreen->sendSync();
1981 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001982 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001983 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1984 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001985
1986 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001987 TestFixture::mStylus->pressKey(BTN_STYLUS);
1988 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001989 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001990 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1991 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001992 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1993 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001994 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1995 WithDeviceId(touchscreenId))));
1996 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001997 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1998 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001999 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2000 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002001
Prabir Pradhan124ea442022-10-28 20:27:44 +00002002 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2003 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002004 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002005 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2006 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002007 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002008 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2009 WithDeviceId(touchscreenId))));
2010 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002011 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002012 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2013 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002014
2015 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002016 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2017 TestFixture::mTouchscreen->sendSync();
2018 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002019 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002020 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2021 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002022}
2023
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002024TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002025 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2026 TestFixture::mReader->requestRefreshConfiguration(
2027 InputReaderConfiguration::CHANGE_STYLUS_BUTTON_REPORTING);
2028
2029 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2030 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2031 const auto stylusId = TestFixture::mStylusInfo.getId();
2032
2033 // Start a stylus gesture. By the time this event is processed, the configuration change that
2034 // was requested is guaranteed to be completed.
2035 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2036 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2037 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2038 TestFixture::mTouchscreen->sendDown(centerPoint);
2039 TestFixture::mTouchscreen->sendSync();
2040 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2041 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2042 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2043 WithDeviceId(touchscreenId))));
2044
2045 // Press and release a stylus button. Each change only generates a MOVE motion event.
2046 // Key events are unaffected.
2047 TestFixture::mStylus->pressKey(BTN_STYLUS);
2048 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2049 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2050 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2051 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2052 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2053 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2054 WithDeviceId(touchscreenId))));
2055
2056 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2057 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2058 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2059 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2060 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2061 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2062 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2063 WithDeviceId(touchscreenId))));
2064
2065 // Finish the stylus gesture.
2066 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2067 TestFixture::mTouchscreen->sendSync();
2068 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2069 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2070 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2071 WithDeviceId(touchscreenId))));
2072}
2073
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002074// --- ExternalStylusIntegrationTest ---
2075
2076// Verify the behavior of an external stylus. An external stylus can report pressure or button
2077// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2078// ongoing stylus gesture that is being emitted by the touchscreen.
2079using ExternalStylusIntegrationTest = TouchIntegrationTest;
2080
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002081TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002082 const Point centerPoint = mDevice->getCenterPoint();
2083
2084 // Create an external stylus capable of reporting pressure data that
2085 // should be fused with a touch pointer.
2086 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2087 createUinputDevice<UinputExternalStylusWithPressure>();
2088 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2089 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2090 const auto stylusInfo = findDeviceByName(stylus->getName());
2091 ASSERT_TRUE(stylusInfo);
2092
2093 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2094
2095 const auto touchscreenId = mDeviceInfo.getId();
2096
2097 // Set a pressure value on the stylus. It doesn't generate any events.
2098 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2099 stylus->setPressure(100);
2100 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2101
2102 // Start a finger gesture, and ensure it shows up as stylus gesture
2103 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002104 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002105 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002106 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002107 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002108 mDevice->sendSync();
2109 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2110 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002111 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2112 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002113
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002114 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2115 // event with the updated pressure.
2116 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002117 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2118 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002119 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2120 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002121
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002122 // The external stylus did not generate any events.
2123 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2124 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2125}
2126
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002127TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002128 const Point centerPoint = mDevice->getCenterPoint();
2129
2130 // Create an external stylus capable of reporting pressure data that
2131 // should be fused with a touch pointer.
2132 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2133 createUinputDevice<UinputExternalStylusWithPressure>();
2134 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2135 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2136 const auto stylusInfo = findDeviceByName(stylus->getName());
2137 ASSERT_TRUE(stylusInfo);
2138
2139 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2140
2141 const auto touchscreenId = mDeviceInfo.getId();
2142
2143 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2144 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002145 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2146 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002147 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002148 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002149
2150 // Start a finger gesture. The touch device will withhold generating any touches for
2151 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2152 mDevice->sendSlot(FIRST_SLOT);
2153 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2154 mDevice->sendToolType(MT_TOOL_FINGER);
2155 mDevice->sendDown(centerPoint);
2156 auto waitUntil = std::chrono::system_clock::now() +
2157 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002158 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002159 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002160
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002161 // Since the external stylus did not report a pressure value within the timeout,
2162 // it shows up as a finger pointer.
2163 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2164 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2165 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithDeviceId(touchscreenId),
2166 WithPressure(1.f))));
2167
2168 // Change the pressure on the external stylus. Since the pressure was not present at the start
2169 // of the gesture, it is ignored for now.
2170 stylus->setPressure(200);
2171 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2172
2173 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002174 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2175 mDevice->sendSync();
2176 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2177 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002178 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2179
2180 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2181 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2182 mDevice->sendToolType(MT_TOOL_FINGER);
2183 mDevice->sendDown(centerPoint);
2184 mDevice->sendSync();
2185 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2186 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2187 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2188 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2189
2190 // The external stylus did not generate any events.
2191 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2192 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002193}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002194
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002195TEST_F(ExternalStylusIntegrationTest, DISABLED_UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002196 const Point centerPoint = mDevice->getCenterPoint();
2197
2198 // Create an external stylus device that does not support pressure. It should not affect any
2199 // touch pointers.
2200 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2201 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2202 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2203 const auto stylusInfo = findDeviceByName(stylus->getName());
2204 ASSERT_TRUE(stylusInfo);
2205
2206 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2207
2208 const auto touchscreenId = mDeviceInfo.getId();
2209
2210 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2211 // pressure data from the external stylus.
2212 mDevice->sendSlot(FIRST_SLOT);
2213 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2214 mDevice->sendToolType(MT_TOOL_FINGER);
2215 mDevice->sendDown(centerPoint);
2216 auto waitUntil = std::chrono::system_clock::now() +
2217 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2218 mDevice->sendSync();
2219 ASSERT_NO_FATAL_FAILURE(
2220 mTestListener
2221 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2222 WithToolType(
2223 AMOTION_EVENT_TOOL_TYPE_FINGER),
2224 WithButtonState(0),
2225 WithDeviceId(touchscreenId),
2226 WithPressure(1.f)),
2227 waitUntil));
2228
2229 // The external stylus did not generate any events.
2230 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2231 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2232}
2233
Michael Wrightd02c5b62014-02-10 15:10:22 -08002234// --- InputDeviceTest ---
2235class InputDeviceTest : public testing::Test {
2236protected:
2237 static const char* DEVICE_NAME;
2238 static const char* DEVICE_LOCATION;
2239 static const int32_t DEVICE_ID;
2240 static const int32_t DEVICE_GENERATION;
2241 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002242 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002243 static const int32_t EVENTHUB_ID;
2244 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2245
2246 std::shared_ptr<FakeEventHub> mFakeEventHub;
2247 sp<FakeInputReaderPolicy> mFakePolicy;
2248 std::unique_ptr<TestInputListener> mFakeListener;
2249 std::unique_ptr<InstrumentedInputReader> mReader;
2250 std::shared_ptr<InputDevice> mDevice;
2251
2252 void SetUp() override {
2253 mFakeEventHub = std::make_unique<FakeEventHub>();
2254 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2255 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002256 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002257 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002258 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002259 identifier.name = DEVICE_NAME;
2260 identifier.location = DEVICE_LOCATION;
2261 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2262 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2263 identifier);
2264 mReader->pushNextDevice(mDevice);
2265 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002266 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002267 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002268
2269 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002270 mFakeListener.reset();
2271 mFakePolicy.clear();
2272 }
2273};
2274
2275const char* InputDeviceTest::DEVICE_NAME = "device";
2276const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2277const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2278const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002279const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002280const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2281 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002282const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002283const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2284
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002285TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002286 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002287 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2288 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002289}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002290
Michael Wrightd02c5b62014-02-10 15:10:22 -08002291TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2292 ASSERT_EQ(mDevice->isEnabled(), false);
2293}
2294
2295TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2296 // Configuration.
2297 InputReaderConfiguration config;
2298 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2299
2300 // Reset.
2301 unused += mDevice->reset(ARBITRARY_TIME);
2302
2303 NotifyDeviceResetArgs resetArgs;
2304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2305 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2306 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2307
2308 // Metadata.
2309 ASSERT_TRUE(mDevice->isIgnored());
2310 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2311
2312 InputDeviceInfo info = mDevice->getDeviceInfo();
2313 ASSERT_EQ(DEVICE_ID, info.getId());
2314 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2315 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2316 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2317
2318 // State queries.
2319 ASSERT_EQ(0, mDevice->getMetaState());
2320
2321 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2322 << "Ignored device should return unknown key code state.";
2323 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2324 << "Ignored device should return unknown scan code state.";
2325 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2326 << "Ignored device should return unknown switch state.";
2327
2328 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2329 uint8_t flags[2] = { 0, 1 };
2330 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2331 << "Ignored device should never mark any key codes.";
2332 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2333 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2334}
2335
2336TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2337 // Configuration.
2338 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002339
2340 FakeInputMapper& mapper1 =
2341 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2342 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002343 mapper1.setMetaState(AMETA_ALT_ON);
2344 mapper1.addSupportedKeyCode(AKEYCODE_A);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002345 mapper1.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002346 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2347 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2348 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2349 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2350 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2351
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002352 FakeInputMapper& mapper2 =
Michael Wrightd02c5b62014-02-10 15:10:22 -08002353 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
2354 mapper2.setMetaState(AMETA_SHIFT_ON);
2355
2356 InputReaderConfiguration config;
2357 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2358
Harry Cuttsf13161a2023-03-08 14:15:49 +00002359 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2360 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002361 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002362 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002363
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002364 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2365 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002366
2367 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002368 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002369 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2370 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002371
2372 NotifyDeviceResetArgs resetArgs;
2373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2374 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2375 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2376
2377 // Metadata.
2378 ASSERT_FALSE(mDevice->isIgnored());
2379 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2380
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002381 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002382 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002383 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002384 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2385 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2386
2387 // State queries.
2388 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2389 << "Should query mappers and combine meta states.";
2390
2391 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2392 << "Should return unknown key code state when source not supported.";
2393 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2394 << "Should return unknown scan code state when source not supported.";
2395 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2396 << "Should return unknown switch state when source not supported.";
2397
2398 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2399 << "Should query mapper when source is supported.";
2400 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2401 << "Should query mapper when source is supported.";
2402 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2403 << "Should query mapper when source is supported.";
2404
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002405 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002406 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002407 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002408 << "Should do nothing when source is unsupported.";
2409 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2410 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2411 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2412 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2413
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002414 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002415 << "Should query mapper when source is supported.";
2416 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2417 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2418 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2419 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2420
2421 // Event handling.
2422 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002423 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002424 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002425
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002426 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2427 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002428}
2429
Arthur Hung2c9a3342019-07-23 14:18:59 +08002430// A single input device is associated with a specific display. Check that:
2431// 1. Device is disabled if the viewport corresponding to the associated display is not found
2432// 2. Device is disabled when setEnabled API is called
2433TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002434 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002435
2436 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002437 std::list<NotifyArgs> unused =
2438 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002439
2440 // Device should be enabled by default.
2441 ASSERT_TRUE(mDevice->isEnabled());
2442
2443 // Prepare associated info.
2444 constexpr uint8_t hdmi = 1;
2445 const std::string UNIQUE_ID = "local:1";
2446
2447 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002448 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2449 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002450 // Device should be disabled because it is associated with a specific display via
2451 // input port <-> display port association, but the corresponding display is not found
2452 ASSERT_FALSE(mDevice->isEnabled());
2453
2454 // Prepare displays.
2455 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002456 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002457 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002458 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2459 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002460 ASSERT_TRUE(mDevice->isEnabled());
2461
2462 // Device should be disabled after set disable.
2463 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002464 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2465 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002466 ASSERT_FALSE(mDevice->isEnabled());
2467
2468 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002469 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2470 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002471 ASSERT_FALSE(mDevice->isEnabled());
2472}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002473
Christine Franks1ba71cc2021-04-07 14:37:42 -07002474TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2475 // Device should be enabled by default.
2476 mFakePolicy->clearViewports();
2477 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002478 std::list<NotifyArgs> unused =
2479 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002480 ASSERT_TRUE(mDevice->isEnabled());
2481
2482 // Device should be disabled because it is associated with a specific display, but the
2483 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002484 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002485 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2486 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002487 ASSERT_FALSE(mDevice->isEnabled());
2488
2489 // Device should be enabled when a display is found.
2490 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002491 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002492 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002493 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2494 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002495 ASSERT_TRUE(mDevice->isEnabled());
2496
2497 // Device should be disabled after set disable.
2498 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002499 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2500 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002501 ASSERT_FALSE(mDevice->isEnabled());
2502
2503 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002504 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2505 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002506 ASSERT_FALSE(mDevice->isEnabled());
2507}
2508
Christine Franks2a2293c2022-01-18 11:51:16 -08002509TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2510 mFakePolicy->clearViewports();
2511 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002512 std::list<NotifyArgs> unused =
2513 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08002514
Christine Franks2a2293c2022-01-18 11:51:16 -08002515 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2516 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002517 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002518 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002519 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2520 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002521 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2522}
2523
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002524/**
2525 * This test reproduces a crash caused by a dangling reference that remains after device is added
2526 * and removed. The reference is accessed in InputDevice::dump(..);
2527 */
2528TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2529 constexpr int32_t TEST_EVENTHUB_ID = 10;
2530 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2531
Harry Cutts33476232023-01-30 19:57:29 +00002532 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
2533 device.addEventHubDevice(TEST_EVENTHUB_ID, /*populateMappers=*/true);
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002534 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2535 std::string dumpStr, eventHubDevStr;
2536 device.dump(dumpStr, eventHubDevStr);
2537}
2538
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002539TEST_F(InputDeviceTest, GetBluetoothAddress) {
2540 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2541 ASSERT_TRUE(address);
2542 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2543}
2544
Michael Wrightd02c5b62014-02-10 15:10:22 -08002545// --- SwitchInputMapperTest ---
2546
2547class SwitchInputMapperTest : public InputMapperTest {
2548protected:
2549};
2550
2551TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002552 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002553
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002554 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002555}
2556
2557TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002558 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002559
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002560 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002561 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002562
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002563 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002564 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002565}
2566
2567TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002568 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002569 std::list<NotifyArgs> out;
2570 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2571 ASSERT_TRUE(out.empty());
2572 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2573 ASSERT_TRUE(out.empty());
2574 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2575 ASSERT_TRUE(out.empty());
2576 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002577
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002578 ASSERT_EQ(1u, out.size());
2579 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002580 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002581 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2582 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002583 args.switchMask);
2584 ASSERT_EQ(uint32_t(0), args.policyFlags);
2585}
2586
Chris Ye87143712020-11-10 05:05:58 +00002587// --- VibratorInputMapperTest ---
2588class VibratorInputMapperTest : public InputMapperTest {
2589protected:
2590 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2591};
2592
2593TEST_F(VibratorInputMapperTest, GetSources) {
2594 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2595
2596 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2597}
2598
2599TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2600 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2601
2602 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2603}
2604
2605TEST_F(VibratorInputMapperTest, Vibrate) {
2606 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002607 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002608 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2609
2610 VibrationElement pattern(2);
2611 VibrationSequence sequence(2);
2612 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002613 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2614 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002615 sequence.addElement(pattern);
2616 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002617 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2618 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002619 sequence.addElement(pattern);
2620
2621 std::vector<int64_t> timings = {0, 1};
2622 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2623
2624 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002625 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002626 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002627 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002628 // Verify vibrator state listener was notified.
2629 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002630 ASSERT_EQ(1u, out.size());
2631 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2632 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2633 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002634 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002635 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002636 ASSERT_FALSE(mapper.isVibrating());
2637 // Verify vibrator state listener was notified.
2638 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002639 ASSERT_EQ(1u, out.size());
2640 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2641 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2642 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002643}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002644
Chris Yef59a2f42020-10-16 12:55:26 -07002645// --- SensorInputMapperTest ---
2646
2647class SensorInputMapperTest : public InputMapperTest {
2648protected:
2649 static const int32_t ACCEL_RAW_MIN;
2650 static const int32_t ACCEL_RAW_MAX;
2651 static const int32_t ACCEL_RAW_FUZZ;
2652 static const int32_t ACCEL_RAW_FLAT;
2653 static const int32_t ACCEL_RAW_RESOLUTION;
2654
2655 static const int32_t GYRO_RAW_MIN;
2656 static const int32_t GYRO_RAW_MAX;
2657 static const int32_t GYRO_RAW_FUZZ;
2658 static const int32_t GYRO_RAW_FLAT;
2659 static const int32_t GYRO_RAW_RESOLUTION;
2660
2661 static const float GRAVITY_MS2_UNIT;
2662 static const float DEGREE_RADIAN_UNIT;
2663
2664 void prepareAccelAxes();
2665 void prepareGyroAxes();
2666 void setAccelProperties();
2667 void setGyroProperties();
2668 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2669};
2670
2671const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2672const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2673const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2674const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2675const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2676
2677const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2678const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2679const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2680const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2681const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2682
2683const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2684const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2685
2686void SensorInputMapperTest::prepareAccelAxes() {
2687 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2688 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2689 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2690 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2691 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2692 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2693}
2694
2695void SensorInputMapperTest::prepareGyroAxes() {
2696 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2697 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2698 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2699 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2700 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2701 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2702}
2703
2704void SensorInputMapperTest::setAccelProperties() {
2705 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2706 /* sensorDataIndex */ 0);
2707 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2708 /* sensorDataIndex */ 1);
2709 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2710 /* sensorDataIndex */ 2);
2711 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2712 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2713 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2714 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2715 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2716}
2717
2718void SensorInputMapperTest::setGyroProperties() {
2719 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2720 /* sensorDataIndex */ 0);
2721 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2722 /* sensorDataIndex */ 1);
2723 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2724 /* sensorDataIndex */ 2);
2725 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2726 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2727 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2728 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2729 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2730}
2731
2732TEST_F(SensorInputMapperTest, GetSources) {
2733 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2734
2735 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2736}
2737
2738TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2739 setAccelProperties();
2740 prepareAccelAxes();
2741 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2742
2743 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2744 std::chrono::microseconds(10000),
2745 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002746 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2748 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2749 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2750 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2751 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002752
2753 NotifySensorArgs args;
2754 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2755 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2756 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2757
2758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2759 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2760 ASSERT_EQ(args.deviceId, DEVICE_ID);
2761 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2762 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2763 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2764 ASSERT_EQ(args.values, values);
2765 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2766}
2767
2768TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2769 setGyroProperties();
2770 prepareGyroAxes();
2771 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2772
2773 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2774 std::chrono::microseconds(10000),
2775 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002776 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002782
2783 NotifySensorArgs args;
2784 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2785 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2786 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2787
2788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2789 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2790 ASSERT_EQ(args.deviceId, DEVICE_ID);
2791 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2792 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2793 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2794 ASSERT_EQ(args.values, values);
2795 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2796}
2797
Michael Wrightd02c5b62014-02-10 15:10:22 -08002798// --- KeyboardInputMapperTest ---
2799
2800class KeyboardInputMapperTest : public InputMapperTest {
2801protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002802 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002803 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002804 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002805
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002806 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002807 int32_t originalKeyCode, int32_t rotatedKeyCode,
2808 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002809};
2810
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002811/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2812 * orientation.
2813 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002814void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002815 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2816 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002817}
2818
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002819void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002820 int32_t originalScanCode, int32_t originalKeyCode,
2821 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002822 NotifyKeyArgs args;
2823
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2826 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2827 ASSERT_EQ(originalScanCode, args.scanCode);
2828 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002829 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002830
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2833 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2834 ASSERT_EQ(originalScanCode, args.scanCode);
2835 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002836 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002837}
2838
Michael Wrightd02c5b62014-02-10 15:10:22 -08002839TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002840 KeyboardInputMapper& mapper =
2841 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2842 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002843
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002844 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002845}
2846
2847TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2848 const int32_t USAGE_A = 0x070004;
2849 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002850 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2851 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002852 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2853 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2854 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002855
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002856 KeyboardInputMapper& mapper =
2857 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2858 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002859 // Initial metastate is AMETA_NONE.
2860 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002861
2862 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002863 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002864 NotifyKeyArgs args;
2865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2866 ASSERT_EQ(DEVICE_ID, args.deviceId);
2867 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2868 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2869 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2870 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2871 ASSERT_EQ(KEY_HOME, args.scanCode);
2872 ASSERT_EQ(AMETA_NONE, args.metaState);
2873 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2874 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2875 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2876
2877 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002878 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2880 ASSERT_EQ(DEVICE_ID, args.deviceId);
2881 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2882 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2883 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2884 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2885 ASSERT_EQ(KEY_HOME, args.scanCode);
2886 ASSERT_EQ(AMETA_NONE, args.metaState);
2887 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2888 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2889 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2890
2891 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2895 ASSERT_EQ(DEVICE_ID, args.deviceId);
2896 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2897 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2898 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2899 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2900 ASSERT_EQ(0, args.scanCode);
2901 ASSERT_EQ(AMETA_NONE, args.metaState);
2902 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2903 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2904 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2905
2906 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002907 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2908 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2910 ASSERT_EQ(DEVICE_ID, args.deviceId);
2911 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2912 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2913 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2914 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2915 ASSERT_EQ(0, args.scanCode);
2916 ASSERT_EQ(AMETA_NONE, args.metaState);
2917 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2918 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2919 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2920
2921 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002922 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2923 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2925 ASSERT_EQ(DEVICE_ID, args.deviceId);
2926 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2927 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2928 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2929 ASSERT_EQ(0, args.keyCode);
2930 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2931 ASSERT_EQ(AMETA_NONE, args.metaState);
2932 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2933 ASSERT_EQ(0U, args.policyFlags);
2934 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2935
2936 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002937 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2938 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2940 ASSERT_EQ(DEVICE_ID, args.deviceId);
2941 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2942 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2943 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2944 ASSERT_EQ(0, args.keyCode);
2945 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2946 ASSERT_EQ(AMETA_NONE, args.metaState);
2947 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2948 ASSERT_EQ(0U, args.policyFlags);
2949 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2950}
2951
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00002952TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
2953 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
2954 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
2955 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
2956
2957 KeyboardInputMapper& mapper =
2958 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2959 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2960
2961 // Key down by scan code.
2962 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
2963 NotifyKeyArgs args;
2964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2965 ASSERT_EQ(AKEYCODE_B, args.keyCode);
2966
2967 // Key up by scan code.
2968 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
2969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2970 ASSERT_EQ(AKEYCODE_B, args.keyCode);
2971}
2972
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002973/**
2974 * Ensure that the readTime is set to the time when the EV_KEY is received.
2975 */
2976TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
2977 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2978
2979 KeyboardInputMapper& mapper =
2980 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2981 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2982 NotifyKeyArgs args;
2983
2984 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00002985 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2987 ASSERT_EQ(12, args.readTime);
2988
2989 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00002990 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2992 ASSERT_EQ(15, args.readTime);
2993}
2994
Michael Wrightd02c5b62014-02-10 15:10:22 -08002995TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002996 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2997 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07002998 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
2999 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3000 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003001
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003002 KeyboardInputMapper& mapper =
3003 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3004 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003005
Arthur Hung95f68612022-04-07 14:08:22 +08003006 // Initial metastate is AMETA_NONE.
3007 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003008
3009 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003010 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003011 NotifyKeyArgs args;
3012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3013 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003014 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003015 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003016
3017 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003018 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3020 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003021 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003022
3023 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003024 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3026 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003027 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003028
3029 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003030 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3032 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003033 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003034 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003035}
3036
3037TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003038 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3039 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3040 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3041 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003042
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003043 KeyboardInputMapper& mapper =
3044 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3045 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003046
Michael Wrighta9cf4192022-12-01 23:46:39 +00003047 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003048 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3049 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3050 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3051 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3052 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3053 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3054 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3055 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3056}
3057
3058TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003059 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3060 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3061 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3062 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063
Michael Wrightd02c5b62014-02-10 15:10:22 -08003064 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003065 KeyboardInputMapper& mapper =
3066 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3067 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003068
Michael Wrighta9cf4192022-12-01 23:46:39 +00003069 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003070 ASSERT_NO_FATAL_FAILURE(
3071 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3072 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3073 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3074 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3075 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3076 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3077 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003078
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003079 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003080 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003081 ASSERT_NO_FATAL_FAILURE(
3082 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3083 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3084 AKEYCODE_DPAD_UP, DISPLAY_ID));
3085 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3086 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3087 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3088 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003090 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003091 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003092 ASSERT_NO_FATAL_FAILURE(
3093 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3094 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3095 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3096 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3097 AKEYCODE_DPAD_UP, DISPLAY_ID));
3098 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3099 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003101 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003102 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003103 ASSERT_NO_FATAL_FAILURE(
3104 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3105 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3106 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3107 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3108 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3109 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3110 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003111
3112 // Special case: if orientation changes while key is down, we still emit the same keycode
3113 // in the key up as we did in the key down.
3114 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003115 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003116 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003117 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3119 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3120 ASSERT_EQ(KEY_UP, args.scanCode);
3121 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3122
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003123 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003124 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3127 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3128 ASSERT_EQ(KEY_UP, args.scanCode);
3129 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3130}
3131
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003132TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3133 // If the keyboard is not orientation aware,
3134 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003135 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003136
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003137 KeyboardInputMapper& mapper =
3138 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3139 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003140 NotifyKeyArgs args;
3141
3142 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3147 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3148
Michael Wrighta9cf4192022-12-01 23:46:39 +00003149 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3154 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3155}
3156
3157TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3158 // If the keyboard is orientation aware,
3159 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003160 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003161
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003162 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003163 KeyboardInputMapper& mapper =
3164 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3165 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003166 NotifyKeyArgs args;
3167
3168 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3169 // ^--- already checked by the previous test
3170
Michael Wrighta9cf4192022-12-01 23:46:39 +00003171 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003172 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003173 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003175 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3177 ASSERT_EQ(DISPLAY_ID, args.displayId);
3178
3179 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003180 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003181 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003182 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3187 ASSERT_EQ(newDisplayId, args.displayId);
3188}
3189
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003191 KeyboardInputMapper& mapper =
3192 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3193 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003194
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003195 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003196 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003197
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003198 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003199 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003200}
3201
Philip Junker4af3b3d2021-12-14 10:36:55 +01003202TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3203 KeyboardInputMapper& mapper =
3204 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3205 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3206
3207 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3208 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3209 << "If a mapping is available, the result is equal to the mapping";
3210
3211 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3212 << "If no mapping is available, the result is the key location";
3213}
3214
Michael Wrightd02c5b62014-02-10 15:10:22 -08003215TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003216 KeyboardInputMapper& mapper =
3217 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3218 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003219
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003220 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003221 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003222
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003223 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003224 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003225}
3226
3227TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003228 KeyboardInputMapper& mapper =
3229 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3230 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003231
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003232 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003233
Michael Wrightd02c5b62014-02-10 15:10:22 -08003234 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003235 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003236 ASSERT_TRUE(flags[0]);
3237 ASSERT_FALSE(flags[1]);
3238}
3239
3240TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003241 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3242 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3243 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3244 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3245 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3246 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003247
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003248 KeyboardInputMapper& mapper =
3249 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3250 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003251 // Initial metastate is AMETA_NONE.
3252 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003253
3254 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003255 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3256 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3257 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258
3259 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003260 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3261 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003262 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3263 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3264 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003265 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266
3267 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003268 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3269 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003270 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3271 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3272 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003273 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003274
3275 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3277 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003278 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3279 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3280 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003281 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282
3283 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3285 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003286 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3287 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3288 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003289 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003290
3291 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003294 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3295 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3296 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003297 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003298
3299 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003300 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003302 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3303 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3304 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003305 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003306}
3307
Chris Yea52ade12020-08-27 16:49:20 -07003308TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3309 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3310 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3311 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3312 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3313
3314 KeyboardInputMapper& mapper =
3315 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3316 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3317
Chris Yea52ade12020-08-27 16:49:20 -07003318 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003319 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003320 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3321 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3322 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3323 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3324
3325 NotifyKeyArgs args;
3326 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003327 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3329 ASSERT_EQ(AMETA_NONE, args.metaState);
3330 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3331 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3332 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3333
3334 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003335 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3337 ASSERT_EQ(AMETA_NONE, args.metaState);
3338 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3339 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3340 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3341}
3342
Arthur Hung2c9a3342019-07-23 14:18:59 +08003343TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3344 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003345 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3346 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3347 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3348 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003349
3350 // keyboard 2.
3351 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003352 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003353 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003354 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003355 std::shared_ptr<InputDevice> device2 =
3356 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003357 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003358
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003359 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3360 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3361 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3362 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003363
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003364 KeyboardInputMapper& mapper =
3365 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3366 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003367
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003368 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003369 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003370 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003371 std::list<NotifyArgs> unused =
3372 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003373 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003374 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003375
3376 // Prepared displays and associated info.
3377 constexpr uint8_t hdmi1 = 0;
3378 constexpr uint8_t hdmi2 = 1;
3379 const std::string SECONDARY_UNIQUE_ID = "local:1";
3380
3381 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3382 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3383
3384 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003385 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3386 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003387 ASSERT_FALSE(device2->isEnabled());
3388
3389 // Prepare second display.
3390 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003391 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003392 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003393 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003394 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003395 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003396 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3397 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003398
3399 // Device should be enabled after the associated display is found.
3400 ASSERT_TRUE(mDevice->isEnabled());
3401 ASSERT_TRUE(device2->isEnabled());
3402
3403 // Test pad key events
3404 ASSERT_NO_FATAL_FAILURE(
3405 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3406 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3407 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3408 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3409 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3410 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3411 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3412
3413 ASSERT_NO_FATAL_FAILURE(
3414 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3415 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3416 AKEYCODE_DPAD_RIGHT, newDisplayId));
3417 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3418 AKEYCODE_DPAD_DOWN, newDisplayId));
3419 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3420 AKEYCODE_DPAD_LEFT, newDisplayId));
3421}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003422
arthurhungc903df12020-08-11 15:08:42 +08003423TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3424 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3425 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3426 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3427 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3428 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3429 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3430
3431 KeyboardInputMapper& mapper =
3432 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3433 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003434 // Initial metastate is AMETA_NONE.
3435 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003436
3437 // Initialization should have turned all of the lights off.
3438 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3439 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3440 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3441
3442 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003443 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003445 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3446 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3447
3448 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003451 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3452 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3453
3454 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003457 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3458 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3459
3460 mFakeEventHub->removeDevice(EVENTHUB_ID);
3461 mReader->loopOnce();
3462
3463 // keyboard 2 should default toggle keys.
3464 const std::string USB2 = "USB2";
3465 const std::string DEVICE_NAME2 = "KEYBOARD2";
3466 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3467 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3468 std::shared_ptr<InputDevice> device2 =
3469 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003470 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003471 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3472 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3473 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3474 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3475 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3476 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3477
arthurhung6fe95782020-10-05 22:41:16 +08003478 KeyboardInputMapper& mapper2 =
3479 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3480 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003481 std::list<NotifyArgs> unused =
3482 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003483 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003484 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003485
3486 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3487 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3488 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003489 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3490 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003491}
3492
Arthur Hungcb40a002021-08-03 14:31:01 +00003493TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3494 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3495 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3496 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3497
3498 // Suppose we have two mappers. (DPAD + KEYBOARD)
3499 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3500 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3501 KeyboardInputMapper& mapper =
3502 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3503 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003504 // Initial metastate is AMETA_NONE.
3505 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003506
3507 mReader->toggleCapsLockState(DEVICE_ID);
3508 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3509}
3510
Arthur Hungfb3cc112022-04-13 07:39:50 +00003511TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3512 // keyboard 1.
3513 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3514 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3515 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3516 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3517 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3518 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3519
3520 KeyboardInputMapper& mapper1 =
3521 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3522 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3523
3524 // keyboard 2.
3525 const std::string USB2 = "USB2";
3526 const std::string DEVICE_NAME2 = "KEYBOARD2";
3527 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3528 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3529 std::shared_ptr<InputDevice> device2 =
3530 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3531 ftl::Flags<InputDeviceClass>(0));
3532 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3533 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3534 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3535 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3536 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3537 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3538
3539 KeyboardInputMapper& mapper2 =
3540 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3541 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003542 std::list<NotifyArgs> unused =
3543 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003544 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003545 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003546
Arthur Hung95f68612022-04-07 14:08:22 +08003547 // Initial metastate is AMETA_NONE.
3548 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3549 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3550
3551 // Toggle num lock on and off.
3552 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3553 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003554 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3555 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3556 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3557
3558 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3559 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3560 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3561 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3562 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3563
3564 // Toggle caps lock on and off.
3565 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3566 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3567 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3568 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3569 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3570
3571 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3572 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3573 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3574 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3575 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3576
3577 // Toggle scroll lock on and off.
3578 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3579 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3580 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3581 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3582 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3583
3584 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3585 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3586 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3587 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3588 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3589}
3590
Arthur Hung2141d542022-08-23 07:45:21 +00003591TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3592 const int32_t USAGE_A = 0x070004;
3593 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3594 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3595
3596 KeyboardInputMapper& mapper =
3597 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3598 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3599 // Key down by scan code.
3600 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3601 NotifyKeyArgs args;
3602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3603 ASSERT_EQ(DEVICE_ID, args.deviceId);
3604 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3605 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3606 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3607 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3608 ASSERT_EQ(KEY_HOME, args.scanCode);
3609 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3610
3611 // Disable device, it should synthesize cancellation events for down events.
3612 mFakePolicy->addDisabledDevice(DEVICE_ID);
3613 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
3614
3615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3616 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3617 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3618 ASSERT_EQ(KEY_HOME, args.scanCode);
3619 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3620}
3621
Zixuan Qufecb6062022-11-12 04:44:31 +00003622TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
3623 mDevice->addMapper<KeyboardInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3624 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3625 std::list<NotifyArgs> unused =
3626 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
3627
3628 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3629
3630 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3631 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUT_ASSOCIATION);
3632
3633 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3634 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3635 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3636 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3637 deviceInfo.getKeyboardLayoutInfo()->layoutType);
3638}
3639
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003640TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3641 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3642 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3643
3644 // Configuration
3645 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3646 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3647 InputReaderConfiguration config;
3648 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
3649
3650 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3651 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3652}
3653
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003654// --- KeyboardInputMapperTest_ExternalDevice ---
3655
3656class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3657protected:
Chris Yea52ade12020-08-27 16:49:20 -07003658 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003659};
3660
3661TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003662 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3663 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003664
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003665 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3666 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3667 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3668 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003669
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003670 KeyboardInputMapper& mapper =
3671 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3672 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003673
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003674 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003675 NotifyKeyArgs args;
3676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3677 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3678
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003679 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3681 ASSERT_EQ(uint32_t(0), args.policyFlags);
3682
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003685 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003686
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003687 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3689 ASSERT_EQ(uint32_t(0), args.policyFlags);
3690
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3693 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3694
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003695 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3697 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3698}
3699
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003700TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003701 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003702
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003703 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3704 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3705 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003706
Powei Fengd041c5d2019-05-03 17:11:33 -07003707 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003708 KeyboardInputMapper& mapper =
3709 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3710 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003711
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003713 NotifyKeyArgs args;
3714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3715 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3716
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003717 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3719 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3720
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003721 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3723 ASSERT_EQ(uint32_t(0), args.policyFlags);
3724
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003725 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3727 ASSERT_EQ(uint32_t(0), args.policyFlags);
3728
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003729 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3731 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3732
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003733 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3735 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3736}
3737
Michael Wrightd02c5b62014-02-10 15:10:22 -08003738// --- CursorInputMapperTest ---
3739
3740class CursorInputMapperTest : public InputMapperTest {
3741protected:
3742 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3743
Michael Wright17db18e2020-06-26 20:51:44 +01003744 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003745
Chris Yea52ade12020-08-27 16:49:20 -07003746 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003747 InputMapperTest::SetUp();
3748
Michael Wright17db18e2020-06-26 20:51:44 +01003749 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003750 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003751 }
3752
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003753 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3754 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003755
Michael Wrighta9cf4192022-12-01 23:46:39 +00003756 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003757 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3758 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3759 }
3760
3761 void prepareSecondaryDisplay() {
3762 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003763 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003764 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003765 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003766
3767 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3768 float pressure) {
3769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3770 0.0f, 0.0f, 0.0f, EPSILON));
3771 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003772};
3773
3774const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3775
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003776void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3777 int32_t originalY, int32_t rotatedX,
3778 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003779 NotifyMotionArgs args;
3780
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3785 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003786 ASSERT_NO_FATAL_FAILURE(
3787 assertCursorPointerCoords(args.pointerCoords[0],
3788 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3789 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003790}
3791
3792TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003793 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003794 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003795
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003796 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797}
3798
3799TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003800 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003801 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003802
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003803 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003804}
3805
3806TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003807 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003808 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003809
3810 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003811 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003812
3813 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003814 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3815 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003816 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3817 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3818
3819 // When the bounds are set, then there should be a valid motion range.
3820 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3821
3822 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003823 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003824
3825 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3826 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3827 1, 800 - 1, 0.0f, 0.0f));
3828 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3829 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3830 2, 480 - 1, 0.0f, 0.0f));
3831 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3832 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3833 0.0f, 1.0f, 0.0f, 0.0f));
3834}
3835
3836TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003837 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003838 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003839
3840 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003841 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003842
3843 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3844 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3845 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3846 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3847 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3848 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3849 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3850 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3851 0.0f, 1.0f, 0.0f, 0.0f));
3852}
3853
3854TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003855 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003856 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003857
arthurhungdcef2dc2020-08-11 14:47:50 +08003858 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003859
3860 NotifyMotionArgs args;
3861
3862 // Button press.
3863 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003864 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3865 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3867 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3868 ASSERT_EQ(DEVICE_ID, args.deviceId);
3869 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3870 ASSERT_EQ(uint32_t(0), args.policyFlags);
3871 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3872 ASSERT_EQ(0, args.flags);
3873 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3874 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3875 ASSERT_EQ(0, args.edgeFlags);
3876 ASSERT_EQ(uint32_t(1), args.pointerCount);
3877 ASSERT_EQ(0, args.pointerProperties[0].id);
3878 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003879 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003880 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3881 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3882 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3883
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3885 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3886 ASSERT_EQ(DEVICE_ID, args.deviceId);
3887 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3888 ASSERT_EQ(uint32_t(0), args.policyFlags);
3889 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3890 ASSERT_EQ(0, args.flags);
3891 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3892 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3893 ASSERT_EQ(0, args.edgeFlags);
3894 ASSERT_EQ(uint32_t(1), args.pointerCount);
3895 ASSERT_EQ(0, args.pointerProperties[0].id);
3896 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003897 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003898 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3899 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3900 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3901
Michael Wrightd02c5b62014-02-10 15:10:22 -08003902 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003903 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3904 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3906 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3907 ASSERT_EQ(DEVICE_ID, args.deviceId);
3908 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3909 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003910 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3911 ASSERT_EQ(0, args.flags);
3912 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3913 ASSERT_EQ(0, args.buttonState);
3914 ASSERT_EQ(0, args.edgeFlags);
3915 ASSERT_EQ(uint32_t(1), args.pointerCount);
3916 ASSERT_EQ(0, args.pointerProperties[0].id);
3917 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003918 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003919 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3920 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3921 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3922
3923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3924 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3925 ASSERT_EQ(DEVICE_ID, args.deviceId);
3926 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3927 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003928 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3929 ASSERT_EQ(0, args.flags);
3930 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3931 ASSERT_EQ(0, args.buttonState);
3932 ASSERT_EQ(0, args.edgeFlags);
3933 ASSERT_EQ(uint32_t(1), args.pointerCount);
3934 ASSERT_EQ(0, args.pointerProperties[0].id);
3935 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003936 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003937 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3938 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3939 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3940}
3941
3942TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003943 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003944 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003945
3946 NotifyMotionArgs args;
3947
3948 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
3950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3952 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003953 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3954 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
3955 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003956
3957 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003958 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
3959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3961 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003962 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
3963 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003964}
3965
3966TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003967 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003968 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003969
3970 NotifyMotionArgs args;
3971
3972 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3974 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3976 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003977 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003978
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3980 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003981 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003982
Michael Wrightd02c5b62014-02-10 15:10:22 -08003983 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3985 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003987 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003988 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003989
3990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003991 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003992 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003993}
3994
3995TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003996 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003997 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003998
3999 NotifyMotionArgs args;
4000
4001 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004002 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4005 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4007 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004008 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4009 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4010 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004011
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4013 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004014 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4015 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4016 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004017
Michael Wrightd02c5b62014-02-10 15:10:22 -08004018 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004024 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4025 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4026 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004027
4028 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4030 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004032 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004033 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004034
4035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004036 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004037 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004038}
4039
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004040TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004041 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004042 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004043 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4044 // need to be rotated.
4045 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004046 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004047
Michael Wrighta9cf4192022-12-01 23:46:39 +00004048 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4050 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4051 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4052 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4053 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4054 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4055 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4056 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4057}
4058
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004059TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004060 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004061 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004062 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4063 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004064 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004065
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004066 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004067 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004068 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4069 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4070 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4071 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4072 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4073 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4074 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4075 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4076
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004077 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004078 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004079 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4080 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4081 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4082 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4083 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4084 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4085 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4086 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004088 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004089 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004090 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4091 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4092 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4093 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4094 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4095 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4096 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4097 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4098
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004099 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004100 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004101 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4102 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4103 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4104 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4105 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4106 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4107 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4108 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109}
4110
4111TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004113 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004114
4115 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4116 mFakePointerController->setPosition(100, 200);
4117 mFakePointerController->setButtonState(0);
4118
4119 NotifyMotionArgs motionArgs;
4120 NotifyKeyArgs keyArgs;
4121
4122 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4126 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4127 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4128 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004129 ASSERT_NO_FATAL_FAILURE(
4130 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004131
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4133 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4134 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4135 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004136 ASSERT_NO_FATAL_FAILURE(
4137 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004138
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004142 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143 ASSERT_EQ(0, motionArgs.buttonState);
4144 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004145 ASSERT_NO_FATAL_FAILURE(
4146 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004147
4148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004149 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004150 ASSERT_EQ(0, motionArgs.buttonState);
4151 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004152 ASSERT_NO_FATAL_FAILURE(
4153 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004154
4155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004156 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004157 ASSERT_EQ(0, motionArgs.buttonState);
4158 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004159 ASSERT_NO_FATAL_FAILURE(
4160 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004161
4162 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4167 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4168 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4169 motionArgs.buttonState);
4170 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4171 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004172 ASSERT_NO_FATAL_FAILURE(
4173 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004174
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4176 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4177 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4178 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4179 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004180 ASSERT_NO_FATAL_FAILURE(
4181 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004182
4183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4184 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4185 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4186 motionArgs.buttonState);
4187 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4188 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004189 ASSERT_NO_FATAL_FAILURE(
4190 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004191
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004192 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004195 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4197 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004198 ASSERT_NO_FATAL_FAILURE(
4199 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004200
4201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004203 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4204 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004205 ASSERT_NO_FATAL_FAILURE(
4206 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004207
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004211 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4212 ASSERT_EQ(0, motionArgs.buttonState);
4213 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004214 ASSERT_NO_FATAL_FAILURE(
4215 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004218
4219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004220 ASSERT_EQ(0, motionArgs.buttonState);
4221 ASSERT_EQ(0, mFakePointerController->getButtonState());
4222 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004223 ASSERT_NO_FATAL_FAILURE(
4224 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004225
Michael Wrightd02c5b62014-02-10 15:10:22 -08004226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4227 ASSERT_EQ(0, motionArgs.buttonState);
4228 ASSERT_EQ(0, mFakePointerController->getButtonState());
4229 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004230 ASSERT_NO_FATAL_FAILURE(
4231 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232
4233 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4237 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4238 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004239
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004241 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4243 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004244 ASSERT_NO_FATAL_FAILURE(
4245 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004246
4247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4248 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4249 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4250 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004251 ASSERT_NO_FATAL_FAILURE(
4252 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004254 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4255 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004257 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004258 ASSERT_EQ(0, motionArgs.buttonState);
4259 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004260 ASSERT_NO_FATAL_FAILURE(
4261 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004262
4263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004265 ASSERT_EQ(0, motionArgs.buttonState);
4266 ASSERT_EQ(0, mFakePointerController->getButtonState());
4267
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004268 ASSERT_NO_FATAL_FAILURE(
4269 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4271 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4272 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4273
4274 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4278 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4279 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004280
Michael Wrightd02c5b62014-02-10 15:10:22 -08004281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004282 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4284 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004285 ASSERT_NO_FATAL_FAILURE(
4286 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004287
4288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4289 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4290 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4291 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004292 ASSERT_NO_FATAL_FAILURE(
4293 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004298 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299 ASSERT_EQ(0, motionArgs.buttonState);
4300 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004301 ASSERT_NO_FATAL_FAILURE(
4302 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004303
4304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4305 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4306 ASSERT_EQ(0, motionArgs.buttonState);
4307 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004308 ASSERT_NO_FATAL_FAILURE(
4309 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004310
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4312 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4313 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4314
4315 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4319 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4320 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004321
Michael Wrightd02c5b62014-02-10 15:10:22 -08004322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004323 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004324 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4325 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
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);
4332 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004333 ASSERT_NO_FATAL_FAILURE(
4334 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4337 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004339 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340 ASSERT_EQ(0, motionArgs.buttonState);
4341 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004342 ASSERT_NO_FATAL_FAILURE(
4343 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004344
4345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4346 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4347 ASSERT_EQ(0, motionArgs.buttonState);
4348 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004349 ASSERT_NO_FATAL_FAILURE(
4350 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004351
Michael Wrightd02c5b62014-02-10 15:10:22 -08004352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4353 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4354 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4355
4356 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4360 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4361 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004362
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004364 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4366 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004367 ASSERT_NO_FATAL_FAILURE(
4368 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004369
4370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4371 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4372 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4373 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004374 ASSERT_NO_FATAL_FAILURE(
4375 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004380 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381 ASSERT_EQ(0, motionArgs.buttonState);
4382 ASSERT_EQ(0, mFakePointerController->getButtonState());
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
4386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4387 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4388 ASSERT_EQ(0, motionArgs.buttonState);
4389 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004390 ASSERT_NO_FATAL_FAILURE(
4391 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004392
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4394 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4395 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4396}
4397
4398TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004399 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004400 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004401
4402 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4403 mFakePointerController->setPosition(100, 200);
4404 mFakePointerController->setButtonState(0);
4405
4406 NotifyMotionArgs args;
4407
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004412 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4413 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4415 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 +00004416 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004417}
4418
4419TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004420 addConfigurationProperty("cursor.mode", "pointer");
4421 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004422 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004423
4424 NotifyDeviceResetArgs resetArgs;
4425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4426 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4427 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4428
4429 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4430 mFakePointerController->setPosition(100, 200);
4431 mFakePointerController->setButtonState(0);
4432
4433 NotifyMotionArgs args;
4434
4435 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004436 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4437 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4438 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4440 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4441 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4443 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 +00004444 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004445
4446 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004447 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4450 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4451 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4453 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4455 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4456 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4457 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4458 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4459
4460 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004461 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4462 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4464 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4465 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4467 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4469 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4470 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4472 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4473
4474 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4479 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4481 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4482 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 +00004483 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004484
4485 // Disable pointer capture and check that the device generation got bumped
4486 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004487 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004488 mFakePolicy->setPointerCapture(false);
4489 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004490 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004491
4492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004493 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4494
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4497 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4499 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004500 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4501 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4502 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 +00004503 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004504}
4505
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004506/**
4507 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4508 * pointer acceleration or speed processing should not be applied.
4509 */
4510TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4511 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004512 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4513 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004514 mFakePolicy->setVelocityControlParams(testParams);
4515 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4516
4517 NotifyDeviceResetArgs resetArgs;
4518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4519 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4520 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4521
4522 NotifyMotionArgs args;
4523
4524 // Move and verify scale is applied.
4525 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4527 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4529 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4530 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4531 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4532 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4533 ASSERT_GT(relX, 10);
4534 ASSERT_GT(relY, 20);
4535
4536 // Enable Pointer Capture
4537 mFakePolicy->setPointerCapture(true);
4538 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4539 NotifyPointerCaptureChangedArgs captureArgs;
4540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4541 ASSERT_TRUE(captureArgs.request.enable);
4542
4543 // Move and verify scale is not applied.
4544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4548 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4550 ASSERT_EQ(10, args.pointerCoords[0].getX());
4551 ASSERT_EQ(20, args.pointerCoords[0].getY());
4552}
4553
Prabir Pradhan208360b2022-06-24 18:37:04 +00004554TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4555 addConfigurationProperty("cursor.mode", "pointer");
4556 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4557
4558 NotifyDeviceResetArgs resetArgs;
4559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4560 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4561 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4562
4563 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004564 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004565
4566 NotifyMotionArgs args;
4567
4568 // Verify that the coordinates are rotated.
4569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4570 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4573 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4574 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4575 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4576 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4577
4578 // Enable Pointer Capture.
4579 mFakePolicy->setPointerCapture(true);
4580 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4581 NotifyPointerCaptureChangedArgs captureArgs;
4582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4583 ASSERT_TRUE(captureArgs.request.enable);
4584
4585 // Move and verify rotation is not applied.
4586 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4588 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4590 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4591 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4592 ASSERT_EQ(10, args.pointerCoords[0].getX());
4593 ASSERT_EQ(20, args.pointerCoords[0].getY());
4594}
4595
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004596TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004597 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004598
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004599 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004600 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004601
4602 // Set up the secondary display as the display on which the pointer should be shown.
4603 // The InputDevice is not associated with any display.
4604 prepareSecondaryDisplay();
4605 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08004606 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4607
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004608 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004609 mFakePointerController->setPosition(100, 200);
4610 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004611
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004612 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4615 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004617 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4618 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4619 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004620 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004621}
4622
4623TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
4624 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4625
4626 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004627 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004628
4629 // Set up the secondary display as the display on which the pointer should be shown,
4630 // and associate the InputDevice with the secondary display.
4631 prepareSecondaryDisplay();
4632 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4633 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4634 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4635
4636 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4637 mFakePointerController->setPosition(100, 200);
4638 mFakePointerController->setButtonState(0);
4639
4640 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4642 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004644 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4645 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4646 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004647 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004648}
4649
4650TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
4651 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4652
4653 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004654 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004655 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4656
4657 // Associate the InputDevice with the secondary display.
4658 prepareSecondaryDisplay();
4659 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4660 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4661
4662 // The mapper should not generate any events because it is associated with a display that is
4663 // different from the pointer display.
4664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4665 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4666 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004668}
4669
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004670// --- BluetoothCursorInputMapperTest ---
4671
4672class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4673protected:
4674 void SetUp() override {
4675 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4676
4677 mFakePointerController = std::make_shared<FakePointerController>();
4678 mFakePolicy->setPointerController(mFakePointerController);
4679 }
4680};
4681
4682TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4683 addConfigurationProperty("cursor.mode", "pointer");
4684 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4685
4686 nsecs_t kernelEventTime = ARBITRARY_TIME;
4687 nsecs_t expectedEventTime = ARBITRARY_TIME;
4688 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4689 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4691 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4692 WithEventTime(expectedEventTime))));
4693
4694 // Process several events that come in quick succession, according to their timestamps.
4695 for (int i = 0; i < 3; i++) {
4696 constexpr static nsecs_t delta = ms2ns(1);
4697 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4698 kernelEventTime += delta;
4699 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4700
4701 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4702 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4704 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4705 WithEventTime(expectedEventTime))));
4706 }
4707}
4708
4709TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4710 addConfigurationProperty("cursor.mode", "pointer");
4711 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4712
4713 nsecs_t expectedEventTime = ARBITRARY_TIME;
4714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4715 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4717 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4718 WithEventTime(expectedEventTime))));
4719
4720 // Process several events with the same timestamp from the kernel.
4721 // Ensure that we do not generate events too far into the future.
4722 constexpr static int32_t numEvents =
4723 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4724 for (int i = 0; i < numEvents; i++) {
4725 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4726
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(expectedEventTime))));
4732 }
4733
4734 // By processing more events with the same timestamp, we should not generate events with a
4735 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4736 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4737 for (int i = 0; i < 3; i++) {
4738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4739 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4741 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4742 WithEventTime(cappedEventTime))));
4743 }
4744}
4745
4746TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4747 addConfigurationProperty("cursor.mode", "pointer");
4748 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4749
4750 nsecs_t kernelEventTime = ARBITRARY_TIME;
4751 nsecs_t expectedEventTime = ARBITRARY_TIME;
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 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4759 // smoothening is not needed, its timestamp is not affected.
4760 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4761 expectedEventTime = kernelEventTime;
4762
4763 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4764 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4766 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4767 WithEventTime(expectedEventTime))));
4768}
4769
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770// --- TouchInputMapperTest ---
4771
4772class TouchInputMapperTest : public InputMapperTest {
4773protected:
4774 static const int32_t RAW_X_MIN;
4775 static const int32_t RAW_X_MAX;
4776 static const int32_t RAW_Y_MIN;
4777 static const int32_t RAW_Y_MAX;
4778 static const int32_t RAW_TOUCH_MIN;
4779 static const int32_t RAW_TOUCH_MAX;
4780 static const int32_t RAW_TOOL_MIN;
4781 static const int32_t RAW_TOOL_MAX;
4782 static const int32_t RAW_PRESSURE_MIN;
4783 static const int32_t RAW_PRESSURE_MAX;
4784 static const int32_t RAW_ORIENTATION_MIN;
4785 static const int32_t RAW_ORIENTATION_MAX;
4786 static const int32_t RAW_DISTANCE_MIN;
4787 static const int32_t RAW_DISTANCE_MAX;
4788 static const int32_t RAW_TILT_MIN;
4789 static const int32_t RAW_TILT_MAX;
4790 static const int32_t RAW_ID_MIN;
4791 static const int32_t RAW_ID_MAX;
4792 static const int32_t RAW_SLOT_MIN;
4793 static const int32_t RAW_SLOT_MAX;
4794 static const float X_PRECISION;
4795 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004796 static const float X_PRECISION_VIRTUAL;
4797 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798
4799 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004800 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801
4802 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4803
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004804 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004805 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004806
Michael Wrightd02c5b62014-02-10 15:10:22 -08004807 enum Axes {
4808 POSITION = 1 << 0,
4809 TOUCH = 1 << 1,
4810 TOOL = 1 << 2,
4811 PRESSURE = 1 << 3,
4812 ORIENTATION = 1 << 4,
4813 MINOR = 1 << 5,
4814 ID = 1 << 6,
4815 DISTANCE = 1 << 7,
4816 TILT = 1 << 8,
4817 SLOT = 1 << 9,
4818 TOOL_TYPE = 1 << 10,
4819 };
4820
Michael Wrighta9cf4192022-12-01 23:46:39 +00004821 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004822 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004823 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004824 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004825 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826 int32_t toRawX(float displayX);
4827 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004828 int32_t toRotatedRawX(float displayX);
4829 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004830 float toCookedX(float rawX, float rawY);
4831 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004832 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004833 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004835 float toDisplayY(int32_t rawY, int32_t displayHeight);
4836
Michael Wrightd02c5b62014-02-10 15:10:22 -08004837};
4838
4839const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4840const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4841const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4842const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4843const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4844const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4845const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4846const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004847const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4848const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004849const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4850const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4851const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4852const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4853const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4854const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4855const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4856const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4857const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4858const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4859const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4860const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004861const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4862 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4863const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4864 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004865const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4866 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004867
4868const float TouchInputMapperTest::GEOMETRIC_SCALE =
4869 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4870 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4871
4872const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4873 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4874 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4875};
4876
Michael Wrighta9cf4192022-12-01 23:46:39 +00004877void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004878 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4879 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004880}
4881
4882void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4883 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004884 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004885}
4886
Michael Wrighta9cf4192022-12-01 23:46:39 +00004887void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004888 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4889 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4890 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004891}
4892
Michael Wrightd02c5b62014-02-10 15:10:22 -08004893void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004894 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4895 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4896 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4897 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004898}
4899
Jason Gerecke489fda82012-09-07 17:19:40 -07004900void TouchInputMapperTest::prepareLocationCalibration() {
4901 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4902}
4903
Michael Wrightd02c5b62014-02-10 15:10:22 -08004904int32_t TouchInputMapperTest::toRawX(float displayX) {
4905 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4906}
4907
4908int32_t TouchInputMapperTest::toRawY(float displayY) {
4909 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4910}
4911
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004912int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4913 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4914}
4915
4916int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4917 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4918}
4919
Jason Gerecke489fda82012-09-07 17:19:40 -07004920float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4921 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4922 return rawX;
4923}
4924
4925float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4926 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4927 return rawY;
4928}
4929
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004931 return toDisplayX(rawX, DISPLAY_WIDTH);
4932}
4933
4934float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4935 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936}
4937
4938float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004939 return toDisplayY(rawY, DISPLAY_HEIGHT);
4940}
4941
4942float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4943 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004944}
4945
4946
4947// --- SingleTouchInputMapperTest ---
4948
4949class SingleTouchInputMapperTest : public TouchInputMapperTest {
4950protected:
4951 void prepareButtons();
4952 void prepareAxes(int axes);
4953
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004954 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4955 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4956 void processUp(SingleTouchInputMapper& mappery);
4957 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4958 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4959 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4960 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4961 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4962 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004963};
4964
4965void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004966 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967}
4968
4969void SingleTouchInputMapperTest::prepareAxes(int axes) {
4970 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004971 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4972 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004973 }
4974 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004975 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4976 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004977 }
4978 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004979 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4980 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004981 }
4982 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004983 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4984 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004985 }
4986 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004987 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4988 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004989 }
4990}
4991
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004992void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004996}
4997
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004998void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005001}
5002
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005003void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005005}
5006
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005007void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005008 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009}
5010
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005011void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5012 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005013 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005014}
5015
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005016void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005018}
5019
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005020void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5021 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005024}
5025
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005026void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5027 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005028 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005029}
5030
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005031void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005032 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005033}
5034
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005036 prepareButtons();
5037 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005038 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005039
Josep del Río2d8c79a2023-01-23 19:33:50 +00005040 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005041}
5042
Michael Wrightd02c5b62014-02-10 15:10:22 -08005043TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005044 prepareButtons();
5045 prepareAxes(POSITION);
5046 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005047 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005048
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005049 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005050}
5051
5052TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005053 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005054 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005055 prepareButtons();
5056 prepareAxes(POSITION);
5057 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005058 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005059
5060 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005061 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062
5063 // Virtual key is down.
5064 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5065 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5066 processDown(mapper, x, y);
5067 processSync(mapper);
5068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5069
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005070 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005071
5072 // Virtual key is up.
5073 processUp(mapper);
5074 processSync(mapper);
5075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5076
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005077 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005078}
5079
5080TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005081 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005082 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005083 prepareButtons();
5084 prepareAxes(POSITION);
5085 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005086 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087
5088 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005089 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005090
5091 // Virtual key is down.
5092 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5093 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5094 processDown(mapper, x, y);
5095 processSync(mapper);
5096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5097
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005098 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005099
5100 // Virtual key is up.
5101 processUp(mapper);
5102 processSync(mapper);
5103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5104
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005105 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005106}
5107
5108TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005109 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005110 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111 prepareButtons();
5112 prepareAxes(POSITION);
5113 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005114 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005115
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005117 ASSERT_TRUE(
5118 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005119 ASSERT_TRUE(flags[0]);
5120 ASSERT_FALSE(flags[1]);
5121}
5122
5123TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005124 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005125 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005126 prepareButtons();
5127 prepareAxes(POSITION);
5128 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005129 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130
arthurhungdcef2dc2020-08-11 14:47:50 +08005131 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005132
5133 NotifyKeyArgs args;
5134
5135 // Press virtual key.
5136 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5137 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5138 processDown(mapper, x, y);
5139 processSync(mapper);
5140
5141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5142 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5143 ASSERT_EQ(DEVICE_ID, args.deviceId);
5144 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5145 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5146 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5147 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5148 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5149 ASSERT_EQ(KEY_HOME, args.scanCode);
5150 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5151 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5152
5153 // Release virtual key.
5154 processUp(mapper);
5155 processSync(mapper);
5156
5157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5158 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5159 ASSERT_EQ(DEVICE_ID, args.deviceId);
5160 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5161 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5162 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5163 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5164 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5165 ASSERT_EQ(KEY_HOME, args.scanCode);
5166 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5167 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5168
5169 // Should not have sent any motions.
5170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5171}
5172
5173TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005174 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005175 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005176 prepareButtons();
5177 prepareAxes(POSITION);
5178 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005179 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180
arthurhungdcef2dc2020-08-11 14:47:50 +08005181 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005182
5183 NotifyKeyArgs keyArgs;
5184
5185 // Press virtual key.
5186 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5187 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5188 processDown(mapper, x, y);
5189 processSync(mapper);
5190
5191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5192 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5193 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5194 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5195 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5196 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5197 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5198 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5199 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5200 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5201 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5202
5203 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5204 // into the display area.
5205 y -= 100;
5206 processMove(mapper, x, y);
5207 processSync(mapper);
5208
5209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5210 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5211 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5212 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5213 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5214 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5215 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5216 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5217 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5218 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5219 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5220 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5221
5222 NotifyMotionArgs motionArgs;
5223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5224 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5225 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5226 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5227 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5228 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5229 ASSERT_EQ(0, motionArgs.flags);
5230 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5231 ASSERT_EQ(0, motionArgs.buttonState);
5232 ASSERT_EQ(0, motionArgs.edgeFlags);
5233 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5234 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5235 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5237 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5238 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5239 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5240 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5241
5242 // Keep moving out of bounds. Should generate a pointer move.
5243 y -= 50;
5244 processMove(mapper, x, y);
5245 processSync(mapper);
5246
5247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5248 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5249 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5250 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5251 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5253 ASSERT_EQ(0, motionArgs.flags);
5254 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5255 ASSERT_EQ(0, motionArgs.buttonState);
5256 ASSERT_EQ(0, motionArgs.edgeFlags);
5257 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5258 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5259 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5261 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5262 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5263 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5264 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5265
5266 // Release out of bounds. Should generate a pointer up.
5267 processUp(mapper);
5268 processSync(mapper);
5269
5270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5271 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5272 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5273 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5274 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5275 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5276 ASSERT_EQ(0, motionArgs.flags);
5277 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5278 ASSERT_EQ(0, motionArgs.buttonState);
5279 ASSERT_EQ(0, motionArgs.edgeFlags);
5280 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5281 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5282 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5283 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5284 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5285 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5286 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5287 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5288
5289 // Should not have sent any more keys or motions.
5290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5292}
5293
5294TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005295 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005296 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005297 prepareButtons();
5298 prepareAxes(POSITION);
5299 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005300 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005301
arthurhungdcef2dc2020-08-11 14:47:50 +08005302 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005303
5304 NotifyMotionArgs motionArgs;
5305
5306 // Initially go down out of bounds.
5307 int32_t x = -10;
5308 int32_t y = -10;
5309 processDown(mapper, x, y);
5310 processSync(mapper);
5311
5312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5313
5314 // Move into the display area. Should generate a pointer down.
5315 x = 50;
5316 y = 75;
5317 processMove(mapper, x, y);
5318 processSync(mapper);
5319
5320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5321 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5322 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5323 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5324 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5325 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5326 ASSERT_EQ(0, motionArgs.flags);
5327 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5328 ASSERT_EQ(0, motionArgs.buttonState);
5329 ASSERT_EQ(0, motionArgs.edgeFlags);
5330 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5331 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5334 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5335 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5336 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5337 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5338
5339 // Release. Should generate a pointer up.
5340 processUp(mapper);
5341 processSync(mapper);
5342
5343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5344 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5345 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5346 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5347 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5348 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5349 ASSERT_EQ(0, motionArgs.flags);
5350 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5351 ASSERT_EQ(0, motionArgs.buttonState);
5352 ASSERT_EQ(0, motionArgs.edgeFlags);
5353 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5354 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5355 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5356 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5357 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5358 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5359 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5360 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5361
5362 // Should not have sent any more keys or motions.
5363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5365}
5366
Santos Cordonfa5cf462017-04-05 10:37:00 -07005367TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005368 addConfigurationProperty("touch.deviceType", "touchScreen");
5369 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5370
Michael Wrighta9cf4192022-12-01 23:46:39 +00005371 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005372 prepareButtons();
5373 prepareAxes(POSITION);
5374 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005375 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005376
arthurhungdcef2dc2020-08-11 14:47:50 +08005377 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005378
5379 NotifyMotionArgs motionArgs;
5380
5381 // Down.
5382 int32_t x = 100;
5383 int32_t y = 125;
5384 processDown(mapper, x, y);
5385 processSync(mapper);
5386
5387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5388 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5389 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5390 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5391 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5392 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5393 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5394 ASSERT_EQ(0, motionArgs.flags);
5395 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5396 ASSERT_EQ(0, motionArgs.buttonState);
5397 ASSERT_EQ(0, motionArgs.edgeFlags);
5398 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5399 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5400 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5402 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5403 1, 0, 0, 0, 0, 0, 0, 0));
5404 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5405 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5406 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5407
5408 // Move.
5409 x += 50;
5410 y += 75;
5411 processMove(mapper, x, y);
5412 processSync(mapper);
5413
5414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5415 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5416 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5417 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5418 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5419 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5420 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5421 ASSERT_EQ(0, motionArgs.flags);
5422 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5423 ASSERT_EQ(0, motionArgs.buttonState);
5424 ASSERT_EQ(0, motionArgs.edgeFlags);
5425 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5426 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5427 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5428 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5429 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5430 1, 0, 0, 0, 0, 0, 0, 0));
5431 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5432 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5433 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5434
5435 // Up.
5436 processUp(mapper);
5437 processSync(mapper);
5438
5439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5440 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5441 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5442 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5443 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5444 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5445 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5446 ASSERT_EQ(0, motionArgs.flags);
5447 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5448 ASSERT_EQ(0, motionArgs.buttonState);
5449 ASSERT_EQ(0, motionArgs.edgeFlags);
5450 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5451 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5452 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5454 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5455 1, 0, 0, 0, 0, 0, 0, 0));
5456 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5457 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5458 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5459
5460 // Should not have sent any more keys or motions.
5461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5463}
5464
Michael Wrightd02c5b62014-02-10 15:10:22 -08005465TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005466 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005467 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005468 prepareButtons();
5469 prepareAxes(POSITION);
5470 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005471 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005472
arthurhungdcef2dc2020-08-11 14:47:50 +08005473 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005474
5475 NotifyMotionArgs motionArgs;
5476
5477 // Down.
5478 int32_t x = 100;
5479 int32_t y = 125;
5480 processDown(mapper, x, y);
5481 processSync(mapper);
5482
5483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5484 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5485 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5486 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5487 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5488 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5489 ASSERT_EQ(0, motionArgs.flags);
5490 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5491 ASSERT_EQ(0, motionArgs.buttonState);
5492 ASSERT_EQ(0, motionArgs.edgeFlags);
5493 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5494 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5495 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5497 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5498 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5499 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5500 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5501
5502 // Move.
5503 x += 50;
5504 y += 75;
5505 processMove(mapper, x, y);
5506 processSync(mapper);
5507
5508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5509 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5510 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5511 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5512 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5513 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5514 ASSERT_EQ(0, motionArgs.flags);
5515 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5516 ASSERT_EQ(0, motionArgs.buttonState);
5517 ASSERT_EQ(0, motionArgs.edgeFlags);
5518 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5519 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5520 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5522 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5523 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5524 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5525 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5526
5527 // Up.
5528 processUp(mapper);
5529 processSync(mapper);
5530
5531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5532 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5533 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5534 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5535 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5536 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5537 ASSERT_EQ(0, motionArgs.flags);
5538 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5539 ASSERT_EQ(0, motionArgs.buttonState);
5540 ASSERT_EQ(0, motionArgs.edgeFlags);
5541 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5542 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5545 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5546 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5547 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5548 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5549
5550 // Should not have sent any more keys or motions.
5551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5553}
5554
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005555TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005556 addConfigurationProperty("touch.deviceType", "touchScreen");
5557 prepareButtons();
5558 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005559 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5560 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005561 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005562
5563 NotifyMotionArgs args;
5564
5565 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005566 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005567 processDown(mapper, toRawX(50), toRawY(75));
5568 processSync(mapper);
5569
5570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5571 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5572 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5573
5574 processUp(mapper);
5575 processSync(mapper);
5576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5577}
5578
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005579TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005580 addConfigurationProperty("touch.deviceType", "touchScreen");
5581 prepareButtons();
5582 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005583 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5584 // orientation-aware are affected by display rotation.
5585 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005586 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005587
5588 NotifyMotionArgs args;
5589
5590 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005591 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005592 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005593 processDown(mapper, toRawX(50), toRawY(75));
5594 processSync(mapper);
5595
5596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5597 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5598 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5599
5600 processUp(mapper);
5601 processSync(mapper);
5602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5603
5604 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005605 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005606 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005607 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005608 processSync(mapper);
5609
5610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5611 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5612 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5613
5614 processUp(mapper);
5615 processSync(mapper);
5616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5617
5618 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005619 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005620 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005621 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5622 processSync(mapper);
5623
5624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5625 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5626 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5627
5628 processUp(mapper);
5629 processSync(mapper);
5630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5631
5632 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005633 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005634 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005635 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005636 processSync(mapper);
5637
5638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5639 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5640 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5641
5642 processUp(mapper);
5643 processSync(mapper);
5644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5645}
5646
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005647TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5648 addConfigurationProperty("touch.deviceType", "touchScreen");
5649 prepareButtons();
5650 prepareAxes(POSITION);
5651 addConfigurationProperty("touch.orientationAware", "1");
5652 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5653 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005654 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005655 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5656 NotifyMotionArgs args;
5657
5658 // Orientation 0.
5659 processDown(mapper, toRawX(50), toRawY(75));
5660 processSync(mapper);
5661
5662 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5663 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5664 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5665
5666 processUp(mapper);
5667 processSync(mapper);
5668 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5669}
5670
5671TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5672 addConfigurationProperty("touch.deviceType", "touchScreen");
5673 prepareButtons();
5674 prepareAxes(POSITION);
5675 addConfigurationProperty("touch.orientationAware", "1");
5676 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5677 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005678 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005679 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5680 NotifyMotionArgs args;
5681
5682 // Orientation 90.
5683 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5684 processSync(mapper);
5685
5686 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5687 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5688 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5689
5690 processUp(mapper);
5691 processSync(mapper);
5692 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5693}
5694
5695TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5696 addConfigurationProperty("touch.deviceType", "touchScreen");
5697 prepareButtons();
5698 prepareAxes(POSITION);
5699 addConfigurationProperty("touch.orientationAware", "1");
5700 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5701 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005702 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005703 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5704 NotifyMotionArgs args;
5705
5706 // Orientation 180.
5707 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5708 processSync(mapper);
5709
5710 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5711 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5712 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5713
5714 processUp(mapper);
5715 processSync(mapper);
5716 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5717}
5718
5719TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5720 addConfigurationProperty("touch.deviceType", "touchScreen");
5721 prepareButtons();
5722 prepareAxes(POSITION);
5723 addConfigurationProperty("touch.orientationAware", "1");
5724 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5725 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005726 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005727 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5728 NotifyMotionArgs args;
5729
5730 // Orientation 270.
5731 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5732 processSync(mapper);
5733
5734 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5735 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5736 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5737
5738 processUp(mapper);
5739 processSync(mapper);
5740 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5741}
5742
5743TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5744 addConfigurationProperty("touch.deviceType", "touchScreen");
5745 prepareButtons();
5746 prepareAxes(POSITION);
5747 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5748 // orientation-aware are affected by display rotation.
5749 addConfigurationProperty("touch.orientationAware", "0");
5750 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5751 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5752
5753 NotifyMotionArgs args;
5754
5755 // Orientation 90, Rotation 0.
5756 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005757 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005758 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5759 processSync(mapper);
5760
5761 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5762 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5763 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5764
5765 processUp(mapper);
5766 processSync(mapper);
5767 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5768
5769 // Orientation 90, Rotation 90.
5770 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005771 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005772 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005773 processSync(mapper);
5774
5775 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5776 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5777 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5778
5779 processUp(mapper);
5780 processSync(mapper);
5781 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5782
5783 // Orientation 90, Rotation 180.
5784 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005785 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005786 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5787 processSync(mapper);
5788
5789 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5790 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5791 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5792
5793 processUp(mapper);
5794 processSync(mapper);
5795 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5796
5797 // Orientation 90, Rotation 270.
5798 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005799 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005800 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 -07005801 processSync(mapper);
5802
5803 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5804 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5805 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5806
5807 processUp(mapper);
5808 processSync(mapper);
5809 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5810}
5811
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005812TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5813 addConfigurationProperty("touch.deviceType", "touchScreen");
5814 prepareButtons();
5815 prepareAxes(POSITION);
5816 addConfigurationProperty("touch.orientationAware", "1");
5817 prepareDisplay(ui::ROTATION_0);
5818 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5819
5820 // Set a physical frame in the display viewport.
5821 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5822 viewport->physicalLeft = 20;
5823 viewport->physicalTop = 600;
5824 viewport->physicalRight = 30;
5825 viewport->physicalBottom = 610;
5826 mFakePolicy->updateViewport(*viewport);
5827 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5828
5829 // Start the touch.
5830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5831 processSync(mapper);
5832
5833 // Expect all input starting outside the physical frame to be ignored.
5834 const std::array<Point, 6> outsidePoints = {
5835 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5836 for (const auto& p : outsidePoints) {
5837 processMove(mapper, toRawX(p.x), toRawY(p.y));
5838 processSync(mapper);
5839 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5840 }
5841
5842 // Move the touch into the physical frame.
5843 processMove(mapper, toRawX(25), toRawY(605));
5844 processSync(mapper);
5845 NotifyMotionArgs args;
5846 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5847 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5848 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5849 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5850
5851 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5852 for (const auto& p : outsidePoints) {
5853 processMove(mapper, toRawX(p.x), toRawY(p.y));
5854 processSync(mapper);
5855 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5856 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5857 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5858 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5859 }
5860
5861 processUp(mapper);
5862 processSync(mapper);
5863 EXPECT_NO_FATAL_FAILURE(
5864 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5865}
5866
Michael Wrightd02c5b62014-02-10 15:10:22 -08005867TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005868 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005869 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005870 prepareButtons();
5871 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005872 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005873
5874 // These calculations are based on the input device calibration documentation.
5875 int32_t rawX = 100;
5876 int32_t rawY = 200;
5877 int32_t rawPressure = 10;
5878 int32_t rawToolMajor = 12;
5879 int32_t rawDistance = 2;
5880 int32_t rawTiltX = 30;
5881 int32_t rawTiltY = 110;
5882
5883 float x = toDisplayX(rawX);
5884 float y = toDisplayY(rawY);
5885 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5886 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5887 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5888 float distance = float(rawDistance);
5889
5890 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5891 float tiltScale = M_PI / 180;
5892 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5893 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5894 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5895 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5896
5897 processDown(mapper, rawX, rawY);
5898 processPressure(mapper, rawPressure);
5899 processToolMajor(mapper, rawToolMajor);
5900 processDistance(mapper, rawDistance);
5901 processTilt(mapper, rawTiltX, rawTiltY);
5902 processSync(mapper);
5903
5904 NotifyMotionArgs args;
5905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5907 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5908 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5909}
5910
Jason Gerecke489fda82012-09-07 17:19:40 -07005911TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005912 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005913 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005914 prepareLocationCalibration();
5915 prepareButtons();
5916 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005917 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005918
5919 int32_t rawX = 100;
5920 int32_t rawY = 200;
5921
5922 float x = toDisplayX(toCookedX(rawX, rawY));
5923 float y = toDisplayY(toCookedY(rawX, rawY));
5924
5925 processDown(mapper, rawX, rawY);
5926 processSync(mapper);
5927
5928 NotifyMotionArgs args;
5929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5930 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5931 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5932}
5933
Michael Wrightd02c5b62014-02-10 15:10:22 -08005934TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005935 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005936 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005937 prepareButtons();
5938 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005939 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005940
5941 NotifyMotionArgs motionArgs;
5942 NotifyKeyArgs keyArgs;
5943
5944 processDown(mapper, 100, 200);
5945 processSync(mapper);
5946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5947 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5948 ASSERT_EQ(0, motionArgs.buttonState);
5949
5950 // press BTN_LEFT, release BTN_LEFT
5951 processKey(mapper, BTN_LEFT, 1);
5952 processSync(mapper);
5953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5954 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5955 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5956
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5958 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5959 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5960
Michael Wrightd02c5b62014-02-10 15:10:22 -08005961 processKey(mapper, BTN_LEFT, 0);
5962 processSync(mapper);
5963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005964 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005965 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005966
5967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005968 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005969 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005970
5971 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5972 processKey(mapper, BTN_RIGHT, 1);
5973 processKey(mapper, BTN_MIDDLE, 1);
5974 processSync(mapper);
5975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5976 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5977 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5978 motionArgs.buttonState);
5979
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5981 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5982 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5983
5984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5985 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5986 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5987 motionArgs.buttonState);
5988
Michael Wrightd02c5b62014-02-10 15:10:22 -08005989 processKey(mapper, BTN_RIGHT, 0);
5990 processSync(mapper);
5991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005992 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005993 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005994
5995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005997 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005998
5999 processKey(mapper, BTN_MIDDLE, 0);
6000 processSync(mapper);
6001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006002 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006003 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006004
6005 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(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006008
6009 // press BTN_BACK, release BTN_BACK
6010 processKey(mapper, BTN_BACK, 1);
6011 processSync(mapper);
6012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6013 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6014 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006015
Michael Wrightd02c5b62014-02-10 15:10:22 -08006016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006017 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006018 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6019
6020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6021 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6022 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006023
6024 processKey(mapper, BTN_BACK, 0);
6025 processSync(mapper);
6026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006027 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006028 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006029
6030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006031 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006032 ASSERT_EQ(0, motionArgs.buttonState);
6033
Michael Wrightd02c5b62014-02-10 15:10:22 -08006034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6035 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6036 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6037
6038 // press BTN_SIDE, release BTN_SIDE
6039 processKey(mapper, BTN_SIDE, 1);
6040 processSync(mapper);
6041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6042 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6043 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006044
Michael Wrightd02c5b62014-02-10 15:10:22 -08006045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006046 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006047 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6048
6049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6050 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6051 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006052
6053 processKey(mapper, BTN_SIDE, 0);
6054 processSync(mapper);
6055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006056 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006057 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006058
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006060 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006061 ASSERT_EQ(0, motionArgs.buttonState);
6062
Michael Wrightd02c5b62014-02-10 15:10:22 -08006063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6064 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6065 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6066
6067 // press BTN_FORWARD, release BTN_FORWARD
6068 processKey(mapper, BTN_FORWARD, 1);
6069 processSync(mapper);
6070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6071 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6072 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006073
Michael Wrightd02c5b62014-02-10 15:10:22 -08006074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006075 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006076 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6077
6078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6079 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6080 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006081
6082 processKey(mapper, BTN_FORWARD, 0);
6083 processSync(mapper);
6084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006085 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006086 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006087
6088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006089 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006090 ASSERT_EQ(0, motionArgs.buttonState);
6091
Michael Wrightd02c5b62014-02-10 15:10:22 -08006092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6093 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6094 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6095
6096 // press BTN_EXTRA, release BTN_EXTRA
6097 processKey(mapper, BTN_EXTRA, 1);
6098 processSync(mapper);
6099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6100 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6101 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006102
Michael Wrightd02c5b62014-02-10 15:10:22 -08006103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006105 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6106
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6108 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6109 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006110
6111 processKey(mapper, BTN_EXTRA, 0);
6112 processSync(mapper);
6113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006114 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006115 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006116
6117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006119 ASSERT_EQ(0, motionArgs.buttonState);
6120
Michael Wrightd02c5b62014-02-10 15:10:22 -08006121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6122 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6123 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6124
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6126
Michael Wrightd02c5b62014-02-10 15:10:22 -08006127 // press BTN_STYLUS, release BTN_STYLUS
6128 processKey(mapper, BTN_STYLUS, 1);
6129 processSync(mapper);
6130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006132 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6133
6134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6135 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6136 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006137
6138 processKey(mapper, BTN_STYLUS, 0);
6139 processSync(mapper);
6140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006141 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006142 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006143
6144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006145 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006146 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006147
6148 // press BTN_STYLUS2, release BTN_STYLUS2
6149 processKey(mapper, BTN_STYLUS2, 1);
6150 processSync(mapper);
6151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006153 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6154
6155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6156 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6157 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006158
6159 processKey(mapper, BTN_STYLUS2, 0);
6160 processSync(mapper);
6161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006162 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006163 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006164
6165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006167 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006168
6169 // release touch
6170 processUp(mapper);
6171 processSync(mapper);
6172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6173 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6174 ASSERT_EQ(0, motionArgs.buttonState);
6175}
6176
6177TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006178 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006179 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006180 prepareButtons();
6181 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006182 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006183
6184 NotifyMotionArgs motionArgs;
6185
6186 // default tool type is finger
6187 processDown(mapper, 100, 200);
6188 processSync(mapper);
6189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6190 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6192
6193 // eraser
6194 processKey(mapper, BTN_TOOL_RUBBER, 1);
6195 processSync(mapper);
6196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6198 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6199
6200 // stylus
6201 processKey(mapper, BTN_TOOL_RUBBER, 0);
6202 processKey(mapper, BTN_TOOL_PEN, 1);
6203 processSync(mapper);
6204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6205 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6206 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6207
6208 // brush
6209 processKey(mapper, BTN_TOOL_PEN, 0);
6210 processKey(mapper, BTN_TOOL_BRUSH, 1);
6211 processSync(mapper);
6212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6214 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6215
6216 // pencil
6217 processKey(mapper, BTN_TOOL_BRUSH, 0);
6218 processKey(mapper, BTN_TOOL_PENCIL, 1);
6219 processSync(mapper);
6220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6222 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6223
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006224 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006225 processKey(mapper, BTN_TOOL_PENCIL, 0);
6226 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6227 processSync(mapper);
6228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6230 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6231
6232 // mouse
6233 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6234 processKey(mapper, BTN_TOOL_MOUSE, 1);
6235 processSync(mapper);
6236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6238 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6239
6240 // lens
6241 processKey(mapper, BTN_TOOL_MOUSE, 0);
6242 processKey(mapper, BTN_TOOL_LENS, 1);
6243 processSync(mapper);
6244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6246 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6247
6248 // double-tap
6249 processKey(mapper, BTN_TOOL_LENS, 0);
6250 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6251 processSync(mapper);
6252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6255
6256 // triple-tap
6257 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6258 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6259 processSync(mapper);
6260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6263
6264 // quad-tap
6265 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6266 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6267 processSync(mapper);
6268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6270 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6271
6272 // finger
6273 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6274 processKey(mapper, BTN_TOOL_FINGER, 1);
6275 processSync(mapper);
6276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6278 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6279
6280 // stylus trumps finger
6281 processKey(mapper, BTN_TOOL_PEN, 1);
6282 processSync(mapper);
6283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6284 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6285 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6286
6287 // eraser trumps stylus
6288 processKey(mapper, BTN_TOOL_RUBBER, 1);
6289 processSync(mapper);
6290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6291 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6292 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6293
6294 // mouse trumps eraser
6295 processKey(mapper, BTN_TOOL_MOUSE, 1);
6296 processSync(mapper);
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6299 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6300
6301 // back to default tool type
6302 processKey(mapper, BTN_TOOL_MOUSE, 0);
6303 processKey(mapper, BTN_TOOL_RUBBER, 0);
6304 processKey(mapper, BTN_TOOL_PEN, 0);
6305 processKey(mapper, BTN_TOOL_FINGER, 0);
6306 processSync(mapper);
6307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6309 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6310}
6311
6312TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006313 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006314 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006315 prepareButtons();
6316 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006317 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006318 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319
6320 NotifyMotionArgs motionArgs;
6321
6322 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6323 processKey(mapper, BTN_TOOL_FINGER, 1);
6324 processMove(mapper, 100, 200);
6325 processSync(mapper);
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6327 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6329 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6330
6331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6332 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6334 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6335
6336 // move a little
6337 processMove(mapper, 150, 250);
6338 processSync(mapper);
6339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6340 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6342 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6343
6344 // down when BTN_TOUCH is pressed, pressure defaults to 1
6345 processKey(mapper, BTN_TOUCH, 1);
6346 processSync(mapper);
6347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6348 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6350 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6351
6352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6353 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6355 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6356
6357 // up when BTN_TOUCH is released, hover restored
6358 processKey(mapper, BTN_TOUCH, 0);
6359 processSync(mapper);
6360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6361 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6363 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6364
6365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6366 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6368 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6369
6370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6371 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6373 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6374
6375 // exit hover when pointer goes away
6376 processKey(mapper, BTN_TOOL_FINGER, 0);
6377 processSync(mapper);
6378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6379 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6381 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6382}
6383
6384TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006385 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006386 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006387 prepareButtons();
6388 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006389 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006390
6391 NotifyMotionArgs motionArgs;
6392
6393 // initially hovering because pressure is 0
6394 processDown(mapper, 100, 200);
6395 processPressure(mapper, 0);
6396 processSync(mapper);
6397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6398 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6400 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6401
6402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6403 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6405 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6406
6407 // move a little
6408 processMove(mapper, 150, 250);
6409 processSync(mapper);
6410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6411 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6413 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6414
6415 // down when pressure is non-zero
6416 processPressure(mapper, RAW_PRESSURE_MAX);
6417 processSync(mapper);
6418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6419 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6421 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6422
6423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6424 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6426 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6427
6428 // up when pressure becomes 0, hover restored
6429 processPressure(mapper, 0);
6430 processSync(mapper);
6431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6432 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6434 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6435
6436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6437 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6439 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6440
6441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6442 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6444 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6445
6446 // exit hover when pointer goes away
6447 processUp(mapper);
6448 processSync(mapper);
6449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6450 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6452 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6453}
6454
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006455TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6456 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006457 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006458 prepareButtons();
6459 prepareAxes(POSITION | PRESSURE);
6460 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6461
6462 // Touch down.
6463 processDown(mapper, 100, 200);
6464 processPressure(mapper, 1);
6465 processSync(mapper);
6466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6467 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6468
6469 // Reset the mapper. This should cancel the ongoing gesture.
6470 resetMapper(mapper, ARBITRARY_TIME);
6471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6472 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6473
6474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6475}
6476
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006477TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6478 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006479 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006480 prepareButtons();
6481 prepareAxes(POSITION | PRESSURE);
6482 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6483
6484 // Set the initial state for the touch pointer.
6485 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6486 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6487 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6488 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6489
6490 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006491 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6492 // does not generate any events.
6493 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006494
6495 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6496 // the recreated touch state to generate a down event.
6497 processSync(mapper);
6498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6499 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6500
6501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6502}
6503
lilinnan687e58f2022-07-19 16:00:50 +08006504TEST_F(SingleTouchInputMapperTest,
6505 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6506 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006507 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006508 prepareButtons();
6509 prepareAxes(POSITION);
6510 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6511 NotifyMotionArgs motionArgs;
6512
6513 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006514 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006515 processSync(mapper);
6516
6517 // We should receive a down event
6518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6519 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6520
6521 // Change display id
6522 clearViewports();
6523 prepareSecondaryDisplay(ViewportType::INTERNAL);
6524
6525 // We should receive a cancel event
6526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6527 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6528 // Then receive reset called
6529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6530}
6531
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006532TEST_F(SingleTouchInputMapperTest,
6533 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6534 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006535 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006536 prepareButtons();
6537 prepareAxes(POSITION);
6538 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6540 NotifyMotionArgs motionArgs;
6541
6542 // Start a new gesture.
6543 processDown(mapper, 100, 200);
6544 processSync(mapper);
6545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6546 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6547
6548 // Make the viewport inactive. This will put the device in disabled mode.
6549 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6550 viewport->isActive = false;
6551 mFakePolicy->updateViewport(*viewport);
6552 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6553
6554 // We should receive a cancel event for the ongoing gesture.
6555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6556 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6557 // Then we should be notified that the device was reset.
6558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6559
6560 // No events are generated while the viewport is inactive.
6561 processMove(mapper, 101, 201);
6562 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006563 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006564 processSync(mapper);
6565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6566
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006567 // Start a new gesture while the viewport is still inactive.
6568 processDown(mapper, 300, 400);
6569 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6570 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6571 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6572 processSync(mapper);
6573
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006574 // Make the viewport active again. The device should resume processing events.
6575 viewport->isActive = true;
6576 mFakePolicy->updateViewport(*viewport);
6577 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6578
6579 // The device is reset because it changes back to direct mode, without generating any events.
6580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6582
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006583 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006584 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6586 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006587
6588 // No more events.
6589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6591}
6592
Prabir Pradhan211ba622022-10-31 21:09:21 +00006593TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6594 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006595 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006596 prepareButtons();
6597 prepareAxes(POSITION);
6598 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6600
6601 // Press a stylus button.
6602 processKey(mapper, BTN_STYLUS, 1);
6603 processSync(mapper);
6604
6605 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6606 processDown(mapper, 100, 200);
6607 processSync(mapper);
6608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6609 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6610 WithCoords(toDisplayX(100), toDisplayY(200)),
6611 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6613 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6614 WithCoords(toDisplayX(100), toDisplayY(200)),
6615 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6616
6617 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6618 // the button has not actually been released, since there will be no pointers through which the
6619 // button state can be reported. The event is generated at the location of the pointer before
6620 // it went up.
6621 processUp(mapper);
6622 processSync(mapper);
6623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6624 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6625 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6627 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6628 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6629}
6630
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006631TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6632 addConfigurationProperty("touch.deviceType", "touchScreen");
6633 prepareDisplay(ui::ROTATION_0);
6634 prepareButtons();
6635 prepareAxes(POSITION);
6636
6637 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6638
6639 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6641
6642 // Press a stylus button.
6643 processKey(mapper, BTN_STYLUS, 1);
6644 processSync(mapper);
6645
6646 // Start a touch gesture and ensure that the stylus button is not reported.
6647 processDown(mapper, 100, 200);
6648 processSync(mapper);
6649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6650 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6651
6652 // Release and press the stylus button again.
6653 processKey(mapper, BTN_STYLUS, 0);
6654 processSync(mapper);
6655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6656 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6657 processKey(mapper, BTN_STYLUS, 1);
6658 processSync(mapper);
6659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6660 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6661
6662 // Release the touch gesture.
6663 processUp(mapper);
6664 processSync(mapper);
6665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6666 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6667
6668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6669}
6670
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006671TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6672 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6673 prepareDisplay(ui::ROTATION_0);
6674 prepareButtons();
6675 prepareAxes(POSITION);
6676 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6678
6679 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6680}
6681
Seunghwan Choi356026c2023-02-01 14:37:25 +09006682TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6683 std::shared_ptr<FakePointerController> fakePointerController =
6684 std::make_shared<FakePointerController>();
6685 addConfigurationProperty("touch.deviceType", "touchScreen");
6686 prepareDisplay(ui::ROTATION_0);
6687 prepareButtons();
6688 prepareAxes(POSITION);
6689 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6690 mFakePolicy->setPointerController(fakePointerController);
6691 mFakePolicy->setStylusPointerIconEnabled(true);
6692 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6693
6694 processKey(mapper, BTN_TOOL_PEN, 1);
6695 processMove(mapper, 100, 200);
6696 processSync(mapper);
6697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6698 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
6699 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
6700 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6701 ASSERT_TRUE(fakePointerController->isPointerShown());
6702 ASSERT_NO_FATAL_FAILURE(
6703 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6704}
6705
6706TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6707 std::shared_ptr<FakePointerController> fakePointerController =
6708 std::make_shared<FakePointerController>();
6709 addConfigurationProperty("touch.deviceType", "touchScreen");
6710 prepareDisplay(ui::ROTATION_0);
6711 prepareButtons();
6712 prepareAxes(POSITION);
6713 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6714 mFakePolicy->setPointerController(fakePointerController);
6715 mFakePolicy->setStylusPointerIconEnabled(false);
6716 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6717
6718 processKey(mapper, BTN_TOOL_PEN, 1);
6719 processMove(mapper, 100, 200);
6720 processSync(mapper);
6721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6722 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
6723 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
6724 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6725 ASSERT_FALSE(fakePointerController->isPointerShown());
6726}
6727
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006728TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6729 // Initialize the device without setting device source to touch navigation.
6730 addConfigurationProperty("touch.deviceType", "touchScreen");
6731 prepareDisplay(ui::ROTATION_0);
6732 prepareButtons();
6733 prepareAxes(POSITION);
6734 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6735
6736 // Ensure that the device is created as a touchscreen, not touch navigation.
6737 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6738
6739 // Add device type association after the device was created.
6740 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6741
6742 // Send update to the mapper.
6743 std::list<NotifyArgs> unused2 =
6744 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6745 InputReaderConfiguration::CHANGE_DEVICE_TYPE /*changes*/);
6746
6747 // Check whether device type update was successful.
6748 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6749}
6750
Prabir Pradhan5632d622021-09-06 07:57:20 -07006751// --- TouchDisplayProjectionTest ---
6752
6753class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6754public:
6755 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6756 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6757 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006758 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6759 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6760 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006761 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006762 auto rotatedWidth = naturalDisplayWidth;
6763 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006764 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006765 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006766 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006767 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006768 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006769 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006770 inverseRotationFlags = ui::Transform::ROT_180;
6771 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006772 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006773 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006774 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006775 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006776 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006777 inverseRotationFlags = ui::Transform::ROT_0;
6778 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006779 }
6780
Prabir Pradhana9df3162022-12-05 23:57:27 +00006781 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006782 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6783
6784 std::optional<DisplayViewport> internalViewport =
6785 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6786 DisplayViewport& v = *internalViewport;
6787 v.displayId = DISPLAY_ID;
6788 v.orientation = orientation;
6789
6790 v.logicalLeft = 0;
6791 v.logicalTop = 0;
6792 v.logicalRight = 100;
6793 v.logicalBottom = 100;
6794
6795 v.physicalLeft = rotatedPhysicalDisplay.left;
6796 v.physicalTop = rotatedPhysicalDisplay.top;
6797 v.physicalRight = rotatedPhysicalDisplay.right;
6798 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6799
Prabir Pradhana9df3162022-12-05 23:57:27 +00006800 v.deviceWidth = rotatedWidth;
6801 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006802
6803 v.isActive = true;
6804 v.uniqueId = UNIQUE_ID;
6805 v.type = ViewportType::INTERNAL;
6806 mFakePolicy->updateViewport(v);
6807 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6808 }
6809
6810 void assertReceivedMove(const Point& point) {
6811 NotifyMotionArgs motionArgs;
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6813 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6814 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6816 1, 0, 0, 0, 0, 0, 0, 0));
6817 }
6818};
6819
6820TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6821 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006822 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006823
6824 prepareButtons();
6825 prepareAxes(POSITION);
6826 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6827
6828 NotifyMotionArgs motionArgs;
6829
6830 // Configure the DisplayViewport such that the logical display maps to a subsection of
6831 // the display panel called the physical display. Here, the physical display is bounded by the
6832 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6833 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6834 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6835 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6836
Michael Wrighta9cf4192022-12-01 23:46:39 +00006837 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006838 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6839
6840 // Touches outside the physical display should be ignored, and should not generate any
6841 // events. Ensure touches at the following points that lie outside of the physical display
6842 // area do not generate any events.
6843 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6844 processDown(mapper, toRawX(point.x), toRawY(point.y));
6845 processSync(mapper);
6846 processUp(mapper);
6847 processSync(mapper);
6848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6849 << "Unexpected event generated for touch outside physical display at point: "
6850 << point.x << ", " << point.y;
6851 }
6852 }
6853}
6854
6855TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6856 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006857 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006858
6859 prepareButtons();
6860 prepareAxes(POSITION);
6861 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6862
6863 NotifyMotionArgs motionArgs;
6864
6865 // Configure the DisplayViewport such that the logical display maps to a subsection of
6866 // the display panel called the physical display. Here, the physical display is bounded by the
6867 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6868 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6869
Michael Wrighta9cf4192022-12-01 23:46:39 +00006870 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006871 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6872
6873 // Touches that start outside the physical display should be ignored until it enters the
6874 // physical display bounds, at which point it should generate a down event. Start a touch at
6875 // the point (5, 100), which is outside the physical display bounds.
6876 static const Point kOutsidePoint{5, 100};
6877 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6878 processSync(mapper);
6879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6880
6881 // Move the touch into the physical display area. This should generate a pointer down.
6882 processMove(mapper, toRawX(11), toRawY(21));
6883 processSync(mapper);
6884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6885 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6886 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6887 ASSERT_NO_FATAL_FAILURE(
6888 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6889
6890 // Move the touch inside the physical display area. This should generate a pointer move.
6891 processMove(mapper, toRawX(69), toRawY(159));
6892 processSync(mapper);
6893 assertReceivedMove({69, 159});
6894
6895 // Move outside the physical display area. Since the pointer is already down, this should
6896 // now continue generating events.
6897 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6898 processSync(mapper);
6899 assertReceivedMove(kOutsidePoint);
6900
6901 // Release. This should generate a pointer up.
6902 processUp(mapper);
6903 processSync(mapper);
6904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6905 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6907 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6908
6909 // Ensure no more events were generated.
6910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6912 }
6913}
6914
Prabir Pradhana9df3162022-12-05 23:57:27 +00006915// --- TouchscreenPrecisionTests ---
6916
6917// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6918// in various orientations and with different display rotations. We configure the touchscreen to
6919// have a higher resolution than that of the display by an integer scale factor in each axis so that
6920// we can enforce that coordinates match precisely as expected.
6921class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6922 public ::testing::WithParamInterface<ui::Rotation> {
6923public:
6924 void SetUp() override {
6925 SingleTouchInputMapperTest::SetUp();
6926
6927 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6928 // four times the resolution of the display in the Y axis.
6929 prepareButtons();
6930 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006931 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
6932 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006933 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006934 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
6935 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006936 }
6937
6938 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6939 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6940 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6941 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6942
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006943 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
6944 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
6945
6946 static const int32_t PRECISION_RAW_X_FLAT = 16;
6947 static const int32_t PRECISION_RAW_Y_FLAT = 32;
6948
6949 static const int32_t PRECISION_RAW_X_FUZZ = 4;
6950 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
6951
Prabir Pradhana9df3162022-12-05 23:57:27 +00006952 static const std::array<Point, 4> kRawCorners;
6953};
6954
6955const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6956 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6957 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6958 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6959 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6960}};
6961
6962// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
6963// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
6964// touchscreen panel that is used on a device whose natural display orientation is in landscape.
6965TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
6966 enum class Orientation {
6967 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
6968 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
6969 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
6970 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
6971 ftl_last = ORIENTATION_270,
6972 };
6973 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
6974 Orientation::ORIENTATION_270;
6975 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
6976 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6977 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
6978 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
6979 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6980 };
6981
6982 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
6983
6984 // Configure the touchscreen as being installed in the one of the four different orientations
6985 // relative to the display.
6986 addConfigurationProperty("touch.deviceType", "touchScreen");
6987 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
6988 prepareDisplay(ui::ROTATION_0);
6989
6990 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6991
6992 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
6993 // orientations of either 90 or 270) this means the display's natural resolution will be
6994 // flipped.
6995 const bool displayRotated =
6996 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
6997 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
6998 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
6999 const Rect physicalFrame{0, 0, width, height};
7000 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7001
7002 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7003 const float expectedPrecisionX = displayRotated ? 4 : 2;
7004 const float expectedPrecisionY = displayRotated ? 2 : 4;
7005
7006 // Test all four corners.
7007 for (int i = 0; i < 4; i++) {
7008 const auto& raw = kRawCorners[i];
7009 processDown(mapper, raw.x, raw.y);
7010 processSync(mapper);
7011 const auto& expected = expectedPoints[i];
7012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7013 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7014 WithCoords(expected.x, expected.y),
7015 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7016 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7017 << "with touchscreen orientation "
7018 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7019 << expected.x << ", " << expected.y << ").";
7020 processUp(mapper);
7021 processSync(mapper);
7022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7023 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7024 WithCoords(expected.x, expected.y))));
7025 }
7026}
7027
Prabir Pradhan82687402022-12-06 01:32:53 +00007028TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7029 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7030 kMappedCorners = {
7031 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7032 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7033 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7034 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7035 };
7036
7037 const ui::Rotation displayRotation = GetParam();
7038
7039 addConfigurationProperty("touch.deviceType", "touchScreen");
7040 prepareDisplay(displayRotation);
7041
7042 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7043
7044 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7045
7046 // Test all four corners.
7047 for (int i = 0; i < 4; i++) {
7048 const auto& expected = expectedPoints[i];
7049 const auto& raw = kRawCorners[i];
7050 processDown(mapper, raw.x, raw.y);
7051 processSync(mapper);
7052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7053 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7054 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7055 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7056 << "with display rotation " << ui::toCString(displayRotation)
7057 << ", expected point (" << expected.x << ", " << expected.y << ").";
7058 processUp(mapper);
7059 processSync(mapper);
7060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7061 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7062 WithCoords(expected.x, expected.y))));
7063 }
7064}
7065
Prabir Pradhan3e798762022-12-02 21:02:11 +00007066TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7067 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7068 kMappedCorners = {
7069 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7070 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7071 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7072 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7073 };
7074
7075 const ui::Rotation displayRotation = GetParam();
7076
7077 addConfigurationProperty("touch.deviceType", "touchScreen");
7078 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7079
7080 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7081
7082 // Ori 270, so width and height swapped
7083 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7084 prepareDisplay(displayRotation);
7085 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7086
7087 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7088
7089 // Test all four corners.
7090 for (int i = 0; i < 4; i++) {
7091 const auto& expected = expectedPoints[i];
7092 const auto& raw = kRawCorners[i];
7093 processDown(mapper, raw.x, raw.y);
7094 processSync(mapper);
7095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7096 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7097 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7098 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7099 << "with display rotation " << ui::toCString(displayRotation)
7100 << ", expected point (" << expected.x << ", " << expected.y << ").";
7101 processUp(mapper);
7102 processSync(mapper);
7103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7104 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7105 WithCoords(expected.x, expected.y))));
7106 }
7107}
7108
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007109TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7110 const ui::Rotation displayRotation = GetParam();
7111
7112 addConfigurationProperty("touch.deviceType", "touchScreen");
7113 prepareDisplay(displayRotation);
7114
7115 __attribute__((unused)) SingleTouchInputMapper& mapper =
7116 addMapperAndConfigure<SingleTouchInputMapper>();
7117
7118 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7119 // MotionRanges use display pixels as their units
7120 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7121 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7122
7123 // The MotionRanges should be oriented in the rotated display's coordinate space
7124 const bool displayRotated =
7125 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7126
7127 constexpr float MAX_X = 479.5;
7128 constexpr float MAX_Y = 799.75;
7129 EXPECT_EQ(xRange->min, 0.f);
7130 EXPECT_EQ(yRange->min, 0.f);
7131 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7132 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7133
7134 EXPECT_EQ(xRange->flat, 8.f);
7135 EXPECT_EQ(yRange->flat, 8.f);
7136
7137 EXPECT_EQ(xRange->fuzz, 2.f);
7138 EXPECT_EQ(yRange->fuzz, 2.f);
7139
7140 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7141 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7142}
7143
Prabir Pradhana9df3162022-12-05 23:57:27 +00007144// Run the precision tests for all rotations.
7145INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7146 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7147 ui::ROTATION_270),
7148 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7149 return ftl::enum_string(testParamInfo.param);
7150 });
7151
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007152// --- ExternalStylusFusionTest ---
7153
7154class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7155public:
7156 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7157 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007158 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007159 prepareButtons();
7160 prepareAxes(POSITION);
7161 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7162
7163 mStylusState.when = ARBITRARY_TIME;
7164 mStylusState.pressure = 0.f;
7165 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7166 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
7167 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
7168 processExternalStylusState(mapper);
7169 return mapper;
7170 }
7171
7172 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7173 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7174 for (const NotifyArgs& args : generatedArgs) {
7175 mFakeListener->notify(args);
7176 }
7177 // Loop the reader to flush the input listener queue.
7178 mReader->loopOnce();
7179 return generatedArgs;
7180 }
7181
7182protected:
7183 StylusState mStylusState{};
7184 static constexpr uint32_t EXPECTED_SOURCE =
7185 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7186
7187 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7188 auto toolTypeSource =
7189 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7190
7191 // The first pointer is withheld.
7192 processDown(mapper, 100, 200);
7193 processSync(mapper);
7194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7195 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7196 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7197
7198 // The external stylus reports pressure. The withheld finger pointer is released as a
7199 // stylus.
7200 mStylusState.pressure = 1.f;
7201 processExternalStylusState(mapper);
7202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7203 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7204 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7205
7206 // Subsequent pointer events are not withheld.
7207 processMove(mapper, 101, 201);
7208 processSync(mapper);
7209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7210 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7211
7212 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7214 }
7215
7216 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7217 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7218
7219 // Releasing the touch pointer ends the gesture.
7220 processUp(mapper);
7221 processSync(mapper);
7222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7223 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7224 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7225
7226 mStylusState.pressure = 0.f;
7227 processExternalStylusState(mapper);
7228 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7230 }
7231
7232 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7233 auto toolTypeSource =
7234 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER));
7235
7236 // The first pointer is withheld when an external stylus is connected,
7237 // and a timeout is requested.
7238 processDown(mapper, 100, 200);
7239 processSync(mapper);
7240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7241 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7242 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7243
7244 // If the timeout expires early, it is requested again.
7245 handleTimeout(mapper, ARBITRARY_TIME + 1);
7246 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7247 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7248
7249 // When the timeout expires, the withheld touch is released as a finger pointer.
7250 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7252 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7253
7254 // Subsequent pointer events are not withheld.
7255 processMove(mapper, 101, 201);
7256 processSync(mapper);
7257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7258 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7259 processUp(mapper);
7260 processSync(mapper);
7261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7262 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7263
7264 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7266 }
7267
7268private:
7269 InputDeviceInfo mExternalStylusDeviceInfo{};
7270};
7271
7272TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7273 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7274 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7275}
7276
7277TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7278 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7279 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7280}
7281
7282TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7283 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7284 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7285}
7286
7287// Test a successful stylus fusion gesture where the pressure is reported by the external
7288// before the touch is reported by the touchscreen.
7289TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7290 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7291 auto toolTypeSource =
7292 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7293
7294 // The external stylus reports pressure first. It is ignored for now.
7295 mStylusState.pressure = 1.f;
7296 processExternalStylusState(mapper);
7297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7298 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7299
7300 // When the touch goes down afterwards, it is reported as a stylus pointer.
7301 processDown(mapper, 100, 200);
7302 processSync(mapper);
7303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7304 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7305 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7306
7307 processMove(mapper, 101, 201);
7308 processSync(mapper);
7309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7310 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7311 processUp(mapper);
7312 processSync(mapper);
7313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7314 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7315
7316 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7318}
7319
7320TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7321 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7322
7323 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7324 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7325
7326 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7327 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7328 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7329 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7330}
7331
7332TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7333 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7334 auto toolTypeSource =
7335 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7336
7337 mStylusState.pressure = 0.8f;
7338 processExternalStylusState(mapper);
7339 processDown(mapper, 100, 200);
7340 processSync(mapper);
7341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7342 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7343 WithPressure(0.8f))));
7344 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7345
7346 // The external stylus reports a pressure change. We wait for some time for a touch event.
7347 mStylusState.pressure = 0.6f;
7348 processExternalStylusState(mapper);
7349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7350 ASSERT_NO_FATAL_FAILURE(
7351 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7352
7353 // If a touch is reported within the timeout, it reports the updated pressure.
7354 processMove(mapper, 101, 201);
7355 processSync(mapper);
7356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7357 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7358 WithPressure(0.6f))));
7359 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7360
7361 // There is another pressure change.
7362 mStylusState.pressure = 0.5f;
7363 processExternalStylusState(mapper);
7364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7365 ASSERT_NO_FATAL_FAILURE(
7366 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7367
7368 // If a touch is not reported within the timeout, a move event is generated to report
7369 // the new pressure.
7370 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7372 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7373 WithPressure(0.5f))));
7374
7375 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7376 // repeated indefinitely.
7377 mStylusState.pressure = 0.0f;
7378 processExternalStylusState(mapper);
7379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7380 ASSERT_NO_FATAL_FAILURE(
7381 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7382 processMove(mapper, 102, 202);
7383 processSync(mapper);
7384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7385 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7386 WithPressure(0.5f))));
7387 processMove(mapper, 103, 203);
7388 processSync(mapper);
7389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7390 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7391 WithPressure(0.5f))));
7392
7393 processUp(mapper);
7394 processSync(mapper);
7395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7396 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7397 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7398
7399 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7401}
7402
7403TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7404 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7405 auto source = WithSource(EXPECTED_SOURCE);
7406
7407 mStylusState.pressure = 1.f;
7408 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_ERASER;
7409 processExternalStylusState(mapper);
7410 processDown(mapper, 100, 200);
7411 processSync(mapper);
7412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7413 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7414 WithToolType(AMOTION_EVENT_TOOL_TYPE_ERASER))));
7415 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7416
7417 // The external stylus reports a tool change. We wait for some time for a touch event.
7418 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7419 processExternalStylusState(mapper);
7420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7421 ASSERT_NO_FATAL_FAILURE(
7422 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7423
7424 // If a touch is reported within the timeout, it reports the updated pressure.
7425 processMove(mapper, 101, 201);
7426 processSync(mapper);
7427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7428 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7429 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7430 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7431
7432 // There is another tool type change.
7433 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
7434 processExternalStylusState(mapper);
7435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7436 ASSERT_NO_FATAL_FAILURE(
7437 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7438
7439 // If a touch is not reported within the timeout, a move event is generated to report
7440 // the new tool type.
7441 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7443 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7444 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7445
7446 processUp(mapper);
7447 processSync(mapper);
7448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7449 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
7450 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7451
7452 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7454}
7455
7456TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7457 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7458 auto toolTypeSource =
7459 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7460
7461 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7462
7463 // The external stylus reports a button change. We wait for some time for a touch event.
7464 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7465 processExternalStylusState(mapper);
7466 ASSERT_NO_FATAL_FAILURE(
7467 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7468
7469 // If a touch is reported within the timeout, it reports the updated button state.
7470 processMove(mapper, 101, 201);
7471 processSync(mapper);
7472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7473 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7474 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7476 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7477 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7478 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7479
7480 // The button is now released.
7481 mStylusState.buttons = 0;
7482 processExternalStylusState(mapper);
7483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7484 ASSERT_NO_FATAL_FAILURE(
7485 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7486
7487 // If a touch is not reported within the timeout, a move event is generated to report
7488 // the new button state.
7489 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7491 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7492 WithButtonState(0))));
7493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007494 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7495 WithButtonState(0))));
7496
7497 processUp(mapper);
7498 processSync(mapper);
7499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007500 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7501
7502 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7504}
7505
Michael Wrightd02c5b62014-02-10 15:10:22 -08007506// --- MultiTouchInputMapperTest ---
7507
7508class MultiTouchInputMapperTest : public TouchInputMapperTest {
7509protected:
7510 void prepareAxes(int axes);
7511
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007512 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7513 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7514 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7515 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7516 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7517 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7518 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7519 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7520 void processId(MultiTouchInputMapper& mapper, int32_t id);
7521 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7522 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7523 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007524 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007525 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007526 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7527 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007528};
7529
7530void MultiTouchInputMapperTest::prepareAxes(int axes) {
7531 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007532 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7533 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007534 }
7535 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007536 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7537 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007538 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007539 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7540 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007541 }
7542 }
7543 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007544 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7545 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007546 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007547 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007548 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007549 }
7550 }
7551 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007552 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7553 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007554 }
7555 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007556 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7557 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007558 }
7559 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007560 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7561 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007562 }
7563 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007564 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7565 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007566 }
7567 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007568 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7569 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007570 }
7571 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007572 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007573 }
7574}
7575
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007576void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7577 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007578 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7579 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007580}
7581
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007582void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7583 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007584 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007585}
7586
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007587void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7588 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007590}
7591
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007592void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007594}
7595
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007596void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007597 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007598}
7599
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007600void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7601 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007602 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007603}
7604
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007605void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007606 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007607}
7608
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007609void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007610 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007611}
7612
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007613void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007615}
7616
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007617void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007618 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007619}
7620
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007621void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007622 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007623}
7624
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007625void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7626 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007628}
7629
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007630void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7631 int32_t value) {
7632 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7633 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7634}
7635
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007636void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007637 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007638}
7639
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007640void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7641 nsecs_t readTime) {
7642 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007643}
7644
Michael Wrightd02c5b62014-02-10 15:10:22 -08007645TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007646 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007647 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007648 prepareAxes(POSITION);
7649 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007650 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007651
arthurhungdcef2dc2020-08-11 14:47:50 +08007652 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007653
7654 NotifyMotionArgs motionArgs;
7655
7656 // Two fingers down at once.
7657 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7658 processPosition(mapper, x1, y1);
7659 processMTSync(mapper);
7660 processPosition(mapper, x2, y2);
7661 processMTSync(mapper);
7662 processSync(mapper);
7663
7664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7665 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7666 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7667 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7668 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7669 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7670 ASSERT_EQ(0, motionArgs.flags);
7671 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7672 ASSERT_EQ(0, motionArgs.buttonState);
7673 ASSERT_EQ(0, motionArgs.edgeFlags);
7674 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7675 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7676 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7678 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7679 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7680 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7681 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7682
7683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7684 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7685 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7686 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7687 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007688 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007689 ASSERT_EQ(0, motionArgs.flags);
7690 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7691 ASSERT_EQ(0, motionArgs.buttonState);
7692 ASSERT_EQ(0, motionArgs.edgeFlags);
7693 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7694 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7695 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7696 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7697 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7699 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7701 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7702 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7703 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7704 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7705
7706 // Move.
7707 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7708 processPosition(mapper, x1, y1);
7709 processMTSync(mapper);
7710 processPosition(mapper, x2, y2);
7711 processMTSync(mapper);
7712 processSync(mapper);
7713
7714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7715 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7716 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7717 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7718 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7719 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7720 ASSERT_EQ(0, motionArgs.flags);
7721 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7722 ASSERT_EQ(0, motionArgs.buttonState);
7723 ASSERT_EQ(0, motionArgs.edgeFlags);
7724 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7725 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7726 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7727 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7728 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7729 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7730 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7732 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7733 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7734 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7735 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7736
7737 // First finger up.
7738 x2 += 15; y2 -= 20;
7739 processPosition(mapper, x2, y2);
7740 processMTSync(mapper);
7741 processSync(mapper);
7742
7743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7744 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7745 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7746 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7747 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007748 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007749 ASSERT_EQ(0, motionArgs.flags);
7750 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7751 ASSERT_EQ(0, motionArgs.buttonState);
7752 ASSERT_EQ(0, motionArgs.edgeFlags);
7753 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7754 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7755 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7756 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7757 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7759 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7761 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7762 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7763 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7764 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7765
7766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7767 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7768 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7769 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7770 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7771 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7772 ASSERT_EQ(0, motionArgs.flags);
7773 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7774 ASSERT_EQ(0, motionArgs.buttonState);
7775 ASSERT_EQ(0, motionArgs.edgeFlags);
7776 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7777 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7778 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7780 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7781 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7782 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7783 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7784
7785 // Move.
7786 x2 += 20; y2 -= 25;
7787 processPosition(mapper, x2, y2);
7788 processMTSync(mapper);
7789 processSync(mapper);
7790
7791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7792 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7793 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7794 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7795 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7796 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7797 ASSERT_EQ(0, motionArgs.flags);
7798 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7799 ASSERT_EQ(0, motionArgs.buttonState);
7800 ASSERT_EQ(0, motionArgs.edgeFlags);
7801 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7802 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7803 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7805 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7806 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7807 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7808 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7809
7810 // New finger down.
7811 int32_t x3 = 700, y3 = 300;
7812 processPosition(mapper, x2, y2);
7813 processMTSync(mapper);
7814 processPosition(mapper, x3, y3);
7815 processMTSync(mapper);
7816 processSync(mapper);
7817
7818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7819 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7820 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7821 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7822 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007823 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007824 ASSERT_EQ(0, motionArgs.flags);
7825 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7826 ASSERT_EQ(0, motionArgs.buttonState);
7827 ASSERT_EQ(0, motionArgs.edgeFlags);
7828 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7829 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7830 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7831 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7832 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7833 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7834 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7836 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7837 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7838 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7839 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7840
7841 // Second finger up.
7842 x3 += 30; y3 -= 20;
7843 processPosition(mapper, x3, y3);
7844 processMTSync(mapper);
7845 processSync(mapper);
7846
7847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7848 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7849 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7850 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7851 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007852 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007853 ASSERT_EQ(0, motionArgs.flags);
7854 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7855 ASSERT_EQ(0, motionArgs.buttonState);
7856 ASSERT_EQ(0, motionArgs.edgeFlags);
7857 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7858 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7859 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7860 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7861 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7863 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7864 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7865 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7866 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7867 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7868 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7869
7870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7871 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7872 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7873 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7874 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7875 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7876 ASSERT_EQ(0, motionArgs.flags);
7877 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7878 ASSERT_EQ(0, motionArgs.buttonState);
7879 ASSERT_EQ(0, motionArgs.edgeFlags);
7880 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7881 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7882 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7883 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7884 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7885 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7886 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7887 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7888
7889 // Last finger up.
7890 processMTSync(mapper);
7891 processSync(mapper);
7892
7893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7894 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7895 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7896 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7897 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7898 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7899 ASSERT_EQ(0, motionArgs.flags);
7900 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7901 ASSERT_EQ(0, motionArgs.buttonState);
7902 ASSERT_EQ(0, motionArgs.edgeFlags);
7903 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7904 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7905 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7907 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7908 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7909 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7910 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7911
7912 // Should not have sent any more keys or motions.
7913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7915}
7916
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007917TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7918 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007919 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007920
7921 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7922 /*fuzz*/ 0, /*resolution*/ 10);
7923 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7924 /*fuzz*/ 0, /*resolution*/ 11);
7925 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7926 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7927 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7928 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7929 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7930 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7931 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7932 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7933
7934 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7935
7936 // X and Y axes
7937 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7938 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7939 // Touch major and minor
7940 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7941 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7942 // Tool major and minor
7943 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7944 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7945}
7946
7947TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7948 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007949 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007950
7951 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7952 /*fuzz*/ 0, /*resolution*/ 10);
7953 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7954 /*fuzz*/ 0, /*resolution*/ 11);
7955
7956 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7957
7958 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7959
7960 // Touch major and minor
7961 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7962 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7963 // Tool major and minor
7964 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7965 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7966}
7967
Michael Wrightd02c5b62014-02-10 15:10:22 -08007968TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007969 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007970 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007971 prepareAxes(POSITION | ID);
7972 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007973 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007974
arthurhungdcef2dc2020-08-11 14:47:50 +08007975 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007976
7977 NotifyMotionArgs motionArgs;
7978
7979 // Two fingers down at once.
7980 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7981 processPosition(mapper, x1, y1);
7982 processId(mapper, 1);
7983 processMTSync(mapper);
7984 processPosition(mapper, x2, y2);
7985 processId(mapper, 2);
7986 processMTSync(mapper);
7987 processSync(mapper);
7988
7989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7990 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7991 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7992 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7993 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7995 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7996
7997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007998 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007999 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8000 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8001 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8002 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8003 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8004 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8005 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8006 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8007 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8008
8009 // Move.
8010 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8011 processPosition(mapper, x1, y1);
8012 processId(mapper, 1);
8013 processMTSync(mapper);
8014 processPosition(mapper, x2, y2);
8015 processId(mapper, 2);
8016 processMTSync(mapper);
8017 processSync(mapper);
8018
8019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8021 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8022 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8023 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8024 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8025 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8026 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8027 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8028 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8029 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8030
8031 // First finger up.
8032 x2 += 15; y2 -= 20;
8033 processPosition(mapper, x2, y2);
8034 processId(mapper, 2);
8035 processMTSync(mapper);
8036 processSync(mapper);
8037
8038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008039 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008040 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8041 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8042 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8043 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8044 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8046 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8048 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8049
8050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8052 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8053 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8054 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8055 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8056 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8057
8058 // Move.
8059 x2 += 20; y2 -= 25;
8060 processPosition(mapper, x2, y2);
8061 processId(mapper, 2);
8062 processMTSync(mapper);
8063 processSync(mapper);
8064
8065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8066 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8067 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8068 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8069 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8070 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8071 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8072
8073 // New finger down.
8074 int32_t x3 = 700, y3 = 300;
8075 processPosition(mapper, x2, y2);
8076 processId(mapper, 2);
8077 processMTSync(mapper);
8078 processPosition(mapper, x3, y3);
8079 processId(mapper, 3);
8080 processMTSync(mapper);
8081 processSync(mapper);
8082
8083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008084 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008085 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8086 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8087 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8088 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8089 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8090 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8091 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8093 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8094
8095 // Second finger up.
8096 x3 += 30; y3 -= 20;
8097 processPosition(mapper, x3, y3);
8098 processId(mapper, 3);
8099 processMTSync(mapper);
8100 processSync(mapper);
8101
8102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008103 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008104 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8105 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8106 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8107 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8108 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8109 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8110 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8112 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8113
8114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8116 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8117 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8118 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8119 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8120 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8121
8122 // Last finger up.
8123 processMTSync(mapper);
8124 processSync(mapper);
8125
8126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8127 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8128 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8129 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8130 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8131 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8132 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8133
8134 // Should not have sent any more keys or motions.
8135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8137}
8138
8139TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008140 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008141 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008142 prepareAxes(POSITION | ID | SLOT);
8143 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008144 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008145
arthurhungdcef2dc2020-08-11 14:47:50 +08008146 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008147
8148 NotifyMotionArgs motionArgs;
8149
8150 // Two fingers down at once.
8151 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8152 processPosition(mapper, x1, y1);
8153 processId(mapper, 1);
8154 processSlot(mapper, 1);
8155 processPosition(mapper, x2, y2);
8156 processId(mapper, 2);
8157 processSync(mapper);
8158
8159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8160 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8161 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8162 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8163 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8164 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8165 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8166
8167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008168 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008169 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8170 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8171 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8172 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8175 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8176 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8177 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8178
8179 // Move.
8180 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8181 processSlot(mapper, 0);
8182 processPosition(mapper, x1, y1);
8183 processSlot(mapper, 1);
8184 processPosition(mapper, x2, y2);
8185 processSync(mapper);
8186
8187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8189 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8190 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8192 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8193 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8194 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8195 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8196 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8197 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8198
8199 // First finger up.
8200 x2 += 15; y2 -= 20;
8201 processSlot(mapper, 0);
8202 processId(mapper, -1);
8203 processSlot(mapper, 1);
8204 processPosition(mapper, x2, y2);
8205 processSync(mapper);
8206
8207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008208 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008209 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8210 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8211 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8212 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8215 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8216 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8217 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8218
8219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8221 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8222 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8223 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8225 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8226
8227 // Move.
8228 x2 += 20; y2 -= 25;
8229 processPosition(mapper, x2, y2);
8230 processSync(mapper);
8231
8232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8233 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8234 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8235 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8236 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8238 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8239
8240 // New finger down.
8241 int32_t x3 = 700, y3 = 300;
8242 processPosition(mapper, x2, y2);
8243 processSlot(mapper, 0);
8244 processId(mapper, 3);
8245 processPosition(mapper, x3, y3);
8246 processSync(mapper);
8247
8248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008249 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008250 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8251 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8252 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8253 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8256 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8258 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8259
8260 // Second finger up.
8261 x3 += 30; y3 -= 20;
8262 processSlot(mapper, 1);
8263 processId(mapper, -1);
8264 processSlot(mapper, 0);
8265 processPosition(mapper, x3, y3);
8266 processSync(mapper);
8267
8268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008269 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008270 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8271 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8272 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8273 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8274 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8276 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8278 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8279
8280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8281 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8282 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8283 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8286 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8287
8288 // Last finger up.
8289 processId(mapper, -1);
8290 processSync(mapper);
8291
8292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8293 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8294 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8295 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8296 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8298 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8299
8300 // Should not have sent any more keys or motions.
8301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8303}
8304
8305TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008306 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008307 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008308 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008309 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008310
8311 // These calculations are based on the input device calibration documentation.
8312 int32_t rawX = 100;
8313 int32_t rawY = 200;
8314 int32_t rawTouchMajor = 7;
8315 int32_t rawTouchMinor = 6;
8316 int32_t rawToolMajor = 9;
8317 int32_t rawToolMinor = 8;
8318 int32_t rawPressure = 11;
8319 int32_t rawDistance = 0;
8320 int32_t rawOrientation = 3;
8321 int32_t id = 5;
8322
8323 float x = toDisplayX(rawX);
8324 float y = toDisplayY(rawY);
8325 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8326 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8327 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8328 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8329 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8330 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8331 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8332 float distance = float(rawDistance);
8333
8334 processPosition(mapper, rawX, rawY);
8335 processTouchMajor(mapper, rawTouchMajor);
8336 processTouchMinor(mapper, rawTouchMinor);
8337 processToolMajor(mapper, rawToolMajor);
8338 processToolMinor(mapper, rawToolMinor);
8339 processPressure(mapper, rawPressure);
8340 processOrientation(mapper, rawOrientation);
8341 processDistance(mapper, rawDistance);
8342 processId(mapper, id);
8343 processMTSync(mapper);
8344 processSync(mapper);
8345
8346 NotifyMotionArgs args;
8347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8348 ASSERT_EQ(0, args.pointerProperties[0].id);
8349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8350 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8351 orientation, distance));
8352}
8353
8354TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008355 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008356 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008357 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8358 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008359 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008360
8361 // These calculations are based on the input device calibration documentation.
8362 int32_t rawX = 100;
8363 int32_t rawY = 200;
8364 int32_t rawTouchMajor = 140;
8365 int32_t rawTouchMinor = 120;
8366 int32_t rawToolMajor = 180;
8367 int32_t rawToolMinor = 160;
8368
8369 float x = toDisplayX(rawX);
8370 float y = toDisplayY(rawY);
8371 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8372 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8373 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8374 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8375 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8376
8377 processPosition(mapper, rawX, rawY);
8378 processTouchMajor(mapper, rawTouchMajor);
8379 processTouchMinor(mapper, rawTouchMinor);
8380 processToolMajor(mapper, rawToolMajor);
8381 processToolMinor(mapper, rawToolMinor);
8382 processMTSync(mapper);
8383 processSync(mapper);
8384
8385 NotifyMotionArgs args;
8386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8387 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8388 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8389}
8390
8391TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
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);
8395 addConfigurationProperty("touch.size.calibration", "diameter");
8396 addConfigurationProperty("touch.size.scale", "10");
8397 addConfigurationProperty("touch.size.bias", "160");
8398 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008399 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008400
8401 // These calculations are based on the input device calibration documentation.
8402 // Note: We only provide a single common touch/tool value because the device is assumed
8403 // not to emit separate values for each pointer (isSummed = 1).
8404 int32_t rawX = 100;
8405 int32_t rawY = 200;
8406 int32_t rawX2 = 150;
8407 int32_t rawY2 = 250;
8408 int32_t rawTouchMajor = 5;
8409 int32_t rawToolMajor = 8;
8410
8411 float x = toDisplayX(rawX);
8412 float y = toDisplayY(rawY);
8413 float x2 = toDisplayX(rawX2);
8414 float y2 = toDisplayY(rawY2);
8415 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8416 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8417 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8418
8419 processPosition(mapper, rawX, rawY);
8420 processTouchMajor(mapper, rawTouchMajor);
8421 processToolMajor(mapper, rawToolMajor);
8422 processMTSync(mapper);
8423 processPosition(mapper, rawX2, rawY2);
8424 processTouchMajor(mapper, rawTouchMajor);
8425 processToolMajor(mapper, rawToolMajor);
8426 processMTSync(mapper);
8427 processSync(mapper);
8428
8429 NotifyMotionArgs args;
8430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8431 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8432
8433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008434 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008435 ASSERT_EQ(size_t(2), args.pointerCount);
8436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8437 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8439 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8440}
8441
8442TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008443 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008444 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008445 prepareAxes(POSITION | TOUCH | TOOL);
8446 addConfigurationProperty("touch.size.calibration", "area");
8447 addConfigurationProperty("touch.size.scale", "43");
8448 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008449 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008450
8451 // These calculations are based on the input device calibration documentation.
8452 int32_t rawX = 100;
8453 int32_t rawY = 200;
8454 int32_t rawTouchMajor = 5;
8455 int32_t rawToolMajor = 8;
8456
8457 float x = toDisplayX(rawX);
8458 float y = toDisplayY(rawY);
8459 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8460 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8461 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8462
8463 processPosition(mapper, rawX, rawY);
8464 processTouchMajor(mapper, rawTouchMajor);
8465 processToolMajor(mapper, rawToolMajor);
8466 processMTSync(mapper);
8467 processSync(mapper);
8468
8469 NotifyMotionArgs args;
8470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8472 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8473}
8474
8475TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008476 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008477 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008478 prepareAxes(POSITION | PRESSURE);
8479 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8480 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008481 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008482
Michael Wrightaa449c92017-12-13 21:21:43 +00008483 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008484 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008485 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8486 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8487 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8488
Michael Wrightd02c5b62014-02-10 15:10:22 -08008489 // These calculations are based on the input device calibration documentation.
8490 int32_t rawX = 100;
8491 int32_t rawY = 200;
8492 int32_t rawPressure = 60;
8493
8494 float x = toDisplayX(rawX);
8495 float y = toDisplayY(rawY);
8496 float pressure = float(rawPressure) * 0.01f;
8497
8498 processPosition(mapper, rawX, rawY);
8499 processPressure(mapper, rawPressure);
8500 processMTSync(mapper);
8501 processSync(mapper);
8502
8503 NotifyMotionArgs args;
8504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8506 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8507}
8508
8509TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008510 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008511 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008512 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008513 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008514
8515 NotifyMotionArgs motionArgs;
8516 NotifyKeyArgs keyArgs;
8517
8518 processId(mapper, 1);
8519 processPosition(mapper, 100, 200);
8520 processSync(mapper);
8521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8522 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8523 ASSERT_EQ(0, motionArgs.buttonState);
8524
8525 // press BTN_LEFT, release BTN_LEFT
8526 processKey(mapper, BTN_LEFT, 1);
8527 processSync(mapper);
8528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8529 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8530 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8531
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8533 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8534 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8535
Michael Wrightd02c5b62014-02-10 15:10:22 -08008536 processKey(mapper, BTN_LEFT, 0);
8537 processSync(mapper);
8538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008539 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008540 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008541
8542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008543 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008544 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008545
8546 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8547 processKey(mapper, BTN_RIGHT, 1);
8548 processKey(mapper, BTN_MIDDLE, 1);
8549 processSync(mapper);
8550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8552 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8553 motionArgs.buttonState);
8554
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8556 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8557 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8558
8559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8560 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8561 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8562 motionArgs.buttonState);
8563
Michael Wrightd02c5b62014-02-10 15:10:22 -08008564 processKey(mapper, BTN_RIGHT, 0);
8565 processSync(mapper);
8566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008567 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008568 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008569
8570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008572 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008573
8574 processKey(mapper, BTN_MIDDLE, 0);
8575 processSync(mapper);
8576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008577 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008578 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008579
8580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008581 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008582 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008583
8584 // press BTN_BACK, release BTN_BACK
8585 processKey(mapper, BTN_BACK, 1);
8586 processSync(mapper);
8587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8588 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8589 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008590
Michael Wrightd02c5b62014-02-10 15:10:22 -08008591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008592 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008593 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8594
8595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8596 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8597 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008598
8599 processKey(mapper, BTN_BACK, 0);
8600 processSync(mapper);
8601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008602 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008603 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008604
8605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008606 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008607 ASSERT_EQ(0, motionArgs.buttonState);
8608
Michael Wrightd02c5b62014-02-10 15:10:22 -08008609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8610 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8611 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8612
8613 // press BTN_SIDE, release BTN_SIDE
8614 processKey(mapper, BTN_SIDE, 1);
8615 processSync(mapper);
8616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8617 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8618 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008619
Michael Wrightd02c5b62014-02-10 15:10:22 -08008620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008622 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8623
8624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8625 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8626 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008627
8628 processKey(mapper, BTN_SIDE, 0);
8629 processSync(mapper);
8630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008631 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008632 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008633
8634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008635 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008636 ASSERT_EQ(0, motionArgs.buttonState);
8637
Michael Wrightd02c5b62014-02-10 15:10:22 -08008638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8639 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8640 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8641
8642 // press BTN_FORWARD, release BTN_FORWARD
8643 processKey(mapper, BTN_FORWARD, 1);
8644 processSync(mapper);
8645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8646 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8647 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008648
Michael Wrightd02c5b62014-02-10 15:10:22 -08008649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008651 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8652
8653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8654 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8655 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008656
8657 processKey(mapper, BTN_FORWARD, 0);
8658 processSync(mapper);
8659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008660 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008661 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008662
8663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008664 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008665 ASSERT_EQ(0, motionArgs.buttonState);
8666
Michael Wrightd02c5b62014-02-10 15:10:22 -08008667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8668 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8669 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8670
8671 // press BTN_EXTRA, release BTN_EXTRA
8672 processKey(mapper, BTN_EXTRA, 1);
8673 processSync(mapper);
8674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8675 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8676 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008677
Michael Wrightd02c5b62014-02-10 15:10:22 -08008678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008679 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008680 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8681
8682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8683 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8684 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008685
8686 processKey(mapper, BTN_EXTRA, 0);
8687 processSync(mapper);
8688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008689 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008690 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008691
8692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008693 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008694 ASSERT_EQ(0, motionArgs.buttonState);
8695
Michael Wrightd02c5b62014-02-10 15:10:22 -08008696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8697 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8698 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8699
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8701
Michael Wrightd02c5b62014-02-10 15:10:22 -08008702 // press BTN_STYLUS, release BTN_STYLUS
8703 processKey(mapper, BTN_STYLUS, 1);
8704 processSync(mapper);
8705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8706 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008707 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8708
8709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8710 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8711 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008712
8713 processKey(mapper, BTN_STYLUS, 0);
8714 processSync(mapper);
8715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008716 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008717 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008718
8719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008720 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008721 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008722
8723 // press BTN_STYLUS2, release BTN_STYLUS2
8724 processKey(mapper, BTN_STYLUS2, 1);
8725 processSync(mapper);
8726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8727 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008728 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8729
8730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8731 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8732 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008733
8734 processKey(mapper, BTN_STYLUS2, 0);
8735 processSync(mapper);
8736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008737 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008738 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008739
8740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008741 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008742 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008743
8744 // release touch
8745 processId(mapper, -1);
8746 processSync(mapper);
8747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8748 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8749 ASSERT_EQ(0, motionArgs.buttonState);
8750}
8751
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008752TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8753 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008754 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008755 prepareAxes(POSITION | ID | SLOT);
8756 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8757
8758 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8759 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8760
8761 // Touch down.
8762 processId(mapper, 1);
8763 processPosition(mapper, 100, 200);
8764 processSync(mapper);
8765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8766 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8767
8768 // Press and release button mapped to the primary stylus button.
8769 processKey(mapper, BTN_A, 1);
8770 processSync(mapper);
8771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8772 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8773 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8775 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8776 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8777
8778 processKey(mapper, BTN_A, 0);
8779 processSync(mapper);
8780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8781 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8783 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8784
8785 // Press and release the HID usage mapped to the secondary stylus button.
8786 processHidUsage(mapper, 0xabcd, 1);
8787 processSync(mapper);
8788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8789 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8790 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8792 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8793 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8794
8795 processHidUsage(mapper, 0xabcd, 0);
8796 processSync(mapper);
8797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8798 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8800 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8801
8802 // Release touch.
8803 processId(mapper, -1);
8804 processSync(mapper);
8805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8806 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8807}
8808
Michael Wrightd02c5b62014-02-10 15:10:22 -08008809TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008810 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008811 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008812 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008813 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008814
8815 NotifyMotionArgs motionArgs;
8816
8817 // default tool type is finger
8818 processId(mapper, 1);
8819 processPosition(mapper, 100, 200);
8820 processSync(mapper);
8821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8822 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8823 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8824
8825 // eraser
8826 processKey(mapper, BTN_TOOL_RUBBER, 1);
8827 processSync(mapper);
8828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8829 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8830 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8831
8832 // stylus
8833 processKey(mapper, BTN_TOOL_RUBBER, 0);
8834 processKey(mapper, BTN_TOOL_PEN, 1);
8835 processSync(mapper);
8836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8837 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8838 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8839
8840 // brush
8841 processKey(mapper, BTN_TOOL_PEN, 0);
8842 processKey(mapper, BTN_TOOL_BRUSH, 1);
8843 processSync(mapper);
8844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8845 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8846 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8847
8848 // pencil
8849 processKey(mapper, BTN_TOOL_BRUSH, 0);
8850 processKey(mapper, BTN_TOOL_PENCIL, 1);
8851 processSync(mapper);
8852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8853 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8854 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8855
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008856 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008857 processKey(mapper, BTN_TOOL_PENCIL, 0);
8858 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8859 processSync(mapper);
8860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8861 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8862 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8863
8864 // mouse
8865 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8866 processKey(mapper, BTN_TOOL_MOUSE, 1);
8867 processSync(mapper);
8868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8869 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8870 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8871
8872 // lens
8873 processKey(mapper, BTN_TOOL_MOUSE, 0);
8874 processKey(mapper, BTN_TOOL_LENS, 1);
8875 processSync(mapper);
8876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8877 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8878 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8879
8880 // double-tap
8881 processKey(mapper, BTN_TOOL_LENS, 0);
8882 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8883 processSync(mapper);
8884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8885 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8886 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8887
8888 // triple-tap
8889 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8890 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8891 processSync(mapper);
8892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8893 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8894 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8895
8896 // quad-tap
8897 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8898 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8899 processSync(mapper);
8900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8901 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8902 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8903
8904 // finger
8905 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8906 processKey(mapper, BTN_TOOL_FINGER, 1);
8907 processSync(mapper);
8908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8909 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8910 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8911
8912 // stylus trumps finger
8913 processKey(mapper, BTN_TOOL_PEN, 1);
8914 processSync(mapper);
8915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8916 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8917 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8918
8919 // eraser trumps stylus
8920 processKey(mapper, BTN_TOOL_RUBBER, 1);
8921 processSync(mapper);
8922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8923 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8924 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8925
8926 // mouse trumps eraser
8927 processKey(mapper, BTN_TOOL_MOUSE, 1);
8928 processSync(mapper);
8929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8930 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8931 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8932
8933 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8934 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8935 processSync(mapper);
8936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8938 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8939
8940 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8941 processToolType(mapper, MT_TOOL_PEN);
8942 processSync(mapper);
8943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8944 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8945 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8946
8947 // back to default tool type
8948 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8949 processKey(mapper, BTN_TOOL_MOUSE, 0);
8950 processKey(mapper, BTN_TOOL_RUBBER, 0);
8951 processKey(mapper, BTN_TOOL_PEN, 0);
8952 processKey(mapper, BTN_TOOL_FINGER, 0);
8953 processSync(mapper);
8954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8955 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8956 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8957}
8958
8959TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008960 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008961 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008962 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008963 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008964 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008965
8966 NotifyMotionArgs motionArgs;
8967
8968 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8969 processId(mapper, 1);
8970 processPosition(mapper, 100, 200);
8971 processSync(mapper);
8972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8973 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8974 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8975 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8976
8977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8978 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8979 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8980 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8981
8982 // move a little
8983 processPosition(mapper, 150, 250);
8984 processSync(mapper);
8985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8986 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8987 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8988 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8989
8990 // down when BTN_TOUCH is pressed, pressure defaults to 1
8991 processKey(mapper, BTN_TOUCH, 1);
8992 processSync(mapper);
8993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8994 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8996 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8997
8998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8999 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9001 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9002
9003 // up when BTN_TOUCH is released, hover restored
9004 processKey(mapper, BTN_TOUCH, 0);
9005 processSync(mapper);
9006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9007 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9009 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9010
9011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9012 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9014 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9015
9016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9017 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9018 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9019 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9020
9021 // exit hover when pointer goes away
9022 processId(mapper, -1);
9023 processSync(mapper);
9024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9025 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9026 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9027 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9028}
9029
9030TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009031 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009032 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009033 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009034 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009035
9036 NotifyMotionArgs motionArgs;
9037
9038 // initially hovering because pressure is 0
9039 processId(mapper, 1);
9040 processPosition(mapper, 100, 200);
9041 processPressure(mapper, 0);
9042 processSync(mapper);
9043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9044 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9046 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9047
9048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9049 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9051 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9052
9053 // move a little
9054 processPosition(mapper, 150, 250);
9055 processSync(mapper);
9056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9057 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9058 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9059 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9060
9061 // down when pressure becomes non-zero
9062 processPressure(mapper, RAW_PRESSURE_MAX);
9063 processSync(mapper);
9064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9065 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9066 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9067 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9068
9069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9070 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9071 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9072 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9073
9074 // up when pressure becomes 0, hover restored
9075 processPressure(mapper, 0);
9076 processSync(mapper);
9077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9078 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9080 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9081
9082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9083 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9085 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9086
9087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9088 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9089 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9090 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9091
9092 // exit hover when pointer goes away
9093 processId(mapper, -1);
9094 processSync(mapper);
9095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9096 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9098 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9099}
9100
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009101/**
9102 * Set the input device port <--> display port associations, and check that the
9103 * events are routed to the display that matches the display port.
9104 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9105 */
9106TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009107 const std::string usb2 = "USB2";
9108 const uint8_t hdmi1 = 0;
9109 const uint8_t hdmi2 = 1;
9110 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009111 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009112
9113 addConfigurationProperty("touch.deviceType", "touchScreen");
9114 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009115 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009116
9117 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9118 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9119
9120 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9121 // for this input device is specified, and the matching viewport is not present,
9122 // the input device should be disabled (at the mapper level).
9123
9124 // Add viewport for display 2 on hdmi2
9125 prepareSecondaryDisplay(type, hdmi2);
9126 // Send a touch event
9127 processPosition(mapper, 100, 100);
9128 processSync(mapper);
9129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9130
9131 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009132 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009133 // Send a touch event again
9134 processPosition(mapper, 100, 100);
9135 processSync(mapper);
9136
9137 NotifyMotionArgs args;
9138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9139 ASSERT_EQ(DISPLAY_ID, args.displayId);
9140}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009141
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009142TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9143 addConfigurationProperty("touch.deviceType", "touchScreen");
9144 prepareAxes(POSITION);
9145 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9146
9147 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9148
Michael Wrighta9cf4192022-12-01 23:46:39 +00009149 prepareDisplay(ui::ROTATION_0);
9150 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009151
9152 // Send a touch event
9153 processPosition(mapper, 100, 100);
9154 processSync(mapper);
9155
9156 NotifyMotionArgs args;
9157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9158 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9159}
9160
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009161TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009162 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009163 std::shared_ptr<FakePointerController> fakePointerController =
9164 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009165 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009166 fakePointerController->setPosition(100, 200);
9167 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009168 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009169
Garfield Tan888a6a42020-01-09 11:39:16 -08009170 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009171 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009172
Michael Wrighta9cf4192022-12-01 23:46:39 +00009173 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009174 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009175 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009176
Josep del Río2d8c79a2023-01-23 19:33:50 +00009177 // Check source is mouse that would obtain the PointerController.
9178 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009179
9180 NotifyMotionArgs motionArgs;
9181 processPosition(mapper, 100, 100);
9182 processSync(mapper);
9183
9184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9185 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9186 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9187}
9188
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009189/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009190 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9191 */
9192TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9193 addConfigurationProperty("touch.deviceType", "touchScreen");
9194 prepareAxes(POSITION);
9195 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9196
Michael Wrighta9cf4192022-12-01 23:46:39 +00009197 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009198 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9199 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9200 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9201 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009202
9203 NotifyMotionArgs args;
9204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9205 ASSERT_EQ(26, args.readTime);
9206
Harry Cutts33476232023-01-30 19:57:29 +00009207 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9208 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9209 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009210
9211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9212 ASSERT_EQ(33, args.readTime);
9213}
9214
9215/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009216 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9217 * events should not be delivered to the listener.
9218 */
9219TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9220 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009221 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009222 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009223 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009224 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9225 prepareAxes(POSITION);
9226 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9227
9228 NotifyMotionArgs motionArgs;
9229 processPosition(mapper, 100, 100);
9230 processSync(mapper);
9231
9232 mFakeListener->assertNotifyMotionWasNotCalled();
9233}
9234
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009235/**
9236 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9237 * the touch mapper can process the events and the events can be delivered to the listener.
9238 */
9239TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9240 addConfigurationProperty("touch.deviceType", "touchScreen");
9241 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009242 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009243 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009244 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9245 prepareAxes(POSITION);
9246 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9247
9248 NotifyMotionArgs motionArgs;
9249 processPosition(mapper, 100, 100);
9250 processSync(mapper);
9251
9252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9253 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9254}
9255
Garfield Tanc734e4f2021-01-15 20:01:39 -08009256TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9257 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009258 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009259 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009260 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009261 std::optional<DisplayViewport> optionalDisplayViewport =
9262 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9263 ASSERT_TRUE(optionalDisplayViewport.has_value());
9264 DisplayViewport displayViewport = *optionalDisplayViewport;
9265
9266 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9267 prepareAxes(POSITION);
9268 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9269
9270 // Finger down
9271 int32_t x = 100, y = 100;
9272 processPosition(mapper, x, y);
9273 processSync(mapper);
9274
9275 NotifyMotionArgs motionArgs;
9276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9277 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9278
9279 // Deactivate display viewport
9280 displayViewport.isActive = false;
9281 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9282 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9283
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009284 // The ongoing touch should be canceled immediately
9285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9286 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9287
9288 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009289 x += 10, y += 10;
9290 processPosition(mapper, x, y);
9291 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009293
9294 // Reactivate display viewport
9295 displayViewport.isActive = true;
9296 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9297 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9298
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009299 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009300 x += 10, y += 10;
9301 processPosition(mapper, x, y);
9302 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9304 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009305}
9306
Arthur Hung7c645402019-01-25 17:45:42 +08009307TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9308 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009309 prepareAxes(POSITION | ID | SLOT);
9310 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009311 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009312
9313 // Create the second touch screen device, and enable multi fingers.
9314 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009315 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009316 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009317 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009318 std::shared_ptr<InputDevice> device2 =
9319 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009320 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009321
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009322 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009323 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009324 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009325 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009326 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009327 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009328 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009329 /*flat=*/0, /*fuzz=*/0);
9330 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009331 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9332 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009333
9334 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009335 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009336 std::list<NotifyArgs> unused =
9337 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00009338 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009339 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009340
9341 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009342 std::shared_ptr<FakePointerController> fakePointerController =
9343 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009344 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009345
9346 // Setup policy for associated displays and show touches.
9347 const uint8_t hdmi1 = 0;
9348 const uint8_t hdmi2 = 1;
9349 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9350 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9351 mFakePolicy->setShowTouches(true);
9352
9353 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009354 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009355 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009356
9357 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009358 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9359 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9360 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009361
9362 // Two fingers down at default display.
9363 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9364 processPosition(mapper, x1, y1);
9365 processId(mapper, 1);
9366 processSlot(mapper, 1);
9367 processPosition(mapper, x2, y2);
9368 processId(mapper, 2);
9369 processSync(mapper);
9370
9371 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9372 fakePointerController->getSpots().find(DISPLAY_ID);
9373 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9374 ASSERT_EQ(size_t(2), iter->second.size());
9375
9376 // Two fingers down at second display.
9377 processPosition(mapper2, x1, y1);
9378 processId(mapper2, 1);
9379 processSlot(mapper2, 1);
9380 processPosition(mapper2, x2, y2);
9381 processId(mapper2, 2);
9382 processSync(mapper2);
9383
9384 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9385 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9386 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009387
9388 // Disable the show touches configuration and ensure the spots are cleared.
9389 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009390 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9391 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009392
9393 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009394}
9395
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009396TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009397 prepareAxes(POSITION);
9398 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009399 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009400 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009401
9402 NotifyMotionArgs motionArgs;
9403 // Unrotated video frame
9404 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9405 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009406 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009407 processPosition(mapper, 100, 200);
9408 processSync(mapper);
9409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9410 ASSERT_EQ(frames, motionArgs.videoFrames);
9411
9412 // Subsequent touch events should not have any videoframes
9413 // This is implemented separately in FakeEventHub,
9414 // but that should match the behaviour of TouchVideoDevice.
9415 processPosition(mapper, 200, 200);
9416 processSync(mapper);
9417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9418 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9419}
9420
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009421TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009422 prepareAxes(POSITION);
9423 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009424 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009425 // Unrotated video frame
9426 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9427 NotifyMotionArgs motionArgs;
9428
9429 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009430 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009431 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9432 clearViewports();
9433 prepareDisplay(orientation);
9434 std::vector<TouchVideoFrame> frames{frame};
9435 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9436 processPosition(mapper, 100, 200);
9437 processSync(mapper);
9438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9439 ASSERT_EQ(frames, motionArgs.videoFrames);
9440 }
9441}
9442
9443TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9444 prepareAxes(POSITION);
9445 addConfigurationProperty("touch.deviceType", "touchScreen");
9446 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9447 // orientation-aware are affected by display rotation.
9448 addConfigurationProperty("touch.orientationAware", "0");
9449 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9450 // Unrotated video frame
9451 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9452 NotifyMotionArgs motionArgs;
9453
9454 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009455 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009456 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9457 clearViewports();
9458 prepareDisplay(orientation);
9459 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009460 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009461 processPosition(mapper, 100, 200);
9462 processSync(mapper);
9463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009464 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9465 // compared to the display. This is so that when the window transform (which contains the
9466 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9467 // window's coordinate space.
9468 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009469 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009470
9471 // Release finger.
9472 processSync(mapper);
9473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009474 }
9475}
9476
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009477TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009478 prepareAxes(POSITION);
9479 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009480 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009481 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9482 // so mix these.
9483 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9484 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9485 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9486 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9487 NotifyMotionArgs motionArgs;
9488
Michael Wrighta9cf4192022-12-01 23:46:39 +00009489 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009490 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009491 processPosition(mapper, 100, 200);
9492 processSync(mapper);
9493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009494 ASSERT_EQ(frames, motionArgs.videoFrames);
9495}
9496
9497TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9498 prepareAxes(POSITION);
9499 addConfigurationProperty("touch.deviceType", "touchScreen");
9500 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9501 // orientation-aware are affected by display rotation.
9502 addConfigurationProperty("touch.orientationAware", "0");
9503 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9504 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9505 // so mix these.
9506 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9507 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9508 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9509 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9510 NotifyMotionArgs motionArgs;
9511
Michael Wrighta9cf4192022-12-01 23:46:39 +00009512 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009513 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9514 processPosition(mapper, 100, 200);
9515 processSync(mapper);
9516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9517 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9518 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9519 // compared to the display. This is so that when the window transform (which contains the
9520 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9521 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009522 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009523 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009524 ASSERT_EQ(frames, motionArgs.videoFrames);
9525}
9526
Arthur Hung9da14732019-09-02 16:16:58 +08009527/**
9528 * If we had defined port associations, but the viewport is not ready, the touch device would be
9529 * expected to be disabled, and it should be enabled after the viewport has found.
9530 */
9531TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009532 constexpr uint8_t hdmi2 = 1;
9533 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009534 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009535
9536 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9537
9538 addConfigurationProperty("touch.deviceType", "touchScreen");
9539 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009540 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009541
9542 ASSERT_EQ(mDevice->isEnabled(), false);
9543
9544 // Add display on hdmi2, the device should be enabled and can receive touch event.
9545 prepareSecondaryDisplay(type, hdmi2);
9546 ASSERT_EQ(mDevice->isEnabled(), true);
9547
9548 // Send a touch event.
9549 processPosition(mapper, 100, 100);
9550 processSync(mapper);
9551
9552 NotifyMotionArgs args;
9553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9554 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9555}
9556
Arthur Hung421eb1c2020-01-16 00:09:42 +08009557TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009558 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009559 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009560 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009561 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009562
9563 NotifyMotionArgs motionArgs;
9564
9565 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9566 // finger down
9567 processId(mapper, 1);
9568 processPosition(mapper, x1, y1);
9569 processSync(mapper);
9570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9571 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9573
9574 // finger move
9575 processId(mapper, 1);
9576 processPosition(mapper, x2, y2);
9577 processSync(mapper);
9578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9581
9582 // finger up.
9583 processId(mapper, -1);
9584 processSync(mapper);
9585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9586 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9588
9589 // new finger down
9590 processId(mapper, 1);
9591 processPosition(mapper, x3, y3);
9592 processSync(mapper);
9593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9594 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9595 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9596}
9597
9598/**
arthurhungcc7f9802020-04-30 17:55:40 +08009599 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9600 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009601 */
arthurhungcc7f9802020-04-30 17:55:40 +08009602TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009603 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009604 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009605 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009606 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009607
9608 NotifyMotionArgs motionArgs;
9609
9610 // default tool type is finger
9611 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009612 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009613 processPosition(mapper, x1, y1);
9614 processSync(mapper);
9615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9616 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9617 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9618
9619 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9620 processToolType(mapper, MT_TOOL_PALM);
9621 processSync(mapper);
9622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9623 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9624
9625 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009626 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009627 processPosition(mapper, x2, y2);
9628 processSync(mapper);
9629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9630
9631 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009632 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009633 processSync(mapper);
9634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9635
9636 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009637 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009638 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009639 processPosition(mapper, x3, y3);
9640 processSync(mapper);
9641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9642 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9643 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9644}
9645
arthurhungbf89a482020-04-17 17:37:55 +08009646/**
arthurhungcc7f9802020-04-30 17:55:40 +08009647 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9648 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009649 */
arthurhungcc7f9802020-04-30 17:55:40 +08009650TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009651 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009652 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009653 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9654 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9655
9656 NotifyMotionArgs motionArgs;
9657
9658 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009659 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9660 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009661 processPosition(mapper, x1, y1);
9662 processSync(mapper);
9663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9664 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9665 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9666
9667 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009668 processSlot(mapper, SECOND_SLOT);
9669 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009670 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009671 processSync(mapper);
9672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009673 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009674 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9675
9676 // If the tool type of the first finger changes to MT_TOOL_PALM,
9677 // we expect to receive ACTION_POINTER_UP with cancel flag.
9678 processSlot(mapper, FIRST_SLOT);
9679 processId(mapper, FIRST_TRACKING_ID);
9680 processToolType(mapper, MT_TOOL_PALM);
9681 processSync(mapper);
9682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009683 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009684 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9685
9686 // The following MOVE events of second finger should be processed.
9687 processSlot(mapper, SECOND_SLOT);
9688 processId(mapper, SECOND_TRACKING_ID);
9689 processPosition(mapper, x2 + 1, y2 + 1);
9690 processSync(mapper);
9691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9692 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9693 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9694
9695 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9696 // it. Second finger receive move.
9697 processSlot(mapper, FIRST_SLOT);
9698 processId(mapper, INVALID_TRACKING_ID);
9699 processSync(mapper);
9700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9701 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9702 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9703
9704 // Second finger keeps moving.
9705 processSlot(mapper, SECOND_SLOT);
9706 processId(mapper, SECOND_TRACKING_ID);
9707 processPosition(mapper, x2 + 2, y2 + 2);
9708 processSync(mapper);
9709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9710 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9711 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9712
9713 // Second finger up.
9714 processId(mapper, INVALID_TRACKING_ID);
9715 processSync(mapper);
9716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9717 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9718 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9719}
9720
9721/**
9722 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9723 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9724 */
9725TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9726 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009727 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009728 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9729 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9730
9731 NotifyMotionArgs motionArgs;
9732
9733 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9734 // First finger down.
9735 processId(mapper, FIRST_TRACKING_ID);
9736 processPosition(mapper, x1, y1);
9737 processSync(mapper);
9738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9739 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9740 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9741
9742 // Second finger down.
9743 processSlot(mapper, SECOND_SLOT);
9744 processId(mapper, SECOND_TRACKING_ID);
9745 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009746 processSync(mapper);
9747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009748 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009749 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9750
arthurhungcc7f9802020-04-30 17:55:40 +08009751 // If the tool type of the first finger changes to MT_TOOL_PALM,
9752 // we expect to receive ACTION_POINTER_UP with cancel flag.
9753 processSlot(mapper, FIRST_SLOT);
9754 processId(mapper, FIRST_TRACKING_ID);
9755 processToolType(mapper, MT_TOOL_PALM);
9756 processSync(mapper);
9757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009758 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009759 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9760
9761 // Second finger keeps moving.
9762 processSlot(mapper, SECOND_SLOT);
9763 processId(mapper, SECOND_TRACKING_ID);
9764 processPosition(mapper, x2 + 1, y2 + 1);
9765 processSync(mapper);
9766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9767 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9768
9769 // second finger becomes palm, receive cancel due to only 1 finger is active.
9770 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009771 processToolType(mapper, MT_TOOL_PALM);
9772 processSync(mapper);
9773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9774 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9775
arthurhungcc7f9802020-04-30 17:55:40 +08009776 // third finger down.
9777 processSlot(mapper, THIRD_SLOT);
9778 processId(mapper, THIRD_TRACKING_ID);
9779 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009780 processPosition(mapper, x3, y3);
9781 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9783 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9784 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009785 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9786
9787 // third finger move
9788 processId(mapper, THIRD_TRACKING_ID);
9789 processPosition(mapper, x3 + 1, y3 + 1);
9790 processSync(mapper);
9791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9792 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9793
9794 // first finger up, third finger receive move.
9795 processSlot(mapper, FIRST_SLOT);
9796 processId(mapper, INVALID_TRACKING_ID);
9797 processSync(mapper);
9798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9799 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9800 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9801
9802 // second finger up, third finger receive move.
9803 processSlot(mapper, SECOND_SLOT);
9804 processId(mapper, INVALID_TRACKING_ID);
9805 processSync(mapper);
9806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9808 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9809
9810 // third finger up.
9811 processSlot(mapper, THIRD_SLOT);
9812 processId(mapper, INVALID_TRACKING_ID);
9813 processSync(mapper);
9814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9815 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9816 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9817}
9818
9819/**
9820 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9821 * and the active finger could still be allowed to receive the events
9822 */
9823TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9824 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009825 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009826 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9827 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9828
9829 NotifyMotionArgs motionArgs;
9830
9831 // default tool type is finger
9832 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9833 processId(mapper, FIRST_TRACKING_ID);
9834 processPosition(mapper, x1, y1);
9835 processSync(mapper);
9836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9837 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9838 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9839
9840 // Second finger down.
9841 processSlot(mapper, SECOND_SLOT);
9842 processId(mapper, SECOND_TRACKING_ID);
9843 processPosition(mapper, x2, y2);
9844 processSync(mapper);
9845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009846 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009847 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9848
9849 // If the tool type of the second finger changes to MT_TOOL_PALM,
9850 // we expect to receive ACTION_POINTER_UP with cancel flag.
9851 processId(mapper, SECOND_TRACKING_ID);
9852 processToolType(mapper, MT_TOOL_PALM);
9853 processSync(mapper);
9854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009855 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009856 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9857
9858 // The following MOVE event should be processed.
9859 processSlot(mapper, FIRST_SLOT);
9860 processId(mapper, FIRST_TRACKING_ID);
9861 processPosition(mapper, x1 + 1, y1 + 1);
9862 processSync(mapper);
9863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9864 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9865 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9866
9867 // second finger up.
9868 processSlot(mapper, SECOND_SLOT);
9869 processId(mapper, INVALID_TRACKING_ID);
9870 processSync(mapper);
9871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9873
9874 // first finger keep moving
9875 processSlot(mapper, FIRST_SLOT);
9876 processId(mapper, FIRST_TRACKING_ID);
9877 processPosition(mapper, x1 + 2, y1 + 2);
9878 processSync(mapper);
9879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9880 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9881
9882 // first finger up.
9883 processId(mapper, INVALID_TRACKING_ID);
9884 processSync(mapper);
9885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9886 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9887 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009888}
9889
Arthur Hung9ad18942021-06-19 02:04:46 +00009890/**
9891 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9892 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9893 * cause slot be valid again.
9894 */
9895TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9896 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009897 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009898 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9899 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9900
9901 NotifyMotionArgs motionArgs;
9902
9903 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9904 // First finger down.
9905 processId(mapper, FIRST_TRACKING_ID);
9906 processPosition(mapper, x1, y1);
9907 processPressure(mapper, RAW_PRESSURE_MAX);
9908 processSync(mapper);
9909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9910 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9911 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9912
9913 // First finger move.
9914 processId(mapper, FIRST_TRACKING_ID);
9915 processPosition(mapper, x1 + 1, y1 + 1);
9916 processPressure(mapper, RAW_PRESSURE_MAX);
9917 processSync(mapper);
9918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9919 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9920 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9921
9922 // Second finger down.
9923 processSlot(mapper, SECOND_SLOT);
9924 processId(mapper, SECOND_TRACKING_ID);
9925 processPosition(mapper, x2, y2);
9926 processPressure(mapper, RAW_PRESSURE_MAX);
9927 processSync(mapper);
9928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009929 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009930 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9931
9932 // second finger up with some unexpected data.
9933 processSlot(mapper, SECOND_SLOT);
9934 processId(mapper, INVALID_TRACKING_ID);
9935 processPosition(mapper, x2, y2);
9936 processSync(mapper);
9937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009938 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009939 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9940
9941 // first finger up with some unexpected data.
9942 processSlot(mapper, FIRST_SLOT);
9943 processId(mapper, INVALID_TRACKING_ID);
9944 processPosition(mapper, x2, y2);
9945 processPressure(mapper, RAW_PRESSURE_MAX);
9946 processSync(mapper);
9947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9948 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9949 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9950}
9951
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009952TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9953 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009954 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009955 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9956 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9957
9958 // First finger down.
9959 processId(mapper, FIRST_TRACKING_ID);
9960 processPosition(mapper, 100, 200);
9961 processPressure(mapper, RAW_PRESSURE_MAX);
9962 processSync(mapper);
9963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9964 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9965
9966 // Second finger down.
9967 processSlot(mapper, SECOND_SLOT);
9968 processId(mapper, SECOND_TRACKING_ID);
9969 processPosition(mapper, 300, 400);
9970 processPressure(mapper, RAW_PRESSURE_MAX);
9971 processSync(mapper);
9972 ASSERT_NO_FATAL_FAILURE(
9973 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9974
9975 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009976 // preserved. Resetting should cancel the ongoing gesture.
9977 resetMapper(mapper, ARBITRARY_TIME);
9978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9979 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009980
9981 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9982 // the existing touch state to generate a down event.
9983 processPosition(mapper, 301, 302);
9984 processSync(mapper);
9985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9986 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9988 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9989
9990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9991}
9992
9993TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9994 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009995 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009996 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9997 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9998
9999 // First finger touches down and releases.
10000 processId(mapper, FIRST_TRACKING_ID);
10001 processPosition(mapper, 100, 200);
10002 processPressure(mapper, RAW_PRESSURE_MAX);
10003 processSync(mapper);
10004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10005 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10006 processId(mapper, INVALID_TRACKING_ID);
10007 processSync(mapper);
10008 ASSERT_NO_FATAL_FAILURE(
10009 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10010
10011 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10012 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010013 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10015
10016 // Send an empty sync frame. Since there are no pointers, no events are generated.
10017 processSync(mapper);
10018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10019}
10020
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010021TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010022 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010023 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010024 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
10025 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010027
10028 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10029 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10030 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10031 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10032 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10033
10034 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010035 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010036 processId(mapper, FIRST_TRACKING_ID);
10037 processToolType(mapper, MT_TOOL_PEN);
10038 processPosition(mapper, 100, 200);
10039 processPressure(mapper, RAW_PRESSURE_MAX);
10040 processSync(mapper);
10041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10042 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10043 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
10044 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10045
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010046 // Now that we know the device supports styluses, ensure that the device is re-configured with
10047 // the stylus source.
10048 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10049 {
10050 const auto& devices = mReader->getInputDevices();
10051 auto deviceInfo =
10052 std::find_if(devices.begin(), devices.end(),
10053 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10054 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10055 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10056 }
10057
10058 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10060
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010061 processId(mapper, INVALID_TRACKING_ID);
10062 processSync(mapper);
10063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10064 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10065 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
10066 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010067}
10068
Seunghwan Choi356026c2023-02-01 14:37:25 +090010069TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10070 addConfigurationProperty("touch.deviceType", "touchScreen");
10071 prepareDisplay(ui::ROTATION_0);
10072 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10073 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10074 // indicate stylus presence dynamically.
10075 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10076 std::shared_ptr<FakePointerController> fakePointerController =
10077 std::make_shared<FakePointerController>();
10078 mFakePolicy->setPointerController(fakePointerController);
10079 mFakePolicy->setStylusPointerIconEnabled(true);
10080 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10081
10082 processId(mapper, FIRST_TRACKING_ID);
10083 processPressure(mapper, RAW_PRESSURE_MIN);
10084 processPosition(mapper, 100, 200);
10085 processToolType(mapper, MT_TOOL_PEN);
10086 processSync(mapper);
10087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10088 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
10089 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
10090 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10091 ASSERT_TRUE(fakePointerController->isPointerShown());
10092 ASSERT_NO_FATAL_FAILURE(
10093 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10094}
10095
10096TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10097 addConfigurationProperty("touch.deviceType", "touchScreen");
10098 prepareDisplay(ui::ROTATION_0);
10099 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10100 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10101 // indicate stylus presence dynamically.
10102 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10103 std::shared_ptr<FakePointerController> fakePointerController =
10104 std::make_shared<FakePointerController>();
10105 mFakePolicy->setPointerController(fakePointerController);
10106 mFakePolicy->setStylusPointerIconEnabled(false);
10107 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10108
10109 processId(mapper, FIRST_TRACKING_ID);
10110 processPressure(mapper, RAW_PRESSURE_MIN);
10111 processPosition(mapper, 100, 200);
10112 processToolType(mapper, MT_TOOL_PEN);
10113 processSync(mapper);
10114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10115 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
10116 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
10117 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10118 ASSERT_FALSE(fakePointerController->isPointerShown());
10119}
10120
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010121// --- MultiTouchInputMapperTest_ExternalDevice ---
10122
10123class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10124protected:
Chris Yea52ade12020-08-27 16:49:20 -070010125 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010126};
10127
10128/**
10129 * Expect fallback to internal viewport if device is external and external viewport is not present.
10130 */
10131TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10132 prepareAxes(POSITION);
10133 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010134 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010135 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10136
10137 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10138
10139 NotifyMotionArgs motionArgs;
10140
10141 // Expect the event to be sent to the internal viewport,
10142 // because an external viewport is not present.
10143 processPosition(mapper, 100, 100);
10144 processSync(mapper);
10145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10146 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10147
10148 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010149 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010150 processPosition(mapper, 100, 100);
10151 processSync(mapper);
10152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10153 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10154}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010155
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010156TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10157 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10158 std::shared_ptr<FakePointerController> fakePointerController =
10159 std::make_shared<FakePointerController>();
10160 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10161 fakePointerController->setPosition(0, 0);
10162 fakePointerController->setButtonState(0);
10163
10164 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010165 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010166 prepareAxes(POSITION | ID | SLOT);
10167 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10168 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10169 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010170 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010171 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10172
10173 // captured touchpad should be a touchpad source
10174 NotifyDeviceResetArgs resetArgs;
10175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10176 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10177
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010178 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010179
10180 const InputDeviceInfo::MotionRange* relRangeX =
10181 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10182 ASSERT_NE(relRangeX, nullptr);
10183 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10184 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10185 const InputDeviceInfo::MotionRange* relRangeY =
10186 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10187 ASSERT_NE(relRangeY, nullptr);
10188 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10189 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10190
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010191 // run captured pointer tests - note that this is unscaled, so input listener events should be
10192 // identical to what the hardware sends (accounting for any
10193 // calibration).
10194 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010195 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010196 processId(mapper, 1);
10197 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10198 processKey(mapper, BTN_TOUCH, 1);
10199 processSync(mapper);
10200
10201 // expect coord[0] to contain initial location of touch 0
10202 NotifyMotionArgs args;
10203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10204 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10205 ASSERT_EQ(1U, args.pointerCount);
10206 ASSERT_EQ(0, args.pointerProperties[0].id);
10207 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10208 ASSERT_NO_FATAL_FAILURE(
10209 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10210
10211 // FINGER 1 DOWN
10212 processSlot(mapper, 1);
10213 processId(mapper, 2);
10214 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10215 processSync(mapper);
10216
10217 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010219 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010220 ASSERT_EQ(2U, args.pointerCount);
10221 ASSERT_EQ(0, args.pointerProperties[0].id);
10222 ASSERT_EQ(1, args.pointerProperties[1].id);
10223 ASSERT_NO_FATAL_FAILURE(
10224 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10225 ASSERT_NO_FATAL_FAILURE(
10226 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10227
10228 // FINGER 1 MOVE
10229 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10230 processSync(mapper);
10231
10232 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10233 // from move
10234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10236 ASSERT_NO_FATAL_FAILURE(
10237 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10238 ASSERT_NO_FATAL_FAILURE(
10239 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10240
10241 // FINGER 0 MOVE
10242 processSlot(mapper, 0);
10243 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10244 processSync(mapper);
10245
10246 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10249 ASSERT_NO_FATAL_FAILURE(
10250 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10251 ASSERT_NO_FATAL_FAILURE(
10252 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10253
10254 // BUTTON DOWN
10255 processKey(mapper, BTN_LEFT, 1);
10256 processSync(mapper);
10257
10258 // touchinputmapper design sends a move before button press
10259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10262 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10263
10264 // BUTTON UP
10265 processKey(mapper, BTN_LEFT, 0);
10266 processSync(mapper);
10267
10268 // touchinputmapper design sends a move after button release
10269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10270 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10272 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10273
10274 // FINGER 0 UP
10275 processId(mapper, -1);
10276 processSync(mapper);
10277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10278 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10279
10280 // FINGER 1 MOVE
10281 processSlot(mapper, 1);
10282 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10283 processSync(mapper);
10284
10285 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10288 ASSERT_EQ(1U, args.pointerCount);
10289 ASSERT_EQ(1, args.pointerProperties[0].id);
10290 ASSERT_NO_FATAL_FAILURE(
10291 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10292
10293 // FINGER 1 UP
10294 processId(mapper, -1);
10295 processKey(mapper, BTN_TOUCH, 0);
10296 processSync(mapper);
10297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10298 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10299
Josep del Río2d8c79a2023-01-23 19:33:50 +000010300 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010301 mFakePolicy->setPointerCapture(false);
10302 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010304 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010305}
10306
10307TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10308 std::shared_ptr<FakePointerController> fakePointerController =
10309 std::make_shared<FakePointerController>();
10310 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10311 fakePointerController->setPosition(0, 0);
10312 fakePointerController->setButtonState(0);
10313
10314 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010315 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010316 prepareAxes(POSITION | ID | SLOT);
10317 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10318 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010319 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010320 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10321 // run uncaptured pointer tests - pushes out generic events
10322 // FINGER 0 DOWN
10323 processId(mapper, 3);
10324 processPosition(mapper, 100, 100);
10325 processKey(mapper, BTN_TOUCH, 1);
10326 processSync(mapper);
10327
10328 // start at (100,100), cursor should be at (0,0) * scale
10329 NotifyMotionArgs args;
10330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10331 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10332 ASSERT_NO_FATAL_FAILURE(
10333 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10334
10335 // FINGER 0 MOVE
10336 processPosition(mapper, 200, 200);
10337 processSync(mapper);
10338
10339 // compute scaling to help with touch position checking
10340 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10341 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10342 float scale =
10343 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10344
10345 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10347 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10349 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010350
10351 // BUTTON DOWN
10352 processKey(mapper, BTN_LEFT, 1);
10353 processSync(mapper);
10354
10355 // touchinputmapper design sends a move before button press
10356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10357 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10359 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10360
10361 // BUTTON UP
10362 processKey(mapper, BTN_LEFT, 0);
10363 processSync(mapper);
10364
10365 // touchinputmapper design sends a move after button release
10366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10367 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10369 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010370}
10371
10372TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10373 std::shared_ptr<FakePointerController> fakePointerController =
10374 std::make_shared<FakePointerController>();
10375
Michael Wrighta9cf4192022-12-01 23:46:39 +000010376 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010377 prepareAxes(POSITION | ID | SLOT);
10378 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010379 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010380 mFakePolicy->setPointerCapture(false);
10381 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10382
Josep del Río2d8c79a2023-01-23 19:33:50 +000010383 // uncaptured touchpad should be a pointer device
10384 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010385
Josep del Río2d8c79a2023-01-23 19:33:50 +000010386 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010387 mFakePolicy->setPointerCapture(true);
10388 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10389 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10390}
10391
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010392// --- BluetoothMultiTouchInputMapperTest ---
10393
10394class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10395protected:
10396 void SetUp() override {
10397 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10398 }
10399};
10400
10401TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10402 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010403 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010404 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10405 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10406
10407 nsecs_t kernelEventTime = ARBITRARY_TIME;
10408 nsecs_t expectedEventTime = ARBITRARY_TIME;
10409 // Touch down.
10410 processId(mapper, FIRST_TRACKING_ID);
10411 processPosition(mapper, 100, 200);
10412 processPressure(mapper, RAW_PRESSURE_MAX);
10413 processSync(mapper, ARBITRARY_TIME);
10414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10415 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10416
10417 // Process several events that come in quick succession, according to their timestamps.
10418 for (int i = 0; i < 3; i++) {
10419 constexpr static nsecs_t delta = ms2ns(1);
10420 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10421 kernelEventTime += delta;
10422 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10423
10424 processPosition(mapper, 101 + i, 201 + i);
10425 processSync(mapper, kernelEventTime);
10426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10427 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10428 WithEventTime(expectedEventTime))));
10429 }
10430
10431 // Release the touch.
10432 processId(mapper, INVALID_TRACKING_ID);
10433 processPressure(mapper, RAW_PRESSURE_MIN);
10434 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10436 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10437 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10438}
10439
10440// --- MultiTouchPointerModeTest ---
10441
HQ Liue6983c72022-04-19 22:14:56 +000010442class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10443protected:
10444 float mPointerMovementScale;
10445 float mPointerXZoomScale;
10446 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10447 addConfigurationProperty("touch.deviceType", "pointer");
10448 std::shared_ptr<FakePointerController> fakePointerController =
10449 std::make_shared<FakePointerController>();
10450 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10451 fakePointerController->setPosition(0, 0);
10452 fakePointerController->setButtonState(0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010453 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010454
10455 prepareAxes(POSITION);
10456 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10457 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10458 // needs to be disabled, and the pointer gesture needs to be enabled.
10459 mFakePolicy->setPointerCapture(false);
10460 mFakePolicy->setPointerGestureEnabled(true);
10461 mFakePolicy->setPointerController(fakePointerController);
10462
10463 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10464 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10465 mPointerMovementScale =
10466 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10467 mPointerXZoomScale =
10468 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10469 }
10470
10471 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10472 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10473 /*flat*/ 0,
10474 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10475 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10476 /*flat*/ 0,
10477 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10478 }
10479};
10480
10481/**
10482 * Two fingers down on a pointer mode touch pad. The width
10483 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10484 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10485 * be greater than the both value to be freeform gesture, so that after two
10486 * fingers start to move downwards, the gesture should be swipe.
10487 */
10488TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10489 // The min freeform gesture width is 25units/mm x 30mm = 750
10490 // which is greater than fraction of the diagnal length of the touchpad (349).
10491 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010492 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010493 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10494 NotifyMotionArgs motionArgs;
10495
10496 // Two fingers down at once.
10497 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10498 // Pointer's initial position is used the [0,0] coordinate.
10499 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10500
10501 processId(mapper, FIRST_TRACKING_ID);
10502 processPosition(mapper, x1, y1);
10503 processMTSync(mapper);
10504 processId(mapper, SECOND_TRACKING_ID);
10505 processPosition(mapper, x2, y2);
10506 processMTSync(mapper);
10507 processSync(mapper);
10508
10509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10510 ASSERT_EQ(1U, motionArgs.pointerCount);
10511 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10512 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010513 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010514 ASSERT_NO_FATAL_FAILURE(
10515 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10516
10517 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10518 // that there should be 1 pointer.
10519 int32_t movingDistance = 200;
10520 y1 += movingDistance;
10521 y2 += movingDistance;
10522
10523 processId(mapper, FIRST_TRACKING_ID);
10524 processPosition(mapper, x1, y1);
10525 processMTSync(mapper);
10526 processId(mapper, SECOND_TRACKING_ID);
10527 processPosition(mapper, x2, y2);
10528 processMTSync(mapper);
10529 processSync(mapper);
10530
10531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10532 ASSERT_EQ(1U, motionArgs.pointerCount);
10533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10534 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010535 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010536 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10537 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10538 0, 0, 0, 0));
10539}
10540
10541/**
10542 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10543 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10544 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10545 * value to be freeform gesture, so that after two fingers start to move downwards,
10546 * the gesture should be swipe.
10547 */
10548TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10549 // The min freeform gesture width is 5units/mm x 30mm = 150
10550 // which is greater than fraction of the diagnal length of the touchpad (349).
10551 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010552 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
HQ Liue6983c72022-04-19 22:14:56 +000010553 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10554 NotifyMotionArgs motionArgs;
10555
10556 // Two fingers down at once.
10557 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10558 // Pointer's initial position is used the [0,0] coordinate.
10559 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10560
10561 processId(mapper, FIRST_TRACKING_ID);
10562 processPosition(mapper, x1, y1);
10563 processMTSync(mapper);
10564 processId(mapper, SECOND_TRACKING_ID);
10565 processPosition(mapper, x2, y2);
10566 processMTSync(mapper);
10567 processSync(mapper);
10568
10569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10570 ASSERT_EQ(1U, motionArgs.pointerCount);
10571 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010573 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010574 ASSERT_NO_FATAL_FAILURE(
10575 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10576
10577 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10578 // and there should be 1 pointer.
10579 int32_t movingDistance = 200;
10580 y1 += movingDistance;
10581 y2 += movingDistance;
10582
10583 processId(mapper, FIRST_TRACKING_ID);
10584 processPosition(mapper, x1, y1);
10585 processMTSync(mapper);
10586 processId(mapper, SECOND_TRACKING_ID);
10587 processPosition(mapper, x2, y2);
10588 processMTSync(mapper);
10589 processSync(mapper);
10590
10591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10592 ASSERT_EQ(1U, motionArgs.pointerCount);
10593 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10594 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010595 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010596 // New coordinate is the scaled relative coordinate from the initial coordinate.
10597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10598 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10599 0, 0, 0, 0));
10600}
10601
10602/**
10603 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10604 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10605 * freeform gestures after two fingers start to move downwards.
10606 */
10607TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010608 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010609 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10610
10611 NotifyMotionArgs motionArgs;
10612
10613 // Two fingers down at once. Wider than the max swipe width.
10614 // The gesture is expected to be PRESS, then transformed to FREEFORM
10615 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10616
10617 processId(mapper, FIRST_TRACKING_ID);
10618 processPosition(mapper, x1, y1);
10619 processMTSync(mapper);
10620 processId(mapper, SECOND_TRACKING_ID);
10621 processPosition(mapper, x2, y2);
10622 processMTSync(mapper);
10623 processSync(mapper);
10624
10625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10626 ASSERT_EQ(1U, motionArgs.pointerCount);
10627 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010629 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010630 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10631 ASSERT_NO_FATAL_FAILURE(
10632 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10633
10634 int32_t movingDistance = 200;
10635
10636 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10637 // then two down events for two pointers.
10638 y1 += movingDistance;
10639 y2 += movingDistance;
10640
10641 processId(mapper, FIRST_TRACKING_ID);
10642 processPosition(mapper, x1, y1);
10643 processMTSync(mapper);
10644 processId(mapper, SECOND_TRACKING_ID);
10645 processPosition(mapper, x2, y2);
10646 processMTSync(mapper);
10647 processSync(mapper);
10648
10649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10650 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10651 ASSERT_EQ(1U, motionArgs.pointerCount);
10652 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10654 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10655 ASSERT_EQ(1U, motionArgs.pointerCount);
10656 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010659 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010660 ASSERT_EQ(2U, motionArgs.pointerCount);
10661 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10662 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010663 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010664 // Two pointers' scaled relative coordinates from their initial centroid.
10665 // Initial y coordinates are 0 as y1 and y2 have the same value.
10666 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10667 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10668 // When pointers move, the new coordinates equal to the initial coordinates plus
10669 // scaled moving distance.
10670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10671 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10672 0, 0, 0, 0));
10673 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10674 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10675 0, 0, 0, 0));
10676
10677 // Move two fingers down again, expect one MOVE motion event.
10678 y1 += movingDistance;
10679 y2 += movingDistance;
10680
10681 processId(mapper, FIRST_TRACKING_ID);
10682 processPosition(mapper, x1, y1);
10683 processMTSync(mapper);
10684 processId(mapper, SECOND_TRACKING_ID);
10685 processPosition(mapper, x2, y2);
10686 processMTSync(mapper);
10687 processSync(mapper);
10688
10689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10690 ASSERT_EQ(2U, motionArgs.pointerCount);
10691 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010693 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010694 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10695 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10696 0, 0, 0, 0, 0));
10697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10698 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10699 0, 0, 0, 0, 0));
10700}
10701
Harry Cutts39b7ca22022-10-05 15:55:48 +000010702TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010703 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Harry Cutts39b7ca22022-10-05 15:55:48 +000010704 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10705 NotifyMotionArgs motionArgs;
10706
10707 // Place two fingers down.
10708 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10709
10710 processId(mapper, FIRST_TRACKING_ID);
10711 processPosition(mapper, x1, y1);
10712 processMTSync(mapper);
10713 processId(mapper, SECOND_TRACKING_ID);
10714 processPosition(mapper, x2, y2);
10715 processMTSync(mapper);
10716 processSync(mapper);
10717
10718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10719 ASSERT_EQ(1U, motionArgs.pointerCount);
10720 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10721 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10722 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10723 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10724
10725 // Move the two fingers down and to the left.
10726 int32_t movingDistance = 200;
10727 x1 -= movingDistance;
10728 y1 += movingDistance;
10729 x2 -= movingDistance;
10730 y2 += movingDistance;
10731
10732 processId(mapper, FIRST_TRACKING_ID);
10733 processPosition(mapper, x1, y1);
10734 processMTSync(mapper);
10735 processId(mapper, SECOND_TRACKING_ID);
10736 processPosition(mapper, x2, y2);
10737 processMTSync(mapper);
10738 processSync(mapper);
10739
10740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10741 ASSERT_EQ(1U, motionArgs.pointerCount);
10742 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10743 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10744 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10745 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10746}
10747
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010748TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010749 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010750 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10751 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10753
10754 // Start a stylus gesture.
10755 processKey(mapper, BTN_TOOL_PEN, 1);
10756 processId(mapper, FIRST_TRACKING_ID);
10757 processPosition(mapper, 100, 200);
10758 processSync(mapper);
10759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10760 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10761 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10762 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10763 // TODO(b/257078296): Pointer mode generates extra event.
10764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10765 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10766 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10767 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10769
10770 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10771 // gesture should be disabled.
10772 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10773 viewport->isActive = false;
10774 mFakePolicy->updateViewport(*viewport);
10775 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10777 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10778 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10779 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10780 // TODO(b/257078296): Pointer mode generates extra event.
10781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10782 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10783 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10784 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10786}
10787
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010788// --- JoystickInputMapperTest ---
10789
10790class JoystickInputMapperTest : public InputMapperTest {
10791protected:
10792 static const int32_t RAW_X_MIN;
10793 static const int32_t RAW_X_MAX;
10794 static const int32_t RAW_Y_MIN;
10795 static const int32_t RAW_Y_MAX;
10796
10797 void SetUp() override {
10798 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10799 }
10800 void prepareAxes() {
10801 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10802 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10803 }
10804
10805 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10807 }
10808
10809 void processSync(JoystickInputMapper& mapper) {
10810 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10811 }
10812
Michael Wrighta9cf4192022-12-01 23:46:39 +000010813 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010814 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10815 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10816 NO_PORT, ViewportType::VIRTUAL);
10817 }
10818};
10819
10820const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10821const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10822const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10823const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10824
10825TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10826 prepareAxes();
10827 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10828
10829 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10830
Michael Wrighta9cf4192022-12-01 23:46:39 +000010831 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010832
10833 // Send an axis event
10834 processAxis(mapper, ABS_X, 100);
10835 processSync(mapper);
10836
10837 NotifyMotionArgs args;
10838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10839 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10840
10841 // Send another axis event
10842 processAxis(mapper, ABS_Y, 100);
10843 processSync(mapper);
10844
10845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10846 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10847}
10848
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010849// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010850
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010851class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010852protected:
10853 static const char* DEVICE_NAME;
10854 static const char* DEVICE_LOCATION;
10855 static const int32_t DEVICE_ID;
10856 static const int32_t DEVICE_GENERATION;
10857 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010858 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010859 static const int32_t EVENTHUB_ID;
10860
10861 std::shared_ptr<FakeEventHub> mFakeEventHub;
10862 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010863 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010864 std::unique_ptr<InstrumentedInputReader> mReader;
10865 std::shared_ptr<InputDevice> mDevice;
10866
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010867 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010868 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010869 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010870 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010871 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010872 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010873 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10874 }
10875
10876 void SetUp() override { SetUp(DEVICE_CLASSES); }
10877
10878 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010879 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010880 mFakePolicy.clear();
10881 }
10882
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010883 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010884 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10885 mReader->requestRefreshConfiguration(changes);
10886 mReader->loopOnce();
10887 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010888 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010889 }
10890
10891 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10892 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010893 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010894 InputDeviceIdentifier identifier;
10895 identifier.name = name;
10896 identifier.location = location;
10897 std::shared_ptr<InputDevice> device =
10898 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10899 identifier);
10900 mReader->pushNextDevice(device);
10901 mFakeEventHub->addDevice(eventHubId, name, classes);
10902 mReader->loopOnce();
10903 return device;
10904 }
10905
10906 template <class T, typename... Args>
10907 T& addControllerAndConfigure(Args... args) {
10908 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10909
10910 return controller;
10911 }
10912};
10913
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010914const char* PeripheralControllerTest::DEVICE_NAME = "device";
10915const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10916const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10917const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10918const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010919const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10920 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010921const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010922
10923// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010924class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010925protected:
10926 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010927 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010928 }
10929};
10930
10931TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010932 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010933
Harry Cuttsa5b71292022-11-28 12:56:17 +000010934 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10935 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10936 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010937}
10938
10939TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010940 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010941
Harry Cuttsa5b71292022-11-28 12:56:17 +000010942 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10943 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10944 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010945}
10946
10947// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010948class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010949protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010950 void SetUp() override {
10951 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10952 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010953};
10954
Chris Ye85758332021-05-16 23:05:17 -070010955TEST_F(LightControllerTest, MonoLight) {
10956 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010957 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010958 .maxBrightness = 255,
10959 .flags = InputLightClass::BRIGHTNESS,
10960 .path = ""};
10961 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010962
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010963 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010964 InputDeviceInfo info;
10965 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010966 std::vector<InputDeviceLightInfo> lights = info.getLights();
10967 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010968 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10969 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10970
10971 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10972 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10973}
10974
10975TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10976 RawLightInfo infoMono = {.id = 1,
10977 .name = "mono_keyboard_backlight",
10978 .maxBrightness = 255,
10979 .flags = InputLightClass::BRIGHTNESS |
10980 InputLightClass::KEYBOARD_BACKLIGHT,
10981 .path = ""};
10982 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10983
10984 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10985 InputDeviceInfo info;
10986 controller.populateDeviceInfo(&info);
10987 std::vector<InputDeviceLightInfo> lights = info.getLights();
10988 ASSERT_EQ(1U, lights.size());
10989 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10990 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010991
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010992 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10993 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010994}
10995
10996TEST_F(LightControllerTest, RGBLight) {
10997 RawLightInfo infoRed = {.id = 1,
10998 .name = "red",
10999 .maxBrightness = 255,
11000 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11001 .path = ""};
11002 RawLightInfo infoGreen = {.id = 2,
11003 .name = "green",
11004 .maxBrightness = 255,
11005 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11006 .path = ""};
11007 RawLightInfo infoBlue = {.id = 3,
11008 .name = "blue",
11009 .maxBrightness = 255,
11010 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11011 .path = ""};
11012 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11013 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11014 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11015
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011016 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011017 InputDeviceInfo info;
11018 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011019 std::vector<InputDeviceLightInfo> lights = info.getLights();
11020 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011021 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11022 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11023 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11024
11025 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11026 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11027}
11028
11029TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11030 RawLightInfo infoRed = {.id = 1,
11031 .name = "red_keyboard_backlight",
11032 .maxBrightness = 255,
11033 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11034 InputLightClass::KEYBOARD_BACKLIGHT,
11035 .path = ""};
11036 RawLightInfo infoGreen = {.id = 2,
11037 .name = "green_keyboard_backlight",
11038 .maxBrightness = 255,
11039 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11040 InputLightClass::KEYBOARD_BACKLIGHT,
11041 .path = ""};
11042 RawLightInfo infoBlue = {.id = 3,
11043 .name = "blue_keyboard_backlight",
11044 .maxBrightness = 255,
11045 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11046 InputLightClass::KEYBOARD_BACKLIGHT,
11047 .path = ""};
11048 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11049 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11050 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11051
11052 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11053 InputDeviceInfo info;
11054 controller.populateDeviceInfo(&info);
11055 std::vector<InputDeviceLightInfo> lights = info.getLights();
11056 ASSERT_EQ(1U, lights.size());
11057 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11058 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11059 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11060
11061 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11062 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11063}
11064
11065TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11066 RawLightInfo infoRed = {.id = 1,
11067 .name = "red",
11068 .maxBrightness = 255,
11069 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11070 .path = ""};
11071 RawLightInfo infoGreen = {.id = 2,
11072 .name = "green",
11073 .maxBrightness = 255,
11074 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11075 .path = ""};
11076 RawLightInfo infoBlue = {.id = 3,
11077 .name = "blue",
11078 .maxBrightness = 255,
11079 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11080 .path = ""};
11081 RawLightInfo infoGlobal = {.id = 3,
11082 .name = "global_keyboard_backlight",
11083 .maxBrightness = 255,
11084 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11085 InputLightClass::KEYBOARD_BACKLIGHT,
11086 .path = ""};
11087 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11088 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11089 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11090 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11091
11092 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11093 InputDeviceInfo info;
11094 controller.populateDeviceInfo(&info);
11095 std::vector<InputDeviceLightInfo> lights = info.getLights();
11096 ASSERT_EQ(1U, lights.size());
11097 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11098 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11099 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011100
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011101 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11102 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011103}
11104
11105TEST_F(LightControllerTest, MultiColorRGBLight) {
11106 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011107 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011108 .maxBrightness = 255,
11109 .flags = InputLightClass::BRIGHTNESS |
11110 InputLightClass::MULTI_INTENSITY |
11111 InputLightClass::MULTI_INDEX,
11112 .path = ""};
11113
11114 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11115
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011116 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011117 InputDeviceInfo info;
11118 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011119 std::vector<InputDeviceLightInfo> lights = info.getLights();
11120 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011121 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11122 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11123 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11124
11125 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11126 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11127}
11128
11129TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11130 RawLightInfo infoColor = {.id = 1,
11131 .name = "multi_color_keyboard_backlight",
11132 .maxBrightness = 255,
11133 .flags = InputLightClass::BRIGHTNESS |
11134 InputLightClass::MULTI_INTENSITY |
11135 InputLightClass::MULTI_INDEX |
11136 InputLightClass::KEYBOARD_BACKLIGHT,
11137 .path = ""};
11138
11139 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11140
11141 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11142 InputDeviceInfo info;
11143 controller.populateDeviceInfo(&info);
11144 std::vector<InputDeviceLightInfo> lights = info.getLights();
11145 ASSERT_EQ(1U, lights.size());
11146 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11147 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11148 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011149
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011150 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11151 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011152}
11153
11154TEST_F(LightControllerTest, PlayerIdLight) {
11155 RawLightInfo info1 = {.id = 1,
11156 .name = "player1",
11157 .maxBrightness = 255,
11158 .flags = InputLightClass::BRIGHTNESS,
11159 .path = ""};
11160 RawLightInfo info2 = {.id = 2,
11161 .name = "player2",
11162 .maxBrightness = 255,
11163 .flags = InputLightClass::BRIGHTNESS,
11164 .path = ""};
11165 RawLightInfo info3 = {.id = 3,
11166 .name = "player3",
11167 .maxBrightness = 255,
11168 .flags = InputLightClass::BRIGHTNESS,
11169 .path = ""};
11170 RawLightInfo info4 = {.id = 4,
11171 .name = "player4",
11172 .maxBrightness = 255,
11173 .flags = InputLightClass::BRIGHTNESS,
11174 .path = ""};
11175 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11176 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11177 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11178 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11179
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011180 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011181 InputDeviceInfo info;
11182 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011183 std::vector<InputDeviceLightInfo> lights = info.getLights();
11184 ASSERT_EQ(1U, lights.size());
11185 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011186 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11187 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011188
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011189 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11190 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11191 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011192}
11193
Michael Wrightd02c5b62014-02-10 15:10:22 -080011194} // namespace android