blob: 0b505045bd566f3b3b621883c77a44331431ab5b [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
Harry Cuttsf13161a2023-03-08 14:15:49 +000019#include <optional>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070020
Prabir Pradhan2770d242019-09-02 18:07:11 -070021#include <CursorInputMapper.h>
22#include <InputDevice.h>
23#include <InputMapper.h>
24#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080025#include <InputReaderBase.h>
26#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000027#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <KeyboardInputMapper.h>
29#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070030#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070031#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070032#include <SingleTouchInputMapper.h>
33#include <SwitchInputMapper.h>
34#include <TestInputListener.h>
Prabir Pradhan739dca42022-09-09 20:12:01 +000035#include <TestInputListenerMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070036#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080037#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000038#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070039#include <android-base/thread_annotations.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000040#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050042#include <gui/constants.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000043#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080044
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070045#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000046#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000047#include "FakeInputReaderPolicy.h"
Harry Cuttsb57f1702022-11-28 15:34:22 +000048#include "FakePointerController.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000049#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000050#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000051#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000052#include "input/DisplayViewport.h"
53#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010054
Michael Wrightd02c5b62014-02-10 15:10:22 -080055namespace android {
56
Dominik Laskowski2f01d772022-03-23 16:01:29 -070057using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000058using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070059using std::chrono_literals::operator""ms;
60
Michael Wrightd02c5b62014-02-10 15:10:22 -080061// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080062static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000063static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080064static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000065static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080066static constexpr int32_t DISPLAY_WIDTH = 480;
67static constexpr int32_t DISPLAY_HEIGHT = 800;
68static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
69static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
70static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070071static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070072static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080073
arthurhungcc7f9802020-04-30 17:55:40 +080074static constexpr int32_t FIRST_SLOT = 0;
75static constexpr int32_t SECOND_SLOT = 1;
76static constexpr int32_t THIRD_SLOT = 2;
77static constexpr int32_t INVALID_TRACKING_ID = -1;
78static constexpr int32_t FIRST_TRACKING_ID = 0;
79static constexpr int32_t SECOND_TRACKING_ID = 1;
80static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080081static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
82static constexpr int32_t LIGHT_COLOR = 0x7F448866;
83static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080084
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080085static constexpr int32_t ACTION_POINTER_0_DOWN =
86 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
87static constexpr int32_t ACTION_POINTER_0_UP =
88 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
89static constexpr int32_t ACTION_POINTER_1_DOWN =
90 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
91static constexpr int32_t ACTION_POINTER_1_UP =
92 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
93
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000094// Minimum timestamp separation between subsequent input events from a Bluetooth device.
95static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
96// Maximum smoothing time delta so that we don't generate events too far into the future.
97constexpr static nsecs_t MAX_BLUETOOTH_SMOOTHING_DELTA = ms2ns(32);
98
Michael Wrightd02c5b62014-02-10 15:10:22 -080099template<typename T>
100static inline T min(T a, T b) {
101 return a < b ? a : b;
102}
103
104static inline float avg(float x, float y) {
105 return (x + y) / 2;
106}
107
Chris Ye3fdbfef2021-01-06 18:45:18 -0800108// Mapping for light color name and the light color
109const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
110 {"green", LightColor::GREEN},
111 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800112
Michael Wrighta9cf4192022-12-01 23:46:39 +0000113static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700114 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000115 case ui::ROTATION_90:
116 return ui::ROTATION_270;
117 case ui::ROTATION_270:
118 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700119 default:
120 return orientation;
121 }
122}
123
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800124static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
125 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000126 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800127
128 const InputDeviceInfo::MotionRange* motionRange =
129 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
130 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
131}
132
133static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
134 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000135 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800136
137 const InputDeviceInfo::MotionRange* motionRange =
138 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
139 ASSERT_EQ(nullptr, motionRange);
140}
141
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700142[[maybe_unused]] static void dumpReader(InputReader& reader) {
143 std::string dump;
144 reader.dump(dump);
145 std::istringstream iss(dump);
146 for (std::string line; std::getline(iss, line);) {
147 ALOGE("%s", line.c_str());
148 std::this_thread::sleep_for(std::chrono::milliseconds(1));
149 }
150}
151
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152// --- FakeInputMapper ---
153
154class FakeInputMapper : public InputMapper {
155 uint32_t mSources;
156 int32_t mKeyboardType;
157 int32_t mMetaState;
158 KeyedVector<int32_t, int32_t> mKeyCodeStates;
159 KeyedVector<int32_t, int32_t> mScanCodeStates;
160 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100161 // fake mapping which would normally come from keyCharacterMap
162 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800163 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700165 std::mutex mLock;
166 std::condition_variable mStateChangedCondition;
167 bool mConfigureWasCalled GUARDED_BY(mLock);
168 bool mResetWasCalled GUARDED_BY(mLock);
169 bool mProcessWasCalled GUARDED_BY(mLock);
170 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171
Arthur Hungc23540e2018-11-29 20:42:11 +0800172 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800173public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000174 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
175 uint32_t sources)
176 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800177 mSources(sources),
178 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800180 mConfigureWasCalled(false),
181 mResetWasCalled(false),
182 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183
Chris Yea52ade12020-08-27 16:49:20 -0700184 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185
186 void setKeyboardType(int32_t keyboardType) {
187 mKeyboardType = keyboardType;
188 }
189
190 void setMetaState(int32_t metaState) {
191 mMetaState = metaState;
192 }
193
194 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700195 std::unique_lock<std::mutex> lock(mLock);
196 base::ScopedLockAssertion assumeLocked(mLock);
197 const bool configureCalled =
198 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
199 return mConfigureWasCalled;
200 });
201 if (!configureCalled) {
202 FAIL() << "Expected configure() to have been called.";
203 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204 mConfigureWasCalled = false;
205 }
206
207 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700208 std::unique_lock<std::mutex> lock(mLock);
209 base::ScopedLockAssertion assumeLocked(mLock);
210 const bool resetCalled =
211 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
212 return mResetWasCalled;
213 });
214 if (!resetCalled) {
215 FAIL() << "Expected reset() to have been called.";
216 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217 mResetWasCalled = false;
218 }
219
Yi Kong9b14ac62018-07-17 13:48:38 -0700220 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700221 std::unique_lock<std::mutex> lock(mLock);
222 base::ScopedLockAssertion assumeLocked(mLock);
223 const bool processCalled =
224 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
225 return mProcessWasCalled;
226 });
227 if (!processCalled) {
228 FAIL() << "Expected process() to have been called.";
229 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230 if (outLastEvent) {
231 *outLastEvent = mLastEvent;
232 }
233 mProcessWasCalled = false;
234 }
235
236 void setKeyCodeState(int32_t keyCode, int32_t state) {
237 mKeyCodeStates.replaceValueFor(keyCode, state);
238 }
239
240 void setScanCodeState(int32_t scanCode, int32_t state) {
241 mScanCodeStates.replaceValueFor(scanCode, state);
242 }
243
244 void setSwitchState(int32_t switchCode, int32_t state) {
245 mSwitchStates.replaceValueFor(switchCode, state);
246 }
247
248 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800249 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800250 }
251
Philip Junker4af3b3d2021-12-14 10:36:55 +0100252 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
253 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
254 }
255
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100257 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258
Harry Cuttsd02ea102023-03-17 18:21:30 +0000259 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260 InputMapper::populateDeviceInfo(deviceInfo);
261
262 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000263 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800264 }
265 }
266
Arpit Singhed6c3de2023-04-05 19:24:37 +0000267 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000268 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700269 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800271
272 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800273 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000274 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000275 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800276 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700277
278 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700279 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280 }
281
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700282 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700283 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700285 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700286 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287 }
288
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700289 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700290 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800291 mLastEvent = *rawEvent;
292 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700293 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700294 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295 }
296
Chris Yea52ade12020-08-27 16:49:20 -0700297 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
299 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
300 }
301
Philip Junker4af3b3d2021-12-14 10:36:55 +0100302 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
303 auto it = mKeyCodeMapping.find(locationKeyCode);
304 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
305 }
306
Chris Yea52ade12020-08-27 16:49:20 -0700307 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800308 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
309 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
310 }
311
Chris Yea52ade12020-08-27 16:49:20 -0700312 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800313 ssize_t index = mSwitchStates.indexOfKey(switchCode);
314 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
315 }
316
Chris Yea52ade12020-08-27 16:49:20 -0700317 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700318 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700319 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700320 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
322 if (keyCodes[i] == mSupportedKeyCodes[j]) {
323 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800324 }
325 }
326 }
Chris Yea52ade12020-08-27 16:49:20 -0700327 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800328 return result;
329 }
330
331 virtual int32_t getMetaState() {
332 return mMetaState;
333 }
334
335 virtual void fadePointer() {
336 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800337
338 virtual std::optional<int32_t> getAssociatedDisplay() {
339 if (mViewport) {
340 return std::make_optional(mViewport->displayId);
341 }
342 return std::nullopt;
343 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344};
345
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700346// --- InputReaderPolicyTest ---
347class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700348protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700349 sp<FakeInputReaderPolicy> mFakePolicy;
350
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700351 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700352 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700353};
354
355/**
356 * Check that empty set of viewports is an acceptable configuration.
357 * Also try to get internal viewport two different ways - by type and by uniqueId.
358 *
359 * There will be confusion if two viewports with empty uniqueId and identical type are present.
360 * Such configuration is not currently allowed.
361 */
362TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700363 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700364
365 // We didn't add any viewports yet, so there shouldn't be any.
366 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100367 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700368 ASSERT_FALSE(internalViewport);
369
370 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000371 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000372 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700373
374 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700375 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700376 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100377 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700378
379 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100380 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700381 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700382 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700383
384 mFakePolicy->clearViewports();
385 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700386 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700387 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100388 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700389 ASSERT_FALSE(internalViewport);
390}
391
392TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
393 const std::string internalUniqueId = "local:0";
394 const std::string externalUniqueId = "local:1";
395 const std::string virtualUniqueId1 = "virtual:2";
396 const std::string virtualUniqueId2 = "virtual:3";
397 constexpr int32_t virtualDisplayId1 = 2;
398 constexpr int32_t virtualDisplayId2 = 3;
399
400 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000401 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000402 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000403 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700404 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000405 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000406 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000407 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700408 // Add an virtual viewport
409 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000410 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000411 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700412 // Add another virtual viewport
413 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000414 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000415 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700416
417 // Check matching by type for internal
418 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100419 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700420 ASSERT_TRUE(internalViewport);
421 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
422
423 // Check matching by type for external
424 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100425 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700426 ASSERT_TRUE(externalViewport);
427 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
428
429 // Check matching by uniqueId for virtual viewport #1
430 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700431 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700432 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100433 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700434 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
435 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
436
437 // Check matching by uniqueId for virtual viewport #2
438 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700439 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700440 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100441 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700442 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
443 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
444}
445
446
447/**
448 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
449 * that lookup works by checking display id.
450 * Check that 2 viewports of each kind is possible, for all existing viewport types.
451 */
452TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
453 const std::string uniqueId1 = "uniqueId1";
454 const std::string uniqueId2 = "uniqueId2";
455 constexpr int32_t displayId1 = 2;
456 constexpr int32_t displayId2 = 3;
457
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100458 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
459 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700460 for (const ViewportType& type : types) {
461 mFakePolicy->clearViewports();
462 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000463 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000464 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700465 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000466 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000467 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700468
469 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700470 std::optional<DisplayViewport> viewport1 =
471 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700472 ASSERT_TRUE(viewport1);
473 ASSERT_EQ(displayId1, viewport1->displayId);
474 ASSERT_EQ(type, viewport1->type);
475
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700476 std::optional<DisplayViewport> viewport2 =
477 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700478 ASSERT_TRUE(viewport2);
479 ASSERT_EQ(displayId2, viewport2->displayId);
480 ASSERT_EQ(type, viewport2->type);
481
482 // When there are multiple viewports of the same kind, and uniqueId is not specified
483 // in the call to getDisplayViewport, then that situation is not supported.
484 // The viewports can be stored in any order, so we cannot rely on the order, since that
485 // is just implementation detail.
486 // However, we can check that it still returns *a* viewport, we just cannot assert
487 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700488 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700489 ASSERT_TRUE(someViewport);
490 }
491}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800492
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700493/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000494 * When we have multiple internal displays make sure we always return the default display when
495 * querying by type.
496 */
497TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
498 const std::string uniqueId1 = "uniqueId1";
499 const std::string uniqueId2 = "uniqueId2";
500 constexpr int32_t nonDefaultDisplayId = 2;
501 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
502 "Test display ID should not be ADISPLAY_ID_DEFAULT");
503
504 // Add the default display first and ensure it gets returned.
505 mFakePolicy->clearViewports();
506 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000507 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000508 ViewportType::INTERNAL);
509 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000510 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000511 ViewportType::INTERNAL);
512
513 std::optional<DisplayViewport> viewport =
514 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
515 ASSERT_TRUE(viewport);
516 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
517 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
518
519 // Add the default display second to make sure order doesn't matter.
520 mFakePolicy->clearViewports();
521 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000522 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000523 ViewportType::INTERNAL);
524 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000525 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000526 ViewportType::INTERNAL);
527
528 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
529 ASSERT_TRUE(viewport);
530 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
531 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
532}
533
534/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700535 * Check getDisplayViewportByPort
536 */
537TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100538 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700539 const std::string uniqueId1 = "uniqueId1";
540 const std::string uniqueId2 = "uniqueId2";
541 constexpr int32_t displayId1 = 1;
542 constexpr int32_t displayId2 = 2;
543 const uint8_t hdmi1 = 0;
544 const uint8_t hdmi2 = 1;
545 const uint8_t hdmi3 = 2;
546
547 mFakePolicy->clearViewports();
548 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000549 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000550 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700551 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000552 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000553 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700554
555 // Check that correct display viewport was returned by comparing the display ports.
556 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
557 ASSERT_TRUE(hdmi1Viewport);
558 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
559 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
560
561 // Check that we can still get the same viewport using the uniqueId
562 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
563 ASSERT_TRUE(hdmi1Viewport);
564 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
565 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
566 ASSERT_EQ(type, hdmi1Viewport->type);
567
568 // Check that we cannot find a port with "HDMI2", because we never added one
569 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
570 ASSERT_FALSE(hdmi2Viewport);
571}
572
Michael Wrightd02c5b62014-02-10 15:10:22 -0800573// --- InputReaderTest ---
574
575class InputReaderTest : public testing::Test {
576protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700577 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800578 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700579 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000580 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581
Chris Yea52ade12020-08-27 16:49:20 -0700582 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700583 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700584 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700585 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586
Prabir Pradhan28efc192019-11-05 01:10:04 +0000587 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700588 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589 }
590
Chris Yea52ade12020-08-27 16:49:20 -0700591 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700592 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594 }
595
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700596 void addDevice(int32_t eventHubId, const std::string& name,
597 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800598 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800599
600 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800601 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602 }
603 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000604 mReader->loopOnce();
605 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700606 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700608 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609 }
610
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800611 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700612 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000613 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700614 }
615
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800616 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700617 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000618 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700619 }
620
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800621 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700622 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700623 ftl::Flags<InputDeviceClass> classes,
624 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800625 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800626 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000627 FakeInputMapper& mapper =
628 device->addMapper<FakeInputMapper>(eventHubId,
629 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800630 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800631 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 return mapper;
633 }
634};
635
Chris Ye98d3f532020-10-01 21:48:59 -0700636TEST_F(InputReaderTest, PolicyGetInputDevices) {
637 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700638 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700639 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640
641 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700642 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800644 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100645 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800646 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
647 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000648 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800649}
650
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000651TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
652 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
653 mFakeEventHub->setSysfsRootPath(1, "xyz");
654
655 // Should also have received a notification describing the new input device.
656 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
657 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
658 ASSERT_EQ(0U, inputDevice.getLights().size());
659
660 RawLightInfo infoMonolight = {.id = 123,
661 .name = "mono_keyboard_backlight",
662 .maxBrightness = 255,
663 .flags = InputLightClass::BRIGHTNESS,
664 .path = ""};
665 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
666 mReader->sysfsNodeChanged("xyz");
667 mReader->loopOnce();
668
669 // Should also have received a notification describing the new recreated input device.
670 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
671 inputDevice = mFakePolicy->getInputDevices()[0];
672 ASSERT_EQ(1U, inputDevice.getLights().size());
673}
674
Chris Yee7310032020-09-22 15:36:28 -0700675TEST_F(InputReaderTest, GetMergedInputDevices) {
676 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
677 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
678 // Add two subdevices to device
679 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
680 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000681 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
682 AINPUT_SOURCE_KEYBOARD);
683 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
684 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700685
686 // Push same device instance for next device to be added, so they'll have same identifier.
687 mReader->pushNextDevice(device);
688 mReader->pushNextDevice(device);
689 ASSERT_NO_FATAL_FAILURE(
690 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
691 ASSERT_NO_FATAL_FAILURE(
692 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
693
694 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000695 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700696}
697
Chris Yee14523a2020-12-19 13:46:00 -0800698TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
699 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
700 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
701 // Add two subdevices to device
702 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
703 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000704 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
705 AINPUT_SOURCE_KEYBOARD);
706 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
707 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800708
709 // Push same device instance for next device to be added, so they'll have same identifier.
710 mReader->pushNextDevice(device);
711 mReader->pushNextDevice(device);
712 // Sensor device is initially disabled
713 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
714 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
715 nullptr));
716 // Device is disabled because the only sub device is a sensor device and disabled initially.
717 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
718 ASSERT_FALSE(device->isEnabled());
719 ASSERT_NO_FATAL_FAILURE(
720 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
721 // The merged device is enabled if any sub device is enabled
722 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
723 ASSERT_TRUE(device->isEnabled());
724}
725
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700726TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800727 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700728 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800729 constexpr int32_t eventHubId = 1;
730 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700731 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000732 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
733 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800734 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800735 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700736
Yi Kong9b14ac62018-07-17 13:48:38 -0700737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700738
739 NotifyDeviceResetArgs resetArgs;
740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700741 ASSERT_EQ(deviceId, resetArgs.deviceId);
742
743 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800744 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000745 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700746
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700748 ASSERT_EQ(deviceId, resetArgs.deviceId);
749 ASSERT_EQ(device->isEnabled(), false);
750
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800751 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000752 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700755 ASSERT_EQ(device->isEnabled(), false);
756
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800757 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000758 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700760 ASSERT_EQ(deviceId, resetArgs.deviceId);
761 ASSERT_EQ(device->isEnabled(), true);
762}
763
Michael Wrightd02c5b62014-02-10 15:10:22 -0800764TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800765 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700766 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800767 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800768 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800769 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800770 AINPUT_SOURCE_KEYBOARD, nullptr);
771 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800772
773 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
774 AINPUT_SOURCE_ANY, AKEYCODE_A))
775 << "Should return unknown when the device id is >= 0 but unknown.";
776
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800777 ASSERT_EQ(AKEY_STATE_UNKNOWN,
778 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
779 << "Should return unknown when the device id is valid but the sources are not "
780 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800781
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800782 ASSERT_EQ(AKEY_STATE_DOWN,
783 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
784 AKEYCODE_A))
785 << "Should return value provided by mapper when device id is valid and the device "
786 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787
788 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
789 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
790 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
791
792 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
793 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
794 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
795}
796
Philip Junker4af3b3d2021-12-14 10:36:55 +0100797TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
798 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
799 constexpr int32_t eventHubId = 1;
800 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
801 InputDeviceClass::KEYBOARD,
802 AINPUT_SOURCE_KEYBOARD, nullptr);
803 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
804
805 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
806 << "Should return unknown when the device with the specified id is not found.";
807
808 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
809 << "Should return correct mapping when device id is valid and mapping exists.";
810
811 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
812 << "Should return the location key code when device id is valid and there's no "
813 "mapping.";
814}
815
816TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
817 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
818 constexpr int32_t eventHubId = 1;
819 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
820 InputDeviceClass::JOYSTICK,
821 AINPUT_SOURCE_GAMEPAD, nullptr);
822 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
823
824 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
825 << "Should return unknown when the device id is valid but there is no keyboard mapper";
826}
827
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800829 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700830 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800831 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800832 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800833 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800834 AINPUT_SOURCE_KEYBOARD, nullptr);
835 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836
837 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
838 AINPUT_SOURCE_ANY, KEY_A))
839 << "Should return unknown when the device id is >= 0 but unknown.";
840
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800841 ASSERT_EQ(AKEY_STATE_UNKNOWN,
842 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
843 << "Should return unknown when the device id is valid but the sources are not "
844 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800846 ASSERT_EQ(AKEY_STATE_DOWN,
847 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
848 KEY_A))
849 << "Should return value provided by mapper when device id is valid and the device "
850 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851
852 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
853 AINPUT_SOURCE_TRACKBALL, KEY_A))
854 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
855
856 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
857 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
858 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
859}
860
861TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800862 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700863 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800864 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800865 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800866 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800867 AINPUT_SOURCE_KEYBOARD, nullptr);
868 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869
870 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
871 AINPUT_SOURCE_ANY, SW_LID))
872 << "Should return unknown when the device id is >= 0 but unknown.";
873
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800874 ASSERT_EQ(AKEY_STATE_UNKNOWN,
875 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
876 << "Should return unknown when the device id is valid but the sources are not "
877 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800878
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800879 ASSERT_EQ(AKEY_STATE_DOWN,
880 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
881 SW_LID))
882 << "Should return value provided by mapper when device id is valid and the device "
883 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800884
885 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
886 AINPUT_SOURCE_TRACKBALL, SW_LID))
887 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
888
889 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
890 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
891 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
892}
893
894TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800895 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700896 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800897 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800898 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800899 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800900 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100901
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800902 mapper.addSupportedKeyCode(AKEYCODE_A);
903 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700905 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800906 uint8_t flags[4] = { 0, 0, 0, 1 };
907
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700908 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909 << "Should return false when device id is >= 0 but unknown.";
910 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
911
912 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700913 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800914 << "Should return false when device id is valid but the sources are not supported by "
915 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
917
918 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700919 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800920 keyCodes, flags))
921 << "Should return value provided by mapper when device id is valid and the device "
922 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800923 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
924
925 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700926 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
927 << "Should return false when the device id is < 0 but the sources are not supported by "
928 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
930
931 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700932 ASSERT_TRUE(
933 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
934 << "Should return value provided by mapper when device id is < 0 and one of the "
935 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800936 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
937}
938
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000939TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800940 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700941 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800942
943 NotifyConfigurationChangedArgs args;
944
945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
946 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
947}
948
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000949TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800950 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700951 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000952 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800953 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000954 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800955 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800956 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800957 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000959 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000960 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
962
963 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800964 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000965 ASSERT_EQ(when, event.when);
966 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800967 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968 ASSERT_EQ(EV_KEY, event.type);
969 ASSERT_EQ(KEY_A, event.code);
970 ASSERT_EQ(1, event.value);
971}
972
Garfield Tan1c7bc862020-01-28 13:24:04 -0800973TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800974 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700975 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800976 constexpr int32_t eventHubId = 1;
977 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800978 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000979 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
980 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800981 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800982 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800983
984 NotifyDeviceResetArgs resetArgs;
985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800986 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800987
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800988 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000989 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800991 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800992 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800993
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800994 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000995 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800997 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800998 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800999
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001000 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001001 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001003 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001004 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001005}
1006
Garfield Tan1c7bc862020-01-28 13:24:04 -08001007TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1008 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001009 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001010 constexpr int32_t eventHubId = 1;
1011 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1012 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001013 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1014 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001015 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001016 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1017
1018 NotifyDeviceResetArgs resetArgs;
1019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1020 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1021}
1022
Arthur Hungc23540e2018-11-29 20:42:11 +08001023TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001024 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001025 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001026 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001027 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001028 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1029 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001030 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1031 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001032 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001033
1034 const uint8_t hdmi1 = 1;
1035
1036 // Associated touch screen with second display.
1037 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1038
1039 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001040 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001041 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001042 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001043 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001044 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001045 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001046 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001047 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001048
1049 // Add the device, and make sure all of the callbacks are triggered.
1050 // The device is added after the input port associations are processed since
1051 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001052 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001055 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001056
Arthur Hung2c9a3342019-07-23 14:18:59 +08001057 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001058 ASSERT_EQ(deviceId, device->getId());
1059 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1060 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001061
1062 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001063 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001064 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001065 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001066}
1067
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001068TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1069 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001070 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001071 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1072 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1073 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001074 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1075 AINPUT_SOURCE_KEYBOARD);
1076 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1077 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001078 mReader->pushNextDevice(device);
1079 mReader->pushNextDevice(device);
1080 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1081 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1082
1083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1084
1085 NotifyDeviceResetArgs resetArgs;
1086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1087 ASSERT_EQ(deviceId, resetArgs.deviceId);
1088 ASSERT_TRUE(device->isEnabled());
1089 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1090 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1091
1092 disableDevice(deviceId);
1093 mReader->loopOnce();
1094
1095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1096 ASSERT_EQ(deviceId, resetArgs.deviceId);
1097 ASSERT_FALSE(device->isEnabled());
1098 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1099 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1100
1101 enableDevice(deviceId);
1102 mReader->loopOnce();
1103
1104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1105 ASSERT_EQ(deviceId, resetArgs.deviceId);
1106 ASSERT_TRUE(device->isEnabled());
1107 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1108 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1109}
1110
1111TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1112 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001113 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001114 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1115 // Add two subdevices to device
1116 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1117 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001118 device->addMapper<FakeInputMapper>(eventHubIds[0],
1119 mFakePolicy->getReaderConfiguration(),
1120 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001121 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001122 device->addMapper<FakeInputMapper>(eventHubIds[1],
1123 mFakePolicy->getReaderConfiguration(),
1124 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001125 mReader->pushNextDevice(device);
1126 mReader->pushNextDevice(device);
1127 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1128 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1129
1130 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1131 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1132
1133 ASSERT_EQ(AKEY_STATE_DOWN,
1134 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1135 ASSERT_EQ(AKEY_STATE_DOWN,
1136 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1137 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1138 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1139}
1140
Prabir Pradhan7e186182020-11-10 13:56:45 -08001141TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1142 NotifyPointerCaptureChangedArgs args;
1143
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001144 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001145 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001146 mReader->loopOnce();
1147 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001148 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1149 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001150
1151 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001152 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001153 mReader->loopOnce();
1154 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001155 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001156
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001157 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001158 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001159 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001160 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001161 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001162}
1163
Chris Ye87143712020-11-10 05:05:58 +00001164class FakeVibratorInputMapper : public FakeInputMapper {
1165public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001166 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1167 const InputReaderConfiguration& readerConfig, uint32_t sources)
1168 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001169
1170 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1171};
1172
1173TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1174 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001175 ftl::Flags<InputDeviceClass> deviceClass =
1176 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001177 constexpr int32_t eventHubId = 1;
1178 const char* DEVICE_LOCATION = "BLUETOOTH";
1179 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1180 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001181 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1182 mFakePolicy->getReaderConfiguration(),
1183 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001184 mReader->pushNextDevice(device);
1185
1186 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1187 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1188
1189 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1190 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1191}
1192
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001193// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001194
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001195class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001196public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001197 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001198
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001199 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001200
Andy Chenf9f1a022022-08-29 20:07:10 -04001201 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1202
Chris Yee2b1e5c2021-03-10 22:45:12 -08001203 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1204
1205 void dump(std::string& dump) override {}
1206
1207 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1208 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001209 }
1210
Chris Yee2b1e5c2021-03-10 22:45:12 -08001211 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1212 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001213 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001214
1215 bool setLightColor(int32_t lightId, int32_t color) override {
1216 getDeviceContext().setLightBrightness(lightId, color >> 24);
1217 return true;
1218 }
1219
1220 std::optional<int32_t> getLightColor(int32_t lightId) override {
1221 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1222 if (!result.has_value()) {
1223 return std::nullopt;
1224 }
1225 return result.value() << 24;
1226 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001227
1228 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1229
1230 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1231
1232private:
1233 InputDeviceContext& mDeviceContext;
1234 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1235 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001236 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001237};
1238
Chris Yee2b1e5c2021-03-10 22:45:12 -08001239TEST_F(InputReaderTest, BatteryGetCapacity) {
1240 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001241 ftl::Flags<InputDeviceClass> deviceClass =
1242 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001243 constexpr int32_t eventHubId = 1;
1244 const char* DEVICE_LOCATION = "BLUETOOTH";
1245 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001246 FakePeripheralController& controller =
1247 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001248 mReader->pushNextDevice(device);
1249
1250 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1251
Harry Cuttsa5b71292022-11-28 12:56:17 +00001252 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1253 FakeEventHub::BATTERY_CAPACITY);
1254 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001255}
1256
1257TEST_F(InputReaderTest, BatteryGetStatus) {
1258 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001259 ftl::Flags<InputDeviceClass> deviceClass =
1260 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001261 constexpr int32_t eventHubId = 1;
1262 const char* DEVICE_LOCATION = "BLUETOOTH";
1263 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001264 FakePeripheralController& controller =
1265 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001266 mReader->pushNextDevice(device);
1267
1268 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1269
Harry Cuttsa5b71292022-11-28 12:56:17 +00001270 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1271 FakeEventHub::BATTERY_STATUS);
1272 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001273}
1274
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001275TEST_F(InputReaderTest, BatteryGetDevicePath) {
1276 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1277 ftl::Flags<InputDeviceClass> deviceClass =
1278 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1279 constexpr int32_t eventHubId = 1;
1280 const char* DEVICE_LOCATION = "BLUETOOTH";
1281 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1282 device->addController<FakePeripheralController>(eventHubId);
1283 mReader->pushNextDevice(device);
1284
1285 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1286
Harry Cuttsa5b71292022-11-28 12:56:17 +00001287 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001288}
1289
Chris Ye3fdbfef2021-01-06 18:45:18 -08001290TEST_F(InputReaderTest, LightGetColor) {
1291 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001292 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001293 constexpr int32_t eventHubId = 1;
1294 const char* DEVICE_LOCATION = "BLUETOOTH";
1295 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001296 FakePeripheralController& controller =
1297 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001298 mReader->pushNextDevice(device);
1299 RawLightInfo info = {.id = 1,
1300 .name = "Mono",
1301 .maxBrightness = 255,
1302 .flags = InputLightClass::BRIGHTNESS,
1303 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001304 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1305 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001306
1307 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001308
Harry Cutts33476232023-01-30 19:57:29 +00001309 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1310 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1311 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1312 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001313}
1314
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001315// --- InputReaderIntegrationTest ---
1316
1317// These tests create and interact with the InputReader only through its interface.
1318// The InputReader is started during SetUp(), which starts its processing in its own
1319// thread. The tests use linux uinput to emulate input devices.
1320// NOTE: Interacting with the physical device while these tests are running may cause
1321// the tests to fail.
1322class InputReaderIntegrationTest : public testing::Test {
1323protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001324 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001325 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001326 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001327
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001328 std::shared_ptr<FakePointerController> mFakePointerController;
1329
Chris Yea52ade12020-08-27 16:49:20 -07001330 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001331#if !defined(__ANDROID__)
1332 GTEST_SKIP();
1333#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001334 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001335 mFakePointerController = std::make_shared<FakePointerController>();
1336 mFakePolicy->setPointerController(mFakePointerController);
Harry Cutts33476232023-01-30 19:57:29 +00001337 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1338 /*eventDidNotHappenTimeout=*/30ms);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001339
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001340 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1341 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001342 ASSERT_EQ(mReader->start(), OK);
1343
1344 // Since this test is run on a real device, all the input devices connected
1345 // to the test device will show up in mReader. We wait for those input devices to
1346 // show up before beginning the tests.
1347 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +00001348 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001349 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1350 }
1351
Chris Yea52ade12020-08-27 16:49:20 -07001352 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001353#if !defined(__ANDROID__)
1354 return;
1355#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001356 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001357 mReader.reset();
1358 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001359 mFakePolicy.clear();
1360 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001361
1362 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1363 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1364 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1365 [&name](const InputDeviceInfo& info) {
1366 return info.getIdentifier().name == name;
1367 });
1368 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1369 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001370};
1371
1372TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1373 // An invalid input device that is only used for this test.
1374 class InvalidUinputDevice : public UinputDevice {
1375 public:
Harry Cutts33476232023-01-30 19:57:29 +00001376 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001377
1378 private:
1379 void configureDevice(int fd, uinput_user_dev* device) override {}
1380 };
1381
1382 const size_t numDevices = mFakePolicy->getInputDevices().size();
1383
1384 // UinputDevice does not set any event or key bits, so InputReader should not
1385 // consider it as a valid device.
1386 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1387 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1388 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1389 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1390
1391 invalidDevice.reset();
1392 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1393 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1394 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1395}
1396
1397TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1398 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1399
1400 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1401 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1402 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1403 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1404
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001405 const auto device = findDeviceByName(keyboard->getName());
1406 ASSERT_TRUE(device.has_value());
1407 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1408 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1409 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001410
1411 keyboard.reset();
1412 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1413 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1414 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1415}
1416
1417TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1418 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1419 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1420
1421 NotifyConfigurationChangedArgs configChangedArgs;
1422 ASSERT_NO_FATAL_FAILURE(
1423 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001424 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001425 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1426
1427 NotifyKeyArgs keyArgs;
1428 keyboard->pressAndReleaseHomeKey();
1429 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1430 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001431 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001432 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001433 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001434 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001435 prevTimestamp = keyArgs.eventTime;
1436
1437 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1438 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001439 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001440 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001441 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001442}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001443
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001444TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1445 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1446 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1447
1448 const auto device = findDeviceByName(stylus->getName());
1449 ASSERT_TRUE(device.has_value());
1450
Prabir Pradhana3621852022-10-14 18:57:23 +00001451 // An external stylus with buttons should also be recognized as a keyboard.
1452 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001453 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1454 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1455
1456 const auto DOWN =
1457 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1458 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1459
1460 stylus->pressAndReleaseKey(BTN_STYLUS);
1461 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1462 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1463 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1464 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1465
1466 stylus->pressAndReleaseKey(BTN_STYLUS2);
1467 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1468 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1469 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1470 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1471
1472 stylus->pressAndReleaseKey(BTN_STYLUS3);
1473 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1474 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1475 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1476 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1477}
1478
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001479/**
1480 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1481 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1482 * are passed to the listener.
1483 */
1484static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1485TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1486 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1487 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1488 NotifyKeyArgs keyArgs;
1489
1490 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1491 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1492 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1493 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1494
1495 controller->pressAndReleaseKey(BTN_GEAR_UP);
1496 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1497 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1498 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1499}
1500
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001501// --- TouchIntegrationTest ---
1502
Arthur Hungaab25622020-01-16 11:22:11 +08001503class TouchIntegrationTest : public InputReaderIntegrationTest {
1504protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001505 const std::string UNIQUE_ID = "local:0";
1506
Chris Yea52ade12020-08-27 16:49:20 -07001507 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001508#if !defined(__ANDROID__)
1509 GTEST_SKIP();
1510#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001511 InputReaderIntegrationTest::SetUp();
1512 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001513 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1514 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001515
1516 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1517 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1518 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001519 const auto info = findDeviceByName(mDevice->getName());
1520 ASSERT_TRUE(info);
1521 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001522 }
1523
1524 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001525 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001526 std::optional<uint8_t> physicalPort,
1527 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001528 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001529 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001530 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001531 }
1532
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001533 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1534 NotifyMotionArgs args;
1535 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1536 EXPECT_EQ(action, args.action);
1537 ASSERT_EQ(points.size(), args.pointerCount);
1538 for (size_t i = 0; i < args.pointerCount; i++) {
1539 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1540 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1541 }
1542 }
1543
Arthur Hungaab25622020-01-16 11:22:11 +08001544 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001545 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001546};
1547
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001548TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
1549 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1550 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1551 // presses).
1552 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1553 mDeviceInfo.getSources());
1554}
1555
Arthur Hungaab25622020-01-16 11:22:11 +08001556TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1557 NotifyMotionArgs args;
1558 const Point centerPoint = mDevice->getCenterPoint();
1559
1560 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001561 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001562 mDevice->sendDown(centerPoint);
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));
1565 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1566
1567 // ACTION_MOVE
1568 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001569 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001570 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1572
1573 // ACTION_UP
1574 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001575 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001576 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1577 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1578}
1579
1580TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1581 NotifyMotionArgs args;
1582 const Point centerPoint = mDevice->getCenterPoint();
1583
1584 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001585 mDevice->sendSlot(FIRST_SLOT);
1586 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001587 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001588 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001589 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1590 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1591
1592 // ACTION_POINTER_DOWN (Second slot)
1593 const Point secondPoint = centerPoint + Point(100, 100);
1594 mDevice->sendSlot(SECOND_SLOT);
1595 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001596 mDevice->sendDown(secondPoint);
1597 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001598 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001599 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001600
1601 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001602 mDevice->sendMove(secondPoint + Point(1, 1));
1603 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001604 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1605 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1606
1607 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001608 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001609 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001610 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001611 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001612
1613 // ACTION_UP
1614 mDevice->sendSlot(FIRST_SLOT);
1615 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001616 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001617 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1618 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1619}
1620
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001621/**
1622 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1623 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1624 * data?
1625 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1626 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1627 * for Pointer 0 only is generated after.
1628 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1629 * events, we will not miss any information.
1630 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1631 * event generated afterwards that contains the newest movement of pointer 0.
1632 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1633 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1634 * losing information about non-palm pointers.
1635 */
1636TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1637 NotifyMotionArgs args;
1638 const Point centerPoint = mDevice->getCenterPoint();
1639
1640 // ACTION_DOWN
1641 mDevice->sendSlot(FIRST_SLOT);
1642 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1643 mDevice->sendDown(centerPoint);
1644 mDevice->sendSync();
1645 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1646
1647 // ACTION_POINTER_DOWN (Second slot)
1648 const Point secondPoint = centerPoint + Point(100, 100);
1649 mDevice->sendSlot(SECOND_SLOT);
1650 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1651 mDevice->sendDown(secondPoint);
1652 mDevice->sendSync();
1653 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1654
1655 // ACTION_MOVE (First slot)
1656 mDevice->sendSlot(FIRST_SLOT);
1657 mDevice->sendMove(centerPoint + Point(5, 5));
1658 // ACTION_POINTER_UP (Second slot)
1659 mDevice->sendSlot(SECOND_SLOT);
1660 mDevice->sendPointerUp();
1661 // Send a single sync for the above 2 pointer updates
1662 mDevice->sendSync();
1663
1664 // First, we should get POINTER_UP for the second pointer
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
1673/**
1674 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1675 * move, and then it will go up, all in the same frame.
1676 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1677 * gets sent to the listener.
1678 */
1679TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1680 NotifyMotionArgs args;
1681 const Point centerPoint = mDevice->getCenterPoint();
1682
1683 // ACTION_DOWN
1684 mDevice->sendSlot(FIRST_SLOT);
1685 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1686 mDevice->sendDown(centerPoint);
1687 mDevice->sendSync();
1688 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1689
1690 // ACTION_POINTER_DOWN (Second slot)
1691 const Point secondPoint = centerPoint + Point(100, 100);
1692 mDevice->sendSlot(SECOND_SLOT);
1693 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1694 mDevice->sendDown(secondPoint);
1695 mDevice->sendSync();
1696 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1697
1698 // ACTION_MOVE (First slot)
1699 mDevice->sendSlot(FIRST_SLOT);
1700 mDevice->sendMove(centerPoint + Point(5, 5));
1701 // ACTION_POINTER_UP (Second slot)
1702 mDevice->sendSlot(SECOND_SLOT);
1703 mDevice->sendMove(secondPoint + Point(6, 6));
1704 mDevice->sendPointerUp();
1705 // Send a single sync for the above 2 pointer updates
1706 mDevice->sendSync();
1707
1708 // First, we should get POINTER_UP for the second pointer
1709 // The movement of the second pointer during the liftoff frame is ignored.
1710 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1711 assertReceivedMotion(ACTION_POINTER_1_UP,
1712 {/*first pointer */ centerPoint + Point(5, 5),
1713 /*second pointer*/ secondPoint});
1714
1715 // Next, the MOVE event for the first pointer
1716 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1717}
1718
Arthur Hungaab25622020-01-16 11:22:11 +08001719TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1720 NotifyMotionArgs args;
1721 const Point centerPoint = mDevice->getCenterPoint();
1722
1723 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001724 mDevice->sendSlot(FIRST_SLOT);
1725 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001726 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001727 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001728 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1729 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1730
arthurhungcc7f9802020-04-30 17:55:40 +08001731 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001732 const Point secondPoint = centerPoint + Point(100, 100);
1733 mDevice->sendSlot(SECOND_SLOT);
1734 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1735 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001736 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001737 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001738 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001739
arthurhungcc7f9802020-04-30 17:55:40 +08001740 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001741 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001742 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001743 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1744 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1745
arthurhungcc7f9802020-04-30 17:55:40 +08001746 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1747 // a palm event.
1748 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001749 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001750 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001751 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001752 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001753 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001754
arthurhungcc7f9802020-04-30 17:55:40 +08001755 // Send up to second slot, expect first slot send moving.
1756 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001757 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001758 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001760
arthurhungcc7f9802020-04-30 17:55:40 +08001761 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001762 mDevice->sendSlot(FIRST_SLOT);
1763 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001764 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001765
arthurhungcc7f9802020-04-30 17:55:40 +08001766 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1767 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001768}
1769
Prabir Pradhanda20b172022-09-26 17:01:18 +00001770TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
1771 const Point centerPoint = mDevice->getCenterPoint();
1772
1773 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1774 mDevice->sendSlot(FIRST_SLOT);
1775 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1776 mDevice->sendToolType(MT_TOOL_PEN);
1777 mDevice->sendDown(centerPoint);
1778 mDevice->sendSync();
1779 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1780 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001781 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001782
1783 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1784
1785 // Release the stylus touch.
1786 mDevice->sendUp();
1787 mDevice->sendSync();
1788 ASSERT_NO_FATAL_FAILURE(
1789 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1790
1791 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1792
1793 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1794 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1795 mDevice->sendToolType(MT_TOOL_FINGER);
1796 mDevice->sendDown(centerPoint);
1797 mDevice->sendSync();
1798 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1799 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001800 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001801
1802 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1803
1804 mDevice->sendUp();
1805 mDevice->sendSync();
1806 ASSERT_NO_FATAL_FAILURE(
1807 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1808
1809 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1810 // The policy should be notified of the stylus presence.
1811 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1812 mDevice->sendToolType(MT_TOOL_PEN);
1813 mDevice->sendMove(centerPoint);
1814 mDevice->sendSync();
1815 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1816 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001817 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001818
1819 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1820}
1821
Prabir Pradhan124ea442022-10-28 20:27:44 +00001822// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001823
Prabir Pradhan124ea442022-10-28 20:27:44 +00001824// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1825// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1826// stylus.
1827template <typename UinputStylusDevice>
1828class StylusButtonIntegrationTest : public TouchIntegrationTest {
1829protected:
1830 void SetUp() override {
1831#if !defined(__ANDROID__)
1832 GTEST_SKIP();
1833#endif
1834 TouchIntegrationTest::SetUp();
1835 mTouchscreen = mDevice.get();
1836 mTouchscreenInfo = mDeviceInfo;
1837
1838 setUpStylusDevice();
1839 }
1840
1841 UinputStylusDevice* mStylus{nullptr};
1842 InputDeviceInfo mStylusInfo{};
1843
1844 UinputTouchScreen* mTouchscreen{nullptr};
1845 InputDeviceInfo mTouchscreenInfo{};
1846
1847private:
1848 // When we are attempting to test stylus button events that are sent from the touchscreen,
1849 // use the same Uinput device for the touchscreen and the stylus.
1850 template <typename T = UinputStylusDevice>
1851 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1852 mStylus = mDevice.get();
1853 mStylusInfo = mDeviceInfo;
1854 }
1855
1856 // When we are attempting to stylus buttons from an external stylus being merged with touches
1857 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1858 template <typename T = UinputStylusDevice>
1859 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1860 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1861 mStylus = mStylusDeviceLifecycleTracker.get();
1862 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1863 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1864 const auto info = findDeviceByName(mStylus->getName());
1865 ASSERT_TRUE(info);
1866 mStylusInfo = *info;
1867 }
1868
1869 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1870
1871 // Hide the base class's device to expose it with a different name for readability.
1872 using TouchIntegrationTest::mDevice;
1873 using TouchIntegrationTest::mDeviceInfo;
1874};
1875
1876using StylusButtonIntegrationTestTypes =
1877 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1878TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1879
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001880TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001881 const auto stylusId = TestFixture::mStylusInfo.getId();
1882
1883 TestFixture::mStylus->pressKey(BTN_STYLUS);
1884 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1885 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1886 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1887
1888 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1889 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +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 Pradhane1a41a82022-10-14 18:06:50 +00001892}
1893
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001894TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001895 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1896 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1897 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001898
1899 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001900 TestFixture::mStylus->pressKey(BTN_STYLUS);
1901 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001902 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001903 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001904
1905 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001906 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1907 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1908 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1909 TestFixture::mTouchscreen->sendDown(centerPoint);
1910 TestFixture::mTouchscreen->sendSync();
1911 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001912 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001913 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001914 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1915 WithDeviceId(touchscreenId))));
1916 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001917 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001918 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001919 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1920 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001921
Prabir Pradhan124ea442022-10-28 20:27:44 +00001922 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1923 TestFixture::mTouchscreen->sendSync();
1924 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001925 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001926 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001927 WithDeviceId(touchscreenId))));
1928 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001929 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001930 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001931 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001932
1933 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001934 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1935 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001936 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001937 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001938}
1939
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001940TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001941 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1942 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1943 const auto stylusId = TestFixture::mStylusInfo.getId();
1944 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001945 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001946
1947 // Press the stylus button.
1948 TestFixture::mStylus->pressKey(BTN_STYLUS);
1949 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1950 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1951 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1952
1953 // Start hovering with the stylus.
1954 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1955 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1956 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1957 TestFixture::mTouchscreen->sendMove(centerPoint);
1958 TestFixture::mTouchscreen->sendSync();
1959 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1960 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1961 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1962 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1963 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1964 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1965 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1966 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1967 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1968
1969 // Touch down with the stylus.
1970 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1971 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1972 TestFixture::mTouchscreen->sendDown(centerPoint);
1973 TestFixture::mTouchscreen->sendSync();
1974 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1975 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1976 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1977
1978 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1979 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1980 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1981
1982 // Stop touching with the stylus, and start hovering.
1983 TestFixture::mTouchscreen->sendUp();
1984 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1985 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1986 TestFixture::mTouchscreen->sendMove(centerPoint);
1987 TestFixture::mTouchscreen->sendSync();
1988 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1989 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
1990 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1991 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1992 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1993 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1994 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1995 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1996 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1997
1998 // Stop hovering.
1999 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2000 TestFixture::mTouchscreen->sendSync();
2001 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2002 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2003 WithButtonState(0))));
2004 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2005 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2006 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2007 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2008
2009 // Release the stylus button.
2010 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2011 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2012 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2013 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2014}
2015
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002016TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002017 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2018 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2019 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002020
2021 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002022 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2023 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2024 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2025 TestFixture::mTouchscreen->sendDown(centerPoint);
2026 TestFixture::mTouchscreen->sendSync();
2027 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002028 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002029 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002030 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002031
2032 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002033 TestFixture::mStylus->pressKey(BTN_STYLUS);
2034 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002035 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002036 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2037 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002038 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002039 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002040 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2041 WithDeviceId(touchscreenId))));
2042 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002043 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002044 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002045 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2046 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002047
Prabir Pradhan124ea442022-10-28 20:27:44 +00002048 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2049 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002050 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002051 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2052 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002053 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002054 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002055 WithDeviceId(touchscreenId))));
2056 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002057 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002058 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002059 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002060
2061 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002062 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2063 TestFixture::mTouchscreen->sendSync();
2064 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002065 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002066 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002067 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002068}
2069
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002070TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002071 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2072 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002073 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002074
2075 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2076 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2077 const auto stylusId = TestFixture::mStylusInfo.getId();
2078
2079 // Start a stylus gesture. By the time this event is processed, the configuration change that
2080 // was requested is guaranteed to be completed.
2081 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2082 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2083 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2084 TestFixture::mTouchscreen->sendDown(centerPoint);
2085 TestFixture::mTouchscreen->sendSync();
2086 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2087 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002088 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002089 WithDeviceId(touchscreenId))));
2090
2091 // Press and release a stylus button. Each change only generates a MOVE motion event.
2092 // Key events are unaffected.
2093 TestFixture::mStylus->pressKey(BTN_STYLUS);
2094 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2095 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2096 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2097 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2098 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002099 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002100 WithDeviceId(touchscreenId))));
2101
2102 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2103 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2104 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2105 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2106 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2107 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002108 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002109 WithDeviceId(touchscreenId))));
2110
2111 // Finish the stylus gesture.
2112 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2113 TestFixture::mTouchscreen->sendSync();
2114 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2115 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002116 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002117 WithDeviceId(touchscreenId))));
2118}
2119
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002120// --- ExternalStylusIntegrationTest ---
2121
2122// Verify the behavior of an external stylus. An external stylus can report pressure or button
2123// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2124// ongoing stylus gesture that is being emitted by the touchscreen.
2125using ExternalStylusIntegrationTest = TouchIntegrationTest;
2126
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002127TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureReported) {
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 on the stylus. It doesn't generate any events.
2144 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2145 stylus->setPressure(100);
2146 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2147
2148 // Start a finger gesture, and ensure it shows up as stylus gesture
2149 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002150 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002151 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002152 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002153 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002154 mDevice->sendSync();
2155 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2156 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002157 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002158 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002159
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002160 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2161 // event with the updated pressure.
2162 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002163 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2164 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002165 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002166 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002167
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002168 // The external stylus did not generate any events.
2169 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2170 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2171}
2172
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002173TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002174 const Point centerPoint = mDevice->getCenterPoint();
2175
2176 // Create an external stylus capable of reporting pressure data that
2177 // should be fused with a touch pointer.
2178 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2179 createUinputDevice<UinputExternalStylusWithPressure>();
2180 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2181 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2182 const auto stylusInfo = findDeviceByName(stylus->getName());
2183 ASSERT_TRUE(stylusInfo);
2184
2185 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2186
2187 const auto touchscreenId = mDeviceInfo.getId();
2188
2189 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2190 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002191 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2192 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002193 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002194 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002195
2196 // Start a finger gesture. The touch device will withhold generating any touches for
2197 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2198 mDevice->sendSlot(FIRST_SLOT);
2199 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2200 mDevice->sendToolType(MT_TOOL_FINGER);
2201 mDevice->sendDown(centerPoint);
2202 auto waitUntil = std::chrono::system_clock::now() +
2203 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002204 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002205 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002206
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002207 // Since the external stylus did not report a pressure value within the timeout,
2208 // it shows up as a finger pointer.
2209 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2210 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002211 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002212 WithPressure(1.f))));
2213
2214 // Change the pressure on the external stylus. Since the pressure was not present at the start
2215 // of the gesture, it is ignored for now.
2216 stylus->setPressure(200);
2217 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2218
2219 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002220 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2221 mDevice->sendSync();
2222 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2223 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002224 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002225
2226 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2227 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2228 mDevice->sendToolType(MT_TOOL_FINGER);
2229 mDevice->sendDown(centerPoint);
2230 mDevice->sendSync();
2231 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2232 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002233 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002234 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2235
2236 // The external stylus did not generate any events.
2237 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2238 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002239}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002240
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002241TEST_F(ExternalStylusIntegrationTest, DISABLED_UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002242 const Point centerPoint = mDevice->getCenterPoint();
2243
2244 // Create an external stylus device that does not support pressure. It should not affect any
2245 // touch pointers.
2246 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2247 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2248 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2249 const auto stylusInfo = findDeviceByName(stylus->getName());
2250 ASSERT_TRUE(stylusInfo);
2251
2252 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2253
2254 const auto touchscreenId = mDeviceInfo.getId();
2255
2256 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2257 // pressure data from the external stylus.
2258 mDevice->sendSlot(FIRST_SLOT);
2259 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2260 mDevice->sendToolType(MT_TOOL_FINGER);
2261 mDevice->sendDown(centerPoint);
2262 auto waitUntil = std::chrono::system_clock::now() +
2263 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2264 mDevice->sendSync();
2265 ASSERT_NO_FATAL_FAILURE(
2266 mTestListener
2267 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2268 WithToolType(
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002269 ToolType::FINGER),
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002270 WithButtonState(0),
2271 WithDeviceId(touchscreenId),
2272 WithPressure(1.f)),
2273 waitUntil));
2274
2275 // The external stylus did not generate any events.
2276 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2277 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2278}
2279
Michael Wrightd02c5b62014-02-10 15:10:22 -08002280// --- InputDeviceTest ---
2281class InputDeviceTest : public testing::Test {
2282protected:
2283 static const char* DEVICE_NAME;
2284 static const char* DEVICE_LOCATION;
2285 static const int32_t DEVICE_ID;
2286 static const int32_t DEVICE_GENERATION;
2287 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002288 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002289 static const int32_t EVENTHUB_ID;
2290 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2291
2292 std::shared_ptr<FakeEventHub> mFakeEventHub;
2293 sp<FakeInputReaderPolicy> mFakePolicy;
2294 std::unique_ptr<TestInputListener> mFakeListener;
2295 std::unique_ptr<InstrumentedInputReader> mReader;
2296 std::shared_ptr<InputDevice> mDevice;
2297
2298 void SetUp() override {
2299 mFakeEventHub = std::make_unique<FakeEventHub>();
2300 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2301 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002302 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002303 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002304 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002305 identifier.name = DEVICE_NAME;
2306 identifier.location = DEVICE_LOCATION;
2307 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2308 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2309 identifier);
2310 mReader->pushNextDevice(mDevice);
2311 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002312 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002313 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002314
2315 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002316 mFakeListener.reset();
2317 mFakePolicy.clear();
2318 }
2319};
2320
2321const char* InputDeviceTest::DEVICE_NAME = "device";
2322const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2323const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2324const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002325const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002326const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2327 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002328const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002329const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2330
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002331TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002332 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002333 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2334 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002335}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002336
Michael Wrightd02c5b62014-02-10 15:10:22 -08002337TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2338 ASSERT_EQ(mDevice->isEnabled(), false);
2339}
2340
2341TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2342 // Configuration.
2343 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002344 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002345
2346 // Reset.
2347 unused += mDevice->reset(ARBITRARY_TIME);
2348
2349 NotifyDeviceResetArgs resetArgs;
2350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2351 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2352 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2353
2354 // Metadata.
2355 ASSERT_TRUE(mDevice->isIgnored());
2356 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2357
2358 InputDeviceInfo info = mDevice->getDeviceInfo();
2359 ASSERT_EQ(DEVICE_ID, info.getId());
2360 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2361 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2362 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2363
2364 // State queries.
2365 ASSERT_EQ(0, mDevice->getMetaState());
2366
2367 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2368 << "Ignored device should return unknown key code state.";
2369 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2370 << "Ignored device should return unknown scan code state.";
2371 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2372 << "Ignored device should return unknown switch state.";
2373
2374 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2375 uint8_t flags[2] = { 0, 1 };
2376 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2377 << "Ignored device should never mark any key codes.";
2378 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2379 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2380}
2381
2382TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2383 // Configuration.
2384 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2385
2386 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002387 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2388 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002389 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2390 mapper1.setMetaState(AMETA_ALT_ON);
2391 mapper1.addSupportedKeyCode(AKEYCODE_A);
2392 mapper1.addSupportedKeyCode(AKEYCODE_B);
2393 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2394 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2395 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2396 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2397 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2398
2399 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002400 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2401 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002402 mapper2.setMetaState(AMETA_SHIFT_ON);
2403
2404 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002405 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002406
Harry Cuttsf13161a2023-03-08 14:15:49 +00002407 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2408 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002409 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002410 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002411
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002412 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2413 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002414
2415 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002416 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002417 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2418 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002419
2420 NotifyDeviceResetArgs resetArgs;
2421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2422 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2423 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2424
2425 // Metadata.
2426 ASSERT_FALSE(mDevice->isIgnored());
2427 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2428
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002429 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002431 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002432 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2433 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2434
2435 // State queries.
2436 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2437 << "Should query mappers and combine meta states.";
2438
2439 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2440 << "Should return unknown key code state when source not supported.";
2441 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2442 << "Should return unknown scan code state when source not supported.";
2443 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2444 << "Should return unknown switch state when source not supported.";
2445
2446 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2447 << "Should query mapper when source is supported.";
2448 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2449 << "Should query mapper when source is supported.";
2450 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2451 << "Should query mapper when source is supported.";
2452
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002453 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002454 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002455 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002456 << "Should do nothing when source is unsupported.";
2457 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2458 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2459 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2460 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2461
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002462 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002463 << "Should query mapper when source is supported.";
2464 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2465 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2466 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2467 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2468
2469 // Event handling.
2470 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002471 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002472 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002473
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002474 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2475 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002476}
2477
Arthur Hung2c9a3342019-07-23 14:18:59 +08002478// A single input device is associated with a specific display. Check that:
2479// 1. Device is disabled if the viewport corresponding to the associated display is not found
2480// 2. Device is disabled when setEnabled API is called
2481TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002482 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2483 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002484
2485 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002486 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002487 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2488 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002489
2490 // Device should be enabled by default.
2491 ASSERT_TRUE(mDevice->isEnabled());
2492
2493 // Prepare associated info.
2494 constexpr uint8_t hdmi = 1;
2495 const std::string UNIQUE_ID = "local:1";
2496
2497 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002498 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002499 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002500 // Device should be disabled because it is associated with a specific display via
2501 // input port <-> display port association, but the corresponding display is not found
2502 ASSERT_FALSE(mDevice->isEnabled());
2503
2504 // Prepare displays.
2505 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002506 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002507 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002508 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002509 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002510 ASSERT_TRUE(mDevice->isEnabled());
2511
2512 // Device should be disabled after set disable.
2513 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002514 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002515 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002516 ASSERT_FALSE(mDevice->isEnabled());
2517
2518 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002519 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002520 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002521 ASSERT_FALSE(mDevice->isEnabled());
2522}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002523
Christine Franks1ba71cc2021-04-07 14:37:42 -07002524TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2525 // Device should be enabled by default.
2526 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002527 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2528 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002529 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002530 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2531 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002532 ASSERT_TRUE(mDevice->isEnabled());
2533
2534 // Device should be disabled because it is associated with a specific display, but the
2535 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002536 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002537 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002538 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002539 ASSERT_FALSE(mDevice->isEnabled());
2540
2541 // Device should be enabled when a display is found.
2542 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002543 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002544 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002545 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002546 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002547 ASSERT_TRUE(mDevice->isEnabled());
2548
2549 // Device should be disabled after set disable.
2550 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002551 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002552 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002553 ASSERT_FALSE(mDevice->isEnabled());
2554
2555 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002556 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002557 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002558 ASSERT_FALSE(mDevice->isEnabled());
2559}
2560
Christine Franks2a2293c2022-01-18 11:51:16 -08002561TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2562 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002563 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2564 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002565 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002566 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2567 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002568
Christine Franks2a2293c2022-01-18 11:51:16 -08002569 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2570 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002571 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002572 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002573 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002574 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002575 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2576}
2577
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002578/**
2579 * This test reproduces a crash caused by a dangling reference that remains after device is added
2580 * and removed. The reference is accessed in InputDevice::dump(..);
2581 */
2582TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2583 constexpr int32_t TEST_EVENTHUB_ID = 10;
2584 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2585
Harry Cutts33476232023-01-30 19:57:29 +00002586 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh8e6fb252023-04-06 11:49:17 +00002587 device.addEventHubDevice(TEST_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002588 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2589 std::string dumpStr, eventHubDevStr;
2590 device.dump(dumpStr, eventHubDevStr);
2591}
2592
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002593TEST_F(InputDeviceTest, GetBluetoothAddress) {
2594 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2595 ASSERT_TRUE(address);
2596 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2597}
2598
Michael Wrightd02c5b62014-02-10 15:10:22 -08002599// --- SwitchInputMapperTest ---
2600
2601class SwitchInputMapperTest : public InputMapperTest {
2602protected:
2603};
2604
2605TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002606 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002607
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002608 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002609}
2610
2611TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002612 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002613
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002614 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002615 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002617 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002618 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002619}
2620
2621TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002622 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002623 std::list<NotifyArgs> out;
2624 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2625 ASSERT_TRUE(out.empty());
2626 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2627 ASSERT_TRUE(out.empty());
2628 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2629 ASSERT_TRUE(out.empty());
2630 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002631
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002632 ASSERT_EQ(1u, out.size());
2633 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002634 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002635 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2636 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637 args.switchMask);
2638 ASSERT_EQ(uint32_t(0), args.policyFlags);
2639}
2640
Chris Ye87143712020-11-10 05:05:58 +00002641// --- VibratorInputMapperTest ---
2642class VibratorInputMapperTest : public InputMapperTest {
2643protected:
2644 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2645};
2646
2647TEST_F(VibratorInputMapperTest, GetSources) {
2648 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2649
2650 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2651}
2652
2653TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2654 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2655
2656 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2657}
2658
2659TEST_F(VibratorInputMapperTest, Vibrate) {
2660 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002661 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002662 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2663
2664 VibrationElement pattern(2);
2665 VibrationSequence sequence(2);
2666 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002667 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2668 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002669 sequence.addElement(pattern);
2670 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002671 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2672 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002673 sequence.addElement(pattern);
2674
2675 std::vector<int64_t> timings = {0, 1};
2676 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2677
2678 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002679 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002680 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002681 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002682 // Verify vibrator state listener was notified.
2683 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002684 ASSERT_EQ(1u, out.size());
2685 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2686 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2687 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002688 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002689 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002690 ASSERT_FALSE(mapper.isVibrating());
2691 // Verify vibrator state listener was notified.
2692 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002693 ASSERT_EQ(1u, out.size());
2694 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2695 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2696 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002697}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002698
Chris Yef59a2f42020-10-16 12:55:26 -07002699// --- SensorInputMapperTest ---
2700
2701class SensorInputMapperTest : public InputMapperTest {
2702protected:
2703 static const int32_t ACCEL_RAW_MIN;
2704 static const int32_t ACCEL_RAW_MAX;
2705 static const int32_t ACCEL_RAW_FUZZ;
2706 static const int32_t ACCEL_RAW_FLAT;
2707 static const int32_t ACCEL_RAW_RESOLUTION;
2708
2709 static const int32_t GYRO_RAW_MIN;
2710 static const int32_t GYRO_RAW_MAX;
2711 static const int32_t GYRO_RAW_FUZZ;
2712 static const int32_t GYRO_RAW_FLAT;
2713 static const int32_t GYRO_RAW_RESOLUTION;
2714
2715 static const float GRAVITY_MS2_UNIT;
2716 static const float DEGREE_RADIAN_UNIT;
2717
2718 void prepareAccelAxes();
2719 void prepareGyroAxes();
2720 void setAccelProperties();
2721 void setGyroProperties();
2722 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2723};
2724
2725const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2726const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2727const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2728const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2729const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2730
2731const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2732const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2733const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2734const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2735const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2736
2737const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2738const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2739
2740void SensorInputMapperTest::prepareAccelAxes() {
2741 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2742 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2743 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2744 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2745 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2746 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2747}
2748
2749void SensorInputMapperTest::prepareGyroAxes() {
2750 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2751 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2752 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2753 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2754 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2755 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2756}
2757
2758void SensorInputMapperTest::setAccelProperties() {
2759 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2760 /* sensorDataIndex */ 0);
2761 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2762 /* sensorDataIndex */ 1);
2763 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2764 /* sensorDataIndex */ 2);
2765 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2766 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2767 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2768 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2769 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2770}
2771
2772void SensorInputMapperTest::setGyroProperties() {
2773 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2774 /* sensorDataIndex */ 0);
2775 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2776 /* sensorDataIndex */ 1);
2777 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2778 /* sensorDataIndex */ 2);
2779 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2780 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2781 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2782 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2783 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2784}
2785
2786TEST_F(SensorInputMapperTest, GetSources) {
2787 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2788
2789 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2790}
2791
2792TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2793 setAccelProperties();
2794 prepareAccelAxes();
2795 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2796
2797 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2798 std::chrono::microseconds(10000),
2799 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002800 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2804 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2805 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002806
2807 NotifySensorArgs args;
2808 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2809 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2810 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2811
2812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2813 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2814 ASSERT_EQ(args.deviceId, DEVICE_ID);
2815 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2816 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2817 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2818 ASSERT_EQ(args.values, values);
2819 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2820}
2821
2822TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2823 setGyroProperties();
2824 prepareGyroAxes();
2825 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2826
2827 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2828 std::chrono::microseconds(10000),
2829 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002830 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2834 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2835 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002836
2837 NotifySensorArgs args;
2838 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2839 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2840 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2841
2842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2843 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2844 ASSERT_EQ(args.deviceId, DEVICE_ID);
2845 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2846 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2847 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2848 ASSERT_EQ(args.values, values);
2849 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2850}
2851
Michael Wrightd02c5b62014-02-10 15:10:22 -08002852// --- KeyboardInputMapperTest ---
2853
2854class KeyboardInputMapperTest : public InputMapperTest {
2855protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002856 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002857 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002858 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002859
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002860 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002861 int32_t originalKeyCode, int32_t rotatedKeyCode,
2862 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002863};
2864
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002865/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2866 * orientation.
2867 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002868void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002869 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2870 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002871}
2872
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002873void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002874 int32_t originalScanCode, int32_t originalKeyCode,
2875 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002876 NotifyKeyArgs args;
2877
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002878 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2880 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2881 ASSERT_EQ(originalScanCode, args.scanCode);
2882 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002883 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002884
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002885 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2887 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2888 ASSERT_EQ(originalScanCode, args.scanCode);
2889 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002890 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002891}
2892
Michael Wrightd02c5b62014-02-10 15:10:22 -08002893TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002894 KeyboardInputMapper& mapper =
2895 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2896 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002897
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002898 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002899}
2900
2901TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2902 const int32_t USAGE_A = 0x070004;
2903 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002904 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2905 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002906 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2907 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2908 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002909
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002910 KeyboardInputMapper& mapper =
2911 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2912 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002913 // Initial metastate is AMETA_NONE.
2914 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002915
2916 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002918 NotifyKeyArgs args;
2919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2920 ASSERT_EQ(DEVICE_ID, args.deviceId);
2921 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2922 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2923 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2924 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2925 ASSERT_EQ(KEY_HOME, args.scanCode);
2926 ASSERT_EQ(AMETA_NONE, args.metaState);
2927 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2928 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2929 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2930
2931 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002932 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2934 ASSERT_EQ(DEVICE_ID, args.deviceId);
2935 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2936 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2937 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2938 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2939 ASSERT_EQ(KEY_HOME, args.scanCode);
2940 ASSERT_EQ(AMETA_NONE, args.metaState);
2941 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2942 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2943 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2944
2945 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2949 ASSERT_EQ(DEVICE_ID, args.deviceId);
2950 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2951 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2952 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2953 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2954 ASSERT_EQ(0, args.scanCode);
2955 ASSERT_EQ(AMETA_NONE, args.metaState);
2956 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2957 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2958 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2959
2960 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2962 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2964 ASSERT_EQ(DEVICE_ID, args.deviceId);
2965 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2966 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2967 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2968 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2969 ASSERT_EQ(0, args.scanCode);
2970 ASSERT_EQ(AMETA_NONE, args.metaState);
2971 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2972 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2973 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2974
2975 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2977 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2979 ASSERT_EQ(DEVICE_ID, args.deviceId);
2980 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2981 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2982 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2983 ASSERT_EQ(0, args.keyCode);
2984 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2985 ASSERT_EQ(AMETA_NONE, args.metaState);
2986 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2987 ASSERT_EQ(0U, args.policyFlags);
2988 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2989
2990 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2992 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2994 ASSERT_EQ(DEVICE_ID, args.deviceId);
2995 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2996 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2997 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2998 ASSERT_EQ(0, args.keyCode);
2999 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3000 ASSERT_EQ(AMETA_NONE, args.metaState);
3001 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3002 ASSERT_EQ(0U, args.policyFlags);
3003 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3004}
3005
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003006TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3007 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3008 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3009 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3010
3011 KeyboardInputMapper& mapper =
3012 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3013 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3014
3015 // Key down by scan code.
3016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3017 NotifyKeyArgs args;
3018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3019 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3020
3021 // Key up by scan code.
3022 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3024 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3025}
3026
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003027/**
3028 * Ensure that the readTime is set to the time when the EV_KEY is received.
3029 */
3030TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3031 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3032
3033 KeyboardInputMapper& mapper =
3034 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3035 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3036 NotifyKeyArgs args;
3037
3038 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003039 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3041 ASSERT_EQ(12, args.readTime);
3042
3043 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003044 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3046 ASSERT_EQ(15, args.readTime);
3047}
3048
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003050 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3051 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003052 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3053 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3054 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003055
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003056 KeyboardInputMapper& mapper =
3057 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3058 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059
Arthur Hung95f68612022-04-07 14:08:22 +08003060 // Initial metastate is AMETA_NONE.
3061 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003062
3063 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003064 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065 NotifyKeyArgs args;
3066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3067 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003068 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003069 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003070
3071 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003072 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3074 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003075 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003076
3077 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003078 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3080 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003081 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082
3083 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003084 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3086 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003087 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003088 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089}
3090
3091TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003092 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3093 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3094 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3095 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003097 KeyboardInputMapper& mapper =
3098 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3099 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100
Michael Wrighta9cf4192022-12-01 23:46:39 +00003101 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3103 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3104 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3105 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3106 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3107 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3108 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3109 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3110}
3111
3112TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003113 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3114 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3115 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3116 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003117
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003119 KeyboardInputMapper& mapper =
3120 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3121 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003122
Michael Wrighta9cf4192022-12-01 23:46:39 +00003123 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003124 ASSERT_NO_FATAL_FAILURE(
3125 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3126 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3127 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3128 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3129 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3130 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3131 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003132
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003133 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003134 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003135 ASSERT_NO_FATAL_FAILURE(
3136 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3137 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3138 AKEYCODE_DPAD_UP, DISPLAY_ID));
3139 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3140 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3141 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3142 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003143
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003144 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003145 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003146 ASSERT_NO_FATAL_FAILURE(
3147 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3148 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3149 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3150 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3151 AKEYCODE_DPAD_UP, DISPLAY_ID));
3152 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3153 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003154
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003155 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003156 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003157 ASSERT_NO_FATAL_FAILURE(
3158 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3159 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3160 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3161 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3162 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3163 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3164 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003165
3166 // Special case: if orientation changes while key is down, we still emit the same keycode
3167 // in the key up as we did in the key down.
3168 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003169 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003170 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3173 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3174 ASSERT_EQ(KEY_UP, args.scanCode);
3175 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3176
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003177 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003178 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3181 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3182 ASSERT_EQ(KEY_UP, args.scanCode);
3183 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3184}
3185
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003186TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3187 // If the keyboard is not orientation aware,
3188 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003189 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003190
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003191 KeyboardInputMapper& mapper =
3192 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3193 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003194 NotifyKeyArgs args;
3195
3196 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3201 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3202
Michael Wrighta9cf4192022-12-01 23:46:39 +00003203 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3208 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3209}
3210
3211TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3212 // If the keyboard is orientation aware,
3213 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003214 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003215
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003216 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003217 KeyboardInputMapper& mapper =
3218 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3219 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003220 NotifyKeyArgs args;
3221
3222 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3223 // ^--- already checked by the previous test
3224
Michael Wrighta9cf4192022-12-01 23:46:39 +00003225 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003226 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003227 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003229 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3231 ASSERT_EQ(DISPLAY_ID, args.displayId);
3232
3233 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003234 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003235 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003236 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003237 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003239 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3241 ASSERT_EQ(newDisplayId, args.displayId);
3242}
3243
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003245 KeyboardInputMapper& mapper =
3246 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3247 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003249 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003250 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003252 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003253 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003254}
3255
Philip Junker4af3b3d2021-12-14 10:36:55 +01003256TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3257 KeyboardInputMapper& mapper =
3258 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3259 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3260
3261 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3262 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3263 << "If a mapping is available, the result is equal to the mapping";
3264
3265 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3266 << "If no mapping is available, the result is the key location";
3267}
3268
Michael Wrightd02c5b62014-02-10 15:10:22 -08003269TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003270 KeyboardInputMapper& mapper =
3271 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3272 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003274 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003275 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003277 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003278 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003279}
3280
3281TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003282 KeyboardInputMapper& mapper =
3283 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3284 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003286 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003287
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003289 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003290 ASSERT_TRUE(flags[0]);
3291 ASSERT_FALSE(flags[1]);
3292}
3293
3294TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003295 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3296 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3297 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3298 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3299 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3300 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003301
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003302 KeyboardInputMapper& mapper =
3303 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3304 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003305 // Initial metastate is AMETA_NONE.
3306 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003307
3308 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003309 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3310 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3311 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312
3313 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003316 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3317 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3318 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003319 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320
3321 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003322 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003324 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3325 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3326 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003327 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328
3329 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003330 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003332 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3333 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3334 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003335 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003336
3337 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003338 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003340 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3341 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3342 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003343 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003344
3345 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003346 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003348 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3349 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3350 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003351 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352
3353 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3355 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003356 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3357 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3358 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003359 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360}
3361
Chris Yea52ade12020-08-27 16:49:20 -07003362TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3363 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3364 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3365 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3366 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3367
3368 KeyboardInputMapper& mapper =
3369 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3370 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3371
Chris Yea52ade12020-08-27 16:49:20 -07003372 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003373 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003374 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3375 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3376 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3377 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3378
3379 NotifyKeyArgs args;
3380 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3383 ASSERT_EQ(AMETA_NONE, args.metaState);
3384 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3385 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3386 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3387
3388 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003389 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3391 ASSERT_EQ(AMETA_NONE, args.metaState);
3392 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3393 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3394 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3395}
3396
Arthur Hung2c9a3342019-07-23 14:18:59 +08003397TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3398 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003399 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3400 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3401 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3402 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003403
3404 // keyboard 2.
3405 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003406 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003407 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003408 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003409 std::shared_ptr<InputDevice> device2 =
3410 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003411 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003412
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003413 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3414 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3415 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3416 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003417
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003418 KeyboardInputMapper& mapper =
3419 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3420 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003421
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003422 KeyboardInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00003423 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3424 mFakePolicy->getReaderConfiguration(),
3425 AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003426 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003427 std::list<NotifyArgs> unused =
3428 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003429 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003430 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003431
3432 // Prepared displays and associated info.
3433 constexpr uint8_t hdmi1 = 0;
3434 constexpr uint8_t hdmi2 = 1;
3435 const std::string SECONDARY_UNIQUE_ID = "local:1";
3436
3437 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3438 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3439
3440 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003441 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003442 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003443 ASSERT_FALSE(device2->isEnabled());
3444
3445 // Prepare second display.
3446 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003447 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003448 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003449 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003450 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003451 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003452 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003453 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003454
3455 // Device should be enabled after the associated display is found.
3456 ASSERT_TRUE(mDevice->isEnabled());
3457 ASSERT_TRUE(device2->isEnabled());
3458
3459 // Test pad key events
3460 ASSERT_NO_FATAL_FAILURE(
3461 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3462 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3463 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3464 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3465 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3466 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3467 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3468
3469 ASSERT_NO_FATAL_FAILURE(
3470 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3471 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3472 AKEYCODE_DPAD_RIGHT, newDisplayId));
3473 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3474 AKEYCODE_DPAD_DOWN, newDisplayId));
3475 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3476 AKEYCODE_DPAD_LEFT, newDisplayId));
3477}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003478
arthurhungc903df12020-08-11 15:08:42 +08003479TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3480 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3481 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3482 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3483 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3484 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3485 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3486
3487 KeyboardInputMapper& mapper =
3488 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3489 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003490 // Initial metastate is AMETA_NONE.
3491 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003492
3493 // Initialization should have turned all of the lights off.
3494 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3495 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3496 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3497
3498 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003499 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003501 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3502 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3503
3504 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003505 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3506 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003507 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3508 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3509
3510 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003511 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3512 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003513 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3514 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3515
3516 mFakeEventHub->removeDevice(EVENTHUB_ID);
3517 mReader->loopOnce();
3518
3519 // keyboard 2 should default toggle keys.
3520 const std::string USB2 = "USB2";
3521 const std::string DEVICE_NAME2 = "KEYBOARD2";
3522 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3523 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3524 std::shared_ptr<InputDevice> device2 =
3525 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003526 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003527 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3528 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3529 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3530 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3531 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3532 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3533
arthurhung6fe95782020-10-05 22:41:16 +08003534 KeyboardInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00003535 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3536 mFakePolicy->getReaderConfiguration(),
3537 AINPUT_SOURCE_KEYBOARD,
arthurhung6fe95782020-10-05 22:41:16 +08003538 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003539 std::list<NotifyArgs> unused =
3540 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003541 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003542 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003543
3544 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3545 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3546 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003547 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3548 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003549}
3550
Arthur Hungcb40a002021-08-03 14:31:01 +00003551TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3552 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3553 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3554 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3555
3556 // Suppose we have two mappers. (DPAD + KEYBOARD)
3557 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3558 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3559 KeyboardInputMapper& mapper =
3560 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3561 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003562 // Initial metastate is AMETA_NONE.
3563 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003564
3565 mReader->toggleCapsLockState(DEVICE_ID);
3566 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3567}
3568
Arthur Hungfb3cc112022-04-13 07:39:50 +00003569TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3570 // keyboard 1.
3571 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3572 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3573 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3574 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3575 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3576 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3577
3578 KeyboardInputMapper& mapper1 =
3579 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3580 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3581
3582 // keyboard 2.
3583 const std::string USB2 = "USB2";
3584 const std::string DEVICE_NAME2 = "KEYBOARD2";
3585 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3586 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3587 std::shared_ptr<InputDevice> device2 =
3588 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3589 ftl::Flags<InputDeviceClass>(0));
3590 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3591 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3592 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3593 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3594 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3595 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3596
3597 KeyboardInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00003598 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3599 mFakePolicy->getReaderConfiguration(),
3600 AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003601 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003602 std::list<NotifyArgs> unused =
3603 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003604 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003605 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003606
Arthur Hung95f68612022-04-07 14:08:22 +08003607 // Initial metastate is AMETA_NONE.
3608 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3609 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3610
3611 // Toggle num lock on and off.
3612 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3613 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003614 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3615 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3616 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3617
3618 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3619 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3620 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3621 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3622 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3623
3624 // Toggle caps lock on and off.
3625 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3626 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3627 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3628 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3629 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3630
3631 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3632 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3633 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3634 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3635 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3636
3637 // Toggle scroll lock on and off.
3638 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3639 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3640 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3641 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3642 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3643
3644 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3645 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3646 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3647 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3648 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3649}
3650
Arthur Hung2141d542022-08-23 07:45:21 +00003651TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3652 const int32_t USAGE_A = 0x070004;
3653 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3654 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3655
3656 KeyboardInputMapper& mapper =
3657 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3658 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3659 // Key down by scan code.
3660 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3661 NotifyKeyArgs args;
3662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3663 ASSERT_EQ(DEVICE_ID, args.deviceId);
3664 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3665 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3666 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3667 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3668 ASSERT_EQ(KEY_HOME, args.scanCode);
3669 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3670
3671 // Disable device, it should synthesize cancellation events for down events.
3672 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003673 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00003674
3675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3676 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3677 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3678 ASSERT_EQ(KEY_HOME, args.scanCode);
3679 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3680}
3681
Zixuan Qufecb6062022-11-12 04:44:31 +00003682TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00003683 mDevice->addMapper<KeyboardInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
3684 AINPUT_SOURCE_KEYBOARD,
Zixuan Qufecb6062022-11-12 04:44:31 +00003685 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3686 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003687 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3688 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00003689
3690 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3691
3692 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003693 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00003694
3695 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3696 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3697 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3698 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3699 deviceInfo.getKeyboardLayoutInfo()->layoutType);
3700}
3701
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003702TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3703 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3704 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3705
3706 // Configuration
3707 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3708 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3709 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003710 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003711
3712 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3713 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3714}
3715
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003716// --- KeyboardInputMapperTest_ExternalDevice ---
3717
3718class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3719protected:
Chris Yea52ade12020-08-27 16:49:20 -07003720 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003721};
3722
3723TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003724 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3725 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003726
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003727 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3728 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3729 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3730 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003731
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003732 KeyboardInputMapper& mapper =
3733 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3734 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003735
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003736 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003737 NotifyKeyArgs args;
3738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3739 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3740
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003741 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3743 ASSERT_EQ(uint32_t(0), args.policyFlags);
3744
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003745 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003747 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003748
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003749 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3751 ASSERT_EQ(uint32_t(0), args.policyFlags);
3752
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3755 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3756
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003757 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3759 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3760}
3761
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003762TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003763 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003764
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003765 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3766 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3767 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003768
Powei Fengd041c5d2019-05-03 17:11:33 -07003769 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003770 KeyboardInputMapper& mapper =
3771 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3772 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003773
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003775 NotifyKeyArgs args;
3776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3777 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3778
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003779 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3781 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3782
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3785 ASSERT_EQ(uint32_t(0), args.policyFlags);
3786
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003787 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3789 ASSERT_EQ(uint32_t(0), args.policyFlags);
3790
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3793 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3794
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003795 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3797 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3798}
3799
Michael Wrightd02c5b62014-02-10 15:10:22 -08003800// --- CursorInputMapperTest ---
3801
3802class CursorInputMapperTest : public InputMapperTest {
3803protected:
3804 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3805
Michael Wright17db18e2020-06-26 20:51:44 +01003806 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003807
Chris Yea52ade12020-08-27 16:49:20 -07003808 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003809 InputMapperTest::SetUp();
3810
Michael Wright17db18e2020-06-26 20:51:44 +01003811 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003812 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003813 }
3814
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003815 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3816 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003817
Michael Wrighta9cf4192022-12-01 23:46:39 +00003818 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003819 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3820 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3821 }
3822
3823 void prepareSecondaryDisplay() {
3824 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003825 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003826 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003827 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003828
3829 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3830 float pressure) {
3831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3832 0.0f, 0.0f, 0.0f, EPSILON));
3833 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003834};
3835
3836const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3837
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003838void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3839 int32_t originalY, int32_t rotatedX,
3840 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003841 NotifyMotionArgs args;
3842
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003843 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3844 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3845 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3847 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003848 ASSERT_NO_FATAL_FAILURE(
3849 assertCursorPointerCoords(args.pointerCoords[0],
3850 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3851 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003852}
3853
3854TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003855 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003856 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003857
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003858 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003859}
3860
3861TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003862 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003863 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003864
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003865 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003866}
3867
3868TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003869 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003870 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003871
3872 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003873 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003874
3875 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003876 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3877 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003878 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3879 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3880
3881 // When the bounds are set, then there should be a valid motion range.
3882 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3883
3884 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003885 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003886
3887 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3888 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3889 1, 800 - 1, 0.0f, 0.0f));
3890 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3891 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3892 2, 480 - 1, 0.0f, 0.0f));
3893 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3894 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3895 0.0f, 1.0f, 0.0f, 0.0f));
3896}
3897
3898TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003899 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003900 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003901
3902 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003903 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003904
3905 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3906 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3907 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3908 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3909 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3910 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3911 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3912 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3913 0.0f, 1.0f, 0.0f, 0.0f));
3914}
3915
3916TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003917 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003918 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003919
arthurhungdcef2dc2020-08-11 14:47:50 +08003920 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003921
3922 NotifyMotionArgs args;
3923
3924 // Button press.
3925 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003926 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3927 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3929 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3930 ASSERT_EQ(DEVICE_ID, args.deviceId);
3931 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3932 ASSERT_EQ(uint32_t(0), args.policyFlags);
3933 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3934 ASSERT_EQ(0, args.flags);
3935 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3936 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3937 ASSERT_EQ(0, args.edgeFlags);
3938 ASSERT_EQ(uint32_t(1), args.pointerCount);
3939 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003940 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003941 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3943 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3944 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3945
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3947 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3948 ASSERT_EQ(DEVICE_ID, args.deviceId);
3949 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3950 ASSERT_EQ(uint32_t(0), args.policyFlags);
3951 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3952 ASSERT_EQ(0, args.flags);
3953 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3954 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3955 ASSERT_EQ(0, args.edgeFlags);
3956 ASSERT_EQ(uint32_t(1), args.pointerCount);
3957 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003958 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003959 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003960 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3961 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3962 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3963
Michael Wrightd02c5b62014-02-10 15:10:22 -08003964 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003965 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3966 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3968 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3969 ASSERT_EQ(DEVICE_ID, args.deviceId);
3970 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3971 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003972 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3973 ASSERT_EQ(0, args.flags);
3974 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3975 ASSERT_EQ(0, args.buttonState);
3976 ASSERT_EQ(0, args.edgeFlags);
3977 ASSERT_EQ(uint32_t(1), args.pointerCount);
3978 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003979 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003980 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003981 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3982 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3983 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3984
3985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3986 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3987 ASSERT_EQ(DEVICE_ID, args.deviceId);
3988 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3989 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003990 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3991 ASSERT_EQ(0, args.flags);
3992 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3993 ASSERT_EQ(0, args.buttonState);
3994 ASSERT_EQ(0, args.edgeFlags);
3995 ASSERT_EQ(uint32_t(1), args.pointerCount);
3996 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003997 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003998 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003999 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4000 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4001 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4002}
4003
4004TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004005 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004006 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007
4008 NotifyMotionArgs args;
4009
4010 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004011 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4014 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004015 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4016 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4017 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004018
4019 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
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], 0.0f,
4025 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004026}
4027
4028TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004029 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004030 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004031
4032 NotifyMotionArgs args;
4033
4034 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4038 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004039 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004040
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4042 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004043 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004044
Michael Wrightd02c5b62014-02-10 15:10:22 -08004045 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004046 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004049 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004050 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004051
4052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004053 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004054 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004055}
4056
4057TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004058 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004059 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004060
4061 NotifyMotionArgs args;
4062
4063 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004064 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4065 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4066 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4069 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004070 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4071 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4072 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004073
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4075 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004076 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4077 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4078 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004079
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004081 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4085 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004086 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4087 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4088 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089
4090 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004091 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004094 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004095 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004096
4097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004098 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004099 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004100}
4101
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004102TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004103 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004104 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004105 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4106 // need to be rotated.
4107 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004108 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109
Michael Wrighta9cf4192022-12-01 23:46:39 +00004110 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004111 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4112 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4113 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4114 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4115 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4116 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4117 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4118 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4119}
4120
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004121TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004122 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004123 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004124 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4125 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004126 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004127
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004128 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004129 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4131 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4132 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4133 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4134 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4135 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4136 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4137 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4138
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004139 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004140 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004141 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4142 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4143 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4144 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4145 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4146 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4147 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4148 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004149
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004150 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004151 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4153 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4154 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4155 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4156 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4157 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4158 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4159 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4160
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004161 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004162 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004163 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4164 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4165 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4166 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4167 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4168 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4169 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4170 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004171}
4172
4173TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004174 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004175 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004176
4177 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4178 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179
4180 NotifyMotionArgs motionArgs;
4181 NotifyKeyArgs keyArgs;
4182
4183 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4187 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4188 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004189 ASSERT_NO_FATAL_FAILURE(
4190 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4193 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4194 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004195 ASSERT_NO_FATAL_FAILURE(
4196 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004197
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004198 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004201 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004203 ASSERT_NO_FATAL_FAILURE(
4204 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004205
4206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004207 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004209 ASSERT_NO_FATAL_FAILURE(
4210 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004211
4212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004214 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004215 ASSERT_NO_FATAL_FAILURE(
4216 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217
4218 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004219 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4220 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4221 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4223 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4224 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004225 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004226 ASSERT_NO_FATAL_FAILURE(
4227 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4230 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4231 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004232 ASSERT_NO_FATAL_FAILURE(
4233 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004234
4235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4236 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4237 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004238 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004239 ASSERT_NO_FATAL_FAILURE(
4240 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004241
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004242 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4243 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004245 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004246 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004247 ASSERT_NO_FATAL_FAILURE(
4248 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004249
4250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004252 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004253 ASSERT_NO_FATAL_FAILURE(
4254 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004255
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004256 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4257 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004259 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4260 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004261 ASSERT_NO_FATAL_FAILURE(
4262 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004265
4266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004269 ASSERT_NO_FATAL_FAILURE(
4270 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004271
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4273 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004275 ASSERT_NO_FATAL_FAILURE(
4276 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277
4278 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004279 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4282 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4283 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004284
Michael Wrightd02c5b62014-02-10 15:10:22 -08004285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004286 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004287 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004288 ASSERT_NO_FATAL_FAILURE(
4289 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004290
4291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4292 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4293 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004294 ASSERT_NO_FATAL_FAILURE(
4295 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004301 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004302 ASSERT_NO_FATAL_FAILURE(
4303 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004304
4305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004307 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004308
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004309 ASSERT_NO_FATAL_FAILURE(
4310 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
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_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 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_BACK, 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_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004325 ASSERT_NO_FATAL_FAILURE(
4326 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004327
4328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4330 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004331 ASSERT_NO_FATAL_FAILURE(
4332 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004333
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004337 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004339 ASSERT_NO_FATAL_FAILURE(
4340 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004341
4342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4343 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4344 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004345 ASSERT_NO_FATAL_FAILURE(
4346 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004347
Michael Wrightd02c5b62014-02-10 15:10:22 -08004348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4349 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4350 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4351
4352 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004353 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4356 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4357 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004358
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004360 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004361 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004362 ASSERT_NO_FATAL_FAILURE(
4363 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004364
4365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4366 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4367 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004368 ASSERT_NO_FATAL_FAILURE(
4369 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004371 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004374 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004376 ASSERT_NO_FATAL_FAILURE(
4377 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004378
4379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4380 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4381 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004382 ASSERT_NO_FATAL_FAILURE(
4383 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004384
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4386 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4387 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4388
4389 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4391 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4393 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4394 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004395
Michael Wrightd02c5b62014-02-10 15:10:22 -08004396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004397 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004399 ASSERT_NO_FATAL_FAILURE(
4400 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004401
4402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4403 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4404 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004405 ASSERT_NO_FATAL_FAILURE(
4406 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004411 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004412 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004413 ASSERT_NO_FATAL_FAILURE(
4414 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004415
4416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4417 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4418 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004419 ASSERT_NO_FATAL_FAILURE(
4420 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004421
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4423 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4424 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4425}
4426
4427TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004428 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004429 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430
4431 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4432 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004433
4434 NotifyMotionArgs args;
4435
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);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004440 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4441 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4443 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 +00004444 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004445}
4446
4447TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004448 addConfigurationProperty("cursor.mode", "pointer");
4449 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004450 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004451
4452 NotifyDeviceResetArgs resetArgs;
4453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4454 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4455 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4456
4457 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4458 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004459
4460 NotifyMotionArgs args;
4461
4462 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004463 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4464 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4465 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4467 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4470 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 +00004471 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004472
4473 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4477 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4478 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4479 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4480 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4482 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4483 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4485 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4486
4487 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004488 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4489 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4491 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4492 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4494 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4496 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4497 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4499 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4500
4501 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4503 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4504 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4506 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4507 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4509 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 +00004510 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004511
4512 // Disable pointer capture and check that the device generation got bumped
4513 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004514 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004515 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004516 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004517 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004518
4519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004520 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4521
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004522 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4523 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4524 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4526 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4529 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 +00004530 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531}
4532
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004533/**
4534 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4535 * pointer acceleration or speed processing should not be applied.
4536 */
4537TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4538 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004539 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4540 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004541 mFakePolicy->setVelocityControlParams(testParams);
4542 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4543
4544 NotifyDeviceResetArgs resetArgs;
4545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4546 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4547 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4548
4549 NotifyMotionArgs args;
4550
4551 // Move and verify scale is applied.
4552 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4554 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4556 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4557 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4558 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4559 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4560 ASSERT_GT(relX, 10);
4561 ASSERT_GT(relY, 20);
4562
4563 // Enable Pointer Capture
4564 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004565 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004566 NotifyPointerCaptureChangedArgs captureArgs;
4567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4568 ASSERT_TRUE(captureArgs.request.enable);
4569
4570 // Move and verify scale is not applied.
4571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4572 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4573 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4575 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4576 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4577 ASSERT_EQ(10, args.pointerCoords[0].getX());
4578 ASSERT_EQ(20, args.pointerCoords[0].getY());
4579}
4580
Prabir Pradhan208360b2022-06-24 18:37:04 +00004581TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4582 addConfigurationProperty("cursor.mode", "pointer");
4583 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4584
4585 NotifyDeviceResetArgs resetArgs;
4586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4587 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4588 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4589
4590 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004591 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004592
4593 NotifyMotionArgs args;
4594
4595 // Verify that the coordinates are rotated.
4596 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4597 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4598 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4600 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4601 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4602 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4603 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4604
4605 // Enable Pointer Capture.
4606 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004607 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004608 NotifyPointerCaptureChangedArgs captureArgs;
4609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4610 ASSERT_TRUE(captureArgs.request.enable);
4611
4612 // Move and verify rotation is not applied.
4613 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);
4616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4617 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4619 ASSERT_EQ(10, args.pointerCoords[0].getX());
4620 ASSERT_EQ(20, args.pointerCoords[0].getY());
4621}
4622
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004623TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004624 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004625
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004626 // 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 // The InputDevice is not associated with any display.
4631 prepareSecondaryDisplay();
4632 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004633 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004634
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004635 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004636 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004637
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004638 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004639 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4640 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004643 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4644 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4645 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004646 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004647}
4648
4649TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
4650 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4651
4652 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004653 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004654
4655 // Set up the secondary display as the display on which the pointer should be shown,
4656 // and associate the InputDevice with the secondary display.
4657 prepareSecondaryDisplay();
4658 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4659 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004660 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004661
4662 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4663 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004664
4665 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4666 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4667 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004669 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4670 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4671 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004672 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004673}
4674
4675TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
4676 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4677
4678 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004679 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004680 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4681
4682 // Associate the InputDevice with the secondary display.
4683 prepareSecondaryDisplay();
4684 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004685 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004686
4687 // The mapper should not generate any events because it is associated with a display that is
4688 // different from the pointer display.
4689 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004693}
4694
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004695// --- BluetoothCursorInputMapperTest ---
4696
4697class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4698protected:
4699 void SetUp() override {
4700 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4701
4702 mFakePointerController = std::make_shared<FakePointerController>();
4703 mFakePolicy->setPointerController(mFakePointerController);
4704 }
4705};
4706
4707TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4708 addConfigurationProperty("cursor.mode", "pointer");
4709 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4710
4711 nsecs_t kernelEventTime = ARBITRARY_TIME;
4712 nsecs_t expectedEventTime = ARBITRARY_TIME;
4713 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4714 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4716 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4717 WithEventTime(expectedEventTime))));
4718
4719 // Process several events that come in quick succession, according to their timestamps.
4720 for (int i = 0; i < 3; i++) {
4721 constexpr static nsecs_t delta = ms2ns(1);
4722 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4723 kernelEventTime += delta;
4724 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4725
4726 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4727 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4729 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4730 WithEventTime(expectedEventTime))));
4731 }
4732}
4733
4734TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4735 addConfigurationProperty("cursor.mode", "pointer");
4736 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4737
4738 nsecs_t expectedEventTime = ARBITRARY_TIME;
4739 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4742 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4743 WithEventTime(expectedEventTime))));
4744
4745 // Process several events with the same timestamp from the kernel.
4746 // Ensure that we do not generate events too far into the future.
4747 constexpr static int32_t numEvents =
4748 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4749 for (int i = 0; i < numEvents; i++) {
4750 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4751
4752 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4753 process(mapper, ARBITRARY_TIME, 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
4759 // By processing more events with the same timestamp, we should not generate events with a
4760 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4761 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4762 for (int i = 0; i < 3; i++) {
4763 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4764 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4766 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4767 WithEventTime(cappedEventTime))));
4768 }
4769}
4770
4771TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4772 addConfigurationProperty("cursor.mode", "pointer");
4773 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4774
4775 nsecs_t kernelEventTime = ARBITRARY_TIME;
4776 nsecs_t expectedEventTime = ARBITRARY_TIME;
4777 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4778 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4780 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4781 WithEventTime(expectedEventTime))));
4782
4783 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4784 // smoothening is not needed, its timestamp is not affected.
4785 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4786 expectedEventTime = kernelEventTime;
4787
4788 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4789 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4791 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4792 WithEventTime(expectedEventTime))));
4793}
4794
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795// --- TouchInputMapperTest ---
4796
4797class TouchInputMapperTest : public InputMapperTest {
4798protected:
4799 static const int32_t RAW_X_MIN;
4800 static const int32_t RAW_X_MAX;
4801 static const int32_t RAW_Y_MIN;
4802 static const int32_t RAW_Y_MAX;
4803 static const int32_t RAW_TOUCH_MIN;
4804 static const int32_t RAW_TOUCH_MAX;
4805 static const int32_t RAW_TOOL_MIN;
4806 static const int32_t RAW_TOOL_MAX;
4807 static const int32_t RAW_PRESSURE_MIN;
4808 static const int32_t RAW_PRESSURE_MAX;
4809 static const int32_t RAW_ORIENTATION_MIN;
4810 static const int32_t RAW_ORIENTATION_MAX;
4811 static const int32_t RAW_DISTANCE_MIN;
4812 static const int32_t RAW_DISTANCE_MAX;
4813 static const int32_t RAW_TILT_MIN;
4814 static const int32_t RAW_TILT_MAX;
4815 static const int32_t RAW_ID_MIN;
4816 static const int32_t RAW_ID_MAX;
4817 static const int32_t RAW_SLOT_MIN;
4818 static const int32_t RAW_SLOT_MAX;
4819 static const float X_PRECISION;
4820 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004821 static const float X_PRECISION_VIRTUAL;
4822 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823
4824 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004825 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826
4827 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4828
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004829 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004830 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004831
Michael Wrightd02c5b62014-02-10 15:10:22 -08004832 enum Axes {
4833 POSITION = 1 << 0,
4834 TOUCH = 1 << 1,
4835 TOOL = 1 << 2,
4836 PRESSURE = 1 << 3,
4837 ORIENTATION = 1 << 4,
4838 MINOR = 1 << 5,
4839 ID = 1 << 6,
4840 DISTANCE = 1 << 7,
4841 TILT = 1 << 8,
4842 SLOT = 1 << 9,
4843 TOOL_TYPE = 1 << 10,
4844 };
4845
Michael Wrighta9cf4192022-12-01 23:46:39 +00004846 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004847 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004848 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004849 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004850 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004851 int32_t toRawX(float displayX);
4852 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004853 int32_t toRotatedRawX(float displayX);
4854 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004855 float toCookedX(float rawX, float rawY);
4856 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004857 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004858 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004859 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004860 float toDisplayY(int32_t rawY, int32_t displayHeight);
4861
Michael Wrightd02c5b62014-02-10 15:10:22 -08004862};
4863
4864const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4865const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4866const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4867const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4868const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4869const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4870const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4871const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004872const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4873const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004874const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4875const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4876const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4877const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4878const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4879const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4880const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4881const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4882const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4883const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4884const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4885const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004886const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4887 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4888const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4889 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004890const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4891 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004892
4893const float TouchInputMapperTest::GEOMETRIC_SCALE =
4894 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4895 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4896
4897const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4898 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4899 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4900};
4901
Michael Wrighta9cf4192022-12-01 23:46:39 +00004902void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004903 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4904 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004905}
4906
4907void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4908 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004909 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004910}
4911
Michael Wrighta9cf4192022-12-01 23:46:39 +00004912void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004913 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4914 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4915 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004916}
4917
Michael Wrightd02c5b62014-02-10 15:10:22 -08004918void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004919 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4920 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4921 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4922 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004923}
4924
Jason Gerecke489fda82012-09-07 17:19:40 -07004925void TouchInputMapperTest::prepareLocationCalibration() {
4926 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4927}
4928
Michael Wrightd02c5b62014-02-10 15:10:22 -08004929int32_t TouchInputMapperTest::toRawX(float displayX) {
4930 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4931}
4932
4933int32_t TouchInputMapperTest::toRawY(float displayY) {
4934 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4935}
4936
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004937int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4938 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4939}
4940
4941int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4942 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4943}
4944
Jason Gerecke489fda82012-09-07 17:19:40 -07004945float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4946 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4947 return rawX;
4948}
4949
4950float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4951 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4952 return rawY;
4953}
4954
Michael Wrightd02c5b62014-02-10 15:10:22 -08004955float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004956 return toDisplayX(rawX, DISPLAY_WIDTH);
4957}
4958
4959float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4960 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004961}
4962
4963float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004964 return toDisplayY(rawY, DISPLAY_HEIGHT);
4965}
4966
4967float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4968 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004969}
4970
4971
4972// --- SingleTouchInputMapperTest ---
4973
4974class SingleTouchInputMapperTest : public TouchInputMapperTest {
4975protected:
4976 void prepareButtons();
4977 void prepareAxes(int axes);
4978
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004979 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4980 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4981 void processUp(SingleTouchInputMapper& mappery);
4982 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4983 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4984 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4985 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4986 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4987 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004988};
4989
4990void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004991 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992}
4993
4994void SingleTouchInputMapperTest::prepareAxes(int axes) {
4995 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004996 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4997 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004998 }
4999 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005000 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5001 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002 }
5003 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005004 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5005 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005006 }
5007 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005008 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5009 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005010 }
5011 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005012 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5013 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005014 }
5015}
5016
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005017void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005021}
5022
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005023void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5025 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005026}
5027
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005028void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030}
5031
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005032void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005033 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005034}
5035
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005036void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5037 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005039}
5040
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005041void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005042 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005043}
5044
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005045void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5046 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5048 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005049}
5050
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005051void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5052 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005054}
5055
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005056void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005057 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005058}
5059
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061 prepareButtons();
5062 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005063 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005064
Josep del Río2d8c79a2023-01-23 19:33:50 +00005065 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005066}
5067
Michael Wrightd02c5b62014-02-10 15:10:22 -08005068TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005069 prepareButtons();
5070 prepareAxes(POSITION);
5071 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005072 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005073
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005074 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005075}
5076
5077TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005078 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005079 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080 prepareButtons();
5081 prepareAxes(POSITION);
5082 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005083 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005084
5085 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005086 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087
5088 // Virtual key is down.
5089 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5090 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5091 processDown(mapper, x, y);
5092 processSync(mapper);
5093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5094
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005095 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005096
5097 // Virtual key is up.
5098 processUp(mapper);
5099 processSync(mapper);
5100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5101
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005102 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005103}
5104
5105TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005106 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005107 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005108 prepareButtons();
5109 prepareAxes(POSITION);
5110 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005111 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005112
5113 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005114 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005115
5116 // Virtual key is down.
5117 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5118 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5119 processDown(mapper, x, y);
5120 processSync(mapper);
5121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5122
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005123 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005124
5125 // Virtual key is up.
5126 processUp(mapper);
5127 processSync(mapper);
5128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5129
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005130 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005131}
5132
5133TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005134 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005135 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005136 prepareButtons();
5137 prepareAxes(POSITION);
5138 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005139 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005140
Michael Wrightd02c5b62014-02-10 15:10:22 -08005141 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005142 ASSERT_TRUE(
5143 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005144 ASSERT_TRUE(flags[0]);
5145 ASSERT_FALSE(flags[1]);
5146}
5147
5148TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005149 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005150 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005151 prepareButtons();
5152 prepareAxes(POSITION);
5153 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005154 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005155
arthurhungdcef2dc2020-08-11 14:47:50 +08005156 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005157
5158 NotifyKeyArgs args;
5159
5160 // Press virtual key.
5161 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5162 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5163 processDown(mapper, x, y);
5164 processSync(mapper);
5165
5166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5167 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5168 ASSERT_EQ(DEVICE_ID, args.deviceId);
5169 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5170 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5171 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5172 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5173 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5174 ASSERT_EQ(KEY_HOME, args.scanCode);
5175 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5176 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5177
5178 // Release virtual key.
5179 processUp(mapper);
5180 processSync(mapper);
5181
5182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5183 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5184 ASSERT_EQ(DEVICE_ID, args.deviceId);
5185 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5186 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5187 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5188 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5189 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5190 ASSERT_EQ(KEY_HOME, args.scanCode);
5191 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5192 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5193
5194 // Should not have sent any motions.
5195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5196}
5197
5198TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005199 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005200 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005201 prepareButtons();
5202 prepareAxes(POSITION);
5203 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005204 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005205
arthurhungdcef2dc2020-08-11 14:47:50 +08005206 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005207
5208 NotifyKeyArgs keyArgs;
5209
5210 // Press virtual key.
5211 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5212 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5213 processDown(mapper, x, y);
5214 processSync(mapper);
5215
5216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5217 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5218 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5219 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5220 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5221 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5222 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5223 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5224 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5225 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5226 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5227
5228 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5229 // into the display area.
5230 y -= 100;
5231 processMove(mapper, x, y);
5232 processSync(mapper);
5233
5234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5235 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5236 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5237 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5238 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5239 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5240 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5241 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5242 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5243 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5244 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5245 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5246
5247 NotifyMotionArgs motionArgs;
5248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5249 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5250 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5251 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5252 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5253 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5254 ASSERT_EQ(0, motionArgs.flags);
5255 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5256 ASSERT_EQ(0, motionArgs.buttonState);
5257 ASSERT_EQ(0, motionArgs.edgeFlags);
5258 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5259 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005260 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5262 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5263 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5264 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5265 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5266
5267 // Keep moving out of bounds. Should generate a pointer move.
5268 y -= 50;
5269 processMove(mapper, x, y);
5270 processSync(mapper);
5271
5272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5273 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5274 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5275 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5276 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5278 ASSERT_EQ(0, motionArgs.flags);
5279 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5280 ASSERT_EQ(0, motionArgs.buttonState);
5281 ASSERT_EQ(0, motionArgs.edgeFlags);
5282 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5283 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005284 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5286 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5287 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5288 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5289 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5290
5291 // Release out of bounds. Should generate a pointer up.
5292 processUp(mapper);
5293 processSync(mapper);
5294
5295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5296 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5297 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5298 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5299 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5300 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5301 ASSERT_EQ(0, motionArgs.flags);
5302 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5303 ASSERT_EQ(0, motionArgs.buttonState);
5304 ASSERT_EQ(0, motionArgs.edgeFlags);
5305 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5306 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005307 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5309 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5310 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5311 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5312 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5313
5314 // Should not have sent any more keys or motions.
5315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5317}
5318
5319TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005320 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005321 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322 prepareButtons();
5323 prepareAxes(POSITION);
5324 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005325 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005326
arthurhungdcef2dc2020-08-11 14:47:50 +08005327 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005328
5329 NotifyMotionArgs motionArgs;
5330
5331 // Initially go down out of bounds.
5332 int32_t x = -10;
5333 int32_t y = -10;
5334 processDown(mapper, x, y);
5335 processSync(mapper);
5336
5337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5338
5339 // Move into the display area. Should generate a pointer down.
5340 x = 50;
5341 y = 75;
5342 processMove(mapper, x, y);
5343 processSync(mapper);
5344
5345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5346 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5347 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5348 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5349 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5350 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5351 ASSERT_EQ(0, motionArgs.flags);
5352 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5353 ASSERT_EQ(0, motionArgs.buttonState);
5354 ASSERT_EQ(0, motionArgs.edgeFlags);
5355 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5356 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005357 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5359 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5360 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5361 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5362 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5363
5364 // Release. Should generate a pointer up.
5365 processUp(mapper);
5366 processSync(mapper);
5367
5368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5369 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5370 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5371 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5372 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5373 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5374 ASSERT_EQ(0, motionArgs.flags);
5375 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5376 ASSERT_EQ(0, motionArgs.buttonState);
5377 ASSERT_EQ(0, motionArgs.edgeFlags);
5378 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5379 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005380 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005381 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5382 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5383 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5384 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5385 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5386
5387 // Should not have sent any more keys or motions.
5388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5390}
5391
Santos Cordonfa5cf462017-04-05 10:37:00 -07005392TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005393 addConfigurationProperty("touch.deviceType", "touchScreen");
5394 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5395
Michael Wrighta9cf4192022-12-01 23:46:39 +00005396 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005397 prepareButtons();
5398 prepareAxes(POSITION);
5399 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005400 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005401
arthurhungdcef2dc2020-08-11 14:47:50 +08005402 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005403
5404 NotifyMotionArgs motionArgs;
5405
5406 // Down.
5407 int32_t x = 100;
5408 int32_t y = 125;
5409 processDown(mapper, x, y);
5410 processSync(mapper);
5411
5412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5413 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5414 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5415 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5416 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5417 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5418 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5419 ASSERT_EQ(0, motionArgs.flags);
5420 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5421 ASSERT_EQ(0, motionArgs.buttonState);
5422 ASSERT_EQ(0, motionArgs.edgeFlags);
5423 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5424 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005425 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005426 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5427 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5428 1, 0, 0, 0, 0, 0, 0, 0));
5429 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5430 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5431 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5432
5433 // Move.
5434 x += 50;
5435 y += 75;
5436 processMove(mapper, x, y);
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_MOVE, 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);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005452 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005453 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 // Up.
5461 processUp(mapper);
5462 processSync(mapper);
5463
5464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5465 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5466 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5467 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5468 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5469 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5470 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5471 ASSERT_EQ(0, motionArgs.flags);
5472 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5473 ASSERT_EQ(0, motionArgs.buttonState);
5474 ASSERT_EQ(0, motionArgs.edgeFlags);
5475 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5476 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005477 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5479 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5480 1, 0, 0, 0, 0, 0, 0, 0));
5481 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5482 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5483 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5484
5485 // Should not have sent any more keys or motions.
5486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5488}
5489
Michael Wrightd02c5b62014-02-10 15:10:22 -08005490TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005491 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005492 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493 prepareButtons();
5494 prepareAxes(POSITION);
5495 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005496 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005497
arthurhungdcef2dc2020-08-11 14:47:50 +08005498 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005499
5500 NotifyMotionArgs motionArgs;
5501
5502 // Down.
5503 int32_t x = 100;
5504 int32_t y = 125;
5505 processDown(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_DOWN, 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);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005520 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005521 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 // Move.
5528 x += 50;
5529 y += 75;
5530 processMove(mapper, x, y);
5531 processSync(mapper);
5532
5533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5534 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5535 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5536 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5537 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5538 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5539 ASSERT_EQ(0, motionArgs.flags);
5540 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5541 ASSERT_EQ(0, motionArgs.buttonState);
5542 ASSERT_EQ(0, motionArgs.edgeFlags);
5543 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5544 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005545 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5547 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5548 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5549 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5550 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5551
5552 // Up.
5553 processUp(mapper);
5554 processSync(mapper);
5555
5556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5557 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5558 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5559 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5560 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5561 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5562 ASSERT_EQ(0, motionArgs.flags);
5563 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5564 ASSERT_EQ(0, motionArgs.buttonState);
5565 ASSERT_EQ(0, motionArgs.edgeFlags);
5566 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5567 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005568 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5570 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5571 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5572 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5573 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5574
5575 // Should not have sent any more keys or motions.
5576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5578}
5579
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005580TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005581 addConfigurationProperty("touch.deviceType", "touchScreen");
5582 prepareButtons();
5583 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005584 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5585 // need to be rotated. Touchscreens are orientation-aware by default.
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 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005591 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005592 processDown(mapper, toRawX(50), toRawY(75));
5593 processSync(mapper);
5594
5595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5596 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5597 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5598
5599 processUp(mapper);
5600 processSync(mapper);
5601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5602}
5603
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005604TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005605 addConfigurationProperty("touch.deviceType", "touchScreen");
5606 prepareButtons();
5607 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005608 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5609 // orientation-aware are affected by display rotation.
5610 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005611 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005612
5613 NotifyMotionArgs args;
5614
5615 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005616 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005617 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005618 processDown(mapper, toRawX(50), toRawY(75));
5619 processSync(mapper);
5620
5621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5622 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5623 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5624
5625 processUp(mapper);
5626 processSync(mapper);
5627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5628
5629 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005630 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005631 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005632 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005633 processSync(mapper);
5634
5635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5636 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5637 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5638
5639 processUp(mapper);
5640 processSync(mapper);
5641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5642
5643 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005644 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005645 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005646 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5647 processSync(mapper);
5648
5649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5650 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5651 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5652
5653 processUp(mapper);
5654 processSync(mapper);
5655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5656
5657 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005658 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005659 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005660 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005661 processSync(mapper);
5662
5663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5664 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5665 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5666
5667 processUp(mapper);
5668 processSync(mapper);
5669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5670}
5671
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005672TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5673 addConfigurationProperty("touch.deviceType", "touchScreen");
5674 prepareButtons();
5675 prepareAxes(POSITION);
5676 addConfigurationProperty("touch.orientationAware", "1");
5677 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5678 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005679 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005680 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5681 NotifyMotionArgs args;
5682
5683 // Orientation 0.
5684 processDown(mapper, toRawX(50), toRawY(75));
5685 processSync(mapper);
5686
5687 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5688 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5689 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5690
5691 processUp(mapper);
5692 processSync(mapper);
5693 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5694}
5695
5696TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5697 addConfigurationProperty("touch.deviceType", "touchScreen");
5698 prepareButtons();
5699 prepareAxes(POSITION);
5700 addConfigurationProperty("touch.orientationAware", "1");
5701 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5702 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005703 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005704 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5705 NotifyMotionArgs args;
5706
5707 // Orientation 90.
5708 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5709 processSync(mapper);
5710
5711 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5712 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5713 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5714
5715 processUp(mapper);
5716 processSync(mapper);
5717 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5718}
5719
5720TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5721 addConfigurationProperty("touch.deviceType", "touchScreen");
5722 prepareButtons();
5723 prepareAxes(POSITION);
5724 addConfigurationProperty("touch.orientationAware", "1");
5725 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5726 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005727 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005728 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5729 NotifyMotionArgs args;
5730
5731 // Orientation 180.
5732 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5733 processSync(mapper);
5734
5735 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5736 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5737 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5738
5739 processUp(mapper);
5740 processSync(mapper);
5741 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5742}
5743
5744TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5745 addConfigurationProperty("touch.deviceType", "touchScreen");
5746 prepareButtons();
5747 prepareAxes(POSITION);
5748 addConfigurationProperty("touch.orientationAware", "1");
5749 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5750 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005751 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005752 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5753 NotifyMotionArgs args;
5754
5755 // Orientation 270.
5756 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5757 processSync(mapper);
5758
5759 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5760 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5761 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5762
5763 processUp(mapper);
5764 processSync(mapper);
5765 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5766}
5767
5768TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5769 addConfigurationProperty("touch.deviceType", "touchScreen");
5770 prepareButtons();
5771 prepareAxes(POSITION);
5772 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5773 // orientation-aware are affected by display rotation.
5774 addConfigurationProperty("touch.orientationAware", "0");
5775 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5776 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5777
5778 NotifyMotionArgs args;
5779
5780 // Orientation 90, Rotation 0.
5781 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005782 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005783 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5784 processSync(mapper);
5785
5786 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5787 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5788 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5789
5790 processUp(mapper);
5791 processSync(mapper);
5792 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5793
5794 // Orientation 90, Rotation 90.
5795 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005796 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005797 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005798 processSync(mapper);
5799
5800 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5801 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5802 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5803
5804 processUp(mapper);
5805 processSync(mapper);
5806 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5807
5808 // Orientation 90, Rotation 180.
5809 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005810 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005811 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5812 processSync(mapper);
5813
5814 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5815 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5816 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5817
5818 processUp(mapper);
5819 processSync(mapper);
5820 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5821
5822 // Orientation 90, Rotation 270.
5823 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005824 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005825 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 -07005826 processSync(mapper);
5827
5828 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5829 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5830 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5831
5832 processUp(mapper);
5833 processSync(mapper);
5834 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5835}
5836
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005837TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5838 addConfigurationProperty("touch.deviceType", "touchScreen");
5839 prepareButtons();
5840 prepareAxes(POSITION);
5841 addConfigurationProperty("touch.orientationAware", "1");
5842 prepareDisplay(ui::ROTATION_0);
5843 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5844
5845 // Set a physical frame in the display viewport.
5846 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5847 viewport->physicalLeft = 20;
5848 viewport->physicalTop = 600;
5849 viewport->physicalRight = 30;
5850 viewport->physicalBottom = 610;
5851 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005852 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005853
5854 // Start the touch.
5855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5856 processSync(mapper);
5857
5858 // Expect all input starting outside the physical frame to be ignored.
5859 const std::array<Point, 6> outsidePoints = {
5860 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5861 for (const auto& p : outsidePoints) {
5862 processMove(mapper, toRawX(p.x), toRawY(p.y));
5863 processSync(mapper);
5864 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5865 }
5866
5867 // Move the touch into the physical frame.
5868 processMove(mapper, toRawX(25), toRawY(605));
5869 processSync(mapper);
5870 NotifyMotionArgs args;
5871 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5872 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5873 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5874 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5875
5876 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5877 for (const auto& p : outsidePoints) {
5878 processMove(mapper, toRawX(p.x), toRawY(p.y));
5879 processSync(mapper);
5880 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5881 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5882 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5883 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5884 }
5885
5886 processUp(mapper);
5887 processSync(mapper);
5888 EXPECT_NO_FATAL_FAILURE(
5889 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5890}
5891
Michael Wrightd02c5b62014-02-10 15:10:22 -08005892TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005893 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005894 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005895 prepareButtons();
5896 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005897 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005898
5899 // These calculations are based on the input device calibration documentation.
5900 int32_t rawX = 100;
5901 int32_t rawY = 200;
5902 int32_t rawPressure = 10;
5903 int32_t rawToolMajor = 12;
5904 int32_t rawDistance = 2;
5905 int32_t rawTiltX = 30;
5906 int32_t rawTiltY = 110;
5907
5908 float x = toDisplayX(rawX);
5909 float y = toDisplayY(rawY);
5910 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5911 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5912 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5913 float distance = float(rawDistance);
5914
5915 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5916 float tiltScale = M_PI / 180;
5917 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5918 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5919 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5920 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5921
5922 processDown(mapper, rawX, rawY);
5923 processPressure(mapper, rawPressure);
5924 processToolMajor(mapper, rawToolMajor);
5925 processDistance(mapper, rawDistance);
5926 processTilt(mapper, rawTiltX, rawTiltY);
5927 processSync(mapper);
5928
5929 NotifyMotionArgs args;
5930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5932 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5933 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5934}
5935
Jason Gerecke489fda82012-09-07 17:19:40 -07005936TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005937 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005938 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005939 prepareLocationCalibration();
5940 prepareButtons();
5941 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005942 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005943
5944 int32_t rawX = 100;
5945 int32_t rawY = 200;
5946
5947 float x = toDisplayX(toCookedX(rawX, rawY));
5948 float y = toDisplayY(toCookedY(rawX, rawY));
5949
5950 processDown(mapper, rawX, rawY);
5951 processSync(mapper);
5952
5953 NotifyMotionArgs args;
5954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5956 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5957}
5958
Michael Wrightd02c5b62014-02-10 15:10:22 -08005959TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005960 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005961 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005962 prepareButtons();
5963 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005964 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005965
5966 NotifyMotionArgs motionArgs;
5967 NotifyKeyArgs keyArgs;
5968
5969 processDown(mapper, 100, 200);
5970 processSync(mapper);
5971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5972 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5973 ASSERT_EQ(0, motionArgs.buttonState);
5974
5975 // press BTN_LEFT, release BTN_LEFT
5976 processKey(mapper, BTN_LEFT, 1);
5977 processSync(mapper);
5978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5979 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5980 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5981
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5983 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5984 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5985
Michael Wrightd02c5b62014-02-10 15:10:22 -08005986 processKey(mapper, BTN_LEFT, 0);
5987 processSync(mapper);
5988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005989 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005990 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005991
5992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005993 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005994 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005995
5996 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5997 processKey(mapper, BTN_RIGHT, 1);
5998 processKey(mapper, BTN_MIDDLE, 1);
5999 processSync(mapper);
6000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6001 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6002 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6003 motionArgs.buttonState);
6004
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6006 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6007 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6008
6009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6010 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6011 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6012 motionArgs.buttonState);
6013
Michael Wrightd02c5b62014-02-10 15:10:22 -08006014 processKey(mapper, BTN_RIGHT, 0);
6015 processSync(mapper);
6016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006017 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006018 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006019
6020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006021 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006022 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006023
6024 processKey(mapper, BTN_MIDDLE, 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);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006033
6034 // press BTN_BACK, release BTN_BACK
6035 processKey(mapper, BTN_BACK, 1);
6036 processSync(mapper);
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6038 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6039 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006040
Michael Wrightd02c5b62014-02-10 15:10:22 -08006041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006042 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006043 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6044
6045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6046 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6047 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006048
6049 processKey(mapper, BTN_BACK, 0);
6050 processSync(mapper);
6051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006052 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006053 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006054
6055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006056 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006057 ASSERT_EQ(0, motionArgs.buttonState);
6058
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6060 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6061 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6062
6063 // press BTN_SIDE, release BTN_SIDE
6064 processKey(mapper, BTN_SIDE, 1);
6065 processSync(mapper);
6066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6067 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6068 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006069
Michael Wrightd02c5b62014-02-10 15:10:22 -08006070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006072 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6073
6074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6075 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6076 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006077
6078 processKey(mapper, BTN_SIDE, 0);
6079 processSync(mapper);
6080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006081 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006082 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006083
6084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006085 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006086 ASSERT_EQ(0, motionArgs.buttonState);
6087
Michael Wrightd02c5b62014-02-10 15:10:22 -08006088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6089 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6090 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6091
6092 // press BTN_FORWARD, release BTN_FORWARD
6093 processKey(mapper, BTN_FORWARD, 1);
6094 processSync(mapper);
6095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6096 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6097 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006098
Michael Wrightd02c5b62014-02-10 15:10:22 -08006099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006101 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6102
6103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6104 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6105 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006106
6107 processKey(mapper, BTN_FORWARD, 0);
6108 processSync(mapper);
6109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006110 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006111 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006112
6113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006115 ASSERT_EQ(0, motionArgs.buttonState);
6116
Michael Wrightd02c5b62014-02-10 15:10:22 -08006117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6118 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6119 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6120
6121 // press BTN_EXTRA, release BTN_EXTRA
6122 processKey(mapper, BTN_EXTRA, 1);
6123 processSync(mapper);
6124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6125 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6126 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006127
Michael Wrightd02c5b62014-02-10 15:10:22 -08006128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006130 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6131
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6133 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6134 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006135
6136 processKey(mapper, BTN_EXTRA, 0);
6137 processSync(mapper);
6138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006139 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006140 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006141
6142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006143 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006144 ASSERT_EQ(0, motionArgs.buttonState);
6145
Michael Wrightd02c5b62014-02-10 15:10:22 -08006146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6147 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6148 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6149
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6151
Michael Wrightd02c5b62014-02-10 15:10:22 -08006152 // press BTN_STYLUS, release BTN_STYLUS
6153 processKey(mapper, BTN_STYLUS, 1);
6154 processSync(mapper);
6155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006157 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6158
6159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6160 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6161 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006162
6163 processKey(mapper, BTN_STYLUS, 0);
6164 processSync(mapper);
6165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006166 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006167 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006168
6169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006170 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006171 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006172
6173 // press BTN_STYLUS2, release BTN_STYLUS2
6174 processKey(mapper, BTN_STYLUS2, 1);
6175 processSync(mapper);
6176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6177 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006178 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6179
6180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6181 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6182 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006183
6184 processKey(mapper, BTN_STYLUS2, 0);
6185 processSync(mapper);
6186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006187 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006188 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006189
6190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006191 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006192 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006193
6194 // release touch
6195 processUp(mapper);
6196 processSync(mapper);
6197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6198 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6199 ASSERT_EQ(0, motionArgs.buttonState);
6200}
6201
6202TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006203 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006204 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006205 prepareButtons();
6206 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006207 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006208
6209 NotifyMotionArgs motionArgs;
6210
6211 // default tool type is finger
6212 processDown(mapper, 100, 200);
6213 processSync(mapper);
6214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6215 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006216 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006217
6218 // eraser
6219 processKey(mapper, BTN_TOOL_RUBBER, 1);
6220 processSync(mapper);
6221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006223 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006224
6225 // stylus
6226 processKey(mapper, BTN_TOOL_RUBBER, 0);
6227 processKey(mapper, BTN_TOOL_PEN, 1);
6228 processSync(mapper);
6229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6230 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006231 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006232
6233 // brush
6234 processKey(mapper, BTN_TOOL_PEN, 0);
6235 processKey(mapper, BTN_TOOL_BRUSH, 1);
6236 processSync(mapper);
6237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6238 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006239 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006240
6241 // pencil
6242 processKey(mapper, BTN_TOOL_BRUSH, 0);
6243 processKey(mapper, BTN_TOOL_PENCIL, 1);
6244 processSync(mapper);
6245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006247 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006248
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006249 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006250 processKey(mapper, BTN_TOOL_PENCIL, 0);
6251 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6252 processSync(mapper);
6253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6254 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006255 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006256
6257 // mouse
6258 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6259 processKey(mapper, BTN_TOOL_MOUSE, 1);
6260 processSync(mapper);
6261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6262 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006263 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006264
6265 // lens
6266 processKey(mapper, BTN_TOOL_MOUSE, 0);
6267 processKey(mapper, BTN_TOOL_LENS, 1);
6268 processSync(mapper);
6269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6270 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006271 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006272
6273 // double-tap
6274 processKey(mapper, BTN_TOOL_LENS, 0);
6275 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6276 processSync(mapper);
6277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6278 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006279 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006280
6281 // triple-tap
6282 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6283 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6284 processSync(mapper);
6285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6286 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006287 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006288
6289 // quad-tap
6290 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6291 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6292 processSync(mapper);
6293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6294 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006295 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006296
6297 // finger
6298 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6299 processKey(mapper, BTN_TOOL_FINGER, 1);
6300 processSync(mapper);
6301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006303 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304
6305 // stylus trumps finger
6306 processKey(mapper, BTN_TOOL_PEN, 1);
6307 processSync(mapper);
6308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006310 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006311
6312 // eraser trumps stylus
6313 processKey(mapper, BTN_TOOL_RUBBER, 1);
6314 processSync(mapper);
6315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006317 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318
6319 // mouse trumps eraser
6320 processKey(mapper, BTN_TOOL_MOUSE, 1);
6321 processSync(mapper);
6322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6323 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006324 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006325
6326 // back to default tool type
6327 processKey(mapper, BTN_TOOL_MOUSE, 0);
6328 processKey(mapper, BTN_TOOL_RUBBER, 0);
6329 processKey(mapper, BTN_TOOL_PEN, 0);
6330 processKey(mapper, BTN_TOOL_FINGER, 0);
6331 processSync(mapper);
6332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006334 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006335}
6336
6337TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006339 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006340 prepareButtons();
6341 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006342 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006343 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006344
6345 NotifyMotionArgs motionArgs;
6346
6347 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6348 processKey(mapper, BTN_TOOL_FINGER, 1);
6349 processMove(mapper, 100, 200);
6350 processSync(mapper);
6351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6352 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6354 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6355
6356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6357 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6359 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6360
6361 // move a little
6362 processMove(mapper, 150, 250);
6363 processSync(mapper);
6364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6365 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6367 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6368
6369 // down when BTN_TOUCH is pressed, pressure defaults to 1
6370 processKey(mapper, BTN_TOUCH, 1);
6371 processSync(mapper);
6372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6373 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6375 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6376
6377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6378 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6380 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6381
6382 // up when BTN_TOUCH is released, hover restored
6383 processKey(mapper, BTN_TOUCH, 0);
6384 processSync(mapper);
6385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6386 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6387 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6388 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6389
6390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6391 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6393 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6394
6395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6396 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6398 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6399
6400 // exit hover when pointer goes away
6401 processKey(mapper, BTN_TOOL_FINGER, 0);
6402 processSync(mapper);
6403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6404 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6406 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6407}
6408
6409TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006410 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006411 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006412 prepareButtons();
6413 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006414 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006415
6416 NotifyMotionArgs motionArgs;
6417
6418 // initially hovering because pressure is 0
6419 processDown(mapper, 100, 200);
6420 processPressure(mapper, 0);
6421 processSync(mapper);
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6423 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6425 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6426
6427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6428 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6429 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6430 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6431
6432 // move a little
6433 processMove(mapper, 150, 250);
6434 processSync(mapper);
6435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6436 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6437 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6438 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6439
6440 // down when pressure is non-zero
6441 processPressure(mapper, RAW_PRESSURE_MAX);
6442 processSync(mapper);
6443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6444 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6446 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6447
6448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6449 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6451 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6452
6453 // up when pressure becomes 0, hover restored
6454 processPressure(mapper, 0);
6455 processSync(mapper);
6456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6457 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6459 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6460
6461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6462 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6464 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6465
6466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6467 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6469 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6470
6471 // exit hover when pointer goes away
6472 processUp(mapper);
6473 processSync(mapper);
6474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6475 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6477 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6478}
6479
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006480TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6481 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006482 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006483 prepareButtons();
6484 prepareAxes(POSITION | PRESSURE);
6485 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6486
6487 // Touch down.
6488 processDown(mapper, 100, 200);
6489 processPressure(mapper, 1);
6490 processSync(mapper);
6491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6492 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6493
6494 // Reset the mapper. This should cancel the ongoing gesture.
6495 resetMapper(mapper, ARBITRARY_TIME);
6496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6497 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6498
6499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6500}
6501
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006502TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6503 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006504 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006505 prepareButtons();
6506 prepareAxes(POSITION | PRESSURE);
6507 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6508
6509 // Set the initial state for the touch pointer.
6510 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6511 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6512 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6513 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6514
6515 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006516 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6517 // does not generate any events.
6518 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006519
6520 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6521 // the recreated touch state to generate a down event.
6522 processSync(mapper);
6523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6524 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6525
6526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6527}
6528
lilinnan687e58f2022-07-19 16:00:50 +08006529TEST_F(SingleTouchInputMapperTest,
6530 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6531 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006532 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006533 prepareButtons();
6534 prepareAxes(POSITION);
6535 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6536 NotifyMotionArgs motionArgs;
6537
6538 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006539 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006540 processSync(mapper);
6541
6542 // We should receive a down event
6543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6544 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6545
6546 // Change display id
6547 clearViewports();
6548 prepareSecondaryDisplay(ViewportType::INTERNAL);
6549
6550 // We should receive a cancel event
6551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6552 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6553 // Then receive reset called
6554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6555}
6556
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006557TEST_F(SingleTouchInputMapperTest,
6558 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6559 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006560 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006561 prepareButtons();
6562 prepareAxes(POSITION);
6563 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6565 NotifyMotionArgs motionArgs;
6566
6567 // Start a new gesture.
6568 processDown(mapper, 100, 200);
6569 processSync(mapper);
6570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6571 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6572
6573 // Make the viewport inactive. This will put the device in disabled mode.
6574 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6575 viewport->isActive = false;
6576 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006577 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006578
6579 // We should receive a cancel event for the ongoing gesture.
6580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6581 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6582 // Then we should be notified that the device was reset.
6583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6584
6585 // No events are generated while the viewport is inactive.
6586 processMove(mapper, 101, 201);
6587 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006588 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006589 processSync(mapper);
6590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6591
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006592 // Start a new gesture while the viewport is still inactive.
6593 processDown(mapper, 300, 400);
6594 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6595 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6596 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6597 processSync(mapper);
6598
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006599 // Make the viewport active again. The device should resume processing events.
6600 viewport->isActive = true;
6601 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006602 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006603
6604 // The device is reset because it changes back to direct mode, without generating any events.
6605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6607
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006608 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006609 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6611 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006612
6613 // No more events.
6614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6616}
6617
Prabir Pradhan211ba622022-10-31 21:09:21 +00006618TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6619 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006620 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006621 prepareButtons();
6622 prepareAxes(POSITION);
6623 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6625
6626 // Press a stylus button.
6627 processKey(mapper, BTN_STYLUS, 1);
6628 processSync(mapper);
6629
6630 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6631 processDown(mapper, 100, 200);
6632 processSync(mapper);
6633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6634 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6635 WithCoords(toDisplayX(100), toDisplayY(200)),
6636 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6638 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6639 WithCoords(toDisplayX(100), toDisplayY(200)),
6640 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6641
6642 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6643 // the button has not actually been released, since there will be no pointers through which the
6644 // button state can be reported. The event is generated at the location of the pointer before
6645 // it went up.
6646 processUp(mapper);
6647 processSync(mapper);
6648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6649 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6650 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6652 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6653 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6654}
6655
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006656TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6657 addConfigurationProperty("touch.deviceType", "touchScreen");
6658 prepareDisplay(ui::ROTATION_0);
6659 prepareButtons();
6660 prepareAxes(POSITION);
6661
6662 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6663
6664 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6666
6667 // Press a stylus button.
6668 processKey(mapper, BTN_STYLUS, 1);
6669 processSync(mapper);
6670
6671 // Start a touch gesture and ensure that the stylus button is not reported.
6672 processDown(mapper, 100, 200);
6673 processSync(mapper);
6674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6675 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6676
6677 // Release and press the stylus button again.
6678 processKey(mapper, BTN_STYLUS, 0);
6679 processSync(mapper);
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6681 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6682 processKey(mapper, BTN_STYLUS, 1);
6683 processSync(mapper);
6684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6685 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6686
6687 // Release the touch gesture.
6688 processUp(mapper);
6689 processSync(mapper);
6690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6691 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6692
6693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6694}
6695
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006696TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6697 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6698 prepareDisplay(ui::ROTATION_0);
6699 prepareButtons();
6700 prepareAxes(POSITION);
6701 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6703
6704 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6705}
6706
Seunghwan Choi356026c2023-02-01 14:37:25 +09006707TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6708 std::shared_ptr<FakePointerController> fakePointerController =
6709 std::make_shared<FakePointerController>();
6710 addConfigurationProperty("touch.deviceType", "touchScreen");
6711 prepareDisplay(ui::ROTATION_0);
6712 prepareButtons();
6713 prepareAxes(POSITION);
6714 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6715 mFakePolicy->setPointerController(fakePointerController);
6716 mFakePolicy->setStylusPointerIconEnabled(true);
6717 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6718
6719 processKey(mapper, BTN_TOOL_PEN, 1);
6720 processMove(mapper, 100, 200);
6721 processSync(mapper);
6722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6723 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006724 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006725 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6726 ASSERT_TRUE(fakePointerController->isPointerShown());
6727 ASSERT_NO_FATAL_FAILURE(
6728 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6729}
6730
6731TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6732 std::shared_ptr<FakePointerController> fakePointerController =
6733 std::make_shared<FakePointerController>();
6734 addConfigurationProperty("touch.deviceType", "touchScreen");
6735 prepareDisplay(ui::ROTATION_0);
6736 prepareButtons();
6737 prepareAxes(POSITION);
6738 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6739 mFakePolicy->setPointerController(fakePointerController);
6740 mFakePolicy->setStylusPointerIconEnabled(false);
6741 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6742
6743 processKey(mapper, BTN_TOOL_PEN, 1);
6744 processMove(mapper, 100, 200);
6745 processSync(mapper);
6746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6747 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006748 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006749 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6750 ASSERT_FALSE(fakePointerController->isPointerShown());
6751}
6752
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006753TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6754 // Initialize the device without setting device source to touch navigation.
6755 addConfigurationProperty("touch.deviceType", "touchScreen");
6756 prepareDisplay(ui::ROTATION_0);
6757 prepareButtons();
6758 prepareAxes(POSITION);
6759 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6760
6761 // Ensure that the device is created as a touchscreen, not touch navigation.
6762 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6763
6764 // Add device type association after the device was created.
6765 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6766
6767 // Send update to the mapper.
6768 std::list<NotifyArgs> unused2 =
6769 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006770 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006771
6772 // Check whether device type update was successful.
6773 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6774}
6775
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006776TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6777 // Initialize the device without setting device source to touch navigation.
6778 addConfigurationProperty("touch.deviceType", "touchScreen");
6779 prepareDisplay(ui::ROTATION_0);
6780 prepareButtons();
6781 prepareAxes(POSITION);
6782 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6783
6784 // Set a physical frame in the display viewport.
6785 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6786 viewport->physicalLeft = 0;
6787 viewport->physicalTop = 0;
6788 viewport->physicalRight = DISPLAY_WIDTH / 2;
6789 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6790 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006791 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006792
6793 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6794
6795 // Hovering inside the physical frame produces events.
6796 processKey(mapper, BTN_TOOL_PEN, 1);
6797 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6798 processSync(mapper);
6799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6800 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6802 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6803
6804 // Leaving the physical frame ends the hovering gesture.
6805 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6806 processSync(mapper);
6807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6808 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6809
6810 // Moving outside the physical frame does not produce events.
6811 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6812 processSync(mapper);
6813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6814
6815 // Re-entering the physical frame produces events.
6816 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6817 processSync(mapper);
6818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6819 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6821 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6822}
6823
Prabir Pradhan5632d622021-09-06 07:57:20 -07006824// --- TouchDisplayProjectionTest ---
6825
6826class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6827public:
6828 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6829 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6830 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006831 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6832 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6833 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006834 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006835 auto rotatedWidth = naturalDisplayWidth;
6836 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006837 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006838 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006839 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006840 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006841 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006842 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006843 inverseRotationFlags = ui::Transform::ROT_180;
6844 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006845 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006846 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006847 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006848 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006849 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006850 inverseRotationFlags = ui::Transform::ROT_0;
6851 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006852 }
6853
Prabir Pradhana9df3162022-12-05 23:57:27 +00006854 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006855 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6856
6857 std::optional<DisplayViewport> internalViewport =
6858 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6859 DisplayViewport& v = *internalViewport;
6860 v.displayId = DISPLAY_ID;
6861 v.orientation = orientation;
6862
6863 v.logicalLeft = 0;
6864 v.logicalTop = 0;
6865 v.logicalRight = 100;
6866 v.logicalBottom = 100;
6867
6868 v.physicalLeft = rotatedPhysicalDisplay.left;
6869 v.physicalTop = rotatedPhysicalDisplay.top;
6870 v.physicalRight = rotatedPhysicalDisplay.right;
6871 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6872
Prabir Pradhana9df3162022-12-05 23:57:27 +00006873 v.deviceWidth = rotatedWidth;
6874 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006875
6876 v.isActive = true;
6877 v.uniqueId = UNIQUE_ID;
6878 v.type = ViewportType::INTERNAL;
6879 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006880 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006881 }
6882
6883 void assertReceivedMove(const Point& point) {
6884 NotifyMotionArgs motionArgs;
6885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6886 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6887 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6889 1, 0, 0, 0, 0, 0, 0, 0));
6890 }
6891};
6892
6893TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6894 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006895 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006896
6897 prepareButtons();
6898 prepareAxes(POSITION);
6899 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6900
6901 NotifyMotionArgs motionArgs;
6902
6903 // Configure the DisplayViewport such that the logical display maps to a subsection of
6904 // the display panel called the physical display. Here, the physical display is bounded by the
6905 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6906 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6907 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6908 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6909
Michael Wrighta9cf4192022-12-01 23:46:39 +00006910 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006911 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6912
6913 // Touches outside the physical display should be ignored, and should not generate any
6914 // events. Ensure touches at the following points that lie outside of the physical display
6915 // area do not generate any events.
6916 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6917 processDown(mapper, toRawX(point.x), toRawY(point.y));
6918 processSync(mapper);
6919 processUp(mapper);
6920 processSync(mapper);
6921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6922 << "Unexpected event generated for touch outside physical display at point: "
6923 << point.x << ", " << point.y;
6924 }
6925 }
6926}
6927
6928TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6929 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006930 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006931
6932 prepareButtons();
6933 prepareAxes(POSITION);
6934 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6935
6936 NotifyMotionArgs motionArgs;
6937
6938 // Configure the DisplayViewport such that the logical display maps to a subsection of
6939 // the display panel called the physical display. Here, the physical display is bounded by the
6940 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6941 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6942
Michael Wrighta9cf4192022-12-01 23:46:39 +00006943 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006944 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6945
6946 // Touches that start outside the physical display should be ignored until it enters the
6947 // physical display bounds, at which point it should generate a down event. Start a touch at
6948 // the point (5, 100), which is outside the physical display bounds.
6949 static const Point kOutsidePoint{5, 100};
6950 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6951 processSync(mapper);
6952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6953
6954 // Move the touch into the physical display area. This should generate a pointer down.
6955 processMove(mapper, toRawX(11), toRawY(21));
6956 processSync(mapper);
6957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6958 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6959 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6960 ASSERT_NO_FATAL_FAILURE(
6961 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6962
6963 // Move the touch inside the physical display area. This should generate a pointer move.
6964 processMove(mapper, toRawX(69), toRawY(159));
6965 processSync(mapper);
6966 assertReceivedMove({69, 159});
6967
6968 // Move outside the physical display area. Since the pointer is already down, this should
6969 // now continue generating events.
6970 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6971 processSync(mapper);
6972 assertReceivedMove(kOutsidePoint);
6973
6974 // Release. This should generate a pointer up.
6975 processUp(mapper);
6976 processSync(mapper);
6977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6978 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6979 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6980 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6981
6982 // Ensure no more events were generated.
6983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6985 }
6986}
6987
Prabir Pradhana9df3162022-12-05 23:57:27 +00006988// --- TouchscreenPrecisionTests ---
6989
6990// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6991// in various orientations and with different display rotations. We configure the touchscreen to
6992// have a higher resolution than that of the display by an integer scale factor in each axis so that
6993// we can enforce that coordinates match precisely as expected.
6994class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6995 public ::testing::WithParamInterface<ui::Rotation> {
6996public:
6997 void SetUp() override {
6998 SingleTouchInputMapperTest::SetUp();
6999
7000 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7001 // four times the resolution of the display in the Y axis.
7002 prepareButtons();
7003 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007004 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7005 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007006 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007007 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7008 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007009 }
7010
7011 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7012 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7013 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7014 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7015
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007016 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7017 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7018
7019 static const int32_t PRECISION_RAW_X_FLAT = 16;
7020 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7021
7022 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7023 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7024
Prabir Pradhana9df3162022-12-05 23:57:27 +00007025 static const std::array<Point, 4> kRawCorners;
7026};
7027
7028const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7029 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7030 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7031 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7032 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7033}};
7034
7035// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7036// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7037// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7038TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7039 enum class Orientation {
7040 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7041 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7042 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7043 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7044 ftl_last = ORIENTATION_270,
7045 };
7046 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7047 Orientation::ORIENTATION_270;
7048 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7049 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7050 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7051 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7052 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7053 };
7054
7055 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7056
7057 // Configure the touchscreen as being installed in the one of the four different orientations
7058 // relative to the display.
7059 addConfigurationProperty("touch.deviceType", "touchScreen");
7060 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7061 prepareDisplay(ui::ROTATION_0);
7062
7063 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7064
7065 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7066 // orientations of either 90 or 270) this means the display's natural resolution will be
7067 // flipped.
7068 const bool displayRotated =
7069 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7070 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7071 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7072 const Rect physicalFrame{0, 0, width, height};
7073 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7074
7075 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7076 const float expectedPrecisionX = displayRotated ? 4 : 2;
7077 const float expectedPrecisionY = displayRotated ? 2 : 4;
7078
7079 // Test all four corners.
7080 for (int i = 0; i < 4; i++) {
7081 const auto& raw = kRawCorners[i];
7082 processDown(mapper, raw.x, raw.y);
7083 processSync(mapper);
7084 const auto& expected = expectedPoints[i];
7085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7086 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7087 WithCoords(expected.x, expected.y),
7088 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7089 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7090 << "with touchscreen orientation "
7091 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7092 << expected.x << ", " << expected.y << ").";
7093 processUp(mapper);
7094 processSync(mapper);
7095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7096 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7097 WithCoords(expected.x, expected.y))));
7098 }
7099}
7100
Prabir Pradhan82687402022-12-06 01:32:53 +00007101TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7102 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7103 kMappedCorners = {
7104 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7105 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7106 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7107 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7108 };
7109
7110 const ui::Rotation displayRotation = GetParam();
7111
7112 addConfigurationProperty("touch.deviceType", "touchScreen");
7113 prepareDisplay(displayRotation);
7114
7115 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7116
7117 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7118
7119 // Test all four corners.
7120 for (int i = 0; i < 4; i++) {
7121 const auto& expected = expectedPoints[i];
7122 const auto& raw = kRawCorners[i];
7123 processDown(mapper, raw.x, raw.y);
7124 processSync(mapper);
7125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7126 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7127 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7128 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7129 << "with display rotation " << ui::toCString(displayRotation)
7130 << ", expected point (" << expected.x << ", " << expected.y << ").";
7131 processUp(mapper);
7132 processSync(mapper);
7133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7134 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7135 WithCoords(expected.x, expected.y))));
7136 }
7137}
7138
Prabir Pradhan3e798762022-12-02 21:02:11 +00007139TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7140 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7141 kMappedCorners = {
7142 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7143 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7144 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7145 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7146 };
7147
7148 const ui::Rotation displayRotation = GetParam();
7149
7150 addConfigurationProperty("touch.deviceType", "touchScreen");
7151 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7152
7153 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7154
7155 // Ori 270, so width and height swapped
7156 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7157 prepareDisplay(displayRotation);
7158 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7159
7160 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7161
7162 // Test all four corners.
7163 for (int i = 0; i < 4; i++) {
7164 const auto& expected = expectedPoints[i];
7165 const auto& raw = kRawCorners[i];
7166 processDown(mapper, raw.x, raw.y);
7167 processSync(mapper);
7168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7169 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7170 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7171 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7172 << "with display rotation " << ui::toCString(displayRotation)
7173 << ", expected point (" << expected.x << ", " << expected.y << ").";
7174 processUp(mapper);
7175 processSync(mapper);
7176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7177 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7178 WithCoords(expected.x, expected.y))));
7179 }
7180}
7181
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007182TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7183 const ui::Rotation displayRotation = GetParam();
7184
7185 addConfigurationProperty("touch.deviceType", "touchScreen");
7186 prepareDisplay(displayRotation);
7187
7188 __attribute__((unused)) SingleTouchInputMapper& mapper =
7189 addMapperAndConfigure<SingleTouchInputMapper>();
7190
7191 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7192 // MotionRanges use display pixels as their units
7193 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7194 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7195
7196 // The MotionRanges should be oriented in the rotated display's coordinate space
7197 const bool displayRotated =
7198 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7199
7200 constexpr float MAX_X = 479.5;
7201 constexpr float MAX_Y = 799.75;
7202 EXPECT_EQ(xRange->min, 0.f);
7203 EXPECT_EQ(yRange->min, 0.f);
7204 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7205 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7206
7207 EXPECT_EQ(xRange->flat, 8.f);
7208 EXPECT_EQ(yRange->flat, 8.f);
7209
7210 EXPECT_EQ(xRange->fuzz, 2.f);
7211 EXPECT_EQ(yRange->fuzz, 2.f);
7212
7213 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7214 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7215}
7216
Prabir Pradhana9df3162022-12-05 23:57:27 +00007217// Run the precision tests for all rotations.
7218INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7219 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7220 ui::ROTATION_270),
7221 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7222 return ftl::enum_string(testParamInfo.param);
7223 });
7224
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007225// --- ExternalStylusFusionTest ---
7226
7227class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7228public:
7229 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7230 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007231 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007232 prepareButtons();
7233 prepareAxes(POSITION);
7234 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7235
7236 mStylusState.when = ARBITRARY_TIME;
7237 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007238 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007239 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007240 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007241 processExternalStylusState(mapper);
7242 return mapper;
7243 }
7244
7245 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7246 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7247 for (const NotifyArgs& args : generatedArgs) {
7248 mFakeListener->notify(args);
7249 }
7250 // Loop the reader to flush the input listener queue.
7251 mReader->loopOnce();
7252 return generatedArgs;
7253 }
7254
7255protected:
7256 StylusState mStylusState{};
7257 static constexpr uint32_t EXPECTED_SOURCE =
7258 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7259
7260 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7261 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007262 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007263
7264 // The first pointer is withheld.
7265 processDown(mapper, 100, 200);
7266 processSync(mapper);
7267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7268 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7269 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7270
7271 // The external stylus reports pressure. The withheld finger pointer is released as a
7272 // stylus.
7273 mStylusState.pressure = 1.f;
7274 processExternalStylusState(mapper);
7275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7276 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7277 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7278
7279 // Subsequent pointer events are not withheld.
7280 processMove(mapper, 101, 201);
7281 processSync(mapper);
7282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7283 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7284
7285 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7287 }
7288
7289 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7290 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7291
7292 // Releasing the touch pointer ends the gesture.
7293 processUp(mapper);
7294 processSync(mapper);
7295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7296 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007297 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007298
7299 mStylusState.pressure = 0.f;
7300 processExternalStylusState(mapper);
7301 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7303 }
7304
7305 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7306 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007307 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007308
7309 // The first pointer is withheld when an external stylus is connected,
7310 // and a timeout is requested.
7311 processDown(mapper, 100, 200);
7312 processSync(mapper);
7313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7314 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7315 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7316
7317 // If the timeout expires early, it is requested again.
7318 handleTimeout(mapper, ARBITRARY_TIME + 1);
7319 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7320 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7321
7322 // When the timeout expires, the withheld touch is released as a finger pointer.
7323 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7325 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7326
7327 // Subsequent pointer events are not withheld.
7328 processMove(mapper, 101, 201);
7329 processSync(mapper);
7330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7331 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7332 processUp(mapper);
7333 processSync(mapper);
7334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7335 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7336
7337 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7339 }
7340
7341private:
7342 InputDeviceInfo mExternalStylusDeviceInfo{};
7343};
7344
7345TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7346 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7347 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7348}
7349
7350TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7351 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7352 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7353}
7354
7355TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7356 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7357 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7358}
7359
7360// Test a successful stylus fusion gesture where the pressure is reported by the external
7361// before the touch is reported by the touchscreen.
7362TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7363 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7364 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007365 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007366
7367 // The external stylus reports pressure first. It is ignored for now.
7368 mStylusState.pressure = 1.f;
7369 processExternalStylusState(mapper);
7370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7371 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7372
7373 // When the touch goes down afterwards, it is reported as a stylus pointer.
7374 processDown(mapper, 100, 200);
7375 processSync(mapper);
7376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7377 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7378 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7379
7380 processMove(mapper, 101, 201);
7381 processSync(mapper);
7382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7383 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7384 processUp(mapper);
7385 processSync(mapper);
7386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7387 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7388
7389 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7391}
7392
7393TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7394 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7395
7396 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7397 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7398
7399 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7400 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7401 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7402 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7403}
7404
7405TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7406 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7407 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007408 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007409
7410 mStylusState.pressure = 0.8f;
7411 processExternalStylusState(mapper);
7412 processDown(mapper, 100, 200);
7413 processSync(mapper);
7414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7415 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7416 WithPressure(0.8f))));
7417 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7418
7419 // The external stylus reports a pressure change. We wait for some time for a touch event.
7420 mStylusState.pressure = 0.6f;
7421 processExternalStylusState(mapper);
7422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7423 ASSERT_NO_FATAL_FAILURE(
7424 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7425
7426 // If a touch is reported within the timeout, it reports the updated pressure.
7427 processMove(mapper, 101, 201);
7428 processSync(mapper);
7429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7430 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7431 WithPressure(0.6f))));
7432 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7433
7434 // There is another pressure change.
7435 mStylusState.pressure = 0.5f;
7436 processExternalStylusState(mapper);
7437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7438 ASSERT_NO_FATAL_FAILURE(
7439 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7440
7441 // If a touch is not reported within the timeout, a move event is generated to report
7442 // the new pressure.
7443 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7445 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7446 WithPressure(0.5f))));
7447
7448 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7449 // repeated indefinitely.
7450 mStylusState.pressure = 0.0f;
7451 processExternalStylusState(mapper);
7452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7453 ASSERT_NO_FATAL_FAILURE(
7454 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7455 processMove(mapper, 102, 202);
7456 processSync(mapper);
7457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7458 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7459 WithPressure(0.5f))));
7460 processMove(mapper, 103, 203);
7461 processSync(mapper);
7462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7463 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7464 WithPressure(0.5f))));
7465
7466 processUp(mapper);
7467 processSync(mapper);
7468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7469 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007470 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007471
7472 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7474}
7475
7476TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7477 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7478 auto source = WithSource(EXPECTED_SOURCE);
7479
7480 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007481 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007482 processExternalStylusState(mapper);
7483 processDown(mapper, 100, 200);
7484 processSync(mapper);
7485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7486 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007487 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007488 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7489
7490 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007491 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007492 processExternalStylusState(mapper);
7493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7494 ASSERT_NO_FATAL_FAILURE(
7495 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7496
7497 // If a touch is reported within the timeout, it reports the updated pressure.
7498 processMove(mapper, 101, 201);
7499 processSync(mapper);
7500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7501 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007502 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007503 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7504
7505 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007506 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007507 processExternalStylusState(mapper);
7508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7509 ASSERT_NO_FATAL_FAILURE(
7510 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7511
7512 // If a touch is not reported within the timeout, a move event is generated to report
7513 // the new tool type.
7514 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7516 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007517 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007518
7519 processUp(mapper);
7520 processSync(mapper);
7521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7522 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007523 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007524
7525 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7527}
7528
7529TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7530 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7531 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007532 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007533
7534 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7535
7536 // The external stylus reports a button change. We wait for some time for a touch event.
7537 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7538 processExternalStylusState(mapper);
7539 ASSERT_NO_FATAL_FAILURE(
7540 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7541
7542 // If a touch is reported within the timeout, it reports the updated button state.
7543 processMove(mapper, 101, 201);
7544 processSync(mapper);
7545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7546 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7547 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7549 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7550 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7551 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7552
7553 // The button is now released.
7554 mStylusState.buttons = 0;
7555 processExternalStylusState(mapper);
7556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7557 ASSERT_NO_FATAL_FAILURE(
7558 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7559
7560 // If a touch is not reported within the timeout, a move event is generated to report
7561 // the new button state.
7562 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7564 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7565 WithButtonState(0))));
7566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007567 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7568 WithButtonState(0))));
7569
7570 processUp(mapper);
7571 processSync(mapper);
7572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007573 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7574
7575 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7577}
7578
Michael Wrightd02c5b62014-02-10 15:10:22 -08007579// --- MultiTouchInputMapperTest ---
7580
7581class MultiTouchInputMapperTest : public TouchInputMapperTest {
7582protected:
7583 void prepareAxes(int axes);
7584
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007585 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7586 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7587 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7588 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7589 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7590 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7591 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7592 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7593 void processId(MultiTouchInputMapper& mapper, int32_t id);
7594 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7595 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7596 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007597 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007598 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007599 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7600 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007601};
7602
7603void MultiTouchInputMapperTest::prepareAxes(int axes) {
7604 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007605 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7606 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007607 }
7608 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007609 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7610 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007611 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007612 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7613 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007614 }
7615 }
7616 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007617 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7618 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007619 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007620 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007621 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007622 }
7623 }
7624 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007625 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7626 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007627 }
7628 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007629 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7630 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007631 }
7632 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007633 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7634 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007635 }
7636 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007637 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7638 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007639 }
7640 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007641 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7642 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007643 }
7644 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007645 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007646 }
7647}
7648
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007649void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7650 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007651 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007653}
7654
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007655void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7656 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007658}
7659
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007660void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7661 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007662 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007663}
7664
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007665void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007666 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007667}
7668
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007669void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007671}
7672
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007673void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7674 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007675 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007676}
7677
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007678void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007679 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007680}
7681
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007682void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007684}
7685
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007686void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007688}
7689
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007690void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007692}
7693
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007694void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007695 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007696}
7697
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007698void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7699 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007701}
7702
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007703void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7704 int32_t value) {
7705 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7707}
7708
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007709void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007710 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007711}
7712
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007713void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7714 nsecs_t readTime) {
7715 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007716}
7717
Michael Wrightd02c5b62014-02-10 15:10:22 -08007718TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007719 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007720 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007721 prepareAxes(POSITION);
7722 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007723 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007724
arthurhungdcef2dc2020-08-11 14:47:50 +08007725 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007726
7727 NotifyMotionArgs motionArgs;
7728
7729 // Two fingers down at once.
7730 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7731 processPosition(mapper, x1, y1);
7732 processMTSync(mapper);
7733 processPosition(mapper, x2, y2);
7734 processMTSync(mapper);
7735 processSync(mapper);
7736
7737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7738 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7739 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7740 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7741 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7742 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7743 ASSERT_EQ(0, motionArgs.flags);
7744 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7745 ASSERT_EQ(0, motionArgs.buttonState);
7746 ASSERT_EQ(0, motionArgs.edgeFlags);
7747 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7748 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007749 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007750 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7751 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7752 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7753 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7754 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7755
7756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7757 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7758 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7759 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7760 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007761 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007762 ASSERT_EQ(0, motionArgs.flags);
7763 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7764 ASSERT_EQ(0, motionArgs.buttonState);
7765 ASSERT_EQ(0, motionArgs.edgeFlags);
7766 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7767 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007768 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007769 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007770 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7772 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7774 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7775 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7776 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7777 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7778
7779 // Move.
7780 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7781 processPosition(mapper, x1, y1);
7782 processMTSync(mapper);
7783 processPosition(mapper, x2, y2);
7784 processMTSync(mapper);
7785 processSync(mapper);
7786
7787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7788 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7789 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7790 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7791 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7792 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7793 ASSERT_EQ(0, motionArgs.flags);
7794 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7795 ASSERT_EQ(0, motionArgs.buttonState);
7796 ASSERT_EQ(0, motionArgs.edgeFlags);
7797 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7798 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007799 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007800 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007801 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7803 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
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 // First finger up.
7811 x2 += 15; y2 -= 20;
7812 processPosition(mapper, x2, y2);
7813 processMTSync(mapper);
7814 processSync(mapper);
7815
7816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7817 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7818 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7819 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7820 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007821 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007822 ASSERT_EQ(0, motionArgs.flags);
7823 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7824 ASSERT_EQ(0, motionArgs.buttonState);
7825 ASSERT_EQ(0, motionArgs.edgeFlags);
7826 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7827 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007828 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007829 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007830 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7832 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7833 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7834 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7835 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7836 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7837 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7838
7839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7840 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7841 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7842 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7843 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7844 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7845 ASSERT_EQ(0, motionArgs.flags);
7846 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7847 ASSERT_EQ(0, motionArgs.buttonState);
7848 ASSERT_EQ(0, motionArgs.edgeFlags);
7849 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7850 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007851 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7853 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7854 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7855 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7856 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7857
7858 // Move.
7859 x2 += 20; y2 -= 25;
7860 processPosition(mapper, x2, y2);
7861 processMTSync(mapper);
7862 processSync(mapper);
7863
7864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7865 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7866 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7867 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7868 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7869 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7870 ASSERT_EQ(0, motionArgs.flags);
7871 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7872 ASSERT_EQ(0, motionArgs.buttonState);
7873 ASSERT_EQ(0, motionArgs.edgeFlags);
7874 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7875 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007876 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007877 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7878 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7879 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7880 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7881 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7882
7883 // New finger down.
7884 int32_t x3 = 700, y3 = 300;
7885 processPosition(mapper, x2, y2);
7886 processMTSync(mapper);
7887 processPosition(mapper, x3, y3);
7888 processMTSync(mapper);
7889 processSync(mapper);
7890
7891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7892 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7893 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7894 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7895 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007896 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007897 ASSERT_EQ(0, motionArgs.flags);
7898 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7899 ASSERT_EQ(0, motionArgs.buttonState);
7900 ASSERT_EQ(0, motionArgs.edgeFlags);
7901 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7902 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007903 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007904 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007905 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7907 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7909 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7910 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7911 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7912 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7913
7914 // Second finger up.
7915 x3 += 30; y3 -= 20;
7916 processPosition(mapper, x3, y3);
7917 processMTSync(mapper);
7918 processSync(mapper);
7919
7920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7921 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7922 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7923 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7924 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007925 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007926 ASSERT_EQ(0, motionArgs.flags);
7927 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7928 ASSERT_EQ(0, motionArgs.buttonState);
7929 ASSERT_EQ(0, motionArgs.edgeFlags);
7930 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7931 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007932 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007933 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007934 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007935 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7936 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7938 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7939 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7940 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7941 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7942
7943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7944 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7945 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7946 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7947 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7948 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7949 ASSERT_EQ(0, motionArgs.flags);
7950 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7951 ASSERT_EQ(0, motionArgs.buttonState);
7952 ASSERT_EQ(0, motionArgs.edgeFlags);
7953 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7954 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007955 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7957 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7958 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7959 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7960 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7961
7962 // Last finger up.
7963 processMTSync(mapper);
7964 processSync(mapper);
7965
7966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7967 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7968 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7969 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7970 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7971 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7972 ASSERT_EQ(0, motionArgs.flags);
7973 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7974 ASSERT_EQ(0, motionArgs.buttonState);
7975 ASSERT_EQ(0, motionArgs.edgeFlags);
7976 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7977 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007978 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007979 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7980 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7981 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7982 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7983 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7984
7985 // Should not have sent any more keys or motions.
7986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7988}
7989
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007990TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7991 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007992 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007993
7994 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7995 /*fuzz*/ 0, /*resolution*/ 10);
7996 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7997 /*fuzz*/ 0, /*resolution*/ 11);
7998 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7999 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8000 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8001 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8002 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8003 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8004 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8005 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8006
8007 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8008
8009 // X and Y axes
8010 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8011 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8012 // Touch major and minor
8013 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8014 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8015 // Tool major and minor
8016 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8017 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8018}
8019
8020TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8021 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008022 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008023
8024 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8025 /*fuzz*/ 0, /*resolution*/ 10);
8026 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8027 /*fuzz*/ 0, /*resolution*/ 11);
8028
8029 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8030
8031 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8032
8033 // Touch major and minor
8034 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8035 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8036 // Tool major and minor
8037 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8038 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8039}
8040
Michael Wrightd02c5b62014-02-10 15:10:22 -08008041TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008042 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008043 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008044 prepareAxes(POSITION | ID);
8045 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008046 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008047
arthurhungdcef2dc2020-08-11 14:47:50 +08008048 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008049
8050 NotifyMotionArgs motionArgs;
8051
8052 // Two fingers down at once.
8053 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8054 processPosition(mapper, x1, y1);
8055 processId(mapper, 1);
8056 processMTSync(mapper);
8057 processPosition(mapper, x2, y2);
8058 processId(mapper, 2);
8059 processMTSync(mapper);
8060 processSync(mapper);
8061
8062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8063 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8064 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8065 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008066 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008067 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8068 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8069
8070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008071 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8073 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008074 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008075 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008076 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8078 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8080 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8081
8082 // Move.
8083 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8084 processPosition(mapper, x1, y1);
8085 processId(mapper, 1);
8086 processMTSync(mapper);
8087 processPosition(mapper, x2, y2);
8088 processId(mapper, 2);
8089 processMTSync(mapper);
8090 processSync(mapper);
8091
8092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8093 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8094 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8095 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008096 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008097 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008098 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008099 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8100 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8101 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8102 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8103
8104 // First finger up.
8105 x2 += 15; y2 -= 20;
8106 processPosition(mapper, x2, y2);
8107 processId(mapper, 2);
8108 processMTSync(mapper);
8109 processSync(mapper);
8110
8111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008112 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008113 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8114 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008115 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008116 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008117 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8119 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8121 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8122
8123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8125 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8126 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008127 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008128 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8129 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8130
8131 // Move.
8132 x2 += 20; y2 -= 25;
8133 processPosition(mapper, x2, y2);
8134 processId(mapper, 2);
8135 processMTSync(mapper);
8136 processSync(mapper);
8137
8138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8140 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8141 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008142 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008143 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8144 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8145
8146 // New finger down.
8147 int32_t x3 = 700, y3 = 300;
8148 processPosition(mapper, x2, y2);
8149 processId(mapper, 2);
8150 processMTSync(mapper);
8151 processPosition(mapper, x3, y3);
8152 processId(mapper, 3);
8153 processMTSync(mapper);
8154 processSync(mapper);
8155
8156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008157 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008158 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8159 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008160 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008161 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008162 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008163 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8164 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8165 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8166 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8167
8168 // Second finger up.
8169 x3 += 30; y3 -= 20;
8170 processPosition(mapper, x3, y3);
8171 processId(mapper, 3);
8172 processMTSync(mapper);
8173 processSync(mapper);
8174
8175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008176 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008177 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8178 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008179 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008180 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008181 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8183 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8185 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8186
8187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8189 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8190 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008191 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8193 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8194
8195 // Last finger up.
8196 processMTSync(mapper);
8197 processSync(mapper);
8198
8199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8200 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8201 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8202 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008203 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008204 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8205 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8206
8207 // Should not have sent any more keys or motions.
8208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8210}
8211
8212TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008213 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008214 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008215 prepareAxes(POSITION | ID | SLOT);
8216 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008217 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008218
arthurhungdcef2dc2020-08-11 14:47:50 +08008219 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008220
8221 NotifyMotionArgs motionArgs;
8222
8223 // Two fingers down at once.
8224 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8225 processPosition(mapper, x1, y1);
8226 processId(mapper, 1);
8227 processSlot(mapper, 1);
8228 processPosition(mapper, x2, y2);
8229 processId(mapper, 2);
8230 processSync(mapper);
8231
8232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8233 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8234 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8235 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008236 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8238 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8239
8240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008241 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008242 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8243 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008244 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008245 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008246 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008247 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8248 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8250 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8251
8252 // Move.
8253 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8254 processSlot(mapper, 0);
8255 processPosition(mapper, x1, y1);
8256 processSlot(mapper, 1);
8257 processPosition(mapper, x2, y2);
8258 processSync(mapper);
8259
8260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8262 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8263 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008264 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008265 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008266 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008267 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8268 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8270 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8271
8272 // First finger up.
8273 x2 += 15; y2 -= 20;
8274 processSlot(mapper, 0);
8275 processId(mapper, -1);
8276 processSlot(mapper, 1);
8277 processPosition(mapper, x2, y2);
8278 processSync(mapper);
8279
8280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008281 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008282 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8283 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008284 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008285 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008286 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8288 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8290 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8291
8292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8294 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8295 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008296 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8298 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8299
8300 // Move.
8301 x2 += 20; y2 -= 25;
8302 processPosition(mapper, x2, y2);
8303 processSync(mapper);
8304
8305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8306 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8307 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8308 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008309 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008310 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8311 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8312
8313 // New finger down.
8314 int32_t x3 = 700, y3 = 300;
8315 processPosition(mapper, x2, y2);
8316 processSlot(mapper, 0);
8317 processId(mapper, 3);
8318 processPosition(mapper, x3, y3);
8319 processSync(mapper);
8320
8321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008322 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008323 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8324 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008325 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008326 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008327 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8329 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8331 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8332
8333 // Second finger up.
8334 x3 += 30; y3 -= 20;
8335 processSlot(mapper, 1);
8336 processId(mapper, -1);
8337 processSlot(mapper, 0);
8338 processPosition(mapper, x3, y3);
8339 processSync(mapper);
8340
8341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008342 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008343 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8344 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008345 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008346 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008347 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8349 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8350 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8351 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8352
8353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8355 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8356 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008357 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8359 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8360
8361 // Last finger up.
8362 processId(mapper, -1);
8363 processSync(mapper);
8364
8365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8366 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8367 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8368 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008369 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8371 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8372
8373 // Should not have sent any more keys or motions.
8374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8376}
8377
8378TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008379 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008380 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008381 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008382 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008383
8384 // These calculations are based on the input device calibration documentation.
8385 int32_t rawX = 100;
8386 int32_t rawY = 200;
8387 int32_t rawTouchMajor = 7;
8388 int32_t rawTouchMinor = 6;
8389 int32_t rawToolMajor = 9;
8390 int32_t rawToolMinor = 8;
8391 int32_t rawPressure = 11;
8392 int32_t rawDistance = 0;
8393 int32_t rawOrientation = 3;
8394 int32_t id = 5;
8395
8396 float x = toDisplayX(rawX);
8397 float y = toDisplayY(rawY);
8398 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8399 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8400 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8401 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8402 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8403 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8404 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8405 float distance = float(rawDistance);
8406
8407 processPosition(mapper, rawX, rawY);
8408 processTouchMajor(mapper, rawTouchMajor);
8409 processTouchMinor(mapper, rawTouchMinor);
8410 processToolMajor(mapper, rawToolMajor);
8411 processToolMinor(mapper, rawToolMinor);
8412 processPressure(mapper, rawPressure);
8413 processOrientation(mapper, rawOrientation);
8414 processDistance(mapper, rawDistance);
8415 processId(mapper, id);
8416 processMTSync(mapper);
8417 processSync(mapper);
8418
8419 NotifyMotionArgs args;
8420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8421 ASSERT_EQ(0, args.pointerProperties[0].id);
8422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8423 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8424 orientation, distance));
8425}
8426
8427TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008428 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008429 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008430 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8431 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008432 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008433
8434 // These calculations are based on the input device calibration documentation.
8435 int32_t rawX = 100;
8436 int32_t rawY = 200;
8437 int32_t rawTouchMajor = 140;
8438 int32_t rawTouchMinor = 120;
8439 int32_t rawToolMajor = 180;
8440 int32_t rawToolMinor = 160;
8441
8442 float x = toDisplayX(rawX);
8443 float y = toDisplayY(rawY);
8444 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8445 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8446 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8447 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8448 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8449
8450 processPosition(mapper, rawX, rawY);
8451 processTouchMajor(mapper, rawTouchMajor);
8452 processTouchMinor(mapper, rawTouchMinor);
8453 processToolMajor(mapper, rawToolMajor);
8454 processToolMinor(mapper, rawToolMinor);
8455 processMTSync(mapper);
8456 processSync(mapper);
8457
8458 NotifyMotionArgs args;
8459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8460 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8461 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8462}
8463
8464TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008465 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008466 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008467 prepareAxes(POSITION | TOUCH | TOOL);
8468 addConfigurationProperty("touch.size.calibration", "diameter");
8469 addConfigurationProperty("touch.size.scale", "10");
8470 addConfigurationProperty("touch.size.bias", "160");
8471 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008472 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008473
8474 // These calculations are based on the input device calibration documentation.
8475 // Note: We only provide a single common touch/tool value because the device is assumed
8476 // not to emit separate values for each pointer (isSummed = 1).
8477 int32_t rawX = 100;
8478 int32_t rawY = 200;
8479 int32_t rawX2 = 150;
8480 int32_t rawY2 = 250;
8481 int32_t rawTouchMajor = 5;
8482 int32_t rawToolMajor = 8;
8483
8484 float x = toDisplayX(rawX);
8485 float y = toDisplayY(rawY);
8486 float x2 = toDisplayX(rawX2);
8487 float y2 = toDisplayY(rawY2);
8488 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8489 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8490 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8491
8492 processPosition(mapper, rawX, rawY);
8493 processTouchMajor(mapper, rawTouchMajor);
8494 processToolMajor(mapper, rawToolMajor);
8495 processMTSync(mapper);
8496 processPosition(mapper, rawX2, rawY2);
8497 processTouchMajor(mapper, rawTouchMajor);
8498 processToolMajor(mapper, rawToolMajor);
8499 processMTSync(mapper);
8500 processSync(mapper);
8501
8502 NotifyMotionArgs args;
8503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8504 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8505
8506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008507 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008508 ASSERT_EQ(size_t(2), args.pointerCount);
8509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8510 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8512 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8513}
8514
8515TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008516 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008517 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008518 prepareAxes(POSITION | TOUCH | TOOL);
8519 addConfigurationProperty("touch.size.calibration", "area");
8520 addConfigurationProperty("touch.size.scale", "43");
8521 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008522 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008523
8524 // These calculations are based on the input device calibration documentation.
8525 int32_t rawX = 100;
8526 int32_t rawY = 200;
8527 int32_t rawTouchMajor = 5;
8528 int32_t rawToolMajor = 8;
8529
8530 float x = toDisplayX(rawX);
8531 float y = toDisplayY(rawY);
8532 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8533 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8534 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8535
8536 processPosition(mapper, rawX, rawY);
8537 processTouchMajor(mapper, rawTouchMajor);
8538 processToolMajor(mapper, rawToolMajor);
8539 processMTSync(mapper);
8540 processSync(mapper);
8541
8542 NotifyMotionArgs args;
8543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8545 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8546}
8547
8548TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008549 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008550 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008551 prepareAxes(POSITION | PRESSURE);
8552 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8553 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008554 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008555
Michael Wrightaa449c92017-12-13 21:21:43 +00008556 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008557 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008558 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8559 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8560 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8561
Michael Wrightd02c5b62014-02-10 15:10:22 -08008562 // These calculations are based on the input device calibration documentation.
8563 int32_t rawX = 100;
8564 int32_t rawY = 200;
8565 int32_t rawPressure = 60;
8566
8567 float x = toDisplayX(rawX);
8568 float y = toDisplayY(rawY);
8569 float pressure = float(rawPressure) * 0.01f;
8570
8571 processPosition(mapper, rawX, rawY);
8572 processPressure(mapper, rawPressure);
8573 processMTSync(mapper);
8574 processSync(mapper);
8575
8576 NotifyMotionArgs args;
8577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8579 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8580}
8581
8582TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008583 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008584 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008585 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008586 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008587
8588 NotifyMotionArgs motionArgs;
8589 NotifyKeyArgs keyArgs;
8590
8591 processId(mapper, 1);
8592 processPosition(mapper, 100, 200);
8593 processSync(mapper);
8594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8595 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8596 ASSERT_EQ(0, motionArgs.buttonState);
8597
8598 // press BTN_LEFT, release BTN_LEFT
8599 processKey(mapper, BTN_LEFT, 1);
8600 processSync(mapper);
8601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8602 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8603 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8604
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8606 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8607 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8608
Michael Wrightd02c5b62014-02-10 15:10:22 -08008609 processKey(mapper, BTN_LEFT, 0);
8610 processSync(mapper);
8611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008612 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008613 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008614
8615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008616 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008617 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008618
8619 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8620 processKey(mapper, BTN_RIGHT, 1);
8621 processKey(mapper, BTN_MIDDLE, 1);
8622 processSync(mapper);
8623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8625 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8626 motionArgs.buttonState);
8627
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8629 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8630 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8631
8632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8633 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8634 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8635 motionArgs.buttonState);
8636
Michael Wrightd02c5b62014-02-10 15:10:22 -08008637 processKey(mapper, BTN_RIGHT, 0);
8638 processSync(mapper);
8639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008640 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008641 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008642
8643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008645 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008646
8647 processKey(mapper, BTN_MIDDLE, 0);
8648 processSync(mapper);
8649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008650 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008651 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008652
8653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008655 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008656
8657 // press BTN_BACK, release BTN_BACK
8658 processKey(mapper, BTN_BACK, 1);
8659 processSync(mapper);
8660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8661 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8662 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008663
Michael Wrightd02c5b62014-02-10 15:10:22 -08008664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008665 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008666 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8667
8668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8669 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8670 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008671
8672 processKey(mapper, BTN_BACK, 0);
8673 processSync(mapper);
8674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008675 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008676 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008677
8678 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(0, motionArgs.buttonState);
8681
Michael Wrightd02c5b62014-02-10 15:10:22 -08008682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8683 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8684 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8685
8686 // press BTN_SIDE, release BTN_SIDE
8687 processKey(mapper, BTN_SIDE, 1);
8688 processSync(mapper);
8689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8690 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8691 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008692
Michael Wrightd02c5b62014-02-10 15:10:22 -08008693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008694 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008695 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8696
8697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8698 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8699 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008700
8701 processKey(mapper, BTN_SIDE, 0);
8702 processSync(mapper);
8703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008704 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008705 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008706
8707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008708 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008709 ASSERT_EQ(0, motionArgs.buttonState);
8710
Michael Wrightd02c5b62014-02-10 15:10:22 -08008711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8712 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8713 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8714
8715 // press BTN_FORWARD, release BTN_FORWARD
8716 processKey(mapper, BTN_FORWARD, 1);
8717 processSync(mapper);
8718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8719 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8720 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008721
Michael Wrightd02c5b62014-02-10 15:10:22 -08008722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008723 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008724 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8725
8726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8727 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8728 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008729
8730 processKey(mapper, BTN_FORWARD, 0);
8731 processSync(mapper);
8732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008733 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008734 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008735
8736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008737 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008738 ASSERT_EQ(0, motionArgs.buttonState);
8739
Michael Wrightd02c5b62014-02-10 15:10:22 -08008740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8741 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8742 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8743
8744 // press BTN_EXTRA, release BTN_EXTRA
8745 processKey(mapper, BTN_EXTRA, 1);
8746 processSync(mapper);
8747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8748 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8749 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008750
Michael Wrightd02c5b62014-02-10 15:10:22 -08008751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008752 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008753 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8754
8755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8756 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8757 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008758
8759 processKey(mapper, BTN_EXTRA, 0);
8760 processSync(mapper);
8761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008762 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008763 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008764
8765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008766 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008767 ASSERT_EQ(0, motionArgs.buttonState);
8768
Michael Wrightd02c5b62014-02-10 15:10:22 -08008769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8770 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8771 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8772
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8774
Michael Wrightd02c5b62014-02-10 15:10:22 -08008775 // press BTN_STYLUS, release BTN_STYLUS
8776 processKey(mapper, BTN_STYLUS, 1);
8777 processSync(mapper);
8778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8779 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008780 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8781
8782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8783 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8784 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008785
8786 processKey(mapper, BTN_STYLUS, 0);
8787 processSync(mapper);
8788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008789 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008790 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008791
8792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008793 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008794 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008795
8796 // press BTN_STYLUS2, release BTN_STYLUS2
8797 processKey(mapper, BTN_STYLUS2, 1);
8798 processSync(mapper);
8799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8800 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008801 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8802
8803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8804 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8805 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008806
8807 processKey(mapper, BTN_STYLUS2, 0);
8808 processSync(mapper);
8809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008810 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008811 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008812
8813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008814 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008815 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008816
8817 // release touch
8818 processId(mapper, -1);
8819 processSync(mapper);
8820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8821 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8822 ASSERT_EQ(0, motionArgs.buttonState);
8823}
8824
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008825TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8826 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008827 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008828 prepareAxes(POSITION | ID | SLOT);
8829 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8830
8831 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8832 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8833
8834 // Touch down.
8835 processId(mapper, 1);
8836 processPosition(mapper, 100, 200);
8837 processSync(mapper);
8838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8839 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8840
8841 // Press and release button mapped to the primary stylus button.
8842 processKey(mapper, BTN_A, 1);
8843 processSync(mapper);
8844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8845 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8846 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8848 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8849 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8850
8851 processKey(mapper, BTN_A, 0);
8852 processSync(mapper);
8853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8854 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8856 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8857
8858 // Press and release the HID usage mapped to the secondary stylus button.
8859 processHidUsage(mapper, 0xabcd, 1);
8860 processSync(mapper);
8861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8862 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8863 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8865 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8866 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8867
8868 processHidUsage(mapper, 0xabcd, 0);
8869 processSync(mapper);
8870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8871 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8873 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8874
8875 // Release touch.
8876 processId(mapper, -1);
8877 processSync(mapper);
8878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8879 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8880}
8881
Michael Wrightd02c5b62014-02-10 15:10:22 -08008882TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008883 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008884 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008885 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008886 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008887
8888 NotifyMotionArgs motionArgs;
8889
8890 // default tool type is finger
8891 processId(mapper, 1);
8892 processPosition(mapper, 100, 200);
8893 processSync(mapper);
8894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8895 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008896 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008897
8898 // eraser
8899 processKey(mapper, BTN_TOOL_RUBBER, 1);
8900 processSync(mapper);
8901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8902 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008903 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008904
8905 // stylus
8906 processKey(mapper, BTN_TOOL_RUBBER, 0);
8907 processKey(mapper, BTN_TOOL_PEN, 1);
8908 processSync(mapper);
8909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8910 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008911 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008912
8913 // brush
8914 processKey(mapper, BTN_TOOL_PEN, 0);
8915 processKey(mapper, BTN_TOOL_BRUSH, 1);
8916 processSync(mapper);
8917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8918 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008919 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008920
8921 // pencil
8922 processKey(mapper, BTN_TOOL_BRUSH, 0);
8923 processKey(mapper, BTN_TOOL_PENCIL, 1);
8924 processSync(mapper);
8925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8926 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008927 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008928
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008929 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008930 processKey(mapper, BTN_TOOL_PENCIL, 0);
8931 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8932 processSync(mapper);
8933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8934 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008935 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008936
8937 // mouse
8938 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8939 processKey(mapper, BTN_TOOL_MOUSE, 1);
8940 processSync(mapper);
8941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8942 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008943 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008944
8945 // lens
8946 processKey(mapper, BTN_TOOL_MOUSE, 0);
8947 processKey(mapper, BTN_TOOL_LENS, 1);
8948 processSync(mapper);
8949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008951 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008952
8953 // double-tap
8954 processKey(mapper, BTN_TOOL_LENS, 0);
8955 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8956 processSync(mapper);
8957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8958 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008959 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008960
8961 // triple-tap
8962 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8963 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8964 processSync(mapper);
8965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008967 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008968
8969 // quad-tap
8970 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8971 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8972 processSync(mapper);
8973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8974 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008975 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008976
8977 // finger
8978 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8979 processKey(mapper, BTN_TOOL_FINGER, 1);
8980 processSync(mapper);
8981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8982 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008983 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008984
8985 // stylus trumps finger
8986 processKey(mapper, BTN_TOOL_PEN, 1);
8987 processSync(mapper);
8988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8989 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008990 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008991
8992 // eraser trumps stylus
8993 processKey(mapper, BTN_TOOL_RUBBER, 1);
8994 processSync(mapper);
8995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008997 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008998
8999 // mouse trumps eraser
9000 processKey(mapper, BTN_TOOL_MOUSE, 1);
9001 processSync(mapper);
9002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009004 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009005
9006 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9007 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9008 processSync(mapper);
9009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9010 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009011 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009012
9013 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9014 processToolType(mapper, MT_TOOL_PEN);
9015 processSync(mapper);
9016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9017 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009018 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009019
9020 // back to default tool type
9021 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9022 processKey(mapper, BTN_TOOL_MOUSE, 0);
9023 processKey(mapper, BTN_TOOL_RUBBER, 0);
9024 processKey(mapper, BTN_TOOL_PEN, 0);
9025 processKey(mapper, BTN_TOOL_FINGER, 0);
9026 processSync(mapper);
9027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009029 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009030}
9031
9032TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009033 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009034 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009035 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009036 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009037 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009038
9039 NotifyMotionArgs motionArgs;
9040
9041 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9042 processId(mapper, 1);
9043 processPosition(mapper, 100, 200);
9044 processSync(mapper);
9045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9046 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9048 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9049
9050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9051 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9053 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9054
9055 // move a little
9056 processPosition(mapper, 150, 250);
9057 processSync(mapper);
9058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9059 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9060 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9061 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9062
9063 // down when BTN_TOUCH is pressed, pressure defaults to 1
9064 processKey(mapper, BTN_TOUCH, 1);
9065 processSync(mapper);
9066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9067 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9068 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9069 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9070
9071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9072 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9073 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9074 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9075
9076 // up when BTN_TOUCH is released, hover restored
9077 processKey(mapper, BTN_TOUCH, 0);
9078 processSync(mapper);
9079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9080 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9081 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9082 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9083
9084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9085 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9086 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9087 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9088
9089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9090 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9091 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9092 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9093
9094 // exit hover when pointer goes away
9095 processId(mapper, -1);
9096 processSync(mapper);
9097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9098 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9099 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9100 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9101}
9102
9103TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009104 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009105 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009106 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009107 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009108
9109 NotifyMotionArgs motionArgs;
9110
9111 // initially hovering because pressure is 0
9112 processId(mapper, 1);
9113 processPosition(mapper, 100, 200);
9114 processPressure(mapper, 0);
9115 processSync(mapper);
9116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9117 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9119 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9120
9121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9122 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9123 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9124 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9125
9126 // move a little
9127 processPosition(mapper, 150, 250);
9128 processSync(mapper);
9129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9130 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9131 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9132 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9133
9134 // down when pressure becomes non-zero
9135 processPressure(mapper, RAW_PRESSURE_MAX);
9136 processSync(mapper);
9137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9138 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9140 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9141
9142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9143 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9144 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9145 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9146
9147 // up when pressure becomes 0, hover restored
9148 processPressure(mapper, 0);
9149 processSync(mapper);
9150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9151 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9152 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9153 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9154
9155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9156 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9157 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9158 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9159
9160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9161 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9162 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9163 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9164
9165 // exit hover when pointer goes away
9166 processId(mapper, -1);
9167 processSync(mapper);
9168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9169 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9171 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9172}
9173
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009174/**
9175 * Set the input device port <--> display port associations, and check that the
9176 * events are routed to the display that matches the display port.
9177 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9178 */
9179TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009180 const std::string usb2 = "USB2";
9181 const uint8_t hdmi1 = 0;
9182 const uint8_t hdmi2 = 1;
9183 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009184 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009185
9186 addConfigurationProperty("touch.deviceType", "touchScreen");
9187 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009188 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009189
9190 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9191 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9192
9193 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9194 // for this input device is specified, and the matching viewport is not present,
9195 // the input device should be disabled (at the mapper level).
9196
9197 // Add viewport for display 2 on hdmi2
9198 prepareSecondaryDisplay(type, hdmi2);
9199 // Send a touch event
9200 processPosition(mapper, 100, 100);
9201 processSync(mapper);
9202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9203
9204 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009205 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009206 // Send a touch event again
9207 processPosition(mapper, 100, 100);
9208 processSync(mapper);
9209
9210 NotifyMotionArgs args;
9211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9212 ASSERT_EQ(DISPLAY_ID, args.displayId);
9213}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009214
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009215TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9216 addConfigurationProperty("touch.deviceType", "touchScreen");
9217 prepareAxes(POSITION);
9218 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9219
9220 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9221
Michael Wrighta9cf4192022-12-01 23:46:39 +00009222 prepareDisplay(ui::ROTATION_0);
9223 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009224
9225 // Send a touch event
9226 processPosition(mapper, 100, 100);
9227 processSync(mapper);
9228
9229 NotifyMotionArgs args;
9230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9231 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9232}
9233
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009234TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009235 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009236 std::shared_ptr<FakePointerController> fakePointerController =
9237 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009238 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009239 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009240 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009241
Garfield Tan888a6a42020-01-09 11:39:16 -08009242 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009243 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009244
Michael Wrighta9cf4192022-12-01 23:46:39 +00009245 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009246 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009247 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009248
Josep del Río2d8c79a2023-01-23 19:33:50 +00009249 // Check source is mouse that would obtain the PointerController.
9250 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009251
9252 NotifyMotionArgs motionArgs;
9253 processPosition(mapper, 100, 100);
9254 processSync(mapper);
9255
9256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9257 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9258 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9259}
9260
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009261/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009262 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9263 */
9264TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9265 addConfigurationProperty("touch.deviceType", "touchScreen");
9266 prepareAxes(POSITION);
9267 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9268
Michael Wrighta9cf4192022-12-01 23:46:39 +00009269 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009270 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9271 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9272 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9273 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009274
9275 NotifyMotionArgs args;
9276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9277 ASSERT_EQ(26, args.readTime);
9278
Harry Cutts33476232023-01-30 19:57:29 +00009279 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9280 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9281 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009282
9283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9284 ASSERT_EQ(33, args.readTime);
9285}
9286
9287/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009288 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9289 * events should not be delivered to the listener.
9290 */
9291TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9292 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009293 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009294 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009295 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009296 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009297 prepareAxes(POSITION);
9298 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9299
9300 NotifyMotionArgs motionArgs;
9301 processPosition(mapper, 100, 100);
9302 processSync(mapper);
9303
9304 mFakeListener->assertNotifyMotionWasNotCalled();
9305}
9306
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009307/**
9308 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9309 * the touch mapper can process the events and the events can be delivered to the listener.
9310 */
9311TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9312 addConfigurationProperty("touch.deviceType", "touchScreen");
9313 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009314 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009315 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009316 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009317 prepareAxes(POSITION);
9318 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9319
9320 NotifyMotionArgs motionArgs;
9321 processPosition(mapper, 100, 100);
9322 processSync(mapper);
9323
9324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9325 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9326}
9327
Garfield Tanc734e4f2021-01-15 20:01:39 -08009328TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9329 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009330 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009331 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009332 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009333 std::optional<DisplayViewport> optionalDisplayViewport =
9334 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9335 ASSERT_TRUE(optionalDisplayViewport.has_value());
9336 DisplayViewport displayViewport = *optionalDisplayViewport;
9337
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009338 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009339 prepareAxes(POSITION);
9340 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9341
9342 // Finger down
9343 int32_t x = 100, y = 100;
9344 processPosition(mapper, x, y);
9345 processSync(mapper);
9346
9347 NotifyMotionArgs motionArgs;
9348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9349 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9350
9351 // Deactivate display viewport
9352 displayViewport.isActive = false;
9353 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009354 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009355
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009356 // The ongoing touch should be canceled immediately
9357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9358 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9359
9360 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009361 x += 10, y += 10;
9362 processPosition(mapper, x, y);
9363 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009365
9366 // Reactivate display viewport
9367 displayViewport.isActive = true;
9368 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009369 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009370
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009371 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009372 x += 10, y += 10;
9373 processPosition(mapper, x, y);
9374 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9376 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009377}
9378
Arthur Hung7c645402019-01-25 17:45:42 +08009379TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9380 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009381 prepareAxes(POSITION | ID | SLOT);
9382 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009383 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009384
9385 // Create the second touch screen device, and enable multi fingers.
9386 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009387 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009388 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009389 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009390 std::shared_ptr<InputDevice> device2 =
9391 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009392 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009393
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009394 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009395 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009396 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009397 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009398 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009399 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009400 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009401 /*flat=*/0, /*fuzz=*/0);
9402 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009403 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9404 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009405
9406 // Setup the second touch screen device.
Arpit Singh8e6fb252023-04-06 11:49:17 +00009407 MultiTouchInputMapper& mapper2 =
9408 device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID,
9409 mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009410 std::list<NotifyArgs> unused =
9411 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009412 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009413 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009414
9415 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009416 std::shared_ptr<FakePointerController> fakePointerController =
9417 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009418 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009419
9420 // Setup policy for associated displays and show touches.
9421 const uint8_t hdmi1 = 0;
9422 const uint8_t hdmi2 = 1;
9423 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9424 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9425 mFakePolicy->setShowTouches(true);
9426
9427 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009428 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009429 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009430
9431 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009432 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009433 InputReaderConfiguration::Change::DISPLAY_INFO |
9434 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009435
9436 // Two fingers down at default display.
9437 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9438 processPosition(mapper, x1, y1);
9439 processId(mapper, 1);
9440 processSlot(mapper, 1);
9441 processPosition(mapper, x2, y2);
9442 processId(mapper, 2);
9443 processSync(mapper);
9444
9445 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9446 fakePointerController->getSpots().find(DISPLAY_ID);
9447 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9448 ASSERT_EQ(size_t(2), iter->second.size());
9449
9450 // Two fingers down at second display.
9451 processPosition(mapper2, x1, y1);
9452 processId(mapper2, 1);
9453 processSlot(mapper2, 1);
9454 processPosition(mapper2, x2, y2);
9455 processId(mapper2, 2);
9456 processSync(mapper2);
9457
9458 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9459 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9460 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009461
9462 // Disable the show touches configuration and ensure the spots are cleared.
9463 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009464 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009465 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009466
9467 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009468}
9469
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009470TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009471 prepareAxes(POSITION);
9472 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009473 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009474 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009475
9476 NotifyMotionArgs motionArgs;
9477 // Unrotated video frame
9478 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9479 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009480 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009481 processPosition(mapper, 100, 200);
9482 processSync(mapper);
9483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9484 ASSERT_EQ(frames, motionArgs.videoFrames);
9485
9486 // Subsequent touch events should not have any videoframes
9487 // This is implemented separately in FakeEventHub,
9488 // but that should match the behaviour of TouchVideoDevice.
9489 processPosition(mapper, 200, 200);
9490 processSync(mapper);
9491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9492 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9493}
9494
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009495TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009496 prepareAxes(POSITION);
9497 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009498 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009499 // Unrotated video frame
9500 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9501 NotifyMotionArgs motionArgs;
9502
9503 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009504 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009505 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9506 clearViewports();
9507 prepareDisplay(orientation);
9508 std::vector<TouchVideoFrame> frames{frame};
9509 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9510 processPosition(mapper, 100, 200);
9511 processSync(mapper);
9512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9513 ASSERT_EQ(frames, motionArgs.videoFrames);
9514 }
9515}
9516
9517TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9518 prepareAxes(POSITION);
9519 addConfigurationProperty("touch.deviceType", "touchScreen");
9520 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9521 // orientation-aware are affected by display rotation.
9522 addConfigurationProperty("touch.orientationAware", "0");
9523 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9524 // Unrotated video frame
9525 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9526 NotifyMotionArgs motionArgs;
9527
9528 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009529 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009530 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9531 clearViewports();
9532 prepareDisplay(orientation);
9533 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009534 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009535 processPosition(mapper, 100, 200);
9536 processSync(mapper);
9537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009538 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9539 // compared to the display. This is so that when the window transform (which contains the
9540 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9541 // window's coordinate space.
9542 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009543 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009544
9545 // Release finger.
9546 processSync(mapper);
9547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009548 }
9549}
9550
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009551TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009552 prepareAxes(POSITION);
9553 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009554 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009555 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9556 // so mix these.
9557 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9558 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9559 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9560 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9561 NotifyMotionArgs motionArgs;
9562
Michael Wrighta9cf4192022-12-01 23:46:39 +00009563 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009564 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009565 processPosition(mapper, 100, 200);
9566 processSync(mapper);
9567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009568 ASSERT_EQ(frames, motionArgs.videoFrames);
9569}
9570
9571TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9572 prepareAxes(POSITION);
9573 addConfigurationProperty("touch.deviceType", "touchScreen");
9574 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9575 // orientation-aware are affected by display rotation.
9576 addConfigurationProperty("touch.orientationAware", "0");
9577 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9578 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9579 // so mix these.
9580 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9581 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9582 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9583 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9584 NotifyMotionArgs motionArgs;
9585
Michael Wrighta9cf4192022-12-01 23:46:39 +00009586 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009587 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9588 processPosition(mapper, 100, 200);
9589 processSync(mapper);
9590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9591 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9592 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9593 // compared to the display. This is so that when the window transform (which contains the
9594 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9595 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009596 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009597 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009598 ASSERT_EQ(frames, motionArgs.videoFrames);
9599}
9600
Arthur Hung9da14732019-09-02 16:16:58 +08009601/**
9602 * If we had defined port associations, but the viewport is not ready, the touch device would be
9603 * expected to be disabled, and it should be enabled after the viewport has found.
9604 */
9605TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009606 constexpr uint8_t hdmi2 = 1;
9607 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009608 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009609
9610 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9611
9612 addConfigurationProperty("touch.deviceType", "touchScreen");
9613 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009614 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009615
9616 ASSERT_EQ(mDevice->isEnabled(), false);
9617
9618 // Add display on hdmi2, the device should be enabled and can receive touch event.
9619 prepareSecondaryDisplay(type, hdmi2);
9620 ASSERT_EQ(mDevice->isEnabled(), true);
9621
9622 // Send a touch event.
9623 processPosition(mapper, 100, 100);
9624 processSync(mapper);
9625
9626 NotifyMotionArgs args;
9627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9628 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9629}
9630
Arthur Hung421eb1c2020-01-16 00:09:42 +08009631TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009632 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009633 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009634 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009635 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009636
9637 NotifyMotionArgs motionArgs;
9638
9639 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9640 // finger down
9641 processId(mapper, 1);
9642 processPosition(mapper, x1, y1);
9643 processSync(mapper);
9644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9645 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009646 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009647
9648 // finger move
9649 processId(mapper, 1);
9650 processPosition(mapper, x2, y2);
9651 processSync(mapper);
9652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9653 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009654 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009655
9656 // finger up.
9657 processId(mapper, -1);
9658 processSync(mapper);
9659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9660 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009661 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009662
9663 // new finger down
9664 processId(mapper, 1);
9665 processPosition(mapper, x3, y3);
9666 processSync(mapper);
9667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9668 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009669 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009670}
9671
9672/**
arthurhungcc7f9802020-04-30 17:55:40 +08009673 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9674 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009675 */
arthurhungcc7f9802020-04-30 17:55:40 +08009676TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009677 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009678 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009679 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009680 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009681
9682 NotifyMotionArgs motionArgs;
9683
9684 // default tool type is finger
9685 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009686 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009687 processPosition(mapper, x1, y1);
9688 processSync(mapper);
9689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9690 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009691 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009692
9693 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9694 processToolType(mapper, MT_TOOL_PALM);
9695 processSync(mapper);
9696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9697 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9698
9699 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009700 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009701 processPosition(mapper, x2, y2);
9702 processSync(mapper);
9703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9704
9705 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009706 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009707 processSync(mapper);
9708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9709
9710 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009711 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009712 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009713 processPosition(mapper, x3, y3);
9714 processSync(mapper);
9715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9716 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009717 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009718}
9719
arthurhungbf89a482020-04-17 17:37:55 +08009720/**
arthurhungcc7f9802020-04-30 17:55:40 +08009721 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9722 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009723 */
arthurhungcc7f9802020-04-30 17:55:40 +08009724TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009725 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009726 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009727 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9728 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9729
9730 NotifyMotionArgs motionArgs;
9731
9732 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009733 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9734 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009735 processPosition(mapper, x1, y1);
9736 processSync(mapper);
9737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9738 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009739 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009740
9741 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009742 processSlot(mapper, SECOND_SLOT);
9743 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009744 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009745 processSync(mapper);
9746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009747 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009748 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009749
9750 // If the tool type of the first finger changes to MT_TOOL_PALM,
9751 // we expect to receive ACTION_POINTER_UP with cancel flag.
9752 processSlot(mapper, FIRST_SLOT);
9753 processId(mapper, FIRST_TRACKING_ID);
9754 processToolType(mapper, MT_TOOL_PALM);
9755 processSync(mapper);
9756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009757 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009758 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9759
9760 // The following MOVE events of second finger should be processed.
9761 processSlot(mapper, SECOND_SLOT);
9762 processId(mapper, SECOND_TRACKING_ID);
9763 processPosition(mapper, x2 + 1, y2 + 1);
9764 processSync(mapper);
9765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9766 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9767 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9768
9769 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9770 // it. Second finger receive move.
9771 processSlot(mapper, FIRST_SLOT);
9772 processId(mapper, INVALID_TRACKING_ID);
9773 processSync(mapper);
9774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9776 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9777
9778 // Second finger keeps moving.
9779 processSlot(mapper, SECOND_SLOT);
9780 processId(mapper, SECOND_TRACKING_ID);
9781 processPosition(mapper, x2 + 2, y2 + 2);
9782 processSync(mapper);
9783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9784 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9785 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9786
9787 // Second finger up.
9788 processId(mapper, INVALID_TRACKING_ID);
9789 processSync(mapper);
9790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9791 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9792 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9793}
9794
9795/**
9796 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9797 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9798 */
9799TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9800 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009801 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009802 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9803 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9804
9805 NotifyMotionArgs motionArgs;
9806
9807 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9808 // First finger down.
9809 processId(mapper, FIRST_TRACKING_ID);
9810 processPosition(mapper, x1, y1);
9811 processSync(mapper);
9812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9813 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009814 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009815
9816 // Second finger down.
9817 processSlot(mapper, SECOND_SLOT);
9818 processId(mapper, SECOND_TRACKING_ID);
9819 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009820 processSync(mapper);
9821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009822 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009823 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009824
arthurhungcc7f9802020-04-30 17:55:40 +08009825 // If the tool type of the first finger changes to MT_TOOL_PALM,
9826 // we expect to receive ACTION_POINTER_UP with cancel flag.
9827 processSlot(mapper, FIRST_SLOT);
9828 processId(mapper, FIRST_TRACKING_ID);
9829 processToolType(mapper, MT_TOOL_PALM);
9830 processSync(mapper);
9831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009832 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009833 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9834
9835 // Second finger keeps moving.
9836 processSlot(mapper, SECOND_SLOT);
9837 processId(mapper, SECOND_TRACKING_ID);
9838 processPosition(mapper, x2 + 1, y2 + 1);
9839 processSync(mapper);
9840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9841 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9842
9843 // second finger becomes palm, receive cancel due to only 1 finger is active.
9844 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009845 processToolType(mapper, MT_TOOL_PALM);
9846 processSync(mapper);
9847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9848 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9849
arthurhungcc7f9802020-04-30 17:55:40 +08009850 // third finger down.
9851 processSlot(mapper, THIRD_SLOT);
9852 processId(mapper, THIRD_TRACKING_ID);
9853 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009854 processPosition(mapper, x3, y3);
9855 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9857 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009858 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009859 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9860
9861 // third finger move
9862 processId(mapper, THIRD_TRACKING_ID);
9863 processPosition(mapper, x3 + 1, y3 + 1);
9864 processSync(mapper);
9865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9867
9868 // first finger up, third finger receive move.
9869 processSlot(mapper, FIRST_SLOT);
9870 processId(mapper, INVALID_TRACKING_ID);
9871 processSync(mapper);
9872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9874 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9875
9876 // second finger up, third finger receive move.
9877 processSlot(mapper, SECOND_SLOT);
9878 processId(mapper, INVALID_TRACKING_ID);
9879 processSync(mapper);
9880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9882 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9883
9884 // third finger up.
9885 processSlot(mapper, THIRD_SLOT);
9886 processId(mapper, INVALID_TRACKING_ID);
9887 processSync(mapper);
9888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9889 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9890 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9891}
9892
9893/**
9894 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9895 * and the active finger could still be allowed to receive the events
9896 */
9897TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9898 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009899 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009900 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9901 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9902
9903 NotifyMotionArgs motionArgs;
9904
9905 // default tool type is finger
9906 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9907 processId(mapper, FIRST_TRACKING_ID);
9908 processPosition(mapper, x1, y1);
9909 processSync(mapper);
9910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9911 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009912 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009913
9914 // Second finger down.
9915 processSlot(mapper, SECOND_SLOT);
9916 processId(mapper, SECOND_TRACKING_ID);
9917 processPosition(mapper, x2, y2);
9918 processSync(mapper);
9919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009920 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009921 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009922
9923 // If the tool type of the second finger changes to MT_TOOL_PALM,
9924 // we expect to receive ACTION_POINTER_UP with cancel flag.
9925 processId(mapper, SECOND_TRACKING_ID);
9926 processToolType(mapper, MT_TOOL_PALM);
9927 processSync(mapper);
9928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009929 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009930 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9931
9932 // The following MOVE event should be processed.
9933 processSlot(mapper, FIRST_SLOT);
9934 processId(mapper, FIRST_TRACKING_ID);
9935 processPosition(mapper, x1 + 1, y1 + 1);
9936 processSync(mapper);
9937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9938 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9939 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9940
9941 // second finger up.
9942 processSlot(mapper, SECOND_SLOT);
9943 processId(mapper, INVALID_TRACKING_ID);
9944 processSync(mapper);
9945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9946 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9947
9948 // first finger keep moving
9949 processSlot(mapper, FIRST_SLOT);
9950 processId(mapper, FIRST_TRACKING_ID);
9951 processPosition(mapper, x1 + 2, y1 + 2);
9952 processSync(mapper);
9953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9954 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9955
9956 // first finger up.
9957 processId(mapper, INVALID_TRACKING_ID);
9958 processSync(mapper);
9959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9960 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9961 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009962}
9963
Arthur Hung9ad18942021-06-19 02:04:46 +00009964/**
9965 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9966 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9967 * cause slot be valid again.
9968 */
9969TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9970 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009971 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009972 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9973 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9974
9975 NotifyMotionArgs motionArgs;
9976
9977 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9978 // First finger down.
9979 processId(mapper, FIRST_TRACKING_ID);
9980 processPosition(mapper, x1, y1);
9981 processPressure(mapper, RAW_PRESSURE_MAX);
9982 processSync(mapper);
9983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9984 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9985 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9986
9987 // First finger move.
9988 processId(mapper, FIRST_TRACKING_ID);
9989 processPosition(mapper, x1 + 1, y1 + 1);
9990 processPressure(mapper, RAW_PRESSURE_MAX);
9991 processSync(mapper);
9992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9993 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9994 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9995
9996 // Second finger down.
9997 processSlot(mapper, SECOND_SLOT);
9998 processId(mapper, SECOND_TRACKING_ID);
9999 processPosition(mapper, x2, y2);
10000 processPressure(mapper, RAW_PRESSURE_MAX);
10001 processSync(mapper);
10002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010003 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010004 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10005
10006 // second finger up with some unexpected data.
10007 processSlot(mapper, SECOND_SLOT);
10008 processId(mapper, INVALID_TRACKING_ID);
10009 processPosition(mapper, x2, y2);
10010 processSync(mapper);
10011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010012 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010013 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10014
10015 // first finger up with some unexpected data.
10016 processSlot(mapper, FIRST_SLOT);
10017 processId(mapper, INVALID_TRACKING_ID);
10018 processPosition(mapper, x2, y2);
10019 processPressure(mapper, RAW_PRESSURE_MAX);
10020 processSync(mapper);
10021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10022 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10023 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10024}
10025
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010026TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10027 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010028 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010029 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10030 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10031
10032 // First finger down.
10033 processId(mapper, FIRST_TRACKING_ID);
10034 processPosition(mapper, 100, 200);
10035 processPressure(mapper, RAW_PRESSURE_MAX);
10036 processSync(mapper);
10037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10038 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10039
10040 // Second finger down.
10041 processSlot(mapper, SECOND_SLOT);
10042 processId(mapper, SECOND_TRACKING_ID);
10043 processPosition(mapper, 300, 400);
10044 processPressure(mapper, RAW_PRESSURE_MAX);
10045 processSync(mapper);
10046 ASSERT_NO_FATAL_FAILURE(
10047 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10048
10049 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010050 // preserved. Resetting should cancel the ongoing gesture.
10051 resetMapper(mapper, ARBITRARY_TIME);
10052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10053 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010054
10055 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10056 // the existing touch state to generate a down event.
10057 processPosition(mapper, 301, 302);
10058 processSync(mapper);
10059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10060 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10062 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10063
10064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10065}
10066
10067TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10068 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010069 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010070 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10071 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10072
10073 // First finger touches down and releases.
10074 processId(mapper, FIRST_TRACKING_ID);
10075 processPosition(mapper, 100, 200);
10076 processPressure(mapper, RAW_PRESSURE_MAX);
10077 processSync(mapper);
10078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10079 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10080 processId(mapper, INVALID_TRACKING_ID);
10081 processSync(mapper);
10082 ASSERT_NO_FATAL_FAILURE(
10083 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10084
10085 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10086 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010087 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10089
10090 // Send an empty sync frame. Since there are no pointers, no events are generated.
10091 processSync(mapper);
10092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10093}
10094
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010095TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010096 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010097 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010098 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
10099 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010101
10102 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10103 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10104 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10105 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10106 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10107
10108 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010109 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010110 processId(mapper, FIRST_TRACKING_ID);
10111 processToolType(mapper, MT_TOOL_PEN);
10112 processPosition(mapper, 100, 200);
10113 processPressure(mapper, RAW_PRESSURE_MAX);
10114 processSync(mapper);
10115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10116 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10117 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010118 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010119
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010120 // Now that we know the device supports styluses, ensure that the device is re-configured with
10121 // the stylus source.
10122 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10123 {
10124 const auto& devices = mReader->getInputDevices();
10125 auto deviceInfo =
10126 std::find_if(devices.begin(), devices.end(),
10127 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10128 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10129 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10130 }
10131
10132 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10134
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010135 processId(mapper, INVALID_TRACKING_ID);
10136 processSync(mapper);
10137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10138 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10139 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010140 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010141}
10142
Seunghwan Choi356026c2023-02-01 14:37:25 +090010143TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10144 addConfigurationProperty("touch.deviceType", "touchScreen");
10145 prepareDisplay(ui::ROTATION_0);
10146 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10147 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10148 // indicate stylus presence dynamically.
10149 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10150 std::shared_ptr<FakePointerController> fakePointerController =
10151 std::make_shared<FakePointerController>();
10152 mFakePolicy->setPointerController(fakePointerController);
10153 mFakePolicy->setStylusPointerIconEnabled(true);
10154 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10155
10156 processId(mapper, FIRST_TRACKING_ID);
10157 processPressure(mapper, RAW_PRESSURE_MIN);
10158 processPosition(mapper, 100, 200);
10159 processToolType(mapper, MT_TOOL_PEN);
10160 processSync(mapper);
10161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10162 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010163 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010164 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10165 ASSERT_TRUE(fakePointerController->isPointerShown());
10166 ASSERT_NO_FATAL_FAILURE(
10167 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10168}
10169
10170TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10171 addConfigurationProperty("touch.deviceType", "touchScreen");
10172 prepareDisplay(ui::ROTATION_0);
10173 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10174 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10175 // indicate stylus presence dynamically.
10176 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10177 std::shared_ptr<FakePointerController> fakePointerController =
10178 std::make_shared<FakePointerController>();
10179 mFakePolicy->setPointerController(fakePointerController);
10180 mFakePolicy->setStylusPointerIconEnabled(false);
10181 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10182
10183 processId(mapper, FIRST_TRACKING_ID);
10184 processPressure(mapper, RAW_PRESSURE_MIN);
10185 processPosition(mapper, 100, 200);
10186 processToolType(mapper, MT_TOOL_PEN);
10187 processSync(mapper);
10188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10189 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010190 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010191 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10192 ASSERT_FALSE(fakePointerController->isPointerShown());
10193}
10194
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010195// --- MultiTouchInputMapperTest_ExternalDevice ---
10196
10197class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10198protected:
Chris Yea52ade12020-08-27 16:49:20 -070010199 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010200};
10201
10202/**
10203 * Expect fallback to internal viewport if device is external and external viewport is not present.
10204 */
10205TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10206 prepareAxes(POSITION);
10207 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010208 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010209 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10210
10211 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10212
10213 NotifyMotionArgs motionArgs;
10214
10215 // Expect the event to be sent to the internal viewport,
10216 // because an external viewport is not present.
10217 processPosition(mapper, 100, 100);
10218 processSync(mapper);
10219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10220 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10221
10222 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010223 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010224 processPosition(mapper, 100, 100);
10225 processSync(mapper);
10226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10227 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10228}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010229
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010230TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10231 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10232 std::shared_ptr<FakePointerController> fakePointerController =
10233 std::make_shared<FakePointerController>();
10234 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10235 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010236
10237 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010238 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010239 prepareAxes(POSITION | ID | SLOT);
10240 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10241 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10242 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010243 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010244 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10245
10246 // captured touchpad should be a touchpad source
10247 NotifyDeviceResetArgs resetArgs;
10248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10249 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10250
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010251 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010252
10253 const InputDeviceInfo::MotionRange* relRangeX =
10254 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10255 ASSERT_NE(relRangeX, nullptr);
10256 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10257 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10258 const InputDeviceInfo::MotionRange* relRangeY =
10259 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10260 ASSERT_NE(relRangeY, nullptr);
10261 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10262 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10263
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010264 // run captured pointer tests - note that this is unscaled, so input listener events should be
10265 // identical to what the hardware sends (accounting for any
10266 // calibration).
10267 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010268 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010269 processId(mapper, 1);
10270 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10271 processKey(mapper, BTN_TOUCH, 1);
10272 processSync(mapper);
10273
10274 // expect coord[0] to contain initial location of touch 0
10275 NotifyMotionArgs args;
10276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10277 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10278 ASSERT_EQ(1U, args.pointerCount);
10279 ASSERT_EQ(0, args.pointerProperties[0].id);
10280 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10281 ASSERT_NO_FATAL_FAILURE(
10282 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10283
10284 // FINGER 1 DOWN
10285 processSlot(mapper, 1);
10286 processId(mapper, 2);
10287 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10288 processSync(mapper);
10289
10290 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010292 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010293 ASSERT_EQ(2U, args.pointerCount);
10294 ASSERT_EQ(0, args.pointerProperties[0].id);
10295 ASSERT_EQ(1, args.pointerProperties[1].id);
10296 ASSERT_NO_FATAL_FAILURE(
10297 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10298 ASSERT_NO_FATAL_FAILURE(
10299 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10300
10301 // FINGER 1 MOVE
10302 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10303 processSync(mapper);
10304
10305 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10306 // from move
10307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10309 ASSERT_NO_FATAL_FAILURE(
10310 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10311 ASSERT_NO_FATAL_FAILURE(
10312 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10313
10314 // FINGER 0 MOVE
10315 processSlot(mapper, 0);
10316 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10317 processSync(mapper);
10318
10319 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10321 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10322 ASSERT_NO_FATAL_FAILURE(
10323 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10324 ASSERT_NO_FATAL_FAILURE(
10325 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10326
10327 // BUTTON DOWN
10328 processKey(mapper, BTN_LEFT, 1);
10329 processSync(mapper);
10330
10331 // touchinputmapper design sends a move before button press
10332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10335 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10336
10337 // BUTTON UP
10338 processKey(mapper, BTN_LEFT, 0);
10339 processSync(mapper);
10340
10341 // touchinputmapper design sends a move after button release
10342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10343 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10346
10347 // FINGER 0 UP
10348 processId(mapper, -1);
10349 processSync(mapper);
10350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10351 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10352
10353 // FINGER 1 MOVE
10354 processSlot(mapper, 1);
10355 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10356 processSync(mapper);
10357
10358 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10361 ASSERT_EQ(1U, args.pointerCount);
10362 ASSERT_EQ(1, args.pointerProperties[0].id);
10363 ASSERT_NO_FATAL_FAILURE(
10364 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10365
10366 // FINGER 1 UP
10367 processId(mapper, -1);
10368 processKey(mapper, BTN_TOUCH, 0);
10369 processSync(mapper);
10370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10371 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10372
Josep del Río2d8c79a2023-01-23 19:33:50 +000010373 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010374 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010375 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010377 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010378}
10379
10380TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10381 std::shared_ptr<FakePointerController> fakePointerController =
10382 std::make_shared<FakePointerController>();
10383 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10384 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010385
10386 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010387 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010388 prepareAxes(POSITION | ID | SLOT);
10389 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10390 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010391 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010392 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10393 // run uncaptured pointer tests - pushes out generic events
10394 // FINGER 0 DOWN
10395 processId(mapper, 3);
10396 processPosition(mapper, 100, 100);
10397 processKey(mapper, BTN_TOUCH, 1);
10398 processSync(mapper);
10399
10400 // start at (100,100), cursor should be at (0,0) * scale
10401 NotifyMotionArgs args;
10402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10403 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10404 ASSERT_NO_FATAL_FAILURE(
10405 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10406
10407 // FINGER 0 MOVE
10408 processPosition(mapper, 200, 200);
10409 processSync(mapper);
10410
10411 // compute scaling to help with touch position checking
10412 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10413 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10414 float scale =
10415 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10416
10417 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10419 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10421 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010422
10423 // BUTTON DOWN
10424 processKey(mapper, BTN_LEFT, 1);
10425 processSync(mapper);
10426
10427 // touchinputmapper design sends a move before button press
10428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10429 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10431 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10432
10433 // BUTTON UP
10434 processKey(mapper, BTN_LEFT, 0);
10435 processSync(mapper);
10436
10437 // touchinputmapper design sends a move after button release
10438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10439 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10441 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010442}
10443
10444TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10445 std::shared_ptr<FakePointerController> fakePointerController =
10446 std::make_shared<FakePointerController>();
10447
Michael Wrighta9cf4192022-12-01 23:46:39 +000010448 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010449 prepareAxes(POSITION | ID | SLOT);
10450 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010451 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010452 mFakePolicy->setPointerCapture(false);
10453 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10454
Josep del Río2d8c79a2023-01-23 19:33:50 +000010455 // uncaptured touchpad should be a pointer device
10456 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010457
Josep del Río2d8c79a2023-01-23 19:33:50 +000010458 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010459 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010460 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010461 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10462}
10463
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010464// --- BluetoothMultiTouchInputMapperTest ---
10465
10466class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10467protected:
10468 void SetUp() override {
10469 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10470 }
10471};
10472
10473TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10474 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010475 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010476 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10477 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10478
10479 nsecs_t kernelEventTime = ARBITRARY_TIME;
10480 nsecs_t expectedEventTime = ARBITRARY_TIME;
10481 // Touch down.
10482 processId(mapper, FIRST_TRACKING_ID);
10483 processPosition(mapper, 100, 200);
10484 processPressure(mapper, RAW_PRESSURE_MAX);
10485 processSync(mapper, ARBITRARY_TIME);
10486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10487 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10488
10489 // Process several events that come in quick succession, according to their timestamps.
10490 for (int i = 0; i < 3; i++) {
10491 constexpr static nsecs_t delta = ms2ns(1);
10492 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10493 kernelEventTime += delta;
10494 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10495
10496 processPosition(mapper, 101 + i, 201 + i);
10497 processSync(mapper, kernelEventTime);
10498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10499 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10500 WithEventTime(expectedEventTime))));
10501 }
10502
10503 // Release the touch.
10504 processId(mapper, INVALID_TRACKING_ID);
10505 processPressure(mapper, RAW_PRESSURE_MIN);
10506 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10508 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10509 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10510}
10511
10512// --- MultiTouchPointerModeTest ---
10513
HQ Liue6983c72022-04-19 22:14:56 +000010514class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10515protected:
10516 float mPointerMovementScale;
10517 float mPointerXZoomScale;
10518 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10519 addConfigurationProperty("touch.deviceType", "pointer");
10520 std::shared_ptr<FakePointerController> fakePointerController =
10521 std::make_shared<FakePointerController>();
10522 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10523 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010524 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010525
10526 prepareAxes(POSITION);
10527 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10528 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10529 // needs to be disabled, and the pointer gesture needs to be enabled.
10530 mFakePolicy->setPointerCapture(false);
10531 mFakePolicy->setPointerGestureEnabled(true);
10532 mFakePolicy->setPointerController(fakePointerController);
10533
10534 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10535 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10536 mPointerMovementScale =
10537 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10538 mPointerXZoomScale =
10539 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10540 }
10541
10542 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10543 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10544 /*flat*/ 0,
10545 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10546 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10547 /*flat*/ 0,
10548 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10549 }
10550};
10551
10552/**
10553 * Two fingers down on a pointer mode touch pad. The width
10554 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10555 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10556 * be greater than the both value to be freeform gesture, so that after two
10557 * fingers start to move downwards, the gesture should be swipe.
10558 */
10559TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10560 // The min freeform gesture width is 25units/mm x 30mm = 750
10561 // which is greater than fraction of the diagnal length of the touchpad (349).
10562 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010563 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010564 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10565 NotifyMotionArgs motionArgs;
10566
10567 // Two fingers down at once.
10568 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10569 // Pointer's initial position is used the [0,0] coordinate.
10570 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10571
10572 processId(mapper, FIRST_TRACKING_ID);
10573 processPosition(mapper, x1, y1);
10574 processMTSync(mapper);
10575 processId(mapper, SECOND_TRACKING_ID);
10576 processPosition(mapper, x2, y2);
10577 processMTSync(mapper);
10578 processSync(mapper);
10579
10580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10581 ASSERT_EQ(1U, motionArgs.pointerCount);
10582 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010583 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010584 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010585 ASSERT_NO_FATAL_FAILURE(
10586 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10587
10588 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10589 // that there should be 1 pointer.
10590 int32_t movingDistance = 200;
10591 y1 += movingDistance;
10592 y2 += movingDistance;
10593
10594 processId(mapper, FIRST_TRACKING_ID);
10595 processPosition(mapper, x1, y1);
10596 processMTSync(mapper);
10597 processId(mapper, SECOND_TRACKING_ID);
10598 processPosition(mapper, x2, y2);
10599 processMTSync(mapper);
10600 processSync(mapper);
10601
10602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10603 ASSERT_EQ(1U, motionArgs.pointerCount);
10604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010605 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010606 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10608 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10609 0, 0, 0, 0));
10610}
10611
10612/**
10613 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10614 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10615 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10616 * value to be freeform gesture, so that after two fingers start to move downwards,
10617 * the gesture should be swipe.
10618 */
10619TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10620 // The min freeform gesture width is 5units/mm x 30mm = 150
10621 // which is greater than fraction of the diagnal length of the touchpad (349).
10622 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010623 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
HQ Liue6983c72022-04-19 22:14:56 +000010624 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10625 NotifyMotionArgs motionArgs;
10626
10627 // Two fingers down at once.
10628 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10629 // Pointer's initial position is used the [0,0] coordinate.
10630 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10631
10632 processId(mapper, FIRST_TRACKING_ID);
10633 processPosition(mapper, x1, y1);
10634 processMTSync(mapper);
10635 processId(mapper, SECOND_TRACKING_ID);
10636 processPosition(mapper, x2, y2);
10637 processMTSync(mapper);
10638 processSync(mapper);
10639
10640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10641 ASSERT_EQ(1U, motionArgs.pointerCount);
10642 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010643 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010644 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010645 ASSERT_NO_FATAL_FAILURE(
10646 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10647
10648 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10649 // and there should be 1 pointer.
10650 int32_t movingDistance = 200;
10651 y1 += movingDistance;
10652 y2 += movingDistance;
10653
10654 processId(mapper, FIRST_TRACKING_ID);
10655 processPosition(mapper, x1, y1);
10656 processMTSync(mapper);
10657 processId(mapper, SECOND_TRACKING_ID);
10658 processPosition(mapper, x2, y2);
10659 processMTSync(mapper);
10660 processSync(mapper);
10661
10662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10663 ASSERT_EQ(1U, motionArgs.pointerCount);
10664 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010665 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010666 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010667 // New coordinate is the scaled relative coordinate from the initial coordinate.
10668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10669 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10670 0, 0, 0, 0));
10671}
10672
10673/**
10674 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10675 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10676 * freeform gestures after two fingers start to move downwards.
10677 */
10678TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010679 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010680 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10681
10682 NotifyMotionArgs motionArgs;
10683
10684 // Two fingers down at once. Wider than the max swipe width.
10685 // The gesture is expected to be PRESS, then transformed to FREEFORM
10686 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10687
10688 processId(mapper, FIRST_TRACKING_ID);
10689 processPosition(mapper, x1, y1);
10690 processMTSync(mapper);
10691 processId(mapper, SECOND_TRACKING_ID);
10692 processPosition(mapper, x2, y2);
10693 processMTSync(mapper);
10694 processSync(mapper);
10695
10696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10697 ASSERT_EQ(1U, motionArgs.pointerCount);
10698 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010699 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010700 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010701 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10702 ASSERT_NO_FATAL_FAILURE(
10703 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10704
10705 int32_t movingDistance = 200;
10706
10707 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10708 // then two down events for two pointers.
10709 y1 += movingDistance;
10710 y2 += movingDistance;
10711
10712 processId(mapper, FIRST_TRACKING_ID);
10713 processPosition(mapper, x1, y1);
10714 processMTSync(mapper);
10715 processId(mapper, SECOND_TRACKING_ID);
10716 processPosition(mapper, x2, y2);
10717 processMTSync(mapper);
10718 processSync(mapper);
10719
10720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10721 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10722 ASSERT_EQ(1U, motionArgs.pointerCount);
10723 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010725 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
HQ Liue6983c72022-04-19 22:14:56 +000010726 ASSERT_EQ(1U, motionArgs.pointerCount);
10727 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010729 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010730 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010731 ASSERT_EQ(2U, motionArgs.pointerCount);
10732 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010733 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010734 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010735 // Two pointers' scaled relative coordinates from their initial centroid.
10736 // Initial y coordinates are 0 as y1 and y2 have the same value.
10737 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10738 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10739 // When pointers move, the new coordinates equal to the initial coordinates plus
10740 // scaled moving distance.
10741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10742 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10743 0, 0, 0, 0));
10744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10745 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10746 0, 0, 0, 0));
10747
10748 // Move two fingers down again, expect one MOVE motion event.
10749 y1 += movingDistance;
10750 y2 += movingDistance;
10751
10752 processId(mapper, FIRST_TRACKING_ID);
10753 processPosition(mapper, x1, y1);
10754 processMTSync(mapper);
10755 processId(mapper, SECOND_TRACKING_ID);
10756 processPosition(mapper, x2, y2);
10757 processMTSync(mapper);
10758 processSync(mapper);
10759
10760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10761 ASSERT_EQ(2U, motionArgs.pointerCount);
10762 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010763 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010764 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10766 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10767 0, 0, 0, 0, 0));
10768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10769 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10770 0, 0, 0, 0, 0));
10771}
10772
Harry Cutts39b7ca22022-10-05 15:55:48 +000010773TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010774 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Harry Cutts39b7ca22022-10-05 15:55:48 +000010775 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10776 NotifyMotionArgs motionArgs;
10777
10778 // Place two fingers down.
10779 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10780
10781 processId(mapper, FIRST_TRACKING_ID);
10782 processPosition(mapper, x1, y1);
10783 processMTSync(mapper);
10784 processId(mapper, SECOND_TRACKING_ID);
10785 processPosition(mapper, x2, y2);
10786 processMTSync(mapper);
10787 processSync(mapper);
10788
10789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10790 ASSERT_EQ(1U, motionArgs.pointerCount);
10791 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10792 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10793 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10794 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10795
10796 // Move the two fingers down and to the left.
10797 int32_t movingDistance = 200;
10798 x1 -= movingDistance;
10799 y1 += movingDistance;
10800 x2 -= movingDistance;
10801 y2 += movingDistance;
10802
10803 processId(mapper, FIRST_TRACKING_ID);
10804 processPosition(mapper, x1, y1);
10805 processMTSync(mapper);
10806 processId(mapper, SECOND_TRACKING_ID);
10807 processPosition(mapper, x2, y2);
10808 processMTSync(mapper);
10809 processSync(mapper);
10810
10811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10812 ASSERT_EQ(1U, motionArgs.pointerCount);
10813 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10814 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10815 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10816 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10817}
10818
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010819TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010820 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010821 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10822 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10824
10825 // Start a stylus gesture.
10826 processKey(mapper, BTN_TOOL_PEN, 1);
10827 processId(mapper, FIRST_TRACKING_ID);
10828 processPosition(mapper, 100, 200);
10829 processSync(mapper);
10830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10831 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10832 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010833 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010834 // TODO(b/257078296): Pointer mode generates extra event.
10835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10836 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10837 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010838 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10840
10841 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10842 // gesture should be disabled.
10843 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10844 viewport->isActive = false;
10845 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010846 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10848 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10849 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010850 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010851 // TODO(b/257078296): Pointer mode generates extra event.
10852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10853 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10854 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010855 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10857}
10858
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010859// --- JoystickInputMapperTest ---
10860
10861class JoystickInputMapperTest : public InputMapperTest {
10862protected:
10863 static const int32_t RAW_X_MIN;
10864 static const int32_t RAW_X_MAX;
10865 static const int32_t RAW_Y_MIN;
10866 static const int32_t RAW_Y_MAX;
10867
10868 void SetUp() override {
10869 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10870 }
10871 void prepareAxes() {
10872 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10873 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10874 }
10875
10876 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10877 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10878 }
10879
10880 void processSync(JoystickInputMapper& mapper) {
10881 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10882 }
10883
Michael Wrighta9cf4192022-12-01 23:46:39 +000010884 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010885 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10886 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10887 NO_PORT, ViewportType::VIRTUAL);
10888 }
10889};
10890
10891const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10892const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10893const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10894const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10895
10896TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10897 prepareAxes();
10898 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10899
10900 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10901
Michael Wrighta9cf4192022-12-01 23:46:39 +000010902 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010903
10904 // Send an axis event
10905 processAxis(mapper, ABS_X, 100);
10906 processSync(mapper);
10907
10908 NotifyMotionArgs args;
10909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10910 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10911
10912 // Send another axis event
10913 processAxis(mapper, ABS_Y, 100);
10914 processSync(mapper);
10915
10916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10917 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10918}
10919
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010920// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010921
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010922class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010923protected:
10924 static const char* DEVICE_NAME;
10925 static const char* DEVICE_LOCATION;
10926 static const int32_t DEVICE_ID;
10927 static const int32_t DEVICE_GENERATION;
10928 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010929 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010930 static const int32_t EVENTHUB_ID;
10931
10932 std::shared_ptr<FakeEventHub> mFakeEventHub;
10933 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010934 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010935 std::unique_ptr<InstrumentedInputReader> mReader;
10936 std::shared_ptr<InputDevice> mDevice;
10937
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010938 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010939 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010940 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010941 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010942 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010943 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010944 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10945 }
10946
10947 void SetUp() override { SetUp(DEVICE_CLASSES); }
10948
10949 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010950 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010951 mFakePolicy.clear();
10952 }
10953
Chris Yee2b1e5c2021-03-10 22:45:12 -080010954 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10955 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010956 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010957 InputDeviceIdentifier identifier;
10958 identifier.name = name;
10959 identifier.location = location;
10960 std::shared_ptr<InputDevice> device =
10961 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10962 identifier);
10963 mReader->pushNextDevice(device);
10964 mFakeEventHub->addDevice(eventHubId, name, classes);
10965 mReader->loopOnce();
10966 return device;
10967 }
10968
10969 template <class T, typename... Args>
10970 T& addControllerAndConfigure(Args... args) {
10971 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10972
10973 return controller;
10974 }
10975};
10976
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010977const char* PeripheralControllerTest::DEVICE_NAME = "device";
10978const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10979const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10980const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10981const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010982const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10983 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010984const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010985
10986// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010987class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010988protected:
10989 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010990 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010991 }
10992};
10993
10994TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010995 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010996
Harry Cuttsa5b71292022-11-28 12:56:17 +000010997 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10998 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10999 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011000}
11001
11002TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011003 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011004
Harry Cuttsa5b71292022-11-28 12:56:17 +000011005 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11006 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11007 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011008}
11009
11010// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011011class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011012protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011013 void SetUp() override {
11014 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11015 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011016};
11017
Chris Ye85758332021-05-16 23:05:17 -070011018TEST_F(LightControllerTest, MonoLight) {
11019 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011020 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011021 .maxBrightness = 255,
11022 .flags = InputLightClass::BRIGHTNESS,
11023 .path = ""};
11024 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011025
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011026 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011027 InputDeviceInfo info;
11028 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011029 std::vector<InputDeviceLightInfo> lights = info.getLights();
11030 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011031 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11032 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11033
11034 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11035 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11036}
11037
11038TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11039 RawLightInfo infoMono = {.id = 1,
11040 .name = "mono_keyboard_backlight",
11041 .maxBrightness = 255,
11042 .flags = InputLightClass::BRIGHTNESS |
11043 InputLightClass::KEYBOARD_BACKLIGHT,
11044 .path = ""};
11045 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11046
11047 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11048 InputDeviceInfo info;
11049 controller.populateDeviceInfo(&info);
11050 std::vector<InputDeviceLightInfo> lights = info.getLights();
11051 ASSERT_EQ(1U, lights.size());
11052 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11053 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011054
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011055 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11056 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011057}
11058
11059TEST_F(LightControllerTest, RGBLight) {
11060 RawLightInfo infoRed = {.id = 1,
11061 .name = "red",
11062 .maxBrightness = 255,
11063 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11064 .path = ""};
11065 RawLightInfo infoGreen = {.id = 2,
11066 .name = "green",
11067 .maxBrightness = 255,
11068 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11069 .path = ""};
11070 RawLightInfo infoBlue = {.id = 3,
11071 .name = "blue",
11072 .maxBrightness = 255,
11073 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11074 .path = ""};
11075 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11076 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11077 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11078
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011079 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011080 InputDeviceInfo info;
11081 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011082 std::vector<InputDeviceLightInfo> lights = info.getLights();
11083 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011084 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11085 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11086 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11087
11088 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11089 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11090}
11091
11092TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11093 RawLightInfo infoRed = {.id = 1,
11094 .name = "red_keyboard_backlight",
11095 .maxBrightness = 255,
11096 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11097 InputLightClass::KEYBOARD_BACKLIGHT,
11098 .path = ""};
11099 RawLightInfo infoGreen = {.id = 2,
11100 .name = "green_keyboard_backlight",
11101 .maxBrightness = 255,
11102 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11103 InputLightClass::KEYBOARD_BACKLIGHT,
11104 .path = ""};
11105 RawLightInfo infoBlue = {.id = 3,
11106 .name = "blue_keyboard_backlight",
11107 .maxBrightness = 255,
11108 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11109 InputLightClass::KEYBOARD_BACKLIGHT,
11110 .path = ""};
11111 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11112 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11113 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11114
11115 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11116 InputDeviceInfo info;
11117 controller.populateDeviceInfo(&info);
11118 std::vector<InputDeviceLightInfo> lights = info.getLights();
11119 ASSERT_EQ(1U, lights.size());
11120 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11121 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11122 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11123
11124 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11125 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11126}
11127
11128TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11129 RawLightInfo infoRed = {.id = 1,
11130 .name = "red",
11131 .maxBrightness = 255,
11132 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11133 .path = ""};
11134 RawLightInfo infoGreen = {.id = 2,
11135 .name = "green",
11136 .maxBrightness = 255,
11137 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11138 .path = ""};
11139 RawLightInfo infoBlue = {.id = 3,
11140 .name = "blue",
11141 .maxBrightness = 255,
11142 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11143 .path = ""};
11144 RawLightInfo infoGlobal = {.id = 3,
11145 .name = "global_keyboard_backlight",
11146 .maxBrightness = 255,
11147 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11148 InputLightClass::KEYBOARD_BACKLIGHT,
11149 .path = ""};
11150 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11151 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11152 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11153 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11154
11155 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11156 InputDeviceInfo info;
11157 controller.populateDeviceInfo(&info);
11158 std::vector<InputDeviceLightInfo> lights = info.getLights();
11159 ASSERT_EQ(1U, lights.size());
11160 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11161 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11162 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011163
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011164 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11165 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011166}
11167
11168TEST_F(LightControllerTest, MultiColorRGBLight) {
11169 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011170 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011171 .maxBrightness = 255,
11172 .flags = InputLightClass::BRIGHTNESS |
11173 InputLightClass::MULTI_INTENSITY |
11174 InputLightClass::MULTI_INDEX,
11175 .path = ""};
11176
11177 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11178
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011179 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011180 InputDeviceInfo info;
11181 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011182 std::vector<InputDeviceLightInfo> lights = info.getLights();
11183 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011184 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11185 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11186 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11187
11188 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11189 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11190}
11191
11192TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11193 RawLightInfo infoColor = {.id = 1,
11194 .name = "multi_color_keyboard_backlight",
11195 .maxBrightness = 255,
11196 .flags = InputLightClass::BRIGHTNESS |
11197 InputLightClass::MULTI_INTENSITY |
11198 InputLightClass::MULTI_INDEX |
11199 InputLightClass::KEYBOARD_BACKLIGHT,
11200 .path = ""};
11201
11202 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11203
11204 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11205 InputDeviceInfo info;
11206 controller.populateDeviceInfo(&info);
11207 std::vector<InputDeviceLightInfo> lights = info.getLights();
11208 ASSERT_EQ(1U, lights.size());
11209 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11210 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11211 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011212
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011213 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11214 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011215}
11216
11217TEST_F(LightControllerTest, PlayerIdLight) {
11218 RawLightInfo info1 = {.id = 1,
11219 .name = "player1",
11220 .maxBrightness = 255,
11221 .flags = InputLightClass::BRIGHTNESS,
11222 .path = ""};
11223 RawLightInfo info2 = {.id = 2,
11224 .name = "player2",
11225 .maxBrightness = 255,
11226 .flags = InputLightClass::BRIGHTNESS,
11227 .path = ""};
11228 RawLightInfo info3 = {.id = 3,
11229 .name = "player3",
11230 .maxBrightness = 255,
11231 .flags = InputLightClass::BRIGHTNESS,
11232 .path = ""};
11233 RawLightInfo info4 = {.id = 4,
11234 .name = "player4",
11235 .maxBrightness = 255,
11236 .flags = InputLightClass::BRIGHTNESS,
11237 .path = ""};
11238 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11239 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11240 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11241 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11242
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011243 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011244 InputDeviceInfo info;
11245 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011246 std::vector<InputDeviceLightInfo> lights = info.getLights();
11247 ASSERT_EQ(1U, lights.size());
11248 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011249 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11250 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011251
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011252 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11253 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11254 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011255}
11256
Michael Wrightd02c5b62014-02-10 15:10:22 -080011257} // namespace android