blob: 014cc781ee97c51bae6d5ed0a1da26edd821b16d [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,
Arpit Singh4be4eef2023-03-28 14:26:01 +0000268 uint32_t 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();
Arthur Hungc23540e2018-11-29 20:42:11 +0800274 if (displayPort && (changes & 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());
607 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800608 }
609
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800610 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700611 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000612 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700613 }
614
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800615 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700616 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000617 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700618 }
619
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800620 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700621 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700622 ftl::Flags<InputDeviceClass> classes,
623 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800624 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800625 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000626 FakeInputMapper& mapper =
627 device->addMapper<FakeInputMapper>(eventHubId,
628 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800629 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800630 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800631 return mapper;
632 }
633};
634
Chris Ye98d3f532020-10-01 21:48:59 -0700635TEST_F(InputReaderTest, PolicyGetInputDevices) {
636 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700637 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700638 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800639
640 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700641 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800642 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800643 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100644 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800645 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
646 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000647 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800648}
649
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000650TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
651 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
652 mFakeEventHub->setSysfsRootPath(1, "xyz");
653
654 // Should also have received a notification describing the new input device.
655 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
656 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
657 ASSERT_EQ(0U, inputDevice.getLights().size());
658
659 RawLightInfo infoMonolight = {.id = 123,
660 .name = "mono_keyboard_backlight",
661 .maxBrightness = 255,
662 .flags = InputLightClass::BRIGHTNESS,
663 .path = ""};
664 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
665 mReader->sysfsNodeChanged("xyz");
666 mReader->loopOnce();
667
668 // Should also have received a notification describing the new recreated input device.
669 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
670 inputDevice = mFakePolicy->getInputDevices()[0];
671 ASSERT_EQ(1U, inputDevice.getLights().size());
672}
673
Chris Yee7310032020-09-22 15:36:28 -0700674TEST_F(InputReaderTest, GetMergedInputDevices) {
675 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
676 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
677 // Add two subdevices to device
678 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
679 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000680 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
681 AINPUT_SOURCE_KEYBOARD);
682 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
683 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700684
685 // Push same device instance for next device to be added, so they'll have same identifier.
686 mReader->pushNextDevice(device);
687 mReader->pushNextDevice(device);
688 ASSERT_NO_FATAL_FAILURE(
689 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
690 ASSERT_NO_FATAL_FAILURE(
691 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
692
693 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000694 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700695}
696
Chris Yee14523a2020-12-19 13:46:00 -0800697TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
698 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
699 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
700 // Add two subdevices to device
701 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
702 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000703 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
704 AINPUT_SOURCE_KEYBOARD);
705 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
706 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800707
708 // Push same device instance for next device to be added, so they'll have same identifier.
709 mReader->pushNextDevice(device);
710 mReader->pushNextDevice(device);
711 // Sensor device is initially disabled
712 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
713 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
714 nullptr));
715 // Device is disabled because the only sub device is a sensor device and disabled initially.
716 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
717 ASSERT_FALSE(device->isEnabled());
718 ASSERT_NO_FATAL_FAILURE(
719 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
720 // The merged device is enabled if any sub device is enabled
721 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
722 ASSERT_TRUE(device->isEnabled());
723}
724
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700725TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800726 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700727 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800728 constexpr int32_t eventHubId = 1;
729 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700730 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000731 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
732 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800733 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800734 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700735
Yi Kong9b14ac62018-07-17 13:48:38 -0700736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700737
738 NotifyDeviceResetArgs resetArgs;
739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700740 ASSERT_EQ(deviceId, resetArgs.deviceId);
741
742 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800743 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000744 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700745
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700747 ASSERT_EQ(deviceId, resetArgs.deviceId);
748 ASSERT_EQ(device->isEnabled(), false);
749
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800750 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000751 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700754 ASSERT_EQ(device->isEnabled(), false);
755
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800756 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000757 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700759 ASSERT_EQ(deviceId, resetArgs.deviceId);
760 ASSERT_EQ(device->isEnabled(), true);
761}
762
Michael Wrightd02c5b62014-02-10 15:10:22 -0800763TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800764 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700765 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800766 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800767 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800768 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800769 AINPUT_SOURCE_KEYBOARD, nullptr);
770 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800771
772 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
773 AINPUT_SOURCE_ANY, AKEYCODE_A))
774 << "Should return unknown when the device id is >= 0 but unknown.";
775
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800776 ASSERT_EQ(AKEY_STATE_UNKNOWN,
777 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
778 << "Should return unknown when the device id is valid but the sources are not "
779 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800780
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800781 ASSERT_EQ(AKEY_STATE_DOWN,
782 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
783 AKEYCODE_A))
784 << "Should return value provided by mapper when device id is valid and the device "
785 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800786
787 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
788 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
789 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
790
791 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
792 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
793 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
794}
795
Philip Junker4af3b3d2021-12-14 10:36:55 +0100796TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
797 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
798 constexpr int32_t eventHubId = 1;
799 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
800 InputDeviceClass::KEYBOARD,
801 AINPUT_SOURCE_KEYBOARD, nullptr);
802 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
803
804 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
805 << "Should return unknown when the device with the specified id is not found.";
806
807 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
808 << "Should return correct mapping when device id is valid and mapping exists.";
809
810 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
811 << "Should return the location key code when device id is valid and there's no "
812 "mapping.";
813}
814
815TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
816 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
817 constexpr int32_t eventHubId = 1;
818 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
819 InputDeviceClass::JOYSTICK,
820 AINPUT_SOURCE_GAMEPAD, nullptr);
821 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
822
823 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
824 << "Should return unknown when the device id is valid but there is no keyboard mapper";
825}
826
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800828 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700829 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800830 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800831 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800832 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800833 AINPUT_SOURCE_KEYBOARD, nullptr);
834 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800835
836 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
837 AINPUT_SOURCE_ANY, KEY_A))
838 << "Should return unknown when the device id is >= 0 but unknown.";
839
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800840 ASSERT_EQ(AKEY_STATE_UNKNOWN,
841 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
842 << "Should return unknown when the device id is valid but the sources are not "
843 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800844
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800845 ASSERT_EQ(AKEY_STATE_DOWN,
846 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
847 KEY_A))
848 << "Should return value provided by mapper when device id is valid and the device "
849 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800850
851 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
852 AINPUT_SOURCE_TRACKBALL, KEY_A))
853 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
854
855 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
856 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
857 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
858}
859
860TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800861 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700862 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800863 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800864 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800865 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800866 AINPUT_SOURCE_KEYBOARD, nullptr);
867 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868
869 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
870 AINPUT_SOURCE_ANY, SW_LID))
871 << "Should return unknown when the device id is >= 0 but unknown.";
872
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800873 ASSERT_EQ(AKEY_STATE_UNKNOWN,
874 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
875 << "Should return unknown when the device id is valid but the sources are not "
876 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800877
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800878 ASSERT_EQ(AKEY_STATE_DOWN,
879 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
880 SW_LID))
881 << "Should return value provided by mapper when device id is valid and the device "
882 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800883
884 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
885 AINPUT_SOURCE_TRACKBALL, SW_LID))
886 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
887
888 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
889 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
890 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
891}
892
893TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800894 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700895 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800896 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800897 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800898 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800899 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100900
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800901 mapper.addSupportedKeyCode(AKEYCODE_A);
902 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800903
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700904 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905 uint8_t flags[4] = { 0, 0, 0, 1 };
906
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700907 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800908 << "Should return false when device id is >= 0 but unknown.";
909 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
910
911 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700912 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800913 << "Should return false when device id is valid but the sources are not supported by "
914 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800915 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
916
917 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700918 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800919 keyCodes, flags))
920 << "Should return value provided by mapper when device id is valid and the device "
921 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800922 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
923
924 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700925 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
926 << "Should return false when the device id is < 0 but the sources are not supported by "
927 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800928 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
929
930 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700931 ASSERT_TRUE(
932 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
933 << "Should return value provided by mapper when device id is < 0 and one of the "
934 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800935 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
936}
937
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000938TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800939 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700940 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800941
942 NotifyConfigurationChangedArgs args;
943
944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
945 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
946}
947
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000948TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800949 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700950 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000951 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800952 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000953 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800954 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800955 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800956 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800957
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000958 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000959 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
961
962 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800963 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000964 ASSERT_EQ(when, event.when);
965 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800966 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800967 ASSERT_EQ(EV_KEY, event.type);
968 ASSERT_EQ(KEY_A, event.code);
969 ASSERT_EQ(1, event.value);
970}
971
Garfield Tan1c7bc862020-01-28 13:24:04 -0800972TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800973 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700974 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800975 constexpr int32_t eventHubId = 1;
976 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800977 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000978 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
979 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800980 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800981 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800982
983 NotifyDeviceResetArgs resetArgs;
984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800985 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800986
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800987 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000988 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800990 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800991 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800992
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800993 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000994 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800996 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800997 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800998
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800999 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001000 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001002 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001003 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001004}
1005
Garfield Tan1c7bc862020-01-28 13:24:04 -08001006TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1007 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001008 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001009 constexpr int32_t eventHubId = 1;
1010 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1011 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001012 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1013 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001014 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001015 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1016
1017 NotifyDeviceResetArgs resetArgs;
1018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1019 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1020}
1021
Arthur Hungc23540e2018-11-29 20:42:11 +08001022TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001023 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001024 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001025 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001026 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001027 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1028 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001029 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1030 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001031 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001032
1033 const uint8_t hdmi1 = 1;
1034
1035 // Associated touch screen with second display.
1036 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1037
1038 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001039 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001040 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001041 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001042 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001043 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001044 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001045 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001046 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001047
1048 // Add the device, and make sure all of the callbacks are triggered.
1049 // The device is added after the input port associations are processed since
1050 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001051 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001054 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001055
Arthur Hung2c9a3342019-07-23 14:18:59 +08001056 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001057 ASSERT_EQ(deviceId, device->getId());
1058 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1059 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001060
1061 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001062 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001063 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001064 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001065}
1066
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001067TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1068 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001069 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001070 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1071 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1072 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001073 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1074 AINPUT_SOURCE_KEYBOARD);
1075 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1076 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001077 mReader->pushNextDevice(device);
1078 mReader->pushNextDevice(device);
1079 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1080 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1081
1082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1083
1084 NotifyDeviceResetArgs resetArgs;
1085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1086 ASSERT_EQ(deviceId, resetArgs.deviceId);
1087 ASSERT_TRUE(device->isEnabled());
1088 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1089 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1090
1091 disableDevice(deviceId);
1092 mReader->loopOnce();
1093
1094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1095 ASSERT_EQ(deviceId, resetArgs.deviceId);
1096 ASSERT_FALSE(device->isEnabled());
1097 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1098 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1099
1100 enableDevice(deviceId);
1101 mReader->loopOnce();
1102
1103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1104 ASSERT_EQ(deviceId, resetArgs.deviceId);
1105 ASSERT_TRUE(device->isEnabled());
1106 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1107 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1108}
1109
1110TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1111 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001112 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001113 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1114 // Add two subdevices to device
1115 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1116 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001117 device->addMapper<FakeInputMapper>(eventHubIds[0],
1118 mFakePolicy->getReaderConfiguration(),
1119 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001120 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001121 device->addMapper<FakeInputMapper>(eventHubIds[1],
1122 mFakePolicy->getReaderConfiguration(),
1123 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001124 mReader->pushNextDevice(device);
1125 mReader->pushNextDevice(device);
1126 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1127 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1128
1129 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1130 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1131
1132 ASSERT_EQ(AKEY_STATE_DOWN,
1133 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1134 ASSERT_EQ(AKEY_STATE_DOWN,
1135 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1136 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1137 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1138}
1139
Prabir Pradhan7e186182020-11-10 13:56:45 -08001140TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1141 NotifyPointerCaptureChangedArgs args;
1142
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001143 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001144 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1145 mReader->loopOnce();
1146 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001147 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1148 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001149
1150 mFakePolicy->setPointerCapture(false);
1151 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1152 mReader->loopOnce();
1153 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001154 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001155
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001156 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001157 // does not change.
1158 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1159 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001160 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001161}
1162
Chris Ye87143712020-11-10 05:05:58 +00001163class FakeVibratorInputMapper : public FakeInputMapper {
1164public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001165 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1166 const InputReaderConfiguration& readerConfig, uint32_t sources)
1167 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001168
1169 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1170};
1171
1172TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1173 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001174 ftl::Flags<InputDeviceClass> deviceClass =
1175 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001176 constexpr int32_t eventHubId = 1;
1177 const char* DEVICE_LOCATION = "BLUETOOTH";
1178 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1179 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001180 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1181 mFakePolicy->getReaderConfiguration(),
1182 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001183 mReader->pushNextDevice(device);
1184
1185 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1186 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1187
1188 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1189 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1190}
1191
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001192// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001193
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001194class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001195public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001196 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001197
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001198 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001199
Andy Chenf9f1a022022-08-29 20:07:10 -04001200 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1201
Chris Yee2b1e5c2021-03-10 22:45:12 -08001202 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1203
1204 void dump(std::string& dump) override {}
1205
1206 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1207 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001208 }
1209
Chris Yee2b1e5c2021-03-10 22:45:12 -08001210 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1211 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001212 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001213
1214 bool setLightColor(int32_t lightId, int32_t color) override {
1215 getDeviceContext().setLightBrightness(lightId, color >> 24);
1216 return true;
1217 }
1218
1219 std::optional<int32_t> getLightColor(int32_t lightId) override {
1220 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1221 if (!result.has_value()) {
1222 return std::nullopt;
1223 }
1224 return result.value() << 24;
1225 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001226
1227 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1228
1229 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1230
1231private:
1232 InputDeviceContext& mDeviceContext;
1233 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1234 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001235 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001236};
1237
Chris Yee2b1e5c2021-03-10 22:45:12 -08001238TEST_F(InputReaderTest, BatteryGetCapacity) {
1239 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001240 ftl::Flags<InputDeviceClass> deviceClass =
1241 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001242 constexpr int32_t eventHubId = 1;
1243 const char* DEVICE_LOCATION = "BLUETOOTH";
1244 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001245 FakePeripheralController& controller =
1246 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001247 mReader->pushNextDevice(device);
1248
1249 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1250
Harry Cuttsa5b71292022-11-28 12:56:17 +00001251 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1252 FakeEventHub::BATTERY_CAPACITY);
1253 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001254}
1255
1256TEST_F(InputReaderTest, BatteryGetStatus) {
1257 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001258 ftl::Flags<InputDeviceClass> deviceClass =
1259 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001260 constexpr int32_t eventHubId = 1;
1261 const char* DEVICE_LOCATION = "BLUETOOTH";
1262 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001263 FakePeripheralController& controller =
1264 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001265 mReader->pushNextDevice(device);
1266
1267 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1268
Harry Cuttsa5b71292022-11-28 12:56:17 +00001269 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1270 FakeEventHub::BATTERY_STATUS);
1271 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001272}
1273
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001274TEST_F(InputReaderTest, BatteryGetDevicePath) {
1275 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1276 ftl::Flags<InputDeviceClass> deviceClass =
1277 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1278 constexpr int32_t eventHubId = 1;
1279 const char* DEVICE_LOCATION = "BLUETOOTH";
1280 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1281 device->addController<FakePeripheralController>(eventHubId);
1282 mReader->pushNextDevice(device);
1283
1284 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1285
Harry Cuttsa5b71292022-11-28 12:56:17 +00001286 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001287}
1288
Chris Ye3fdbfef2021-01-06 18:45:18 -08001289TEST_F(InputReaderTest, LightGetColor) {
1290 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001291 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001292 constexpr int32_t eventHubId = 1;
1293 const char* DEVICE_LOCATION = "BLUETOOTH";
1294 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001295 FakePeripheralController& controller =
1296 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001297 mReader->pushNextDevice(device);
1298 RawLightInfo info = {.id = 1,
1299 .name = "Mono",
1300 .maxBrightness = 255,
1301 .flags = InputLightClass::BRIGHTNESS,
1302 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001303 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1304 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001305
1306 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001307
Harry Cutts33476232023-01-30 19:57:29 +00001308 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1309 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1310 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1311 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001312}
1313
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001314// --- InputReaderIntegrationTest ---
1315
1316// These tests create and interact with the InputReader only through its interface.
1317// The InputReader is started during SetUp(), which starts its processing in its own
1318// thread. The tests use linux uinput to emulate input devices.
1319// NOTE: Interacting with the physical device while these tests are running may cause
1320// the tests to fail.
1321class InputReaderIntegrationTest : public testing::Test {
1322protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001323 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001324 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001325 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001326
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001327 std::shared_ptr<FakePointerController> mFakePointerController;
1328
Chris Yea52ade12020-08-27 16:49:20 -07001329 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001330#if !defined(__ANDROID__)
1331 GTEST_SKIP();
1332#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001333 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001334 mFakePointerController = std::make_shared<FakePointerController>();
1335 mFakePolicy->setPointerController(mFakePointerController);
Harry Cutts33476232023-01-30 19:57:29 +00001336 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1337 /*eventDidNotHappenTimeout=*/30ms);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001338
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001339 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1340 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001341 ASSERT_EQ(mReader->start(), OK);
1342
1343 // Since this test is run on a real device, all the input devices connected
1344 // to the test device will show up in mReader. We wait for those input devices to
1345 // show up before beginning the tests.
1346 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1347 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1348 }
1349
Chris Yea52ade12020-08-27 16:49:20 -07001350 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001351#if !defined(__ANDROID__)
1352 return;
1353#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001354 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001355 mReader.reset();
1356 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001357 mFakePolicy.clear();
1358 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001359
1360 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1361 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1362 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1363 [&name](const InputDeviceInfo& info) {
1364 return info.getIdentifier().name == name;
1365 });
1366 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1367 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001368};
1369
1370TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1371 // An invalid input device that is only used for this test.
1372 class InvalidUinputDevice : public UinputDevice {
1373 public:
Harry Cutts33476232023-01-30 19:57:29 +00001374 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001375
1376 private:
1377 void configureDevice(int fd, uinput_user_dev* device) override {}
1378 };
1379
1380 const size_t numDevices = mFakePolicy->getInputDevices().size();
1381
1382 // UinputDevice does not set any event or key bits, so InputReader should not
1383 // consider it as a valid device.
1384 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1385 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1386 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1387 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1388
1389 invalidDevice.reset();
1390 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1391 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1392 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1393}
1394
1395TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1396 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1397
1398 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1399 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1400 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1401 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1402
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001403 const auto device = findDeviceByName(keyboard->getName());
1404 ASSERT_TRUE(device.has_value());
1405 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1406 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1407 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001408
1409 keyboard.reset();
1410 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1411 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1412 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1413}
1414
1415TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1416 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1417 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1418
1419 NotifyConfigurationChangedArgs configChangedArgs;
1420 ASSERT_NO_FATAL_FAILURE(
1421 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001422 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001423 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1424
1425 NotifyKeyArgs keyArgs;
1426 keyboard->pressAndReleaseHomeKey();
1427 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1428 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001429 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001430 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001431 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001432 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001433 prevTimestamp = keyArgs.eventTime;
1434
1435 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1436 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001437 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001438 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001439 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001440}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001441
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001442TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1443 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1444 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1445
1446 const auto device = findDeviceByName(stylus->getName());
1447 ASSERT_TRUE(device.has_value());
1448
Prabir Pradhana3621852022-10-14 18:57:23 +00001449 // An external stylus with buttons should also be recognized as a keyboard.
1450 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001451 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1452 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1453
1454 const auto DOWN =
1455 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1456 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1457
1458 stylus->pressAndReleaseKey(BTN_STYLUS);
1459 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1460 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1461 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1462 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1463
1464 stylus->pressAndReleaseKey(BTN_STYLUS2);
1465 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1466 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1467 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1468 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1469
1470 stylus->pressAndReleaseKey(BTN_STYLUS3);
1471 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1472 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1473 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1474 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1475}
1476
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001477/**
1478 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1479 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1480 * are passed to the listener.
1481 */
1482static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1483TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1484 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1485 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1486 NotifyKeyArgs keyArgs;
1487
1488 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1489 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1490 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1491 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1492
1493 controller->pressAndReleaseKey(BTN_GEAR_UP);
1494 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1495 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1496 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1497}
1498
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001499// --- TouchIntegrationTest ---
1500
Arthur Hungaab25622020-01-16 11:22:11 +08001501class TouchIntegrationTest : public InputReaderIntegrationTest {
1502protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001503 const std::string UNIQUE_ID = "local:0";
1504
Chris Yea52ade12020-08-27 16:49:20 -07001505 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001506#if !defined(__ANDROID__)
1507 GTEST_SKIP();
1508#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001509 InputReaderIntegrationTest::SetUp();
1510 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001511 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1512 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001513
1514 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1515 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1516 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001517 const auto info = findDeviceByName(mDevice->getName());
1518 ASSERT_TRUE(info);
1519 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001520 }
1521
1522 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001523 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001524 std::optional<uint8_t> physicalPort,
1525 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001526 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001527 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08001528 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1529 }
1530
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001531 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1532 NotifyMotionArgs args;
1533 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1534 EXPECT_EQ(action, args.action);
1535 ASSERT_EQ(points.size(), args.pointerCount);
1536 for (size_t i = 0; i < args.pointerCount; i++) {
1537 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1538 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1539 }
1540 }
1541
Arthur Hungaab25622020-01-16 11:22:11 +08001542 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001543 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001544};
1545
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001546TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
1547 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1548 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1549 // presses).
1550 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1551 mDeviceInfo.getSources());
1552}
1553
Arthur Hungaab25622020-01-16 11:22:11 +08001554TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1555 NotifyMotionArgs args;
1556 const Point centerPoint = mDevice->getCenterPoint();
1557
1558 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001559 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001560 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001561 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001562 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1563 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1564
1565 // ACTION_MOVE
1566 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001567 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001568 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1570
1571 // ACTION_UP
1572 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001573 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001574 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1575 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1576}
1577
1578TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1579 NotifyMotionArgs args;
1580 const Point centerPoint = mDevice->getCenterPoint();
1581
1582 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001583 mDevice->sendSlot(FIRST_SLOT);
1584 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001585 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001586 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001587 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1588 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1589
1590 // ACTION_POINTER_DOWN (Second slot)
1591 const Point secondPoint = centerPoint + Point(100, 100);
1592 mDevice->sendSlot(SECOND_SLOT);
1593 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001594 mDevice->sendDown(secondPoint);
1595 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001596 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001597 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001598
1599 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001600 mDevice->sendMove(secondPoint + Point(1, 1));
1601 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001602 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1603 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1604
1605 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001606 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001607 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001608 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001609 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001610
1611 // ACTION_UP
1612 mDevice->sendSlot(FIRST_SLOT);
1613 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001614 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001615 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1616 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1617}
1618
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001619/**
1620 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1621 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1622 * data?
1623 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1624 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1625 * for Pointer 0 only is generated after.
1626 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1627 * events, we will not miss any information.
1628 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1629 * event generated afterwards that contains the newest movement of pointer 0.
1630 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1631 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1632 * losing information about non-palm pointers.
1633 */
1634TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1635 NotifyMotionArgs args;
1636 const Point centerPoint = mDevice->getCenterPoint();
1637
1638 // ACTION_DOWN
1639 mDevice->sendSlot(FIRST_SLOT);
1640 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1641 mDevice->sendDown(centerPoint);
1642 mDevice->sendSync();
1643 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1644
1645 // ACTION_POINTER_DOWN (Second slot)
1646 const Point secondPoint = centerPoint + Point(100, 100);
1647 mDevice->sendSlot(SECOND_SLOT);
1648 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1649 mDevice->sendDown(secondPoint);
1650 mDevice->sendSync();
1651 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1652
1653 // ACTION_MOVE (First slot)
1654 mDevice->sendSlot(FIRST_SLOT);
1655 mDevice->sendMove(centerPoint + Point(5, 5));
1656 // ACTION_POINTER_UP (Second slot)
1657 mDevice->sendSlot(SECOND_SLOT);
1658 mDevice->sendPointerUp();
1659 // Send a single sync for the above 2 pointer updates
1660 mDevice->sendSync();
1661
1662 // First, we should get POINTER_UP for the second pointer
1663 assertReceivedMotion(ACTION_POINTER_1_UP,
1664 {/*first pointer */ centerPoint + Point(5, 5),
1665 /*second pointer*/ secondPoint});
1666
1667 // Next, the MOVE event for the first pointer
1668 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1669}
1670
1671/**
1672 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1673 * move, and then it will go up, all in the same frame.
1674 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1675 * gets sent to the listener.
1676 */
1677TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1678 NotifyMotionArgs args;
1679 const Point centerPoint = mDevice->getCenterPoint();
1680
1681 // ACTION_DOWN
1682 mDevice->sendSlot(FIRST_SLOT);
1683 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1684 mDevice->sendDown(centerPoint);
1685 mDevice->sendSync();
1686 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1687
1688 // ACTION_POINTER_DOWN (Second slot)
1689 const Point secondPoint = centerPoint + Point(100, 100);
1690 mDevice->sendSlot(SECOND_SLOT);
1691 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1692 mDevice->sendDown(secondPoint);
1693 mDevice->sendSync();
1694 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1695
1696 // ACTION_MOVE (First slot)
1697 mDevice->sendSlot(FIRST_SLOT);
1698 mDevice->sendMove(centerPoint + Point(5, 5));
1699 // ACTION_POINTER_UP (Second slot)
1700 mDevice->sendSlot(SECOND_SLOT);
1701 mDevice->sendMove(secondPoint + Point(6, 6));
1702 mDevice->sendPointerUp();
1703 // Send a single sync for the above 2 pointer updates
1704 mDevice->sendSync();
1705
1706 // First, we should get POINTER_UP for the second pointer
1707 // The movement of the second pointer during the liftoff frame is ignored.
1708 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1709 assertReceivedMotion(ACTION_POINTER_1_UP,
1710 {/*first pointer */ centerPoint + Point(5, 5),
1711 /*second pointer*/ secondPoint});
1712
1713 // Next, the MOVE event for the first pointer
1714 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1715}
1716
Arthur Hungaab25622020-01-16 11:22:11 +08001717TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1718 NotifyMotionArgs args;
1719 const Point centerPoint = mDevice->getCenterPoint();
1720
1721 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001722 mDevice->sendSlot(FIRST_SLOT);
1723 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001724 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001725 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001726 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1727 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1728
arthurhungcc7f9802020-04-30 17:55:40 +08001729 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001730 const Point secondPoint = centerPoint + Point(100, 100);
1731 mDevice->sendSlot(SECOND_SLOT);
1732 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1733 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001734 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001735 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001736 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001737
arthurhungcc7f9802020-04-30 17:55:40 +08001738 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001739 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001740 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001741 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1742 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1743
arthurhungcc7f9802020-04-30 17:55:40 +08001744 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1745 // a palm event.
1746 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001747 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001748 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001749 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001750 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001751 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001752
arthurhungcc7f9802020-04-30 17:55:40 +08001753 // Send up to second slot, expect first slot send moving.
1754 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001755 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001756 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1757 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001758
arthurhungcc7f9802020-04-30 17:55:40 +08001759 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001760 mDevice->sendSlot(FIRST_SLOT);
1761 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001762 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001763
arthurhungcc7f9802020-04-30 17:55:40 +08001764 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1765 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001766}
1767
Prabir Pradhanda20b172022-09-26 17:01:18 +00001768TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
1769 const Point centerPoint = mDevice->getCenterPoint();
1770
1771 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1772 mDevice->sendSlot(FIRST_SLOT);
1773 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1774 mDevice->sendToolType(MT_TOOL_PEN);
1775 mDevice->sendDown(centerPoint);
1776 mDevice->sendSync();
1777 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1778 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001779 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001780
1781 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1782
1783 // Release the stylus touch.
1784 mDevice->sendUp();
1785 mDevice->sendSync();
1786 ASSERT_NO_FATAL_FAILURE(
1787 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1788
1789 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1790
1791 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1792 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1793 mDevice->sendToolType(MT_TOOL_FINGER);
1794 mDevice->sendDown(centerPoint);
1795 mDevice->sendSync();
1796 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1797 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001798 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001799
1800 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1801
1802 mDevice->sendUp();
1803 mDevice->sendSync();
1804 ASSERT_NO_FATAL_FAILURE(
1805 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1806
1807 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1808 // The policy should be notified of the stylus presence.
1809 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1810 mDevice->sendToolType(MT_TOOL_PEN);
1811 mDevice->sendMove(centerPoint);
1812 mDevice->sendSync();
1813 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1814 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001815 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001816
1817 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1818}
1819
Prabir Pradhan124ea442022-10-28 20:27:44 +00001820// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001821
Prabir Pradhan124ea442022-10-28 20:27:44 +00001822// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1823// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1824// stylus.
1825template <typename UinputStylusDevice>
1826class StylusButtonIntegrationTest : public TouchIntegrationTest {
1827protected:
1828 void SetUp() override {
1829#if !defined(__ANDROID__)
1830 GTEST_SKIP();
1831#endif
1832 TouchIntegrationTest::SetUp();
1833 mTouchscreen = mDevice.get();
1834 mTouchscreenInfo = mDeviceInfo;
1835
1836 setUpStylusDevice();
1837 }
1838
1839 UinputStylusDevice* mStylus{nullptr};
1840 InputDeviceInfo mStylusInfo{};
1841
1842 UinputTouchScreen* mTouchscreen{nullptr};
1843 InputDeviceInfo mTouchscreenInfo{};
1844
1845private:
1846 // When we are attempting to test stylus button events that are sent from the touchscreen,
1847 // use the same Uinput device for the touchscreen and the stylus.
1848 template <typename T = UinputStylusDevice>
1849 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1850 mStylus = mDevice.get();
1851 mStylusInfo = mDeviceInfo;
1852 }
1853
1854 // When we are attempting to stylus buttons from an external stylus being merged with touches
1855 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1856 template <typename T = UinputStylusDevice>
1857 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1858 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1859 mStylus = mStylusDeviceLifecycleTracker.get();
1860 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1861 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1862 const auto info = findDeviceByName(mStylus->getName());
1863 ASSERT_TRUE(info);
1864 mStylusInfo = *info;
1865 }
1866
1867 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1868
1869 // Hide the base class's device to expose it with a different name for readability.
1870 using TouchIntegrationTest::mDevice;
1871 using TouchIntegrationTest::mDeviceInfo;
1872};
1873
1874using StylusButtonIntegrationTestTypes =
1875 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1876TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1877
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001878TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001879 const auto stylusId = TestFixture::mStylusInfo.getId();
1880
1881 TestFixture::mStylus->pressKey(BTN_STYLUS);
1882 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1883 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1884 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1885
1886 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1887 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001888 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001889 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001890}
1891
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001892TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001893 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1894 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1895 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001896
1897 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001898 TestFixture::mStylus->pressKey(BTN_STYLUS);
1899 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001900 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001901 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001902
1903 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001904 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1905 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1906 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1907 TestFixture::mTouchscreen->sendDown(centerPoint);
1908 TestFixture::mTouchscreen->sendSync();
1909 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001910 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001911 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001912 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1913 WithDeviceId(touchscreenId))));
1914 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001915 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001916 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001917 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1918 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001919
Prabir Pradhan124ea442022-10-28 20:27:44 +00001920 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1921 TestFixture::mTouchscreen->sendSync();
1922 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001923 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001924 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001925 WithDeviceId(touchscreenId))));
1926 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001927 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001928 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001929 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001930
1931 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001932 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1933 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001934 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001935 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001936}
1937
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001938TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001939 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1940 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1941 const auto stylusId = TestFixture::mStylusInfo.getId();
1942 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001943 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001944
1945 // Press the stylus button.
1946 TestFixture::mStylus->pressKey(BTN_STYLUS);
1947 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1948 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1949 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1950
1951 // Start hovering with the stylus.
1952 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1953 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1954 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1955 TestFixture::mTouchscreen->sendMove(centerPoint);
1956 TestFixture::mTouchscreen->sendSync();
1957 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1958 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1959 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1960 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1961 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1962 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1963 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1964 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1965 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1966
1967 // Touch down with the stylus.
1968 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1969 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1970 TestFixture::mTouchscreen->sendDown(centerPoint);
1971 TestFixture::mTouchscreen->sendSync();
1972 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1973 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1974 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1975
1976 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1977 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1978 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1979
1980 // Stop touching with the stylus, and start hovering.
1981 TestFixture::mTouchscreen->sendUp();
1982 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1983 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1984 TestFixture::mTouchscreen->sendMove(centerPoint);
1985 TestFixture::mTouchscreen->sendSync();
1986 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1987 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
1988 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1989 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1990 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1991 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1992 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1993 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1994 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1995
1996 // Stop hovering.
1997 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1998 TestFixture::mTouchscreen->sendSync();
1999 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2000 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2001 WithButtonState(0))));
2002 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2003 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2004 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2005 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2006
2007 // Release the stylus button.
2008 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2009 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2010 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2011 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2012}
2013
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002014TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002015 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2016 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2017 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002018
2019 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002020 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2021 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2022 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2023 TestFixture::mTouchscreen->sendDown(centerPoint);
2024 TestFixture::mTouchscreen->sendSync();
2025 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002026 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002027 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002028 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002029
2030 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002031 TestFixture::mStylus->pressKey(BTN_STYLUS);
2032 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002033 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002034 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2035 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002036 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002037 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002038 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2039 WithDeviceId(touchscreenId))));
2040 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002041 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002042 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002043 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2044 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002045
Prabir Pradhan124ea442022-10-28 20:27:44 +00002046 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2047 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002048 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002049 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2050 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002051 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002052 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002053 WithDeviceId(touchscreenId))));
2054 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002055 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002056 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002057 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002058
2059 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002060 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2061 TestFixture::mTouchscreen->sendSync();
2062 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002063 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002064 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002065 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002066}
2067
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002068TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002069 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2070 TestFixture::mReader->requestRefreshConfiguration(
2071 InputReaderConfiguration::CHANGE_STYLUS_BUTTON_REPORTING);
2072
2073 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2074 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2075 const auto stylusId = TestFixture::mStylusInfo.getId();
2076
2077 // Start a stylus gesture. By the time this event is processed, the configuration change that
2078 // was requested is guaranteed to be completed.
2079 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2080 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2081 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2082 TestFixture::mTouchscreen->sendDown(centerPoint);
2083 TestFixture::mTouchscreen->sendSync();
2084 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2085 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002086 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002087 WithDeviceId(touchscreenId))));
2088
2089 // Press and release a stylus button. Each change only generates a MOVE motion event.
2090 // Key events are unaffected.
2091 TestFixture::mStylus->pressKey(BTN_STYLUS);
2092 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2093 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2094 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2095 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2096 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002097 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002098 WithDeviceId(touchscreenId))));
2099
2100 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2101 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2102 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2103 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2104 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2105 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002106 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002107 WithDeviceId(touchscreenId))));
2108
2109 // Finish the stylus gesture.
2110 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2111 TestFixture::mTouchscreen->sendSync();
2112 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2113 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002114 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002115 WithDeviceId(touchscreenId))));
2116}
2117
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002118// --- ExternalStylusIntegrationTest ---
2119
2120// Verify the behavior of an external stylus. An external stylus can report pressure or button
2121// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2122// ongoing stylus gesture that is being emitted by the touchscreen.
2123using ExternalStylusIntegrationTest = TouchIntegrationTest;
2124
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002125TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002126 const Point centerPoint = mDevice->getCenterPoint();
2127
2128 // Create an external stylus capable of reporting pressure data that
2129 // should be fused with a touch pointer.
2130 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2131 createUinputDevice<UinputExternalStylusWithPressure>();
2132 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2133 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2134 const auto stylusInfo = findDeviceByName(stylus->getName());
2135 ASSERT_TRUE(stylusInfo);
2136
2137 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2138
2139 const auto touchscreenId = mDeviceInfo.getId();
2140
2141 // Set a pressure value on the stylus. It doesn't generate any events.
2142 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2143 stylus->setPressure(100);
2144 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2145
2146 // Start a finger gesture, and ensure it shows up as stylus gesture
2147 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002148 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002149 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002150 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002151 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002152 mDevice->sendSync();
2153 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2154 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002155 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002156 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002157
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002158 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2159 // event with the updated pressure.
2160 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002161 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2162 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002163 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002164 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002165
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002166 // The external stylus did not generate any events.
2167 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2168 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2169}
2170
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002171TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002172 const Point centerPoint = mDevice->getCenterPoint();
2173
2174 // Create an external stylus capable of reporting pressure data that
2175 // should be fused with a touch pointer.
2176 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2177 createUinputDevice<UinputExternalStylusWithPressure>();
2178 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2179 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2180 const auto stylusInfo = findDeviceByName(stylus->getName());
2181 ASSERT_TRUE(stylusInfo);
2182
2183 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2184
2185 const auto touchscreenId = mDeviceInfo.getId();
2186
2187 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2188 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002189 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2190 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002191 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002192 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002193
2194 // Start a finger gesture. The touch device will withhold generating any touches for
2195 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2196 mDevice->sendSlot(FIRST_SLOT);
2197 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2198 mDevice->sendToolType(MT_TOOL_FINGER);
2199 mDevice->sendDown(centerPoint);
2200 auto waitUntil = std::chrono::system_clock::now() +
2201 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002202 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002203 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002204
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002205 // Since the external stylus did not report a pressure value within the timeout,
2206 // it shows up as a finger pointer.
2207 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2208 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002209 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002210 WithPressure(1.f))));
2211
2212 // Change the pressure on the external stylus. Since the pressure was not present at the start
2213 // of the gesture, it is ignored for now.
2214 stylus->setPressure(200);
2215 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2216
2217 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002218 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2219 mDevice->sendSync();
2220 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2221 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002222 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002223
2224 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2225 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2226 mDevice->sendToolType(MT_TOOL_FINGER);
2227 mDevice->sendDown(centerPoint);
2228 mDevice->sendSync();
2229 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2230 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002231 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002232 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2233
2234 // The external stylus did not generate any events.
2235 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2236 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002237}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002238
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002239TEST_F(ExternalStylusIntegrationTest, DISABLED_UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002240 const Point centerPoint = mDevice->getCenterPoint();
2241
2242 // Create an external stylus device that does not support pressure. It should not affect any
2243 // touch pointers.
2244 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2245 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2246 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2247 const auto stylusInfo = findDeviceByName(stylus->getName());
2248 ASSERT_TRUE(stylusInfo);
2249
2250 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2251
2252 const auto touchscreenId = mDeviceInfo.getId();
2253
2254 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2255 // pressure data from the external stylus.
2256 mDevice->sendSlot(FIRST_SLOT);
2257 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2258 mDevice->sendToolType(MT_TOOL_FINGER);
2259 mDevice->sendDown(centerPoint);
2260 auto waitUntil = std::chrono::system_clock::now() +
2261 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2262 mDevice->sendSync();
2263 ASSERT_NO_FATAL_FAILURE(
2264 mTestListener
2265 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2266 WithToolType(
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002267 ToolType::FINGER),
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002268 WithButtonState(0),
2269 WithDeviceId(touchscreenId),
2270 WithPressure(1.f)),
2271 waitUntil));
2272
2273 // The external stylus did not generate any events.
2274 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2275 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2276}
2277
Michael Wrightd02c5b62014-02-10 15:10:22 -08002278// --- InputDeviceTest ---
2279class InputDeviceTest : public testing::Test {
2280protected:
2281 static const char* DEVICE_NAME;
2282 static const char* DEVICE_LOCATION;
2283 static const int32_t DEVICE_ID;
2284 static const int32_t DEVICE_GENERATION;
2285 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002286 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002287 static const int32_t EVENTHUB_ID;
2288 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2289
2290 std::shared_ptr<FakeEventHub> mFakeEventHub;
2291 sp<FakeInputReaderPolicy> mFakePolicy;
2292 std::unique_ptr<TestInputListener> mFakeListener;
2293 std::unique_ptr<InstrumentedInputReader> mReader;
2294 std::shared_ptr<InputDevice> mDevice;
2295
2296 void SetUp() override {
2297 mFakeEventHub = std::make_unique<FakeEventHub>();
2298 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2299 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002300 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002301 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002302 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002303 identifier.name = DEVICE_NAME;
2304 identifier.location = DEVICE_LOCATION;
2305 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2306 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2307 identifier);
2308 mReader->pushNextDevice(mDevice);
2309 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002310 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002311 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002312
2313 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002314 mFakeListener.reset();
2315 mFakePolicy.clear();
2316 }
2317};
2318
2319const char* InputDeviceTest::DEVICE_NAME = "device";
2320const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2321const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2322const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002323const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002324const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2325 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002326const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002327const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2328
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002329TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002330 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002331 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2332 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002333}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002334
Michael Wrightd02c5b62014-02-10 15:10:22 -08002335TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2336 ASSERT_EQ(mDevice->isEnabled(), false);
2337}
2338
2339TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2340 // Configuration.
2341 InputReaderConfiguration config;
Arpit Singhed6c3de2023-04-05 19:24:37 +00002342 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002343
2344 // Reset.
2345 unused += mDevice->reset(ARBITRARY_TIME);
2346
2347 NotifyDeviceResetArgs resetArgs;
2348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2349 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2350 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2351
2352 // Metadata.
2353 ASSERT_TRUE(mDevice->isIgnored());
2354 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2355
2356 InputDeviceInfo info = mDevice->getDeviceInfo();
2357 ASSERT_EQ(DEVICE_ID, info.getId());
2358 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2359 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2360 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2361
2362 // State queries.
2363 ASSERT_EQ(0, mDevice->getMetaState());
2364
2365 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2366 << "Ignored device should return unknown key code state.";
2367 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2368 << "Ignored device should return unknown scan code state.";
2369 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2370 << "Ignored device should return unknown switch state.";
2371
2372 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2373 uint8_t flags[2] = { 0, 1 };
2374 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2375 << "Ignored device should never mark any key codes.";
2376 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2377 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2378}
2379
2380TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2381 // Configuration.
2382 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2383
2384 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002385 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2386 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002387 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2388 mapper1.setMetaState(AMETA_ALT_ON);
2389 mapper1.addSupportedKeyCode(AKEYCODE_A);
2390 mapper1.addSupportedKeyCode(AKEYCODE_B);
2391 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2392 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2393 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2394 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2395 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2396
2397 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002398 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2399 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002400 mapper2.setMetaState(AMETA_SHIFT_ON);
2401
2402 InputReaderConfiguration config;
Arpit Singhed6c3de2023-04-05 19:24:37 +00002403 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002404
Harry Cuttsf13161a2023-03-08 14:15:49 +00002405 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2406 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002407 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002408 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002409
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002410 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2411 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002412
2413 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002414 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002415 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2416 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002417
2418 NotifyDeviceResetArgs resetArgs;
2419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2420 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2421 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2422
2423 // Metadata.
2424 ASSERT_FALSE(mDevice->isIgnored());
2425 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2426
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002427 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002428 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002429 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2431 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2432
2433 // State queries.
2434 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2435 << "Should query mappers and combine meta states.";
2436
2437 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2438 << "Should return unknown key code state when source not supported.";
2439 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2440 << "Should return unknown scan code state when source not supported.";
2441 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2442 << "Should return unknown switch state when source not supported.";
2443
2444 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2445 << "Should query mapper when source is supported.";
2446 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2447 << "Should query mapper when source is supported.";
2448 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2449 << "Should query mapper when source is supported.";
2450
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002451 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002452 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002453 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002454 << "Should do nothing when source is unsupported.";
2455 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2456 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2457 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2458 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2459
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002460 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002461 << "Should query mapper when source is supported.";
2462 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2463 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2464 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2465 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2466
2467 // Event handling.
2468 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002469 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002470 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002471
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002472 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2473 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002474}
2475
Arthur Hung2c9a3342019-07-23 14:18:59 +08002476// A single input device is associated with a specific display. Check that:
2477// 1. Device is disabled if the viewport corresponding to the associated display is not found
2478// 2. Device is disabled when setEnabled API is called
2479TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002480 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2481 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002482
2483 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002484 std::list<NotifyArgs> unused =
2485 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002486
2487 // Device should be enabled by default.
2488 ASSERT_TRUE(mDevice->isEnabled());
2489
2490 // Prepare associated info.
2491 constexpr uint8_t hdmi = 1;
2492 const std::string UNIQUE_ID = "local:1";
2493
2494 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002495 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2496 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002497 // Device should be disabled because it is associated with a specific display via
2498 // input port <-> display port association, but the corresponding display is not found
2499 ASSERT_FALSE(mDevice->isEnabled());
2500
2501 // Prepare displays.
2502 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002503 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002504 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002505 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2506 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002507 ASSERT_TRUE(mDevice->isEnabled());
2508
2509 // Device should be disabled after set disable.
2510 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002511 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2512 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002513 ASSERT_FALSE(mDevice->isEnabled());
2514
2515 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002516 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2517 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002518 ASSERT_FALSE(mDevice->isEnabled());
2519}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002520
Christine Franks1ba71cc2021-04-07 14:37:42 -07002521TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2522 // Device should be enabled by default.
2523 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002524 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2525 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002526 std::list<NotifyArgs> unused =
2527 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002528 ASSERT_TRUE(mDevice->isEnabled());
2529
2530 // Device should be disabled because it is associated with a specific display, but the
2531 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002532 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002533 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2534 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002535 ASSERT_FALSE(mDevice->isEnabled());
2536
2537 // Device should be enabled when a display is found.
2538 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002539 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002540 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002541 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2542 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002543 ASSERT_TRUE(mDevice->isEnabled());
2544
2545 // Device should be disabled after set disable.
2546 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002547 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2548 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002549 ASSERT_FALSE(mDevice->isEnabled());
2550
2551 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002552 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2553 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002554 ASSERT_FALSE(mDevice->isEnabled());
2555}
2556
Christine Franks2a2293c2022-01-18 11:51:16 -08002557TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2558 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002559 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2560 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002561 std::list<NotifyArgs> unused =
2562 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08002563
Christine Franks2a2293c2022-01-18 11:51:16 -08002564 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2565 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002566 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002567 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002568 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2569 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002570 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2571}
2572
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002573/**
2574 * This test reproduces a crash caused by a dangling reference that remains after device is added
2575 * and removed. The reference is accessed in InputDevice::dump(..);
2576 */
2577TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2578 constexpr int32_t TEST_EVENTHUB_ID = 10;
2579 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2580
Harry Cutts33476232023-01-30 19:57:29 +00002581 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh8e6fb252023-04-06 11:49:17 +00002582 device.addEventHubDevice(TEST_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002583 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2584 std::string dumpStr, eventHubDevStr;
2585 device.dump(dumpStr, eventHubDevStr);
2586}
2587
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002588TEST_F(InputDeviceTest, GetBluetoothAddress) {
2589 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2590 ASSERT_TRUE(address);
2591 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2592}
2593
Michael Wrightd02c5b62014-02-10 15:10:22 -08002594// --- SwitchInputMapperTest ---
2595
2596class SwitchInputMapperTest : public InputMapperTest {
2597protected:
2598};
2599
2600TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002601 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002602
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002603 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002604}
2605
2606TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002607 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002608
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002609 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002610 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002611
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002612 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002613 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002614}
2615
2616TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002617 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002618 std::list<NotifyArgs> out;
2619 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2620 ASSERT_TRUE(out.empty());
2621 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2622 ASSERT_TRUE(out.empty());
2623 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2624 ASSERT_TRUE(out.empty());
2625 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002626
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002627 ASSERT_EQ(1u, out.size());
2628 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002629 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002630 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2631 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002632 args.switchMask);
2633 ASSERT_EQ(uint32_t(0), args.policyFlags);
2634}
2635
Chris Ye87143712020-11-10 05:05:58 +00002636// --- VibratorInputMapperTest ---
2637class VibratorInputMapperTest : public InputMapperTest {
2638protected:
2639 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2640};
2641
2642TEST_F(VibratorInputMapperTest, GetSources) {
2643 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2644
2645 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2646}
2647
2648TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2649 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2650
2651 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2652}
2653
2654TEST_F(VibratorInputMapperTest, Vibrate) {
2655 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002656 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002657 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2658
2659 VibrationElement pattern(2);
2660 VibrationSequence sequence(2);
2661 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002662 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2663 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002664 sequence.addElement(pattern);
2665 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002666 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2667 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002668 sequence.addElement(pattern);
2669
2670 std::vector<int64_t> timings = {0, 1};
2671 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2672
2673 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002674 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002675 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002676 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002677 // Verify vibrator state listener was notified.
2678 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002679 ASSERT_EQ(1u, out.size());
2680 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2681 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2682 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002683 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002684 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002685 ASSERT_FALSE(mapper.isVibrating());
2686 // Verify vibrator state listener was notified.
2687 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002688 ASSERT_EQ(1u, out.size());
2689 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2690 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2691 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002692}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002693
Chris Yef59a2f42020-10-16 12:55:26 -07002694// --- SensorInputMapperTest ---
2695
2696class SensorInputMapperTest : public InputMapperTest {
2697protected:
2698 static const int32_t ACCEL_RAW_MIN;
2699 static const int32_t ACCEL_RAW_MAX;
2700 static const int32_t ACCEL_RAW_FUZZ;
2701 static const int32_t ACCEL_RAW_FLAT;
2702 static const int32_t ACCEL_RAW_RESOLUTION;
2703
2704 static const int32_t GYRO_RAW_MIN;
2705 static const int32_t GYRO_RAW_MAX;
2706 static const int32_t GYRO_RAW_FUZZ;
2707 static const int32_t GYRO_RAW_FLAT;
2708 static const int32_t GYRO_RAW_RESOLUTION;
2709
2710 static const float GRAVITY_MS2_UNIT;
2711 static const float DEGREE_RADIAN_UNIT;
2712
2713 void prepareAccelAxes();
2714 void prepareGyroAxes();
2715 void setAccelProperties();
2716 void setGyroProperties();
2717 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2718};
2719
2720const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2721const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2722const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2723const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2724const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2725
2726const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2727const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2728const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2729const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2730const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2731
2732const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2733const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2734
2735void SensorInputMapperTest::prepareAccelAxes() {
2736 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2737 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2738 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2739 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2740 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2741 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2742}
2743
2744void SensorInputMapperTest::prepareGyroAxes() {
2745 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2746 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2747 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2748 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2749 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2750 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2751}
2752
2753void SensorInputMapperTest::setAccelProperties() {
2754 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2755 /* sensorDataIndex */ 0);
2756 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2757 /* sensorDataIndex */ 1);
2758 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2759 /* sensorDataIndex */ 2);
2760 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2761 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2762 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2763 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2764 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2765}
2766
2767void SensorInputMapperTest::setGyroProperties() {
2768 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2769 /* sensorDataIndex */ 0);
2770 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2771 /* sensorDataIndex */ 1);
2772 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2773 /* sensorDataIndex */ 2);
2774 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2775 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2776 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2777 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2778 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2779}
2780
2781TEST_F(SensorInputMapperTest, GetSources) {
2782 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2783
2784 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2785}
2786
2787TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2788 setAccelProperties();
2789 prepareAccelAxes();
2790 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2791
2792 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2793 std::chrono::microseconds(10000),
2794 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002795 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002796 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2797 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2798 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002801
2802 NotifySensorArgs args;
2803 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2804 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2805 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2806
2807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2808 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2809 ASSERT_EQ(args.deviceId, DEVICE_ID);
2810 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2811 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2812 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2813 ASSERT_EQ(args.values, values);
2814 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2815}
2816
2817TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2818 setGyroProperties();
2819 prepareGyroAxes();
2820 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2821
2822 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2823 std::chrono::microseconds(10000),
2824 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002825 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002826 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2827 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002831
2832 NotifySensorArgs args;
2833 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2834 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2835 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2836
2837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2838 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2839 ASSERT_EQ(args.deviceId, DEVICE_ID);
2840 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2841 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2842 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2843 ASSERT_EQ(args.values, values);
2844 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2845}
2846
Michael Wrightd02c5b62014-02-10 15:10:22 -08002847// --- KeyboardInputMapperTest ---
2848
2849class KeyboardInputMapperTest : public InputMapperTest {
2850protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002851 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002852 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002853 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002854
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002855 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002856 int32_t originalKeyCode, int32_t rotatedKeyCode,
2857 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002858};
2859
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002860/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2861 * orientation.
2862 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002863void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002864 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2865 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002866}
2867
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002868void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002869 int32_t originalScanCode, int32_t originalKeyCode,
2870 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002871 NotifyKeyArgs args;
2872
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002873 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2875 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2876 ASSERT_EQ(originalScanCode, args.scanCode);
2877 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002878 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002879
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2882 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2883 ASSERT_EQ(originalScanCode, args.scanCode);
2884 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002885 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002886}
2887
Michael Wrightd02c5b62014-02-10 15:10:22 -08002888TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002889 KeyboardInputMapper& mapper =
2890 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2891 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002892
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002893 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002894}
2895
2896TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2897 const int32_t USAGE_A = 0x070004;
2898 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002899 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2900 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002901 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2902 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2903 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002904
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002905 KeyboardInputMapper& mapper =
2906 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2907 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002908 // Initial metastate is AMETA_NONE.
2909 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002910
2911 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002912 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002913 NotifyKeyArgs args;
2914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2915 ASSERT_EQ(DEVICE_ID, args.deviceId);
2916 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2917 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2918 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2919 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2920 ASSERT_EQ(KEY_HOME, args.scanCode);
2921 ASSERT_EQ(AMETA_NONE, args.metaState);
2922 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2923 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2924 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2925
2926 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002927 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2929 ASSERT_EQ(DEVICE_ID, args.deviceId);
2930 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2931 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2932 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2933 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2934 ASSERT_EQ(KEY_HOME, args.scanCode);
2935 ASSERT_EQ(AMETA_NONE, args.metaState);
2936 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2937 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2938 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2939
2940 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002941 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2944 ASSERT_EQ(DEVICE_ID, args.deviceId);
2945 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2946 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2947 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2948 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2949 ASSERT_EQ(0, args.scanCode);
2950 ASSERT_EQ(AMETA_NONE, args.metaState);
2951 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2952 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2953 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2954
2955 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002956 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2957 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2959 ASSERT_EQ(DEVICE_ID, args.deviceId);
2960 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2961 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2962 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2963 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2964 ASSERT_EQ(0, args.scanCode);
2965 ASSERT_EQ(AMETA_NONE, args.metaState);
2966 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2967 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2968 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2969
2970 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002971 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2972 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2974 ASSERT_EQ(DEVICE_ID, args.deviceId);
2975 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2976 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2977 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2978 ASSERT_EQ(0, args.keyCode);
2979 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2980 ASSERT_EQ(AMETA_NONE, args.metaState);
2981 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2982 ASSERT_EQ(0U, args.policyFlags);
2983 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2984
2985 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002986 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2987 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2989 ASSERT_EQ(DEVICE_ID, args.deviceId);
2990 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2991 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2992 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2993 ASSERT_EQ(0, args.keyCode);
2994 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2995 ASSERT_EQ(AMETA_NONE, args.metaState);
2996 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2997 ASSERT_EQ(0U, args.policyFlags);
2998 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2999}
3000
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003001TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3002 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3003 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3004 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3005
3006 KeyboardInputMapper& mapper =
3007 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3008 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3009
3010 // Key down by scan code.
3011 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3012 NotifyKeyArgs args;
3013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3014 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3015
3016 // Key up by scan code.
3017 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3019 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3020}
3021
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003022/**
3023 * Ensure that the readTime is set to the time when the EV_KEY is received.
3024 */
3025TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3026 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3027
3028 KeyboardInputMapper& mapper =
3029 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3030 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3031 NotifyKeyArgs args;
3032
3033 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003034 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3036 ASSERT_EQ(12, args.readTime);
3037
3038 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003039 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3041 ASSERT_EQ(15, args.readTime);
3042}
3043
Michael Wrightd02c5b62014-02-10 15:10:22 -08003044TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003045 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3046 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003047 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3048 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3049 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003050
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003051 KeyboardInputMapper& mapper =
3052 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3053 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003054
Arthur Hung95f68612022-04-07 14:08:22 +08003055 // Initial metastate is AMETA_NONE.
3056 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003057
3058 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003059 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003060 NotifyKeyArgs args;
3061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3062 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003063 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003064 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065
3066 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003067 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3069 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003070 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003071
3072 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003073 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3075 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003076 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077
3078 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003079 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3081 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003082 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003083 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003084}
3085
3086TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003087 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3088 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3089 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3090 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003091
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003092 KeyboardInputMapper& mapper =
3093 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3094 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003095
Michael Wrighta9cf4192022-12-01 23:46:39 +00003096 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003097 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3098 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3099 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3100 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3101 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3102 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3103 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3104 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3105}
3106
3107TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003108 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3109 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3110 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3111 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003112
Michael Wrightd02c5b62014-02-10 15:10:22 -08003113 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003114 KeyboardInputMapper& mapper =
3115 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3116 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003117
Michael Wrighta9cf4192022-12-01 23:46:39 +00003118 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003119 ASSERT_NO_FATAL_FAILURE(
3120 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3121 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3122 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3123 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3124 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3125 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3126 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003127
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003128 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003129 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003130 ASSERT_NO_FATAL_FAILURE(
3131 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3132 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3133 AKEYCODE_DPAD_UP, DISPLAY_ID));
3134 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3135 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3136 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3137 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003138
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003139 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003140 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003141 ASSERT_NO_FATAL_FAILURE(
3142 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3143 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3144 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3145 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3146 AKEYCODE_DPAD_UP, DISPLAY_ID));
3147 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3148 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003149
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003150 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003151 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003152 ASSERT_NO_FATAL_FAILURE(
3153 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3154 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3155 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3156 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3157 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3158 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3159 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003160
3161 // Special case: if orientation changes while key is down, we still emit the same keycode
3162 // in the key up as we did in the key down.
3163 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003164 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003165 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3168 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3169 ASSERT_EQ(KEY_UP, args.scanCode);
3170 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3171
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003172 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003173 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003174 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3176 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3177 ASSERT_EQ(KEY_UP, args.scanCode);
3178 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3179}
3180
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003181TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3182 // If the keyboard is not orientation aware,
3183 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003184 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003185
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003186 KeyboardInputMapper& mapper =
3187 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3188 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003189 NotifyKeyArgs args;
3190
3191 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003192 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003194 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3196 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3197
Michael Wrighta9cf4192022-12-01 23:46:39 +00003198 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3203 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3204}
3205
3206TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3207 // If the keyboard is orientation aware,
3208 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003209 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003210
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003211 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003212 KeyboardInputMapper& mapper =
3213 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3214 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003215 NotifyKeyArgs args;
3216
3217 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3218 // ^--- already checked by the previous test
3219
Michael Wrighta9cf4192022-12-01 23:46:39 +00003220 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003221 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003222 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003224 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3226 ASSERT_EQ(DISPLAY_ID, args.displayId);
3227
3228 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003229 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003230 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003231 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003232 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3236 ASSERT_EQ(newDisplayId, args.displayId);
3237}
3238
Michael Wrightd02c5b62014-02-10 15:10:22 -08003239TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003240 KeyboardInputMapper& mapper =
3241 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3242 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003243
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003244 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003245 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003246
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003247 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003248 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003249}
3250
Philip Junker4af3b3d2021-12-14 10:36:55 +01003251TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3252 KeyboardInputMapper& mapper =
3253 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3254 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3255
3256 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3257 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3258 << "If a mapping is available, the result is equal to the mapping";
3259
3260 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3261 << "If no mapping is available, the result is the key location";
3262}
3263
Michael Wrightd02c5b62014-02-10 15:10:22 -08003264TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003265 KeyboardInputMapper& mapper =
3266 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3267 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003268
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003269 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003270 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003272 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003273 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003274}
3275
3276TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003277 KeyboardInputMapper& mapper =
3278 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3279 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003280
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003281 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282
Michael Wrightd02c5b62014-02-10 15:10:22 -08003283 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003284 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285 ASSERT_TRUE(flags[0]);
3286 ASSERT_FALSE(flags[1]);
3287}
3288
3289TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003290 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3291 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3292 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3293 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3294 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3295 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003297 KeyboardInputMapper& mapper =
3298 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3299 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003300 // Initial metastate is AMETA_NONE.
3301 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003302
3303 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003304 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3305 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3306 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003307
3308 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003311 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3312 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3313 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003314 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003315
3316 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003319 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3320 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3321 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003322 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003323
3324 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3326 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003327 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3328 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3329 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003330 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003331
3332 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003335 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3336 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3337 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003338 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003339
3340 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003343 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3344 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3345 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003346 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003347
3348 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003349 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3350 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003351 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3352 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3353 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003354 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355}
3356
Chris Yea52ade12020-08-27 16:49:20 -07003357TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3358 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3359 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3360 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3361 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3362
3363 KeyboardInputMapper& mapper =
3364 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3365 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3366
Chris Yea52ade12020-08-27 16:49:20 -07003367 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003368 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003369 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3370 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3371 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3372 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3373
3374 NotifyKeyArgs args;
3375 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3378 ASSERT_EQ(AMETA_NONE, args.metaState);
3379 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3380 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3381 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3382
3383 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003384 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3386 ASSERT_EQ(AMETA_NONE, args.metaState);
3387 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3388 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3389 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3390}
3391
Arthur Hung2c9a3342019-07-23 14:18:59 +08003392TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3393 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003394 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3395 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3396 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3397 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003398
3399 // keyboard 2.
3400 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003401 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003402 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003403 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003404 std::shared_ptr<InputDevice> device2 =
3405 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003406 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003407
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003408 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3409 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3410 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3411 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003412
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003413 KeyboardInputMapper& mapper =
3414 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3415 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003416
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003417 KeyboardInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00003418 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3419 mFakePolicy->getReaderConfiguration(),
3420 AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003421 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003422 std::list<NotifyArgs> unused =
3423 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003424 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003425 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003426
3427 // Prepared displays and associated info.
3428 constexpr uint8_t hdmi1 = 0;
3429 constexpr uint8_t hdmi2 = 1;
3430 const std::string SECONDARY_UNIQUE_ID = "local:1";
3431
3432 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3433 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3434
3435 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003436 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3437 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003438 ASSERT_FALSE(device2->isEnabled());
3439
3440 // Prepare second display.
3441 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003442 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003443 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003444 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003445 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003446 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003447 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3448 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003449
3450 // Device should be enabled after the associated display is found.
3451 ASSERT_TRUE(mDevice->isEnabled());
3452 ASSERT_TRUE(device2->isEnabled());
3453
3454 // Test pad key events
3455 ASSERT_NO_FATAL_FAILURE(
3456 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3457 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3458 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3459 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3460 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3461 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3462 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3463
3464 ASSERT_NO_FATAL_FAILURE(
3465 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3466 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3467 AKEYCODE_DPAD_RIGHT, newDisplayId));
3468 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3469 AKEYCODE_DPAD_DOWN, newDisplayId));
3470 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3471 AKEYCODE_DPAD_LEFT, newDisplayId));
3472}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003473
arthurhungc903df12020-08-11 15:08:42 +08003474TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3475 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3476 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3477 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3478 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3479 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3480 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3481
3482 KeyboardInputMapper& mapper =
3483 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3484 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003485 // Initial metastate is AMETA_NONE.
3486 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003487
3488 // Initialization should have turned all of the lights off.
3489 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3490 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3491 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3492
3493 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003494 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003496 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3497 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3498
3499 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003502 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3503 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3504
3505 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003506 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3507 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003508 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3509 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3510
3511 mFakeEventHub->removeDevice(EVENTHUB_ID);
3512 mReader->loopOnce();
3513
3514 // keyboard 2 should default toggle keys.
3515 const std::string USB2 = "USB2";
3516 const std::string DEVICE_NAME2 = "KEYBOARD2";
3517 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3518 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3519 std::shared_ptr<InputDevice> device2 =
3520 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003521 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003522 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3523 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3524 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3525 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3526 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3527 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3528
arthurhung6fe95782020-10-05 22:41:16 +08003529 KeyboardInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00003530 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3531 mFakePolicy->getReaderConfiguration(),
3532 AINPUT_SOURCE_KEYBOARD,
arthurhung6fe95782020-10-05 22:41:16 +08003533 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003534 std::list<NotifyArgs> unused =
3535 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003536 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003537 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003538
3539 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3540 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3541 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003542 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3543 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003544}
3545
Arthur Hungcb40a002021-08-03 14:31:01 +00003546TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3547 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3548 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3549 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3550
3551 // Suppose we have two mappers. (DPAD + KEYBOARD)
3552 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3553 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3554 KeyboardInputMapper& mapper =
3555 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3556 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003557 // Initial metastate is AMETA_NONE.
3558 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003559
3560 mReader->toggleCapsLockState(DEVICE_ID);
3561 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3562}
3563
Arthur Hungfb3cc112022-04-13 07:39:50 +00003564TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3565 // keyboard 1.
3566 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3567 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3568 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3569 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3570 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3571 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3572
3573 KeyboardInputMapper& mapper1 =
3574 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3575 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3576
3577 // keyboard 2.
3578 const std::string USB2 = "USB2";
3579 const std::string DEVICE_NAME2 = "KEYBOARD2";
3580 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3581 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3582 std::shared_ptr<InputDevice> device2 =
3583 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3584 ftl::Flags<InputDeviceClass>(0));
3585 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3586 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3587 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3588 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3589 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3590 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3591
3592 KeyboardInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00003593 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3594 mFakePolicy->getReaderConfiguration(),
3595 AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003596 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003597 std::list<NotifyArgs> unused =
3598 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003599 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003600 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003601
Arthur Hung95f68612022-04-07 14:08:22 +08003602 // Initial metastate is AMETA_NONE.
3603 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3604 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3605
3606 // Toggle num lock on and off.
3607 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3608 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003609 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3610 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3611 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3612
3613 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3614 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3615 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3616 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3617 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3618
3619 // Toggle caps lock on and off.
3620 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3621 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3622 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3623 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3624 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3625
3626 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3627 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3628 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3629 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3630 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3631
3632 // Toggle scroll lock on and off.
3633 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3634 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3635 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3636 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3637 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3638
3639 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3640 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3641 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3642 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3643 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3644}
3645
Arthur Hung2141d542022-08-23 07:45:21 +00003646TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3647 const int32_t USAGE_A = 0x070004;
3648 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3649 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3650
3651 KeyboardInputMapper& mapper =
3652 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3653 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3654 // Key down by scan code.
3655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3656 NotifyKeyArgs args;
3657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3658 ASSERT_EQ(DEVICE_ID, args.deviceId);
3659 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3660 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3661 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3662 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3663 ASSERT_EQ(KEY_HOME, args.scanCode);
3664 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3665
3666 // Disable device, it should synthesize cancellation events for down events.
3667 mFakePolicy->addDisabledDevice(DEVICE_ID);
3668 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
3669
3670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3671 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3672 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3673 ASSERT_EQ(KEY_HOME, args.scanCode);
3674 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3675}
3676
Zixuan Qufecb6062022-11-12 04:44:31 +00003677TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00003678 mDevice->addMapper<KeyboardInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
3679 AINPUT_SOURCE_KEYBOARD,
Zixuan Qufecb6062022-11-12 04:44:31 +00003680 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3681 std::list<NotifyArgs> unused =
3682 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
3683
3684 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3685
3686 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3687 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUT_ASSOCIATION);
3688
3689 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3690 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3691 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3692 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3693 deviceInfo.getKeyboardLayoutInfo()->layoutType);
3694}
3695
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003696TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3697 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3698 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3699
3700 // Configuration
3701 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3702 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3703 InputReaderConfiguration config;
Arpit Singhed6c3de2023-04-05 19:24:37 +00003704 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, 0);
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003705
3706 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3707 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3708}
3709
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003710// --- KeyboardInputMapperTest_ExternalDevice ---
3711
3712class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3713protected:
Chris Yea52ade12020-08-27 16:49:20 -07003714 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003715};
3716
3717TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003718 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3719 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003720
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003721 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3722 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3723 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3724 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003725
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003726 KeyboardInputMapper& mapper =
3727 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3728 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003729
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003730 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003731 NotifyKeyArgs args;
3732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3733 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3734
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003735 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3737 ASSERT_EQ(uint32_t(0), args.policyFlags);
3738
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003739 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003741 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003742
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003743 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3745 ASSERT_EQ(uint32_t(0), args.policyFlags);
3746
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3749 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3750
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003751 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3753 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3754}
3755
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003756TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003757 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003758
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003759 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3760 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3761 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003762
Powei Fengd041c5d2019-05-03 17:11:33 -07003763 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003764 KeyboardInputMapper& mapper =
3765 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3766 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003767
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003769 NotifyKeyArgs args;
3770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3771 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3772
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003773 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3775 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3776
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3779 ASSERT_EQ(uint32_t(0), args.policyFlags);
3780
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003781 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3783 ASSERT_EQ(uint32_t(0), args.policyFlags);
3784
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003785 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3787 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3788
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003789 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3791 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3792}
3793
Michael Wrightd02c5b62014-02-10 15:10:22 -08003794// --- CursorInputMapperTest ---
3795
3796class CursorInputMapperTest : public InputMapperTest {
3797protected:
3798 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3799
Michael Wright17db18e2020-06-26 20:51:44 +01003800 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003801
Chris Yea52ade12020-08-27 16:49:20 -07003802 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003803 InputMapperTest::SetUp();
3804
Michael Wright17db18e2020-06-26 20:51:44 +01003805 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003806 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003807 }
3808
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003809 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3810 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003811
Michael Wrighta9cf4192022-12-01 23:46:39 +00003812 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003813 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3814 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3815 }
3816
3817 void prepareSecondaryDisplay() {
3818 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003819 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003820 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003821 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003822
3823 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3824 float pressure) {
3825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3826 0.0f, 0.0f, 0.0f, EPSILON));
3827 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003828};
3829
3830const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3831
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003832void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3833 int32_t originalY, int32_t rotatedX,
3834 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003835 NotifyMotionArgs args;
3836
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003837 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3838 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3841 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003842 ASSERT_NO_FATAL_FAILURE(
3843 assertCursorPointerCoords(args.pointerCoords[0],
3844 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3845 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003846}
3847
3848TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003849 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003850 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003851
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003852 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003853}
3854
3855TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003856 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003857 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003859 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003860}
3861
3862TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003863 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003864 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003865
3866 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003867 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003868
3869 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003870 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3871 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003872 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3873 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3874
3875 // When the bounds are set, then there should be a valid motion range.
3876 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3877
3878 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003879 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003880
3881 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3882 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3883 1, 800 - 1, 0.0f, 0.0f));
3884 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3885 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3886 2, 480 - 1, 0.0f, 0.0f));
3887 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3888 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3889 0.0f, 1.0f, 0.0f, 0.0f));
3890}
3891
3892TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003893 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003894 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003895
3896 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003897 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003898
3899 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3900 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3901 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3902 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3903 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3904 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3905 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3906 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3907 0.0f, 1.0f, 0.0f, 0.0f));
3908}
3909
3910TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003911 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003912 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913
arthurhungdcef2dc2020-08-11 14:47:50 +08003914 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003915
3916 NotifyMotionArgs args;
3917
3918 // Button press.
3919 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003920 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3921 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3923 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3924 ASSERT_EQ(DEVICE_ID, args.deviceId);
3925 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3926 ASSERT_EQ(uint32_t(0), args.policyFlags);
3927 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3928 ASSERT_EQ(0, args.flags);
3929 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3930 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3931 ASSERT_EQ(0, args.edgeFlags);
3932 ASSERT_EQ(uint32_t(1), args.pointerCount);
3933 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003934 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003935 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003936 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3937 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3938 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3939
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3941 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3942 ASSERT_EQ(DEVICE_ID, args.deviceId);
3943 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3944 ASSERT_EQ(uint32_t(0), args.policyFlags);
3945 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3946 ASSERT_EQ(0, args.flags);
3947 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3948 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3949 ASSERT_EQ(0, args.edgeFlags);
3950 ASSERT_EQ(uint32_t(1), args.pointerCount);
3951 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003952 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003953 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003954 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3955 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3956 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3957
Michael Wrightd02c5b62014-02-10 15:10:22 -08003958 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003959 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3960 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3962 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3963 ASSERT_EQ(DEVICE_ID, args.deviceId);
3964 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3965 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003966 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3967 ASSERT_EQ(0, args.flags);
3968 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3969 ASSERT_EQ(0, args.buttonState);
3970 ASSERT_EQ(0, args.edgeFlags);
3971 ASSERT_EQ(uint32_t(1), args.pointerCount);
3972 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003973 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003974 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003975 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3976 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3977 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3978
3979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3980 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3981 ASSERT_EQ(DEVICE_ID, args.deviceId);
3982 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3983 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003984 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3985 ASSERT_EQ(0, args.flags);
3986 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3987 ASSERT_EQ(0, args.buttonState);
3988 ASSERT_EQ(0, args.edgeFlags);
3989 ASSERT_EQ(uint32_t(1), args.pointerCount);
3990 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003991 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003992 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003993 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3994 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3995 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3996}
3997
3998TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003999 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004000 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004001
4002 NotifyMotionArgs args;
4003
4004 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004005 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4006 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4008 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004009 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4010 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4011 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004012
4013 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4015 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4017 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004018 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4019 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004020}
4021
4022TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004023 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004024 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004025
4026 NotifyMotionArgs args;
4027
4028 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4030 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4032 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004033 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004034
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4036 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004037 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004038
Michael Wrightd02c5b62014-02-10 15:10:22 -08004039 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004040 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4041 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004043 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004044 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004045
4046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004047 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004048 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049}
4050
4051TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004052 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004053 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054
4055 NotifyMotionArgs args;
4056
4057 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004058 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4059 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4060 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4063 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004064 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4065 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4066 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004067
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4069 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004073
Michael Wrightd02c5b62014-02-10 15:10:22 -08004074 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004075 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4076 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4079 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004080 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4081 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4082 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083
4084 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004085 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4086 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004088 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004089 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004090
4091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004093 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094}
4095
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004096TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004097 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004098 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004099 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4100 // need to be rotated.
4101 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004102 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004103
Michael Wrighta9cf4192022-12-01 23:46:39 +00004104 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4106 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4107 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4108 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4109 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4110 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4111 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4112 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4113}
4114
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004115TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004116 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004117 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004118 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4119 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004120 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004121
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004122 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004123 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004124 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4125 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4126 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4127 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4128 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4129 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4130 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4131 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4132
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004133 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004134 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004135 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4136 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4137 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4138 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4139 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4140 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4141 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4142 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004144 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004145 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004146 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4147 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4148 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4149 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4150 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4151 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4152 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4153 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4154
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004155 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004156 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004157 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4158 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4159 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4160 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4161 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4162 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4163 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4164 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165}
4166
4167TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004169 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170
4171 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4172 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004173
4174 NotifyMotionArgs motionArgs;
4175 NotifyKeyArgs keyArgs;
4176
4177 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004178 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4181 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4182 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004183 ASSERT_NO_FATAL_FAILURE(
4184 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004185
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4187 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004191
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004192 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004195 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004197 ASSERT_NO_FATAL_FAILURE(
4198 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199
4200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004201 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004205
4206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004207 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004211
4212 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004213 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4214 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4217 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4218 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004219 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004220 ASSERT_NO_FATAL_FAILURE(
4221 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4224 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4225 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004228
4229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4230 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4231 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004232 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004233 ASSERT_NO_FATAL_FAILURE(
4234 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004235
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4237 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004239 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004241 ASSERT_NO_FATAL_FAILURE(
4242 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004243
4244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004250 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4251 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004253 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4254 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004255 ASSERT_NO_FATAL_FAILURE(
4256 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004257 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4258 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004259
4260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004262 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004263 ASSERT_NO_FATAL_FAILURE(
4264 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004265
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4267 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271
4272 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4276 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4277 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004278
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004280 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004281 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004282 ASSERT_NO_FATAL_FAILURE(
4283 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004284
4285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4286 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4287 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004291 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004296 ASSERT_NO_FATAL_FAILURE(
4297 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004298
4299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004300 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004301 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004302
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004303 ASSERT_NO_FATAL_FAILURE(
4304 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4306 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4307 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4308
4309 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4313 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4314 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004315
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004317 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004319 ASSERT_NO_FATAL_FAILURE(
4320 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004321
4322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4323 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4324 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004327
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004328 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4329 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004331 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004333 ASSERT_NO_FATAL_FAILURE(
4334 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004335
4336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4337 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4338 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08004342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4343 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4344 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4345
4346 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4348 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4350 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4351 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004352
Michael Wrightd02c5b62014-02-10 15:10:22 -08004353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004354 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004355 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004356 ASSERT_NO_FATAL_FAILURE(
4357 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004358
4359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4360 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4361 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004365 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4366 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004368 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004369 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004370 ASSERT_NO_FATAL_FAILURE(
4371 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004372
4373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4374 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4375 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4380 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4381 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4382
4383 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004384 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4387 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4388 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004389
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004391 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004393 ASSERT_NO_FATAL_FAILURE(
4394 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004395
4396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4397 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4398 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004401
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004402 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4403 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004405 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004407 ASSERT_NO_FATAL_FAILURE(
4408 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004409
4410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4411 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4412 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08004416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4417 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4418 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4419}
4420
4421TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004423 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424
4425 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4426 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427
4428 NotifyMotionArgs args;
4429
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4432 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004434 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4435 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4437 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 +00004438 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004439}
4440
4441TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004442 addConfigurationProperty("cursor.mode", "pointer");
4443 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004444 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004445
4446 NotifyDeviceResetArgs resetArgs;
4447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4448 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4449 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4450
4451 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4452 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004453
4454 NotifyMotionArgs args;
4455
4456 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4461 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4462 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4464 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 +00004465 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004466
4467 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4469 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4471 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4472 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4473 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4474 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4476 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4477 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4479 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4480
4481 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004482 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4483 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4485 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4486 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4488 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4490 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4491 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4493 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4494
4495 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4497 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4498 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4500 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4502 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4503 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 +00004504 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004505
4506 // Disable pointer capture and check that the device generation got bumped
4507 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004508 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004509 mFakePolicy->setPointerCapture(false);
4510 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004511 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004512
4513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004514 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4515
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4520 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4523 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 +00004524 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004525}
4526
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004527/**
4528 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4529 * pointer acceleration or speed processing should not be applied.
4530 */
4531TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4532 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004533 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4534 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004535 mFakePolicy->setVelocityControlParams(testParams);
4536 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4537
4538 NotifyDeviceResetArgs resetArgs;
4539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4540 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4541 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4542
4543 NotifyMotionArgs args;
4544
4545 // Move and verify scale is applied.
4546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4547 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4550 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4551 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4552 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4553 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4554 ASSERT_GT(relX, 10);
4555 ASSERT_GT(relY, 20);
4556
4557 // Enable Pointer Capture
4558 mFakePolicy->setPointerCapture(true);
4559 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4560 NotifyPointerCaptureChangedArgs captureArgs;
4561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4562 ASSERT_TRUE(captureArgs.request.enable);
4563
4564 // Move and verify scale is not applied.
4565 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4569 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4570 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4571 ASSERT_EQ(10, args.pointerCoords[0].getX());
4572 ASSERT_EQ(20, args.pointerCoords[0].getY());
4573}
4574
Prabir Pradhan208360b2022-06-24 18:37:04 +00004575TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4576 addConfigurationProperty("cursor.mode", "pointer");
4577 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4578
4579 NotifyDeviceResetArgs resetArgs;
4580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4581 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4582 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4583
4584 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004585 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004586
4587 NotifyMotionArgs args;
4588
4589 // Verify that the coordinates are rotated.
4590 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4591 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4594 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4595 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4596 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4597 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4598
4599 // Enable Pointer Capture.
4600 mFakePolicy->setPointerCapture(true);
4601 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4602 NotifyPointerCaptureChangedArgs captureArgs;
4603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4604 ASSERT_TRUE(captureArgs.request.enable);
4605
4606 // Move and verify rotation is not applied.
4607 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4608 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4609 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4611 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4612 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4613 ASSERT_EQ(10, args.pointerCoords[0].getX());
4614 ASSERT_EQ(20, args.pointerCoords[0].getY());
4615}
4616
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004617TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004618 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004619
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004620 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004621 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004622
4623 // Set up the secondary display as the display on which the pointer should be shown.
4624 // The InputDevice is not associated with any display.
4625 prepareSecondaryDisplay();
4626 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08004627 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4628
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004629 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004630 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004631
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004632 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004633 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004637 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4638 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4639 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004640 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004641}
4642
4643TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
4644 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4645
4646 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004647 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004648
4649 // Set up the secondary display as the display on which the pointer should be shown,
4650 // and associate the InputDevice with the secondary display.
4651 prepareSecondaryDisplay();
4652 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4653 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4654 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4655
4656 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4657 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004658
4659 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4660 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4661 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004663 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4664 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4665 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004666 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004667}
4668
4669TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
4670 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4671
4672 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004673 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004674 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4675
4676 // Associate the InputDevice with the secondary display.
4677 prepareSecondaryDisplay();
4678 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4679 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4680
4681 // The mapper should not generate any events because it is associated with a display that is
4682 // different from the pointer display.
4683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004687}
4688
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004689// --- BluetoothCursorInputMapperTest ---
4690
4691class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4692protected:
4693 void SetUp() override {
4694 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4695
4696 mFakePointerController = std::make_shared<FakePointerController>();
4697 mFakePolicy->setPointerController(mFakePointerController);
4698 }
4699};
4700
4701TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4702 addConfigurationProperty("cursor.mode", "pointer");
4703 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4704
4705 nsecs_t kernelEventTime = ARBITRARY_TIME;
4706 nsecs_t expectedEventTime = ARBITRARY_TIME;
4707 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4708 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4710 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4711 WithEventTime(expectedEventTime))));
4712
4713 // Process several events that come in quick succession, according to their timestamps.
4714 for (int i = 0; i < 3; i++) {
4715 constexpr static nsecs_t delta = ms2ns(1);
4716 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4717 kernelEventTime += delta;
4718 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4719
4720 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4721 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4723 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4724 WithEventTime(expectedEventTime))));
4725 }
4726}
4727
4728TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4729 addConfigurationProperty("cursor.mode", "pointer");
4730 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4731
4732 nsecs_t expectedEventTime = ARBITRARY_TIME;
4733 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4734 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4736 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4737 WithEventTime(expectedEventTime))));
4738
4739 // Process several events with the same timestamp from the kernel.
4740 // Ensure that we do not generate events too far into the future.
4741 constexpr static int32_t numEvents =
4742 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4743 for (int i = 0; i < numEvents; i++) {
4744 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4745
4746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4749 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4750 WithEventTime(expectedEventTime))));
4751 }
4752
4753 // By processing more events with the same timestamp, we should not generate events with a
4754 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4755 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4756 for (int i = 0; i < 3; i++) {
4757 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4758 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4760 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4761 WithEventTime(cappedEventTime))));
4762 }
4763}
4764
4765TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4766 addConfigurationProperty("cursor.mode", "pointer");
4767 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4768
4769 nsecs_t kernelEventTime = ARBITRARY_TIME;
4770 nsecs_t expectedEventTime = ARBITRARY_TIME;
4771 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4772 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4774 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4775 WithEventTime(expectedEventTime))));
4776
4777 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4778 // smoothening is not needed, its timestamp is not affected.
4779 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4780 expectedEventTime = kernelEventTime;
4781
4782 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4783 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4785 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4786 WithEventTime(expectedEventTime))));
4787}
4788
Michael Wrightd02c5b62014-02-10 15:10:22 -08004789// --- TouchInputMapperTest ---
4790
4791class TouchInputMapperTest : public InputMapperTest {
4792protected:
4793 static const int32_t RAW_X_MIN;
4794 static const int32_t RAW_X_MAX;
4795 static const int32_t RAW_Y_MIN;
4796 static const int32_t RAW_Y_MAX;
4797 static const int32_t RAW_TOUCH_MIN;
4798 static const int32_t RAW_TOUCH_MAX;
4799 static const int32_t RAW_TOOL_MIN;
4800 static const int32_t RAW_TOOL_MAX;
4801 static const int32_t RAW_PRESSURE_MIN;
4802 static const int32_t RAW_PRESSURE_MAX;
4803 static const int32_t RAW_ORIENTATION_MIN;
4804 static const int32_t RAW_ORIENTATION_MAX;
4805 static const int32_t RAW_DISTANCE_MIN;
4806 static const int32_t RAW_DISTANCE_MAX;
4807 static const int32_t RAW_TILT_MIN;
4808 static const int32_t RAW_TILT_MAX;
4809 static const int32_t RAW_ID_MIN;
4810 static const int32_t RAW_ID_MAX;
4811 static const int32_t RAW_SLOT_MIN;
4812 static const int32_t RAW_SLOT_MAX;
4813 static const float X_PRECISION;
4814 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004815 static const float X_PRECISION_VIRTUAL;
4816 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817
4818 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004819 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004820
4821 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4822
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004823 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004824 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004825
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826 enum Axes {
4827 POSITION = 1 << 0,
4828 TOUCH = 1 << 1,
4829 TOOL = 1 << 2,
4830 PRESSURE = 1 << 3,
4831 ORIENTATION = 1 << 4,
4832 MINOR = 1 << 5,
4833 ID = 1 << 6,
4834 DISTANCE = 1 << 7,
4835 TILT = 1 << 8,
4836 SLOT = 1 << 9,
4837 TOOL_TYPE = 1 << 10,
4838 };
4839
Michael Wrighta9cf4192022-12-01 23:46:39 +00004840 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004841 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004842 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004844 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004845 int32_t toRawX(float displayX);
4846 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004847 int32_t toRotatedRawX(float displayX);
4848 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004849 float toCookedX(float rawX, float rawY);
4850 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004851 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004852 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004854 float toDisplayY(int32_t rawY, int32_t displayHeight);
4855
Michael Wrightd02c5b62014-02-10 15:10:22 -08004856};
4857
4858const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4859const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4860const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4861const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4862const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4863const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4864const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4865const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004866const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4867const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004868const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4869const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4870const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4871const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4872const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4873const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4874const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4875const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4876const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4877const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4878const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4879const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004880const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4881 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4882const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4883 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004884const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4885 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004886
4887const float TouchInputMapperTest::GEOMETRIC_SCALE =
4888 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4889 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4890
4891const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4892 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4893 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4894};
4895
Michael Wrighta9cf4192022-12-01 23:46:39 +00004896void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004897 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4898 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004899}
4900
4901void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4902 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004903 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004904}
4905
Michael Wrighta9cf4192022-12-01 23:46:39 +00004906void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004907 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4908 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4909 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004910}
4911
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004913 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4914 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4915 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4916 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004917}
4918
Jason Gerecke489fda82012-09-07 17:19:40 -07004919void TouchInputMapperTest::prepareLocationCalibration() {
4920 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4921}
4922
Michael Wrightd02c5b62014-02-10 15:10:22 -08004923int32_t TouchInputMapperTest::toRawX(float displayX) {
4924 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4925}
4926
4927int32_t TouchInputMapperTest::toRawY(float displayY) {
4928 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4929}
4930
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004931int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4932 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4933}
4934
4935int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4936 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4937}
4938
Jason Gerecke489fda82012-09-07 17:19:40 -07004939float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4940 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4941 return rawX;
4942}
4943
4944float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4945 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4946 return rawY;
4947}
4948
Michael Wrightd02c5b62014-02-10 15:10:22 -08004949float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004950 return toDisplayX(rawX, DISPLAY_WIDTH);
4951}
4952
4953float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4954 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004955}
4956
4957float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004958 return toDisplayY(rawY, DISPLAY_HEIGHT);
4959}
4960
4961float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4962 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004963}
4964
4965
4966// --- SingleTouchInputMapperTest ---
4967
4968class SingleTouchInputMapperTest : public TouchInputMapperTest {
4969protected:
4970 void prepareButtons();
4971 void prepareAxes(int axes);
4972
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004973 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4974 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4975 void processUp(SingleTouchInputMapper& mappery);
4976 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4977 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4978 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4979 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4980 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4981 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004982};
4983
4984void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004985 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004986}
4987
4988void SingleTouchInputMapperTest::prepareAxes(int axes) {
4989 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004990 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4991 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992 }
4993 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004994 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4995 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004996 }
4997 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004998 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4999 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000 }
5001 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005002 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5003 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004 }
5005 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005006 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5007 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005008 }
5009}
5010
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005011void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5013 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005015}
5016
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005017void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005020}
5021
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005022void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005024}
5025
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005026void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005027 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005028}
5029
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005030void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5031 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005032 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005033}
5034
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005035void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005037}
5038
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005039void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5040 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005041 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5042 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005043}
5044
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005045void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5046 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005048}
5049
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005050void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005051 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005052}
5053
Michael Wrightd02c5b62014-02-10 15:10:22 -08005054TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005055 prepareButtons();
5056 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005057 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005058
Josep del Río2d8c79a2023-01-23 19:33:50 +00005059 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060}
5061
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005063 prepareButtons();
5064 prepareAxes(POSITION);
5065 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005066 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005067
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005068 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005069}
5070
5071TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005072 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005073 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005074 prepareButtons();
5075 prepareAxes(POSITION);
5076 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005077 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005078
5079 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005080 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005081
5082 // Virtual key is down.
5083 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5084 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5085 processDown(mapper, x, y);
5086 processSync(mapper);
5087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5088
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005089 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005090
5091 // Virtual key is up.
5092 processUp(mapper);
5093 processSync(mapper);
5094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5095
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005096 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005097}
5098
5099TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005100 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005101 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005102 prepareButtons();
5103 prepareAxes(POSITION);
5104 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005105 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005106
5107 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005108 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005109
5110 // Virtual key is down.
5111 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5112 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5113 processDown(mapper, x, y);
5114 processSync(mapper);
5115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5116
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005117 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118
5119 // Virtual key is up.
5120 processUp(mapper);
5121 processSync(mapper);
5122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5123
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005124 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005125}
5126
5127TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005128 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005129 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130 prepareButtons();
5131 prepareAxes(POSITION);
5132 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005133 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005134
Michael Wrightd02c5b62014-02-10 15:10:22 -08005135 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005136 ASSERT_TRUE(
5137 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005138 ASSERT_TRUE(flags[0]);
5139 ASSERT_FALSE(flags[1]);
5140}
5141
5142TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005143 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005144 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005145 prepareButtons();
5146 prepareAxes(POSITION);
5147 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005148 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005149
arthurhungdcef2dc2020-08-11 14:47:50 +08005150 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005151
5152 NotifyKeyArgs args;
5153
5154 // Press virtual key.
5155 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5156 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5157 processDown(mapper, x, y);
5158 processSync(mapper);
5159
5160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5161 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5162 ASSERT_EQ(DEVICE_ID, args.deviceId);
5163 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5164 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5165 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5166 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5167 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5168 ASSERT_EQ(KEY_HOME, args.scanCode);
5169 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5170 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5171
5172 // Release virtual key.
5173 processUp(mapper);
5174 processSync(mapper);
5175
5176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5177 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5178 ASSERT_EQ(DEVICE_ID, args.deviceId);
5179 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5180 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5181 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5182 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5183 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5184 ASSERT_EQ(KEY_HOME, args.scanCode);
5185 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5186 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5187
5188 // Should not have sent any motions.
5189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5190}
5191
5192TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005193 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005194 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005195 prepareButtons();
5196 prepareAxes(POSITION);
5197 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005198 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005199
arthurhungdcef2dc2020-08-11 14:47:50 +08005200 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005201
5202 NotifyKeyArgs keyArgs;
5203
5204 // Press virtual key.
5205 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5206 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5207 processDown(mapper, x, y);
5208 processSync(mapper);
5209
5210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5211 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5212 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5213 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5214 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5215 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5216 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5217 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5218 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5219 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5220 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5221
5222 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5223 // into the display area.
5224 y -= 100;
5225 processMove(mapper, x, y);
5226 processSync(mapper);
5227
5228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5229 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5230 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5231 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5232 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5233 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5234 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5235 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5236 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5237 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5238 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5239 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5240
5241 NotifyMotionArgs motionArgs;
5242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5243 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5244 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5245 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5246 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5247 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5248 ASSERT_EQ(0, motionArgs.flags);
5249 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5250 ASSERT_EQ(0, motionArgs.buttonState);
5251 ASSERT_EQ(0, motionArgs.edgeFlags);
5252 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5253 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005254 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5256 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5257 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5258 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5259 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5260
5261 // Keep moving out of bounds. Should generate a pointer move.
5262 y -= 50;
5263 processMove(mapper, x, y);
5264 processSync(mapper);
5265
5266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5267 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5268 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5269 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5270 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5272 ASSERT_EQ(0, motionArgs.flags);
5273 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5274 ASSERT_EQ(0, motionArgs.buttonState);
5275 ASSERT_EQ(0, motionArgs.edgeFlags);
5276 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5277 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005278 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5280 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5281 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5282 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5283 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5284
5285 // Release out of bounds. Should generate a pointer up.
5286 processUp(mapper);
5287 processSync(mapper);
5288
5289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5290 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5291 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5292 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5293 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5294 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5295 ASSERT_EQ(0, motionArgs.flags);
5296 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5297 ASSERT_EQ(0, motionArgs.buttonState);
5298 ASSERT_EQ(0, motionArgs.edgeFlags);
5299 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5300 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005301 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005302 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5303 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5304 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5305 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5306 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5307
5308 // Should not have sent any more keys or motions.
5309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5311}
5312
5313TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005315 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005316 prepareButtons();
5317 prepareAxes(POSITION);
5318 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005319 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005320
arthurhungdcef2dc2020-08-11 14:47:50 +08005321 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322
5323 NotifyMotionArgs motionArgs;
5324
5325 // Initially go down out of bounds.
5326 int32_t x = -10;
5327 int32_t y = -10;
5328 processDown(mapper, x, y);
5329 processSync(mapper);
5330
5331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5332
5333 // Move into the display area. Should generate a pointer down.
5334 x = 50;
5335 y = 75;
5336 processMove(mapper, x, y);
5337 processSync(mapper);
5338
5339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5340 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5341 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5342 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5343 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5344 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5345 ASSERT_EQ(0, motionArgs.flags);
5346 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5347 ASSERT_EQ(0, motionArgs.buttonState);
5348 ASSERT_EQ(0, motionArgs.edgeFlags);
5349 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5350 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005351 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005352 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5353 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5354 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5355 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5356 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5357
5358 // Release. Should generate a pointer up.
5359 processUp(mapper);
5360 processSync(mapper);
5361
5362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5363 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5364 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5365 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5366 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5367 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5368 ASSERT_EQ(0, motionArgs.flags);
5369 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5370 ASSERT_EQ(0, motionArgs.buttonState);
5371 ASSERT_EQ(0, motionArgs.edgeFlags);
5372 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5373 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005374 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5376 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5377 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5378 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5379 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5380
5381 // Should not have sent any more keys or motions.
5382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5384}
5385
Santos Cordonfa5cf462017-04-05 10:37:00 -07005386TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005387 addConfigurationProperty("touch.deviceType", "touchScreen");
5388 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5389
Michael Wrighta9cf4192022-12-01 23:46:39 +00005390 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005391 prepareButtons();
5392 prepareAxes(POSITION);
5393 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005394 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005395
arthurhungdcef2dc2020-08-11 14:47:50 +08005396 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005397
5398 NotifyMotionArgs motionArgs;
5399
5400 // Down.
5401 int32_t x = 100;
5402 int32_t y = 125;
5403 processDown(mapper, x, y);
5404 processSync(mapper);
5405
5406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5407 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5408 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5409 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5410 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5411 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5412 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5413 ASSERT_EQ(0, motionArgs.flags);
5414 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5415 ASSERT_EQ(0, motionArgs.buttonState);
5416 ASSERT_EQ(0, motionArgs.edgeFlags);
5417 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5418 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005419 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5421 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5422 1, 0, 0, 0, 0, 0, 0, 0));
5423 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5424 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5425 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5426
5427 // Move.
5428 x += 50;
5429 y += 75;
5430 processMove(mapper, x, y);
5431 processSync(mapper);
5432
5433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5434 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5435 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5436 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5437 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5438 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5440 ASSERT_EQ(0, motionArgs.flags);
5441 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5442 ASSERT_EQ(0, motionArgs.buttonState);
5443 ASSERT_EQ(0, motionArgs.edgeFlags);
5444 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5445 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005446 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5448 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5449 1, 0, 0, 0, 0, 0, 0, 0));
5450 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5451 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5452 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5453
5454 // Up.
5455 processUp(mapper);
5456 processSync(mapper);
5457
5458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5459 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5460 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5461 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5462 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5463 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5464 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5465 ASSERT_EQ(0, motionArgs.flags);
5466 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5467 ASSERT_EQ(0, motionArgs.buttonState);
5468 ASSERT_EQ(0, motionArgs.edgeFlags);
5469 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5470 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005471 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5473 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5474 1, 0, 0, 0, 0, 0, 0, 0));
5475 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5476 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5477 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5478
5479 // Should not have sent any more keys or motions.
5480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5482}
5483
Michael Wrightd02c5b62014-02-10 15:10:22 -08005484TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005485 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005486 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005487 prepareButtons();
5488 prepareAxes(POSITION);
5489 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005490 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005491
arthurhungdcef2dc2020-08-11 14:47:50 +08005492 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493
5494 NotifyMotionArgs motionArgs;
5495
5496 // Down.
5497 int32_t x = 100;
5498 int32_t y = 125;
5499 processDown(mapper, x, y);
5500 processSync(mapper);
5501
5502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5503 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5504 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5505 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5506 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5507 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5508 ASSERT_EQ(0, motionArgs.flags);
5509 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5510 ASSERT_EQ(0, motionArgs.buttonState);
5511 ASSERT_EQ(0, motionArgs.edgeFlags);
5512 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5513 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005514 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5516 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5517 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5518 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5519 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5520
5521 // Move.
5522 x += 50;
5523 y += 75;
5524 processMove(mapper, x, y);
5525 processSync(mapper);
5526
5527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5528 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5529 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5530 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5531 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5532 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5533 ASSERT_EQ(0, motionArgs.flags);
5534 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5535 ASSERT_EQ(0, motionArgs.buttonState);
5536 ASSERT_EQ(0, motionArgs.edgeFlags);
5537 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5538 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005539 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5541 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5542 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5543 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5544 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5545
5546 // Up.
5547 processUp(mapper);
5548 processSync(mapper);
5549
5550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5551 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5552 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5553 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5554 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5555 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5556 ASSERT_EQ(0, motionArgs.flags);
5557 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5558 ASSERT_EQ(0, motionArgs.buttonState);
5559 ASSERT_EQ(0, motionArgs.edgeFlags);
5560 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5561 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005562 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5564 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5565 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5566 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5567 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5568
5569 // Should not have sent any more keys or motions.
5570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5572}
5573
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005574TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005575 addConfigurationProperty("touch.deviceType", "touchScreen");
5576 prepareButtons();
5577 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005578 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5579 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005580 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005581
5582 NotifyMotionArgs args;
5583
5584 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005585 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005586 processDown(mapper, toRawX(50), toRawY(75));
5587 processSync(mapper);
5588
5589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5590 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5591 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5592
5593 processUp(mapper);
5594 processSync(mapper);
5595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5596}
5597
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005598TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005599 addConfigurationProperty("touch.deviceType", "touchScreen");
5600 prepareButtons();
5601 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005602 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5603 // orientation-aware are affected by display rotation.
5604 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005605 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005606
5607 NotifyMotionArgs args;
5608
5609 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005610 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005611 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005612 processDown(mapper, toRawX(50), toRawY(75));
5613 processSync(mapper);
5614
5615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5616 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5617 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5618
5619 processUp(mapper);
5620 processSync(mapper);
5621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5622
5623 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005624 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005625 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005626 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005627 processSync(mapper);
5628
5629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5630 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5631 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5632
5633 processUp(mapper);
5634 processSync(mapper);
5635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5636
5637 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005638 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005639 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005640 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5641 processSync(mapper);
5642
5643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5644 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5645 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5646
5647 processUp(mapper);
5648 processSync(mapper);
5649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5650
5651 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005652 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005653 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005654 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005655 processSync(mapper);
5656
5657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5658 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5659 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5660
5661 processUp(mapper);
5662 processSync(mapper);
5663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5664}
5665
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005666TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5667 addConfigurationProperty("touch.deviceType", "touchScreen");
5668 prepareButtons();
5669 prepareAxes(POSITION);
5670 addConfigurationProperty("touch.orientationAware", "1");
5671 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5672 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005673 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005674 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5675 NotifyMotionArgs args;
5676
5677 // Orientation 0.
5678 processDown(mapper, toRawX(50), toRawY(75));
5679 processSync(mapper);
5680
5681 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5682 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5683 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5684
5685 processUp(mapper);
5686 processSync(mapper);
5687 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5688}
5689
5690TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5691 addConfigurationProperty("touch.deviceType", "touchScreen");
5692 prepareButtons();
5693 prepareAxes(POSITION);
5694 addConfigurationProperty("touch.orientationAware", "1");
5695 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5696 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005697 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005698 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5699 NotifyMotionArgs args;
5700
5701 // Orientation 90.
5702 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5703 processSync(mapper);
5704
5705 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5706 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5707 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5708
5709 processUp(mapper);
5710 processSync(mapper);
5711 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5712}
5713
5714TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5715 addConfigurationProperty("touch.deviceType", "touchScreen");
5716 prepareButtons();
5717 prepareAxes(POSITION);
5718 addConfigurationProperty("touch.orientationAware", "1");
5719 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5720 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005721 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005722 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5723 NotifyMotionArgs args;
5724
5725 // Orientation 180.
5726 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5727 processSync(mapper);
5728
5729 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5730 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5731 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5732
5733 processUp(mapper);
5734 processSync(mapper);
5735 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5736}
5737
5738TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5739 addConfigurationProperty("touch.deviceType", "touchScreen");
5740 prepareButtons();
5741 prepareAxes(POSITION);
5742 addConfigurationProperty("touch.orientationAware", "1");
5743 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5744 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005745 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005746 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5747 NotifyMotionArgs args;
5748
5749 // Orientation 270.
5750 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5751 processSync(mapper);
5752
5753 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5754 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5755 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5756
5757 processUp(mapper);
5758 processSync(mapper);
5759 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5760}
5761
5762TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5763 addConfigurationProperty("touch.deviceType", "touchScreen");
5764 prepareButtons();
5765 prepareAxes(POSITION);
5766 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5767 // orientation-aware are affected by display rotation.
5768 addConfigurationProperty("touch.orientationAware", "0");
5769 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5770 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5771
5772 NotifyMotionArgs args;
5773
5774 // Orientation 90, Rotation 0.
5775 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005776 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005777 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5778 processSync(mapper);
5779
5780 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5781 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5782 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5783
5784 processUp(mapper);
5785 processSync(mapper);
5786 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5787
5788 // Orientation 90, Rotation 90.
5789 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005790 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005791 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005792 processSync(mapper);
5793
5794 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5795 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5796 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5797
5798 processUp(mapper);
5799 processSync(mapper);
5800 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5801
5802 // Orientation 90, Rotation 180.
5803 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005804 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005805 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5806 processSync(mapper);
5807
5808 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5809 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5810 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5811
5812 processUp(mapper);
5813 processSync(mapper);
5814 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5815
5816 // Orientation 90, Rotation 270.
5817 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005818 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005819 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 -07005820 processSync(mapper);
5821
5822 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5823 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5824 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5825
5826 processUp(mapper);
5827 processSync(mapper);
5828 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5829}
5830
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005831TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5832 addConfigurationProperty("touch.deviceType", "touchScreen");
5833 prepareButtons();
5834 prepareAxes(POSITION);
5835 addConfigurationProperty("touch.orientationAware", "1");
5836 prepareDisplay(ui::ROTATION_0);
5837 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5838
5839 // Set a physical frame in the display viewport.
5840 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5841 viewport->physicalLeft = 20;
5842 viewport->physicalTop = 600;
5843 viewport->physicalRight = 30;
5844 viewport->physicalBottom = 610;
5845 mFakePolicy->updateViewport(*viewport);
5846 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5847
5848 // Start the touch.
5849 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5850 processSync(mapper);
5851
5852 // Expect all input starting outside the physical frame to be ignored.
5853 const std::array<Point, 6> outsidePoints = {
5854 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5855 for (const auto& p : outsidePoints) {
5856 processMove(mapper, toRawX(p.x), toRawY(p.y));
5857 processSync(mapper);
5858 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5859 }
5860
5861 // Move the touch into the physical frame.
5862 processMove(mapper, toRawX(25), toRawY(605));
5863 processSync(mapper);
5864 NotifyMotionArgs args;
5865 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5866 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5867 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5868 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5869
5870 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5871 for (const auto& p : outsidePoints) {
5872 processMove(mapper, toRawX(p.x), toRawY(p.y));
5873 processSync(mapper);
5874 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5875 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5876 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5877 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5878 }
5879
5880 processUp(mapper);
5881 processSync(mapper);
5882 EXPECT_NO_FATAL_FAILURE(
5883 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5884}
5885
Michael Wrightd02c5b62014-02-10 15:10:22 -08005886TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005887 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005888 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005889 prepareButtons();
5890 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005891 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005892
5893 // These calculations are based on the input device calibration documentation.
5894 int32_t rawX = 100;
5895 int32_t rawY = 200;
5896 int32_t rawPressure = 10;
5897 int32_t rawToolMajor = 12;
5898 int32_t rawDistance = 2;
5899 int32_t rawTiltX = 30;
5900 int32_t rawTiltY = 110;
5901
5902 float x = toDisplayX(rawX);
5903 float y = toDisplayY(rawY);
5904 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5905 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5906 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5907 float distance = float(rawDistance);
5908
5909 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5910 float tiltScale = M_PI / 180;
5911 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5912 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5913 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5914 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5915
5916 processDown(mapper, rawX, rawY);
5917 processPressure(mapper, rawPressure);
5918 processToolMajor(mapper, rawToolMajor);
5919 processDistance(mapper, rawDistance);
5920 processTilt(mapper, rawTiltX, rawTiltY);
5921 processSync(mapper);
5922
5923 NotifyMotionArgs args;
5924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5926 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5927 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5928}
5929
Jason Gerecke489fda82012-09-07 17:19:40 -07005930TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005931 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005932 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005933 prepareLocationCalibration();
5934 prepareButtons();
5935 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005936 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005937
5938 int32_t rawX = 100;
5939 int32_t rawY = 200;
5940
5941 float x = toDisplayX(toCookedX(rawX, rawY));
5942 float y = toDisplayY(toCookedY(rawX, rawY));
5943
5944 processDown(mapper, rawX, rawY);
5945 processSync(mapper);
5946
5947 NotifyMotionArgs args;
5948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5949 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5950 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5951}
5952
Michael Wrightd02c5b62014-02-10 15:10:22 -08005953TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005954 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005955 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005956 prepareButtons();
5957 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005958 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005959
5960 NotifyMotionArgs motionArgs;
5961 NotifyKeyArgs keyArgs;
5962
5963 processDown(mapper, 100, 200);
5964 processSync(mapper);
5965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5966 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5967 ASSERT_EQ(0, motionArgs.buttonState);
5968
5969 // press BTN_LEFT, release BTN_LEFT
5970 processKey(mapper, BTN_LEFT, 1);
5971 processSync(mapper);
5972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5973 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5974 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5975
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5977 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5978 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5979
Michael Wrightd02c5b62014-02-10 15:10:22 -08005980 processKey(mapper, BTN_LEFT, 0);
5981 processSync(mapper);
5982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005983 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005984 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005985
5986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005987 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005988 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005989
5990 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5991 processKey(mapper, BTN_RIGHT, 1);
5992 processKey(mapper, BTN_MIDDLE, 1);
5993 processSync(mapper);
5994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5996 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5997 motionArgs.buttonState);
5998
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6000 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6001 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6002
6003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6004 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6005 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6006 motionArgs.buttonState);
6007
Michael Wrightd02c5b62014-02-10 15:10:22 -08006008 processKey(mapper, BTN_RIGHT, 0);
6009 processSync(mapper);
6010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006011 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006012 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006013
6014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006015 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006016 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006017
6018 processKey(mapper, BTN_MIDDLE, 0);
6019 processSync(mapper);
6020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006021 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006022 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006023
6024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006025 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006026 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006027
6028 // press BTN_BACK, release BTN_BACK
6029 processKey(mapper, BTN_BACK, 1);
6030 processSync(mapper);
6031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6032 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6033 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006034
Michael Wrightd02c5b62014-02-10 15:10:22 -08006035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006036 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006037 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6038
6039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6040 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6041 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006042
6043 processKey(mapper, BTN_BACK, 0);
6044 processSync(mapper);
6045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006046 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006047 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006048
6049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006050 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006051 ASSERT_EQ(0, motionArgs.buttonState);
6052
Michael Wrightd02c5b62014-02-10 15:10:22 -08006053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6054 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6055 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6056
6057 // press BTN_SIDE, release BTN_SIDE
6058 processKey(mapper, BTN_SIDE, 1);
6059 processSync(mapper);
6060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6061 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6062 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006063
Michael Wrightd02c5b62014-02-10 15:10:22 -08006064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006065 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006066 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6067
6068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6069 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6070 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006071
6072 processKey(mapper, BTN_SIDE, 0);
6073 processSync(mapper);
6074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006075 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006076 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006077
6078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006079 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006080 ASSERT_EQ(0, motionArgs.buttonState);
6081
Michael Wrightd02c5b62014-02-10 15:10:22 -08006082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6083 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6084 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6085
6086 // press BTN_FORWARD, release BTN_FORWARD
6087 processKey(mapper, BTN_FORWARD, 1);
6088 processSync(mapper);
6089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6090 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6091 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006092
Michael Wrightd02c5b62014-02-10 15:10:22 -08006093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006095 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6096
6097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6098 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6099 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006100
6101 processKey(mapper, BTN_FORWARD, 0);
6102 processSync(mapper);
6103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006104 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006105 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006106
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006109 ASSERT_EQ(0, motionArgs.buttonState);
6110
Michael Wrightd02c5b62014-02-10 15:10:22 -08006111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6112 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6113 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6114
6115 // press BTN_EXTRA, release BTN_EXTRA
6116 processKey(mapper, BTN_EXTRA, 1);
6117 processSync(mapper);
6118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6119 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6120 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006121
Michael Wrightd02c5b62014-02-10 15:10:22 -08006122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006124 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6125
6126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6127 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6128 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129
6130 processKey(mapper, BTN_EXTRA, 0);
6131 processSync(mapper);
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006133 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006134 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006135
6136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006138 ASSERT_EQ(0, motionArgs.buttonState);
6139
Michael Wrightd02c5b62014-02-10 15:10:22 -08006140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6141 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6142 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6143
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6145
Michael Wrightd02c5b62014-02-10 15:10:22 -08006146 // press BTN_STYLUS, release BTN_STYLUS
6147 processKey(mapper, BTN_STYLUS, 1);
6148 processSync(mapper);
6149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006151 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6152
6153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6154 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6155 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006156
6157 processKey(mapper, BTN_STYLUS, 0);
6158 processSync(mapper);
6159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006160 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006161 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006162
6163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006164 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006165 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006166
6167 // press BTN_STYLUS2, release BTN_STYLUS2
6168 processKey(mapper, BTN_STYLUS2, 1);
6169 processSync(mapper);
6170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6171 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006172 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6173
6174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6175 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6176 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006177
6178 processKey(mapper, BTN_STYLUS2, 0);
6179 processSync(mapper);
6180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006181 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006183
6184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006185 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006186 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006187
6188 // release touch
6189 processUp(mapper);
6190 processSync(mapper);
6191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6192 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6193 ASSERT_EQ(0, motionArgs.buttonState);
6194}
6195
6196TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006197 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006198 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006199 prepareButtons();
6200 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006201 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006202
6203 NotifyMotionArgs motionArgs;
6204
6205 // default tool type is finger
6206 processDown(mapper, 100, 200);
6207 processSync(mapper);
6208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6209 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006210 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006211
6212 // eraser
6213 processKey(mapper, BTN_TOOL_RUBBER, 1);
6214 processSync(mapper);
6215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006217 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006218
6219 // stylus
6220 processKey(mapper, BTN_TOOL_RUBBER, 0);
6221 processKey(mapper, BTN_TOOL_PEN, 1);
6222 processSync(mapper);
6223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6224 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006225 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226
6227 // brush
6228 processKey(mapper, BTN_TOOL_PEN, 0);
6229 processKey(mapper, BTN_TOOL_BRUSH, 1);
6230 processSync(mapper);
6231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006233 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006234
6235 // pencil
6236 processKey(mapper, BTN_TOOL_BRUSH, 0);
6237 processKey(mapper, BTN_TOOL_PENCIL, 1);
6238 processSync(mapper);
6239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006241 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006242
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006243 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006244 processKey(mapper, BTN_TOOL_PENCIL, 0);
6245 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6246 processSync(mapper);
6247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006249 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006250
6251 // mouse
6252 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6253 processKey(mapper, BTN_TOOL_MOUSE, 1);
6254 processSync(mapper);
6255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6256 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006257 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006258
6259 // lens
6260 processKey(mapper, BTN_TOOL_MOUSE, 0);
6261 processKey(mapper, BTN_TOOL_LENS, 1);
6262 processSync(mapper);
6263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6264 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006265 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006266
6267 // double-tap
6268 processKey(mapper, BTN_TOOL_LENS, 0);
6269 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6270 processSync(mapper);
6271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6272 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006273 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006274
6275 // triple-tap
6276 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6277 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6278 processSync(mapper);
6279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6280 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006281 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006282
6283 // quad-tap
6284 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6285 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6286 processSync(mapper);
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006289 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290
6291 // finger
6292 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6293 processKey(mapper, BTN_TOOL_FINGER, 1);
6294 processSync(mapper);
6295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006297 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298
6299 // stylus trumps finger
6300 processKey(mapper, BTN_TOOL_PEN, 1);
6301 processSync(mapper);
6302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006304 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006305
6306 // eraser trumps stylus
6307 processKey(mapper, BTN_TOOL_RUBBER, 1);
6308 processSync(mapper);
6309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006311 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006312
6313 // mouse trumps eraser
6314 processKey(mapper, BTN_TOOL_MOUSE, 1);
6315 processSync(mapper);
6316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006318 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319
6320 // back to default tool type
6321 processKey(mapper, BTN_TOOL_MOUSE, 0);
6322 processKey(mapper, BTN_TOOL_RUBBER, 0);
6323 processKey(mapper, BTN_TOOL_PEN, 0);
6324 processKey(mapper, BTN_TOOL_FINGER, 0);
6325 processSync(mapper);
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006328 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006329}
6330
6331TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006332 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006333 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006334 prepareButtons();
6335 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006336 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006337 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338
6339 NotifyMotionArgs motionArgs;
6340
6341 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6342 processKey(mapper, BTN_TOOL_FINGER, 1);
6343 processMove(mapper, 100, 200);
6344 processSync(mapper);
6345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6346 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6347 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6348 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6349
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6351 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6352 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6353 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6354
6355 // move a little
6356 processMove(mapper, 150, 250);
6357 processSync(mapper);
6358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6359 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6361 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6362
6363 // down when BTN_TOUCH is pressed, pressure defaults to 1
6364 processKey(mapper, BTN_TOUCH, 1);
6365 processSync(mapper);
6366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6367 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6369 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6370
6371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6372 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6374 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6375
6376 // up when BTN_TOUCH is released, hover restored
6377 processKey(mapper, BTN_TOUCH, 0);
6378 processSync(mapper);
6379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6380 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6381 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6382 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6383
6384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6385 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6386 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6387 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6388
6389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6390 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6392 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6393
6394 // exit hover when pointer goes away
6395 processKey(mapper, BTN_TOOL_FINGER, 0);
6396 processSync(mapper);
6397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6398 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6400 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6401}
6402
6403TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006404 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006405 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006406 prepareButtons();
6407 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006408 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006409
6410 NotifyMotionArgs motionArgs;
6411
6412 // initially hovering because pressure is 0
6413 processDown(mapper, 100, 200);
6414 processPressure(mapper, 0);
6415 processSync(mapper);
6416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6417 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6419 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6420
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6422 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6424 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6425
6426 // move a little
6427 processMove(mapper, 150, 250);
6428 processSync(mapper);
6429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6430 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6432 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6433
6434 // down when pressure is non-zero
6435 processPressure(mapper, RAW_PRESSURE_MAX);
6436 processSync(mapper);
6437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6438 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6440 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6441
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6443 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6445 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6446
6447 // up when pressure becomes 0, hover restored
6448 processPressure(mapper, 0);
6449 processSync(mapper);
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6451 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6453 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6454
6455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6456 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6457 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6458 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6459
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6461 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6463 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6464
6465 // exit hover when pointer goes away
6466 processUp(mapper);
6467 processSync(mapper);
6468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6469 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6471 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6472}
6473
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006474TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6475 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006476 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006477 prepareButtons();
6478 prepareAxes(POSITION | PRESSURE);
6479 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6480
6481 // Touch down.
6482 processDown(mapper, 100, 200);
6483 processPressure(mapper, 1);
6484 processSync(mapper);
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6486 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6487
6488 // Reset the mapper. This should cancel the ongoing gesture.
6489 resetMapper(mapper, ARBITRARY_TIME);
6490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6491 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6492
6493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6494}
6495
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006496TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6497 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006498 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006499 prepareButtons();
6500 prepareAxes(POSITION | PRESSURE);
6501 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6502
6503 // Set the initial state for the touch pointer.
6504 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6505 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6506 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6507 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6508
6509 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006510 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6511 // does not generate any events.
6512 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006513
6514 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6515 // the recreated touch state to generate a down event.
6516 processSync(mapper);
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6518 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6519
6520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6521}
6522
lilinnan687e58f2022-07-19 16:00:50 +08006523TEST_F(SingleTouchInputMapperTest,
6524 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6525 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006526 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006527 prepareButtons();
6528 prepareAxes(POSITION);
6529 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6530 NotifyMotionArgs motionArgs;
6531
6532 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006533 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006534 processSync(mapper);
6535
6536 // We should receive a down event
6537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6538 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6539
6540 // Change display id
6541 clearViewports();
6542 prepareSecondaryDisplay(ViewportType::INTERNAL);
6543
6544 // We should receive a cancel event
6545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6546 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6547 // Then receive reset called
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6549}
6550
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006551TEST_F(SingleTouchInputMapperTest,
6552 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6553 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006554 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006555 prepareButtons();
6556 prepareAxes(POSITION);
6557 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6559 NotifyMotionArgs motionArgs;
6560
6561 // Start a new gesture.
6562 processDown(mapper, 100, 200);
6563 processSync(mapper);
6564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6565 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6566
6567 // Make the viewport inactive. This will put the device in disabled mode.
6568 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6569 viewport->isActive = false;
6570 mFakePolicy->updateViewport(*viewport);
6571 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6572
6573 // We should receive a cancel event for the ongoing gesture.
6574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6575 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6576 // Then we should be notified that the device was reset.
6577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6578
6579 // No events are generated while the viewport is inactive.
6580 processMove(mapper, 101, 201);
6581 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006582 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006583 processSync(mapper);
6584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6585
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006586 // Start a new gesture while the viewport is still inactive.
6587 processDown(mapper, 300, 400);
6588 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6589 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6590 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6591 processSync(mapper);
6592
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006593 // Make the viewport active again. The device should resume processing events.
6594 viewport->isActive = true;
6595 mFakePolicy->updateViewport(*viewport);
6596 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6597
6598 // The device is reset because it changes back to direct mode, without generating any events.
6599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6601
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006602 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006603 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6605 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006606
6607 // No more events.
6608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6610}
6611
Prabir Pradhan211ba622022-10-31 21:09:21 +00006612TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6613 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006614 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006615 prepareButtons();
6616 prepareAxes(POSITION);
6617 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6619
6620 // Press a stylus button.
6621 processKey(mapper, BTN_STYLUS, 1);
6622 processSync(mapper);
6623
6624 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6625 processDown(mapper, 100, 200);
6626 processSync(mapper);
6627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6628 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6629 WithCoords(toDisplayX(100), toDisplayY(200)),
6630 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6632 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6633 WithCoords(toDisplayX(100), toDisplayY(200)),
6634 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6635
6636 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6637 // the button has not actually been released, since there will be no pointers through which the
6638 // button state can be reported. The event is generated at the location of the pointer before
6639 // it went up.
6640 processUp(mapper);
6641 processSync(mapper);
6642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6643 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6644 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6646 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6647 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6648}
6649
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006650TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6651 addConfigurationProperty("touch.deviceType", "touchScreen");
6652 prepareDisplay(ui::ROTATION_0);
6653 prepareButtons();
6654 prepareAxes(POSITION);
6655
6656 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6657
6658 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6660
6661 // Press a stylus button.
6662 processKey(mapper, BTN_STYLUS, 1);
6663 processSync(mapper);
6664
6665 // Start a touch gesture and ensure that the stylus button is not reported.
6666 processDown(mapper, 100, 200);
6667 processSync(mapper);
6668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6669 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6670
6671 // Release and press the stylus button again.
6672 processKey(mapper, BTN_STYLUS, 0);
6673 processSync(mapper);
6674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6675 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6676 processKey(mapper, BTN_STYLUS, 1);
6677 processSync(mapper);
6678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6679 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6680
6681 // Release the touch gesture.
6682 processUp(mapper);
6683 processSync(mapper);
6684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6685 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6686
6687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6688}
6689
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006690TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6691 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6692 prepareDisplay(ui::ROTATION_0);
6693 prepareButtons();
6694 prepareAxes(POSITION);
6695 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6697
6698 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6699}
6700
Seunghwan Choi356026c2023-02-01 14:37:25 +09006701TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6702 std::shared_ptr<FakePointerController> fakePointerController =
6703 std::make_shared<FakePointerController>();
6704 addConfigurationProperty("touch.deviceType", "touchScreen");
6705 prepareDisplay(ui::ROTATION_0);
6706 prepareButtons();
6707 prepareAxes(POSITION);
6708 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6709 mFakePolicy->setPointerController(fakePointerController);
6710 mFakePolicy->setStylusPointerIconEnabled(true);
6711 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6712
6713 processKey(mapper, BTN_TOOL_PEN, 1);
6714 processMove(mapper, 100, 200);
6715 processSync(mapper);
6716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6717 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006718 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006719 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6720 ASSERT_TRUE(fakePointerController->isPointerShown());
6721 ASSERT_NO_FATAL_FAILURE(
6722 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6723}
6724
6725TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6726 std::shared_ptr<FakePointerController> fakePointerController =
6727 std::make_shared<FakePointerController>();
6728 addConfigurationProperty("touch.deviceType", "touchScreen");
6729 prepareDisplay(ui::ROTATION_0);
6730 prepareButtons();
6731 prepareAxes(POSITION);
6732 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6733 mFakePolicy->setPointerController(fakePointerController);
6734 mFakePolicy->setStylusPointerIconEnabled(false);
6735 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6736
6737 processKey(mapper, BTN_TOOL_PEN, 1);
6738 processMove(mapper, 100, 200);
6739 processSync(mapper);
6740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6741 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006742 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006743 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6744 ASSERT_FALSE(fakePointerController->isPointerShown());
6745}
6746
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006747TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6748 // Initialize the device without setting device source to touch navigation.
6749 addConfigurationProperty("touch.deviceType", "touchScreen");
6750 prepareDisplay(ui::ROTATION_0);
6751 prepareButtons();
6752 prepareAxes(POSITION);
6753 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6754
6755 // Ensure that the device is created as a touchscreen, not touch navigation.
6756 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6757
6758 // Add device type association after the device was created.
6759 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6760
6761 // Send update to the mapper.
6762 std::list<NotifyArgs> unused2 =
6763 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6764 InputReaderConfiguration::CHANGE_DEVICE_TYPE /*changes*/);
6765
6766 // Check whether device type update was successful.
6767 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6768}
6769
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006770TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6771 // Initialize the device without setting device source to touch navigation.
6772 addConfigurationProperty("touch.deviceType", "touchScreen");
6773 prepareDisplay(ui::ROTATION_0);
6774 prepareButtons();
6775 prepareAxes(POSITION);
6776 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6777
6778 // Set a physical frame in the display viewport.
6779 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6780 viewport->physicalLeft = 0;
6781 viewport->physicalTop = 0;
6782 viewport->physicalRight = DISPLAY_WIDTH / 2;
6783 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6784 mFakePolicy->updateViewport(*viewport);
6785 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6786
6787 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6788
6789 // Hovering inside the physical frame produces events.
6790 processKey(mapper, BTN_TOOL_PEN, 1);
6791 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6792 processSync(mapper);
6793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6794 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6796 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6797
6798 // Leaving the physical frame ends the hovering gesture.
6799 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6800 processSync(mapper);
6801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6802 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6803
6804 // Moving outside the physical frame does not produce events.
6805 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6806 processSync(mapper);
6807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6808
6809 // Re-entering the physical frame produces events.
6810 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6811 processSync(mapper);
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6813 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6815 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6816}
6817
Prabir Pradhan5632d622021-09-06 07:57:20 -07006818// --- TouchDisplayProjectionTest ---
6819
6820class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6821public:
6822 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6823 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6824 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006825 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6826 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6827 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006828 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006829 auto rotatedWidth = naturalDisplayWidth;
6830 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006831 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006832 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006833 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006834 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006835 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006836 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006837 inverseRotationFlags = ui::Transform::ROT_180;
6838 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006839 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006840 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006841 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006842 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006843 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006844 inverseRotationFlags = ui::Transform::ROT_0;
6845 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006846 }
6847
Prabir Pradhana9df3162022-12-05 23:57:27 +00006848 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006849 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6850
6851 std::optional<DisplayViewport> internalViewport =
6852 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6853 DisplayViewport& v = *internalViewport;
6854 v.displayId = DISPLAY_ID;
6855 v.orientation = orientation;
6856
6857 v.logicalLeft = 0;
6858 v.logicalTop = 0;
6859 v.logicalRight = 100;
6860 v.logicalBottom = 100;
6861
6862 v.physicalLeft = rotatedPhysicalDisplay.left;
6863 v.physicalTop = rotatedPhysicalDisplay.top;
6864 v.physicalRight = rotatedPhysicalDisplay.right;
6865 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6866
Prabir Pradhana9df3162022-12-05 23:57:27 +00006867 v.deviceWidth = rotatedWidth;
6868 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006869
6870 v.isActive = true;
6871 v.uniqueId = UNIQUE_ID;
6872 v.type = ViewportType::INTERNAL;
6873 mFakePolicy->updateViewport(v);
6874 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6875 }
6876
6877 void assertReceivedMove(const Point& point) {
6878 NotifyMotionArgs motionArgs;
6879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6880 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6881 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6882 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6883 1, 0, 0, 0, 0, 0, 0, 0));
6884 }
6885};
6886
6887TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6888 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006889 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006890
6891 prepareButtons();
6892 prepareAxes(POSITION);
6893 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6894
6895 NotifyMotionArgs motionArgs;
6896
6897 // Configure the DisplayViewport such that the logical display maps to a subsection of
6898 // the display panel called the physical display. Here, the physical display is bounded by the
6899 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6900 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6901 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6902 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6903
Michael Wrighta9cf4192022-12-01 23:46:39 +00006904 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006905 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6906
6907 // Touches outside the physical display should be ignored, and should not generate any
6908 // events. Ensure touches at the following points that lie outside of the physical display
6909 // area do not generate any events.
6910 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6911 processDown(mapper, toRawX(point.x), toRawY(point.y));
6912 processSync(mapper);
6913 processUp(mapper);
6914 processSync(mapper);
6915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6916 << "Unexpected event generated for touch outside physical display at point: "
6917 << point.x << ", " << point.y;
6918 }
6919 }
6920}
6921
6922TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6923 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006924 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006925
6926 prepareButtons();
6927 prepareAxes(POSITION);
6928 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6929
6930 NotifyMotionArgs motionArgs;
6931
6932 // Configure the DisplayViewport such that the logical display maps to a subsection of
6933 // the display panel called the physical display. Here, the physical display is bounded by the
6934 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6935 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6936
Michael Wrighta9cf4192022-12-01 23:46:39 +00006937 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006938 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6939
6940 // Touches that start outside the physical display should be ignored until it enters the
6941 // physical display bounds, at which point it should generate a down event. Start a touch at
6942 // the point (5, 100), which is outside the physical display bounds.
6943 static const Point kOutsidePoint{5, 100};
6944 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6945 processSync(mapper);
6946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6947
6948 // Move the touch into the physical display area. This should generate a pointer down.
6949 processMove(mapper, toRawX(11), toRawY(21));
6950 processSync(mapper);
6951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6952 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6953 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6954 ASSERT_NO_FATAL_FAILURE(
6955 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6956
6957 // Move the touch inside the physical display area. This should generate a pointer move.
6958 processMove(mapper, toRawX(69), toRawY(159));
6959 processSync(mapper);
6960 assertReceivedMove({69, 159});
6961
6962 // Move outside the physical display area. Since the pointer is already down, this should
6963 // now continue generating events.
6964 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6965 processSync(mapper);
6966 assertReceivedMove(kOutsidePoint);
6967
6968 // Release. This should generate a pointer up.
6969 processUp(mapper);
6970 processSync(mapper);
6971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6972 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6973 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6974 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6975
6976 // Ensure no more events were generated.
6977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6979 }
6980}
6981
Prabir Pradhana9df3162022-12-05 23:57:27 +00006982// --- TouchscreenPrecisionTests ---
6983
6984// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6985// in various orientations and with different display rotations. We configure the touchscreen to
6986// have a higher resolution than that of the display by an integer scale factor in each axis so that
6987// we can enforce that coordinates match precisely as expected.
6988class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6989 public ::testing::WithParamInterface<ui::Rotation> {
6990public:
6991 void SetUp() override {
6992 SingleTouchInputMapperTest::SetUp();
6993
6994 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6995 // four times the resolution of the display in the Y axis.
6996 prepareButtons();
6997 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006998 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
6999 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007000 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007001 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7002 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007003 }
7004
7005 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7006 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7007 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7008 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7009
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007010 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7011 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7012
7013 static const int32_t PRECISION_RAW_X_FLAT = 16;
7014 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7015
7016 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7017 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7018
Prabir Pradhana9df3162022-12-05 23:57:27 +00007019 static const std::array<Point, 4> kRawCorners;
7020};
7021
7022const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7023 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7024 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7025 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7026 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7027}};
7028
7029// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7030// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7031// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7032TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7033 enum class Orientation {
7034 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7035 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7036 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7037 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7038 ftl_last = ORIENTATION_270,
7039 };
7040 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7041 Orientation::ORIENTATION_270;
7042 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7043 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7044 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7045 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7046 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7047 };
7048
7049 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7050
7051 // Configure the touchscreen as being installed in the one of the four different orientations
7052 // relative to the display.
7053 addConfigurationProperty("touch.deviceType", "touchScreen");
7054 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7055 prepareDisplay(ui::ROTATION_0);
7056
7057 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7058
7059 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7060 // orientations of either 90 or 270) this means the display's natural resolution will be
7061 // flipped.
7062 const bool displayRotated =
7063 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7064 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7065 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7066 const Rect physicalFrame{0, 0, width, height};
7067 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7068
7069 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7070 const float expectedPrecisionX = displayRotated ? 4 : 2;
7071 const float expectedPrecisionY = displayRotated ? 2 : 4;
7072
7073 // Test all four corners.
7074 for (int i = 0; i < 4; i++) {
7075 const auto& raw = kRawCorners[i];
7076 processDown(mapper, raw.x, raw.y);
7077 processSync(mapper);
7078 const auto& expected = expectedPoints[i];
7079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7080 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7081 WithCoords(expected.x, expected.y),
7082 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7083 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7084 << "with touchscreen orientation "
7085 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7086 << expected.x << ", " << expected.y << ").";
7087 processUp(mapper);
7088 processSync(mapper);
7089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7090 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7091 WithCoords(expected.x, expected.y))));
7092 }
7093}
7094
Prabir Pradhan82687402022-12-06 01:32:53 +00007095TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7096 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7097 kMappedCorners = {
7098 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7099 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7100 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7101 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7102 };
7103
7104 const ui::Rotation displayRotation = GetParam();
7105
7106 addConfigurationProperty("touch.deviceType", "touchScreen");
7107 prepareDisplay(displayRotation);
7108
7109 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7110
7111 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7112
7113 // Test all four corners.
7114 for (int i = 0; i < 4; i++) {
7115 const auto& expected = expectedPoints[i];
7116 const auto& raw = kRawCorners[i];
7117 processDown(mapper, raw.x, raw.y);
7118 processSync(mapper);
7119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7120 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7121 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7122 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7123 << "with display rotation " << ui::toCString(displayRotation)
7124 << ", expected point (" << expected.x << ", " << expected.y << ").";
7125 processUp(mapper);
7126 processSync(mapper);
7127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7128 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7129 WithCoords(expected.x, expected.y))));
7130 }
7131}
7132
Prabir Pradhan3e798762022-12-02 21:02:11 +00007133TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7134 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7135 kMappedCorners = {
7136 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7137 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7138 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7139 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7140 };
7141
7142 const ui::Rotation displayRotation = GetParam();
7143
7144 addConfigurationProperty("touch.deviceType", "touchScreen");
7145 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7146
7147 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7148
7149 // Ori 270, so width and height swapped
7150 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7151 prepareDisplay(displayRotation);
7152 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7153
7154 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7155
7156 // Test all four corners.
7157 for (int i = 0; i < 4; i++) {
7158 const auto& expected = expectedPoints[i];
7159 const auto& raw = kRawCorners[i];
7160 processDown(mapper, raw.x, raw.y);
7161 processSync(mapper);
7162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7163 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7164 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7165 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7166 << "with display rotation " << ui::toCString(displayRotation)
7167 << ", expected point (" << expected.x << ", " << expected.y << ").";
7168 processUp(mapper);
7169 processSync(mapper);
7170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7171 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7172 WithCoords(expected.x, expected.y))));
7173 }
7174}
7175
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007176TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7177 const ui::Rotation displayRotation = GetParam();
7178
7179 addConfigurationProperty("touch.deviceType", "touchScreen");
7180 prepareDisplay(displayRotation);
7181
7182 __attribute__((unused)) SingleTouchInputMapper& mapper =
7183 addMapperAndConfigure<SingleTouchInputMapper>();
7184
7185 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7186 // MotionRanges use display pixels as their units
7187 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7188 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7189
7190 // The MotionRanges should be oriented in the rotated display's coordinate space
7191 const bool displayRotated =
7192 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7193
7194 constexpr float MAX_X = 479.5;
7195 constexpr float MAX_Y = 799.75;
7196 EXPECT_EQ(xRange->min, 0.f);
7197 EXPECT_EQ(yRange->min, 0.f);
7198 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7199 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7200
7201 EXPECT_EQ(xRange->flat, 8.f);
7202 EXPECT_EQ(yRange->flat, 8.f);
7203
7204 EXPECT_EQ(xRange->fuzz, 2.f);
7205 EXPECT_EQ(yRange->fuzz, 2.f);
7206
7207 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7208 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7209}
7210
Prabir Pradhana9df3162022-12-05 23:57:27 +00007211// Run the precision tests for all rotations.
7212INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7213 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7214 ui::ROTATION_270),
7215 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7216 return ftl::enum_string(testParamInfo.param);
7217 });
7218
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007219// --- ExternalStylusFusionTest ---
7220
7221class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7222public:
7223 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7224 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007225 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007226 prepareButtons();
7227 prepareAxes(POSITION);
7228 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7229
7230 mStylusState.when = ARBITRARY_TIME;
7231 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007232 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007233 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
7234 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
7235 processExternalStylusState(mapper);
7236 return mapper;
7237 }
7238
7239 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7240 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7241 for (const NotifyArgs& args : generatedArgs) {
7242 mFakeListener->notify(args);
7243 }
7244 // Loop the reader to flush the input listener queue.
7245 mReader->loopOnce();
7246 return generatedArgs;
7247 }
7248
7249protected:
7250 StylusState mStylusState{};
7251 static constexpr uint32_t EXPECTED_SOURCE =
7252 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7253
7254 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7255 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007256 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007257
7258 // The first pointer is withheld.
7259 processDown(mapper, 100, 200);
7260 processSync(mapper);
7261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7262 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7263 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7264
7265 // The external stylus reports pressure. The withheld finger pointer is released as a
7266 // stylus.
7267 mStylusState.pressure = 1.f;
7268 processExternalStylusState(mapper);
7269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7270 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7271 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7272
7273 // Subsequent pointer events are not withheld.
7274 processMove(mapper, 101, 201);
7275 processSync(mapper);
7276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7277 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7278
7279 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7281 }
7282
7283 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7284 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7285
7286 // Releasing the touch pointer ends the gesture.
7287 processUp(mapper);
7288 processSync(mapper);
7289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7290 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007291 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007292
7293 mStylusState.pressure = 0.f;
7294 processExternalStylusState(mapper);
7295 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7297 }
7298
7299 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7300 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007301 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007302
7303 // The first pointer is withheld when an external stylus is connected,
7304 // and a timeout is requested.
7305 processDown(mapper, 100, 200);
7306 processSync(mapper);
7307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7308 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7309 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7310
7311 // If the timeout expires early, it is requested again.
7312 handleTimeout(mapper, ARBITRARY_TIME + 1);
7313 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7314 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7315
7316 // When the timeout expires, the withheld touch is released as a finger pointer.
7317 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7319 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7320
7321 // Subsequent pointer events are not withheld.
7322 processMove(mapper, 101, 201);
7323 processSync(mapper);
7324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7325 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7326 processUp(mapper);
7327 processSync(mapper);
7328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7329 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7330
7331 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7333 }
7334
7335private:
7336 InputDeviceInfo mExternalStylusDeviceInfo{};
7337};
7338
7339TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7340 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7341 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7342}
7343
7344TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7345 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7346 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7347}
7348
7349TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7350 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7351 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7352}
7353
7354// Test a successful stylus fusion gesture where the pressure is reported by the external
7355// before the touch is reported by the touchscreen.
7356TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7357 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7358 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007359 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007360
7361 // The external stylus reports pressure first. It is ignored for now.
7362 mStylusState.pressure = 1.f;
7363 processExternalStylusState(mapper);
7364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7365 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7366
7367 // When the touch goes down afterwards, it is reported as a stylus pointer.
7368 processDown(mapper, 100, 200);
7369 processSync(mapper);
7370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7371 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7372 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7373
7374 processMove(mapper, 101, 201);
7375 processSync(mapper);
7376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7377 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7378 processUp(mapper);
7379 processSync(mapper);
7380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7381 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7382
7383 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7385}
7386
7387TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7388 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7389
7390 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7391 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7392
7393 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7394 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7395 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7396 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7397}
7398
7399TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7400 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7401 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007402 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007403
7404 mStylusState.pressure = 0.8f;
7405 processExternalStylusState(mapper);
7406 processDown(mapper, 100, 200);
7407 processSync(mapper);
7408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7409 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7410 WithPressure(0.8f))));
7411 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7412
7413 // The external stylus reports a pressure change. We wait for some time for a touch event.
7414 mStylusState.pressure = 0.6f;
7415 processExternalStylusState(mapper);
7416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7417 ASSERT_NO_FATAL_FAILURE(
7418 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7419
7420 // If a touch is reported within the timeout, it reports the updated pressure.
7421 processMove(mapper, 101, 201);
7422 processSync(mapper);
7423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7424 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7425 WithPressure(0.6f))));
7426 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7427
7428 // There is another pressure change.
7429 mStylusState.pressure = 0.5f;
7430 processExternalStylusState(mapper);
7431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7432 ASSERT_NO_FATAL_FAILURE(
7433 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7434
7435 // If a touch is not reported within the timeout, a move event is generated to report
7436 // the new pressure.
7437 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7439 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7440 WithPressure(0.5f))));
7441
7442 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7443 // repeated indefinitely.
7444 mStylusState.pressure = 0.0f;
7445 processExternalStylusState(mapper);
7446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7447 ASSERT_NO_FATAL_FAILURE(
7448 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7449 processMove(mapper, 102, 202);
7450 processSync(mapper);
7451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7452 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7453 WithPressure(0.5f))));
7454 processMove(mapper, 103, 203);
7455 processSync(mapper);
7456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7457 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7458 WithPressure(0.5f))));
7459
7460 processUp(mapper);
7461 processSync(mapper);
7462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7463 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007464 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007465
7466 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7468}
7469
7470TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7471 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7472 auto source = WithSource(EXPECTED_SOURCE);
7473
7474 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007475 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007476 processExternalStylusState(mapper);
7477 processDown(mapper, 100, 200);
7478 processSync(mapper);
7479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7480 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007481 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007482 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7483
7484 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007485 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007486 processExternalStylusState(mapper);
7487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7488 ASSERT_NO_FATAL_FAILURE(
7489 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7490
7491 // If a touch is reported within the timeout, it reports the updated pressure.
7492 processMove(mapper, 101, 201);
7493 processSync(mapper);
7494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7495 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007496 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007497 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7498
7499 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007500 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007501 processExternalStylusState(mapper);
7502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7503 ASSERT_NO_FATAL_FAILURE(
7504 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7505
7506 // If a touch is not reported within the timeout, a move event is generated to report
7507 // the new tool type.
7508 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7510 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007511 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007512
7513 processUp(mapper);
7514 processSync(mapper);
7515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7516 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007517 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007518
7519 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7521}
7522
7523TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7524 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7525 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007526 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007527
7528 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7529
7530 // The external stylus reports a button change. We wait for some time for a touch event.
7531 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7532 processExternalStylusState(mapper);
7533 ASSERT_NO_FATAL_FAILURE(
7534 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7535
7536 // If a touch is reported within the timeout, it reports the updated button state.
7537 processMove(mapper, 101, 201);
7538 processSync(mapper);
7539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7540 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7541 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7543 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7544 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7545 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7546
7547 // The button is now released.
7548 mStylusState.buttons = 0;
7549 processExternalStylusState(mapper);
7550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7551 ASSERT_NO_FATAL_FAILURE(
7552 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7553
7554 // If a touch is not reported within the timeout, a move event is generated to report
7555 // the new button state.
7556 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7558 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7559 WithButtonState(0))));
7560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007561 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7562 WithButtonState(0))));
7563
7564 processUp(mapper);
7565 processSync(mapper);
7566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007567 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7568
7569 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7571}
7572
Michael Wrightd02c5b62014-02-10 15:10:22 -08007573// --- MultiTouchInputMapperTest ---
7574
7575class MultiTouchInputMapperTest : public TouchInputMapperTest {
7576protected:
7577 void prepareAxes(int axes);
7578
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007579 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7580 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7581 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7582 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7583 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7584 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7585 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7586 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7587 void processId(MultiTouchInputMapper& mapper, int32_t id);
7588 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7589 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7590 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007591 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007592 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007593 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7594 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007595};
7596
7597void MultiTouchInputMapperTest::prepareAxes(int axes) {
7598 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007599 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7600 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007601 }
7602 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007603 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7604 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007605 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007606 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7607 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007608 }
7609 }
7610 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007611 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7612 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007613 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007614 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007615 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007616 }
7617 }
7618 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007619 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7620 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007621 }
7622 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007623 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7624 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007625 }
7626 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007627 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7628 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007629 }
7630 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007631 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7632 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007633 }
7634 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007635 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7636 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007637 }
7638 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007639 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007640 }
7641}
7642
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007643void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7644 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007645 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7646 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007647}
7648
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007649void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7650 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007651 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007652}
7653
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007654void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7655 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007657}
7658
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007659void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007660 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007661}
7662
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007663void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007665}
7666
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007667void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7668 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007670}
7671
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007672void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007673 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007674}
7675
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007676void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007678}
7679
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007680void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007681 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007682}
7683
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007684void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007686}
7687
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007688void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007689 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007690}
7691
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007692void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7693 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007694 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007695}
7696
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007697void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7698 int32_t value) {
7699 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7701}
7702
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007703void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007704 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007705}
7706
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007707void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7708 nsecs_t readTime) {
7709 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007710}
7711
Michael Wrightd02c5b62014-02-10 15:10:22 -08007712TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007713 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007714 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007715 prepareAxes(POSITION);
7716 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007717 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007718
arthurhungdcef2dc2020-08-11 14:47:50 +08007719 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007720
7721 NotifyMotionArgs motionArgs;
7722
7723 // Two fingers down at once.
7724 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7725 processPosition(mapper, x1, y1);
7726 processMTSync(mapper);
7727 processPosition(mapper, x2, y2);
7728 processMTSync(mapper);
7729 processSync(mapper);
7730
7731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7732 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7733 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7734 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7735 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7736 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7737 ASSERT_EQ(0, motionArgs.flags);
7738 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7739 ASSERT_EQ(0, motionArgs.buttonState);
7740 ASSERT_EQ(0, motionArgs.edgeFlags);
7741 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7742 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007743 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7745 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7746 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7747 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7748 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7749
7750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7751 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7752 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7753 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7754 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007755 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007756 ASSERT_EQ(0, motionArgs.flags);
7757 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7758 ASSERT_EQ(0, motionArgs.buttonState);
7759 ASSERT_EQ(0, motionArgs.edgeFlags);
7760 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7761 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007762 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007763 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007764 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7766 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7768 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7769 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7770 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7771 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7772
7773 // Move.
7774 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7775 processPosition(mapper, x1, y1);
7776 processMTSync(mapper);
7777 processPosition(mapper, x2, y2);
7778 processMTSync(mapper);
7779 processSync(mapper);
7780
7781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7782 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7783 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7784 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7785 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7786 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7787 ASSERT_EQ(0, motionArgs.flags);
7788 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7789 ASSERT_EQ(0, motionArgs.buttonState);
7790 ASSERT_EQ(0, motionArgs.edgeFlags);
7791 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7792 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007793 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007794 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007795 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7797 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7799 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7800 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7801 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7802 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7803
7804 // First finger up.
7805 x2 += 15; y2 -= 20;
7806 processPosition(mapper, x2, y2);
7807 processMTSync(mapper);
7808 processSync(mapper);
7809
7810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7811 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7812 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7813 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7814 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007815 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007816 ASSERT_EQ(0, motionArgs.flags);
7817 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7818 ASSERT_EQ(0, motionArgs.buttonState);
7819 ASSERT_EQ(0, motionArgs.edgeFlags);
7820 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7821 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007822 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007823 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007824 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7826 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7828 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7829 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7830 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7831 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7832
7833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7834 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7835 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7836 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7837 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7838 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7839 ASSERT_EQ(0, motionArgs.flags);
7840 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7841 ASSERT_EQ(0, motionArgs.buttonState);
7842 ASSERT_EQ(0, motionArgs.edgeFlags);
7843 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7844 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007845 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7847 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7848 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7849 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7850 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7851
7852 // Move.
7853 x2 += 20; y2 -= 25;
7854 processPosition(mapper, x2, y2);
7855 processMTSync(mapper);
7856 processSync(mapper);
7857
7858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7859 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7860 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7861 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7862 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7863 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7864 ASSERT_EQ(0, motionArgs.flags);
7865 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7866 ASSERT_EQ(0, motionArgs.buttonState);
7867 ASSERT_EQ(0, motionArgs.edgeFlags);
7868 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7869 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007870 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7872 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7873 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7874 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7875 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7876
7877 // New finger down.
7878 int32_t x3 = 700, y3 = 300;
7879 processPosition(mapper, x2, y2);
7880 processMTSync(mapper);
7881 processPosition(mapper, x3, y3);
7882 processMTSync(mapper);
7883 processSync(mapper);
7884
7885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7886 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7887 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7888 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7889 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007890 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007891 ASSERT_EQ(0, motionArgs.flags);
7892 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7893 ASSERT_EQ(0, motionArgs.buttonState);
7894 ASSERT_EQ(0, motionArgs.edgeFlags);
7895 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7896 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007897 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007898 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007899 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007900 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7901 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7902 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7903 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7904 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7905 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7906 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7907
7908 // Second finger up.
7909 x3 += 30; y3 -= 20;
7910 processPosition(mapper, x3, y3);
7911 processMTSync(mapper);
7912 processSync(mapper);
7913
7914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7915 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7916 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7917 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7918 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007919 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007920 ASSERT_EQ(0, motionArgs.flags);
7921 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7922 ASSERT_EQ(0, motionArgs.buttonState);
7923 ASSERT_EQ(0, motionArgs.edgeFlags);
7924 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7925 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007926 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007927 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007928 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7930 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7932 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7933 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7934 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7935 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7936
7937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7938 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7939 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7940 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7941 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7942 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7943 ASSERT_EQ(0, motionArgs.flags);
7944 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7945 ASSERT_EQ(0, motionArgs.buttonState);
7946 ASSERT_EQ(0, motionArgs.edgeFlags);
7947 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7948 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007949 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7951 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7952 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7953 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7954 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7955
7956 // Last finger up.
7957 processMTSync(mapper);
7958 processSync(mapper);
7959
7960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7961 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7962 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7963 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7964 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7965 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7966 ASSERT_EQ(0, motionArgs.flags);
7967 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7968 ASSERT_EQ(0, motionArgs.buttonState);
7969 ASSERT_EQ(0, motionArgs.edgeFlags);
7970 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7971 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007972 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007973 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7974 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7975 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7976 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7977 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7978
7979 // Should not have sent any more keys or motions.
7980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7982}
7983
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007984TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7985 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007986 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007987
7988 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7989 /*fuzz*/ 0, /*resolution*/ 10);
7990 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7991 /*fuzz*/ 0, /*resolution*/ 11);
7992 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7993 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7994 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7995 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7996 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7997 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7998 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7999 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8000
8001 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8002
8003 // X and Y axes
8004 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8005 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8006 // Touch major and minor
8007 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8008 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8009 // Tool major and minor
8010 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8011 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8012}
8013
8014TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8015 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008016 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008017
8018 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8019 /*fuzz*/ 0, /*resolution*/ 10);
8020 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8021 /*fuzz*/ 0, /*resolution*/ 11);
8022
8023 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8024
8025 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8026
8027 // Touch major and minor
8028 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8029 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8030 // Tool major and minor
8031 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8032 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8033}
8034
Michael Wrightd02c5b62014-02-10 15:10:22 -08008035TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008036 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008037 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008038 prepareAxes(POSITION | ID);
8039 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008040 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008041
arthurhungdcef2dc2020-08-11 14:47:50 +08008042 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008043
8044 NotifyMotionArgs motionArgs;
8045
8046 // Two fingers down at once.
8047 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8048 processPosition(mapper, x1, y1);
8049 processId(mapper, 1);
8050 processMTSync(mapper);
8051 processPosition(mapper, x2, y2);
8052 processId(mapper, 2);
8053 processMTSync(mapper);
8054 processSync(mapper);
8055
8056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8057 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8058 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8059 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008060 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8062 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8063
8064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008065 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008066 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8067 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008068 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008069 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008070 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008071 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8072 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8073 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8074 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8075
8076 // Move.
8077 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8078 processPosition(mapper, x1, y1);
8079 processId(mapper, 1);
8080 processMTSync(mapper);
8081 processPosition(mapper, x2, y2);
8082 processId(mapper, 2);
8083 processMTSync(mapper);
8084 processSync(mapper);
8085
8086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8087 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8088 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8089 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008090 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008091 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008092 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8094 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8095 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8096 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8097
8098 // First finger up.
8099 x2 += 15; y2 -= 20;
8100 processPosition(mapper, x2, y2);
8101 processId(mapper, 2);
8102 processMTSync(mapper);
8103 processSync(mapper);
8104
8105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008106 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008107 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8108 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008109 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008110 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008111 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008112 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8113 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8114 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8115 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8116
8117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8119 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8120 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008121 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008122 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8123 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8124
8125 // Move.
8126 x2 += 20; y2 -= 25;
8127 processPosition(mapper, x2, y2);
8128 processId(mapper, 2);
8129 processMTSync(mapper);
8130 processSync(mapper);
8131
8132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8134 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8135 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008136 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008137 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8138 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8139
8140 // New finger down.
8141 int32_t x3 = 700, y3 = 300;
8142 processPosition(mapper, x2, y2);
8143 processId(mapper, 2);
8144 processMTSync(mapper);
8145 processPosition(mapper, x3, y3);
8146 processId(mapper, 3);
8147 processMTSync(mapper);
8148 processSync(mapper);
8149
8150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008151 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008152 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8153 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008154 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008155 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008156 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008157 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8158 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8159 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8160 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8161
8162 // Second finger up.
8163 x3 += 30; y3 -= 20;
8164 processPosition(mapper, x3, y3);
8165 processId(mapper, 3);
8166 processMTSync(mapper);
8167 processSync(mapper);
8168
8169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008170 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008171 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8172 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008173 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008174 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008175 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008176 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8177 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8178 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8179 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8180
8181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8183 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8184 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008185 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008186 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8187 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8188
8189 // Last finger up.
8190 processMTSync(mapper);
8191 processSync(mapper);
8192
8193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8194 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8195 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8196 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008197 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008198 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8199 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8200
8201 // Should not have sent any more keys or motions.
8202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8204}
8205
8206TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008207 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008208 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008209 prepareAxes(POSITION | ID | SLOT);
8210 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008211 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008212
arthurhungdcef2dc2020-08-11 14:47:50 +08008213 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008214
8215 NotifyMotionArgs motionArgs;
8216
8217 // Two fingers down at once.
8218 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8219 processPosition(mapper, x1, y1);
8220 processId(mapper, 1);
8221 processSlot(mapper, 1);
8222 processPosition(mapper, x2, y2);
8223 processId(mapper, 2);
8224 processSync(mapper);
8225
8226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8227 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8228 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8229 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008230 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8232 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8233
8234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008235 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008236 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8237 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008238 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008239 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008240 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8242 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8244 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8245
8246 // Move.
8247 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8248 processSlot(mapper, 0);
8249 processPosition(mapper, x1, y1);
8250 processSlot(mapper, 1);
8251 processPosition(mapper, x2, y2);
8252 processSync(mapper);
8253
8254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8256 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8257 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008258 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008259 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008260 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8262 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8264 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8265
8266 // First finger up.
8267 x2 += 15; y2 -= 20;
8268 processSlot(mapper, 0);
8269 processId(mapper, -1);
8270 processSlot(mapper, 1);
8271 processPosition(mapper, x2, y2);
8272 processSync(mapper);
8273
8274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008275 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008276 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8277 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008278 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008279 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008280 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8282 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8283 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8284 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8285
8286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8288 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8289 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008290 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8292 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8293
8294 // Move.
8295 x2 += 20; y2 -= 25;
8296 processPosition(mapper, x2, y2);
8297 processSync(mapper);
8298
8299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8301 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8302 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008303 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8305 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8306
8307 // New finger down.
8308 int32_t x3 = 700, y3 = 300;
8309 processPosition(mapper, x2, y2);
8310 processSlot(mapper, 0);
8311 processId(mapper, 3);
8312 processPosition(mapper, x3, y3);
8313 processSync(mapper);
8314
8315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008316 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008317 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8318 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008319 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008320 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008321 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8323 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8324 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8325 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8326
8327 // Second finger up.
8328 x3 += 30; y3 -= 20;
8329 processSlot(mapper, 1);
8330 processId(mapper, -1);
8331 processSlot(mapper, 0);
8332 processPosition(mapper, x3, y3);
8333 processSync(mapper);
8334
8335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008336 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008337 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8338 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008339 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008340 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008341 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008342 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8343 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8345 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8346
8347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8348 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8349 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8350 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008351 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008352 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8353 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8354
8355 // Last finger up.
8356 processId(mapper, -1);
8357 processSync(mapper);
8358
8359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8360 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8361 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8362 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008363 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8365 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8366
8367 // Should not have sent any more keys or motions.
8368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8370}
8371
8372TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008373 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008374 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008375 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008376 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008377
8378 // These calculations are based on the input device calibration documentation.
8379 int32_t rawX = 100;
8380 int32_t rawY = 200;
8381 int32_t rawTouchMajor = 7;
8382 int32_t rawTouchMinor = 6;
8383 int32_t rawToolMajor = 9;
8384 int32_t rawToolMinor = 8;
8385 int32_t rawPressure = 11;
8386 int32_t rawDistance = 0;
8387 int32_t rawOrientation = 3;
8388 int32_t id = 5;
8389
8390 float x = toDisplayX(rawX);
8391 float y = toDisplayY(rawY);
8392 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8393 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8394 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8395 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8396 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8397 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8398 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8399 float distance = float(rawDistance);
8400
8401 processPosition(mapper, rawX, rawY);
8402 processTouchMajor(mapper, rawTouchMajor);
8403 processTouchMinor(mapper, rawTouchMinor);
8404 processToolMajor(mapper, rawToolMajor);
8405 processToolMinor(mapper, rawToolMinor);
8406 processPressure(mapper, rawPressure);
8407 processOrientation(mapper, rawOrientation);
8408 processDistance(mapper, rawDistance);
8409 processId(mapper, id);
8410 processMTSync(mapper);
8411 processSync(mapper);
8412
8413 NotifyMotionArgs args;
8414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8415 ASSERT_EQ(0, args.pointerProperties[0].id);
8416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8417 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8418 orientation, distance));
8419}
8420
8421TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008422 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008423 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008424 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8425 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008426 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008427
8428 // These calculations are based on the input device calibration documentation.
8429 int32_t rawX = 100;
8430 int32_t rawY = 200;
8431 int32_t rawTouchMajor = 140;
8432 int32_t rawTouchMinor = 120;
8433 int32_t rawToolMajor = 180;
8434 int32_t rawToolMinor = 160;
8435
8436 float x = toDisplayX(rawX);
8437 float y = toDisplayY(rawY);
8438 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8439 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8440 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8441 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8442 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8443
8444 processPosition(mapper, rawX, rawY);
8445 processTouchMajor(mapper, rawTouchMajor);
8446 processTouchMinor(mapper, rawTouchMinor);
8447 processToolMajor(mapper, rawToolMajor);
8448 processToolMinor(mapper, rawToolMinor);
8449 processMTSync(mapper);
8450 processSync(mapper);
8451
8452 NotifyMotionArgs args;
8453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8455 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8456}
8457
8458TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008459 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008460 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008461 prepareAxes(POSITION | TOUCH | TOOL);
8462 addConfigurationProperty("touch.size.calibration", "diameter");
8463 addConfigurationProperty("touch.size.scale", "10");
8464 addConfigurationProperty("touch.size.bias", "160");
8465 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008466 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008467
8468 // These calculations are based on the input device calibration documentation.
8469 // Note: We only provide a single common touch/tool value because the device is assumed
8470 // not to emit separate values for each pointer (isSummed = 1).
8471 int32_t rawX = 100;
8472 int32_t rawY = 200;
8473 int32_t rawX2 = 150;
8474 int32_t rawY2 = 250;
8475 int32_t rawTouchMajor = 5;
8476 int32_t rawToolMajor = 8;
8477
8478 float x = toDisplayX(rawX);
8479 float y = toDisplayY(rawY);
8480 float x2 = toDisplayX(rawX2);
8481 float y2 = toDisplayY(rawY2);
8482 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8483 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8484 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8485
8486 processPosition(mapper, rawX, rawY);
8487 processTouchMajor(mapper, rawTouchMajor);
8488 processToolMajor(mapper, rawToolMajor);
8489 processMTSync(mapper);
8490 processPosition(mapper, rawX2, rawY2);
8491 processTouchMajor(mapper, rawTouchMajor);
8492 processToolMajor(mapper, rawToolMajor);
8493 processMTSync(mapper);
8494 processSync(mapper);
8495
8496 NotifyMotionArgs args;
8497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8498 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8499
8500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008501 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008502 ASSERT_EQ(size_t(2), args.pointerCount);
8503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8504 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8506 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8507}
8508
8509TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008510 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008511 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008512 prepareAxes(POSITION | TOUCH | TOOL);
8513 addConfigurationProperty("touch.size.calibration", "area");
8514 addConfigurationProperty("touch.size.scale", "43");
8515 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008516 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008517
8518 // These calculations are based on the input device calibration documentation.
8519 int32_t rawX = 100;
8520 int32_t rawY = 200;
8521 int32_t rawTouchMajor = 5;
8522 int32_t rawToolMajor = 8;
8523
8524 float x = toDisplayX(rawX);
8525 float y = toDisplayY(rawY);
8526 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8527 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8528 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8529
8530 processPosition(mapper, rawX, rawY);
8531 processTouchMajor(mapper, rawTouchMajor);
8532 processToolMajor(mapper, rawToolMajor);
8533 processMTSync(mapper);
8534 processSync(mapper);
8535
8536 NotifyMotionArgs args;
8537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8539 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8540}
8541
8542TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008543 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008544 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008545 prepareAxes(POSITION | PRESSURE);
8546 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8547 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008548 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008549
Michael Wrightaa449c92017-12-13 21:21:43 +00008550 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008551 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008552 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8553 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8554 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8555
Michael Wrightd02c5b62014-02-10 15:10:22 -08008556 // These calculations are based on the input device calibration documentation.
8557 int32_t rawX = 100;
8558 int32_t rawY = 200;
8559 int32_t rawPressure = 60;
8560
8561 float x = toDisplayX(rawX);
8562 float y = toDisplayY(rawY);
8563 float pressure = float(rawPressure) * 0.01f;
8564
8565 processPosition(mapper, rawX, rawY);
8566 processPressure(mapper, rawPressure);
8567 processMTSync(mapper);
8568 processSync(mapper);
8569
8570 NotifyMotionArgs args;
8571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8573 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8574}
8575
8576TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008577 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008578 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008579 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008580 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008581
8582 NotifyMotionArgs motionArgs;
8583 NotifyKeyArgs keyArgs;
8584
8585 processId(mapper, 1);
8586 processPosition(mapper, 100, 200);
8587 processSync(mapper);
8588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8589 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8590 ASSERT_EQ(0, motionArgs.buttonState);
8591
8592 // press BTN_LEFT, release BTN_LEFT
8593 processKey(mapper, BTN_LEFT, 1);
8594 processSync(mapper);
8595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8596 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8597 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8598
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8600 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8601 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8602
Michael Wrightd02c5b62014-02-10 15:10:22 -08008603 processKey(mapper, BTN_LEFT, 0);
8604 processSync(mapper);
8605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008606 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008607 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008608
8609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008611 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008612
8613 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8614 processKey(mapper, BTN_RIGHT, 1);
8615 processKey(mapper, BTN_MIDDLE, 1);
8616 processSync(mapper);
8617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8619 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8620 motionArgs.buttonState);
8621
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8623 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8624 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8625
8626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8627 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8628 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8629 motionArgs.buttonState);
8630
Michael Wrightd02c5b62014-02-10 15:10:22 -08008631 processKey(mapper, BTN_RIGHT, 0);
8632 processSync(mapper);
8633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008634 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008635 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008636
8637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008638 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008639 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008640
8641 processKey(mapper, BTN_MIDDLE, 0);
8642 processSync(mapper);
8643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008644 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008645 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008646
8647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008648 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008649 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008650
8651 // press BTN_BACK, release BTN_BACK
8652 processKey(mapper, BTN_BACK, 1);
8653 processSync(mapper);
8654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8655 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8656 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008657
Michael Wrightd02c5b62014-02-10 15:10:22 -08008658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008660 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8661
8662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8663 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8664 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008665
8666 processKey(mapper, BTN_BACK, 0);
8667 processSync(mapper);
8668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008669 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008670 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008671
8672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008673 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008674 ASSERT_EQ(0, motionArgs.buttonState);
8675
Michael Wrightd02c5b62014-02-10 15:10:22 -08008676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8677 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8678 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8679
8680 // press BTN_SIDE, release BTN_SIDE
8681 processKey(mapper, BTN_SIDE, 1);
8682 processSync(mapper);
8683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8684 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8685 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008686
Michael Wrightd02c5b62014-02-10 15:10:22 -08008687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008688 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008689 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8690
8691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8692 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8693 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008694
8695 processKey(mapper, BTN_SIDE, 0);
8696 processSync(mapper);
8697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008698 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008699 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008700
8701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008702 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008703 ASSERT_EQ(0, motionArgs.buttonState);
8704
Michael Wrightd02c5b62014-02-10 15:10:22 -08008705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8706 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8707 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8708
8709 // press BTN_FORWARD, release BTN_FORWARD
8710 processKey(mapper, BTN_FORWARD, 1);
8711 processSync(mapper);
8712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8713 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8714 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008715
Michael Wrightd02c5b62014-02-10 15:10:22 -08008716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008717 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008718 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8719
8720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8721 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8722 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008723
8724 processKey(mapper, BTN_FORWARD, 0);
8725 processSync(mapper);
8726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008727 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008728 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008729
8730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008731 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008732 ASSERT_EQ(0, motionArgs.buttonState);
8733
Michael Wrightd02c5b62014-02-10 15:10:22 -08008734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8735 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8736 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8737
8738 // press BTN_EXTRA, release BTN_EXTRA
8739 processKey(mapper, BTN_EXTRA, 1);
8740 processSync(mapper);
8741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8742 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8743 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008744
Michael Wrightd02c5b62014-02-10 15:10:22 -08008745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008747 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8748
8749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8750 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8751 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008752
8753 processKey(mapper, BTN_EXTRA, 0);
8754 processSync(mapper);
8755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008756 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008757 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008758
8759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008761 ASSERT_EQ(0, motionArgs.buttonState);
8762
Michael Wrightd02c5b62014-02-10 15:10:22 -08008763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8764 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8765 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8766
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8768
Michael Wrightd02c5b62014-02-10 15:10:22 -08008769 // press BTN_STYLUS, release BTN_STYLUS
8770 processKey(mapper, BTN_STYLUS, 1);
8771 processSync(mapper);
8772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8773 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008774 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8775
8776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8777 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8778 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008779
8780 processKey(mapper, BTN_STYLUS, 0);
8781 processSync(mapper);
8782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008783 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008784 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008785
8786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008787 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008788 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008789
8790 // press BTN_STYLUS2, release BTN_STYLUS2
8791 processKey(mapper, BTN_STYLUS2, 1);
8792 processSync(mapper);
8793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8794 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008795 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8796
8797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8798 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8799 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008800
8801 processKey(mapper, BTN_STYLUS2, 0);
8802 processSync(mapper);
8803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008804 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008805 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008806
8807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008808 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008809 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008810
8811 // release touch
8812 processId(mapper, -1);
8813 processSync(mapper);
8814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8815 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8816 ASSERT_EQ(0, motionArgs.buttonState);
8817}
8818
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008819TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8820 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008821 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008822 prepareAxes(POSITION | ID | SLOT);
8823 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8824
8825 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8826 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8827
8828 // Touch down.
8829 processId(mapper, 1);
8830 processPosition(mapper, 100, 200);
8831 processSync(mapper);
8832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8833 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8834
8835 // Press and release button mapped to the primary stylus button.
8836 processKey(mapper, BTN_A, 1);
8837 processSync(mapper);
8838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8839 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8840 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8842 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8843 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8844
8845 processKey(mapper, BTN_A, 0);
8846 processSync(mapper);
8847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8848 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8850 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8851
8852 // Press and release the HID usage mapped to the secondary stylus button.
8853 processHidUsage(mapper, 0xabcd, 1);
8854 processSync(mapper);
8855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8856 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8857 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8859 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8860 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8861
8862 processHidUsage(mapper, 0xabcd, 0);
8863 processSync(mapper);
8864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8865 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8867 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8868
8869 // Release touch.
8870 processId(mapper, -1);
8871 processSync(mapper);
8872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8873 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8874}
8875
Michael Wrightd02c5b62014-02-10 15:10:22 -08008876TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008877 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008878 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008879 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008880 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008881
8882 NotifyMotionArgs motionArgs;
8883
8884 // default tool type is finger
8885 processId(mapper, 1);
8886 processPosition(mapper, 100, 200);
8887 processSync(mapper);
8888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8889 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008890 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008891
8892 // eraser
8893 processKey(mapper, BTN_TOOL_RUBBER, 1);
8894 processSync(mapper);
8895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008897 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008898
8899 // stylus
8900 processKey(mapper, BTN_TOOL_RUBBER, 0);
8901 processKey(mapper, BTN_TOOL_PEN, 1);
8902 processSync(mapper);
8903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8904 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008905 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008906
8907 // brush
8908 processKey(mapper, BTN_TOOL_PEN, 0);
8909 processKey(mapper, BTN_TOOL_BRUSH, 1);
8910 processSync(mapper);
8911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8912 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008913 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008914
8915 // pencil
8916 processKey(mapper, BTN_TOOL_BRUSH, 0);
8917 processKey(mapper, BTN_TOOL_PENCIL, 1);
8918 processSync(mapper);
8919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8920 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008921 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008922
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008923 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008924 processKey(mapper, BTN_TOOL_PENCIL, 0);
8925 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8926 processSync(mapper);
8927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8928 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008929 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008930
8931 // mouse
8932 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8933 processKey(mapper, BTN_TOOL_MOUSE, 1);
8934 processSync(mapper);
8935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8936 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008937 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008938
8939 // lens
8940 processKey(mapper, BTN_TOOL_MOUSE, 0);
8941 processKey(mapper, BTN_TOOL_LENS, 1);
8942 processSync(mapper);
8943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8944 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008945 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008946
8947 // double-tap
8948 processKey(mapper, BTN_TOOL_LENS, 0);
8949 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8950 processSync(mapper);
8951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8952 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008953 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008954
8955 // triple-tap
8956 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8957 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8958 processSync(mapper);
8959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8960 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008961 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008962
8963 // quad-tap
8964 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8965 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8966 processSync(mapper);
8967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8968 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008969 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008970
8971 // finger
8972 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8973 processKey(mapper, BTN_TOOL_FINGER, 1);
8974 processSync(mapper);
8975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8976 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008977 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008978
8979 // stylus trumps finger
8980 processKey(mapper, BTN_TOOL_PEN, 1);
8981 processSync(mapper);
8982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8983 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008984 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008985
8986 // eraser trumps stylus
8987 processKey(mapper, BTN_TOOL_RUBBER, 1);
8988 processSync(mapper);
8989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8990 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008991 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008992
8993 // mouse trumps eraser
8994 processKey(mapper, BTN_TOOL_MOUSE, 1);
8995 processSync(mapper);
8996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8997 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008998 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008999
9000 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9001 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9002 processSync(mapper);
9003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9004 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009005 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009006
9007 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9008 processToolType(mapper, MT_TOOL_PEN);
9009 processSync(mapper);
9010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9011 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009012 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009013
9014 // back to default tool type
9015 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9016 processKey(mapper, BTN_TOOL_MOUSE, 0);
9017 processKey(mapper, BTN_TOOL_RUBBER, 0);
9018 processKey(mapper, BTN_TOOL_PEN, 0);
9019 processKey(mapper, BTN_TOOL_FINGER, 0);
9020 processSync(mapper);
9021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9022 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009023 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009024}
9025
9026TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009027 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009028 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009029 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009030 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009031 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009032
9033 NotifyMotionArgs motionArgs;
9034
9035 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9036 processId(mapper, 1);
9037 processPosition(mapper, 100, 200);
9038 processSync(mapper);
9039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9040 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9042 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9043
9044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9045 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9046 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9047 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9048
9049 // move a little
9050 processPosition(mapper, 150, 250);
9051 processSync(mapper);
9052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9053 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9054 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9055 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9056
9057 // down when BTN_TOUCH is pressed, pressure defaults to 1
9058 processKey(mapper, BTN_TOUCH, 1);
9059 processSync(mapper);
9060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9061 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9063 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9064
9065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9066 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9067 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9068 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9069
9070 // up when BTN_TOUCH is released, hover restored
9071 processKey(mapper, BTN_TOUCH, 0);
9072 processSync(mapper);
9073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9074 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9076 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9077
9078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9079 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9080 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9081 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9082
9083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9084 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9085 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9086 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9087
9088 // exit hover when pointer goes away
9089 processId(mapper, -1);
9090 processSync(mapper);
9091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9092 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9094 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9095}
9096
9097TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009098 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009099 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009100 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009101 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009102
9103 NotifyMotionArgs motionArgs;
9104
9105 // initially hovering because pressure is 0
9106 processId(mapper, 1);
9107 processPosition(mapper, 100, 200);
9108 processPressure(mapper, 0);
9109 processSync(mapper);
9110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9111 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9112 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9113 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9114
9115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9116 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9117 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9118 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9119
9120 // move a little
9121 processPosition(mapper, 150, 250);
9122 processSync(mapper);
9123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9124 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9126 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9127
9128 // down when pressure becomes non-zero
9129 processPressure(mapper, RAW_PRESSURE_MAX);
9130 processSync(mapper);
9131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9132 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9134 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9135
9136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9137 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9138 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9139 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9140
9141 // up when pressure becomes 0, hover restored
9142 processPressure(mapper, 0);
9143 processSync(mapper);
9144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9145 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9147 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9148
9149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9150 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9151 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9152 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9153
9154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9155 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9156 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9157 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9158
9159 // exit hover when pointer goes away
9160 processId(mapper, -1);
9161 processSync(mapper);
9162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9163 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9164 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9165 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9166}
9167
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009168/**
9169 * Set the input device port <--> display port associations, and check that the
9170 * events are routed to the display that matches the display port.
9171 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9172 */
9173TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009174 const std::string usb2 = "USB2";
9175 const uint8_t hdmi1 = 0;
9176 const uint8_t hdmi2 = 1;
9177 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009178 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009179
9180 addConfigurationProperty("touch.deviceType", "touchScreen");
9181 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009182 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009183
9184 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9185 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9186
9187 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9188 // for this input device is specified, and the matching viewport is not present,
9189 // the input device should be disabled (at the mapper level).
9190
9191 // Add viewport for display 2 on hdmi2
9192 prepareSecondaryDisplay(type, hdmi2);
9193 // Send a touch event
9194 processPosition(mapper, 100, 100);
9195 processSync(mapper);
9196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9197
9198 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009199 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009200 // Send a touch event again
9201 processPosition(mapper, 100, 100);
9202 processSync(mapper);
9203
9204 NotifyMotionArgs args;
9205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9206 ASSERT_EQ(DISPLAY_ID, args.displayId);
9207}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009208
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009209TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9210 addConfigurationProperty("touch.deviceType", "touchScreen");
9211 prepareAxes(POSITION);
9212 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9213
9214 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9215
Michael Wrighta9cf4192022-12-01 23:46:39 +00009216 prepareDisplay(ui::ROTATION_0);
9217 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009218
9219 // Send a touch event
9220 processPosition(mapper, 100, 100);
9221 processSync(mapper);
9222
9223 NotifyMotionArgs args;
9224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9225 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9226}
9227
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009228TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009229 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009230 std::shared_ptr<FakePointerController> fakePointerController =
9231 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009232 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009233 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009234 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009235
Garfield Tan888a6a42020-01-09 11:39:16 -08009236 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009237 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009238
Michael Wrighta9cf4192022-12-01 23:46:39 +00009239 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009240 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009241 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009242
Josep del Río2d8c79a2023-01-23 19:33:50 +00009243 // Check source is mouse that would obtain the PointerController.
9244 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009245
9246 NotifyMotionArgs motionArgs;
9247 processPosition(mapper, 100, 100);
9248 processSync(mapper);
9249
9250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9251 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9252 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9253}
9254
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009255/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009256 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9257 */
9258TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9259 addConfigurationProperty("touch.deviceType", "touchScreen");
9260 prepareAxes(POSITION);
9261 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9262
Michael Wrighta9cf4192022-12-01 23:46:39 +00009263 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009264 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9265 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9266 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9267 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009268
9269 NotifyMotionArgs args;
9270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9271 ASSERT_EQ(26, args.readTime);
9272
Harry Cutts33476232023-01-30 19:57:29 +00009273 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9274 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9275 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009276
9277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9278 ASSERT_EQ(33, args.readTime);
9279}
9280
9281/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009282 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9283 * events should not be delivered to the listener.
9284 */
9285TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9286 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009287 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009288 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009289 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009290 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9291 prepareAxes(POSITION);
9292 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9293
9294 NotifyMotionArgs motionArgs;
9295 processPosition(mapper, 100, 100);
9296 processSync(mapper);
9297
9298 mFakeListener->assertNotifyMotionWasNotCalled();
9299}
9300
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009301/**
9302 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9303 * the touch mapper can process the events and the events can be delivered to the listener.
9304 */
9305TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9306 addConfigurationProperty("touch.deviceType", "touchScreen");
9307 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009308 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009309 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009310 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9311 prepareAxes(POSITION);
9312 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9313
9314 NotifyMotionArgs motionArgs;
9315 processPosition(mapper, 100, 100);
9316 processSync(mapper);
9317
9318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9319 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9320}
9321
Garfield Tanc734e4f2021-01-15 20:01:39 -08009322TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9323 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009324 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009325 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009326 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009327 std::optional<DisplayViewport> optionalDisplayViewport =
9328 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9329 ASSERT_TRUE(optionalDisplayViewport.has_value());
9330 DisplayViewport displayViewport = *optionalDisplayViewport;
9331
9332 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9333 prepareAxes(POSITION);
9334 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9335
9336 // Finger down
9337 int32_t x = 100, y = 100;
9338 processPosition(mapper, x, y);
9339 processSync(mapper);
9340
9341 NotifyMotionArgs motionArgs;
9342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9343 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9344
9345 // Deactivate display viewport
9346 displayViewport.isActive = false;
9347 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9348 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9349
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009350 // The ongoing touch should be canceled immediately
9351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9352 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9353
9354 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009355 x += 10, y += 10;
9356 processPosition(mapper, x, y);
9357 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009359
9360 // Reactivate display viewport
9361 displayViewport.isActive = true;
9362 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9363 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9364
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009365 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009366 x += 10, y += 10;
9367 processPosition(mapper, x, y);
9368 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9370 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009371}
9372
Arthur Hung7c645402019-01-25 17:45:42 +08009373TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9374 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009375 prepareAxes(POSITION | ID | SLOT);
9376 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009377 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009378
9379 // Create the second touch screen device, and enable multi fingers.
9380 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009381 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009382 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009383 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009384 std::shared_ptr<InputDevice> device2 =
9385 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009386 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009387
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009388 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009389 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009390 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009391 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009392 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009393 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009394 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009395 /*flat=*/0, /*fuzz=*/0);
9396 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009397 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9398 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009399
9400 // Setup the second touch screen device.
Arpit Singh8e6fb252023-04-06 11:49:17 +00009401 MultiTouchInputMapper& mapper2 =
9402 device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID,
9403 mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009404 std::list<NotifyArgs> unused =
9405 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00009406 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009407 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009408
9409 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009410 std::shared_ptr<FakePointerController> fakePointerController =
9411 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009412 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009413
9414 // Setup policy for associated displays and show touches.
9415 const uint8_t hdmi1 = 0;
9416 const uint8_t hdmi2 = 1;
9417 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9418 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9419 mFakePolicy->setShowTouches(true);
9420
9421 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009422 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009423 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009424
9425 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009426 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9427 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9428 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009429
9430 // Two fingers down at default display.
9431 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9432 processPosition(mapper, x1, y1);
9433 processId(mapper, 1);
9434 processSlot(mapper, 1);
9435 processPosition(mapper, x2, y2);
9436 processId(mapper, 2);
9437 processSync(mapper);
9438
9439 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9440 fakePointerController->getSpots().find(DISPLAY_ID);
9441 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9442 ASSERT_EQ(size_t(2), iter->second.size());
9443
9444 // Two fingers down at second display.
9445 processPosition(mapper2, x1, y1);
9446 processId(mapper2, 1);
9447 processSlot(mapper2, 1);
9448 processPosition(mapper2, x2, y2);
9449 processId(mapper2, 2);
9450 processSync(mapper2);
9451
9452 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9453 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9454 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009455
9456 // Disable the show touches configuration and ensure the spots are cleared.
9457 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009458 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9459 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009460
9461 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009462}
9463
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009464TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009465 prepareAxes(POSITION);
9466 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009467 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009468 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009469
9470 NotifyMotionArgs motionArgs;
9471 // Unrotated video frame
9472 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9473 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009474 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009475 processPosition(mapper, 100, 200);
9476 processSync(mapper);
9477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9478 ASSERT_EQ(frames, motionArgs.videoFrames);
9479
9480 // Subsequent touch events should not have any videoframes
9481 // This is implemented separately in FakeEventHub,
9482 // but that should match the behaviour of TouchVideoDevice.
9483 processPosition(mapper, 200, 200);
9484 processSync(mapper);
9485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9486 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9487}
9488
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009489TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009490 prepareAxes(POSITION);
9491 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009492 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009493 // Unrotated video frame
9494 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9495 NotifyMotionArgs motionArgs;
9496
9497 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009498 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009499 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9500 clearViewports();
9501 prepareDisplay(orientation);
9502 std::vector<TouchVideoFrame> frames{frame};
9503 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9504 processPosition(mapper, 100, 200);
9505 processSync(mapper);
9506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9507 ASSERT_EQ(frames, motionArgs.videoFrames);
9508 }
9509}
9510
9511TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9512 prepareAxes(POSITION);
9513 addConfigurationProperty("touch.deviceType", "touchScreen");
9514 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9515 // orientation-aware are affected by display rotation.
9516 addConfigurationProperty("touch.orientationAware", "0");
9517 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9518 // Unrotated video frame
9519 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9520 NotifyMotionArgs motionArgs;
9521
9522 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009523 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009524 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9525 clearViewports();
9526 prepareDisplay(orientation);
9527 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009528 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009529 processPosition(mapper, 100, 200);
9530 processSync(mapper);
9531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009532 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9533 // compared to the display. This is so that when the window transform (which contains the
9534 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9535 // window's coordinate space.
9536 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009537 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009538
9539 // Release finger.
9540 processSync(mapper);
9541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009542 }
9543}
9544
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009545TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009546 prepareAxes(POSITION);
9547 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009548 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009549 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9550 // so mix these.
9551 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9552 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9553 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9554 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9555 NotifyMotionArgs motionArgs;
9556
Michael Wrighta9cf4192022-12-01 23:46:39 +00009557 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009558 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009559 processPosition(mapper, 100, 200);
9560 processSync(mapper);
9561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009562 ASSERT_EQ(frames, motionArgs.videoFrames);
9563}
9564
9565TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9566 prepareAxes(POSITION);
9567 addConfigurationProperty("touch.deviceType", "touchScreen");
9568 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9569 // orientation-aware are affected by display rotation.
9570 addConfigurationProperty("touch.orientationAware", "0");
9571 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9572 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9573 // so mix these.
9574 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9575 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9576 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9577 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9578 NotifyMotionArgs motionArgs;
9579
Michael Wrighta9cf4192022-12-01 23:46:39 +00009580 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009581 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9582 processPosition(mapper, 100, 200);
9583 processSync(mapper);
9584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9585 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9586 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9587 // compared to the display. This is so that when the window transform (which contains the
9588 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9589 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009590 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009591 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009592 ASSERT_EQ(frames, motionArgs.videoFrames);
9593}
9594
Arthur Hung9da14732019-09-02 16:16:58 +08009595/**
9596 * If we had defined port associations, but the viewport is not ready, the touch device would be
9597 * expected to be disabled, and it should be enabled after the viewport has found.
9598 */
9599TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009600 constexpr uint8_t hdmi2 = 1;
9601 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009602 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009603
9604 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9605
9606 addConfigurationProperty("touch.deviceType", "touchScreen");
9607 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009608 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009609
9610 ASSERT_EQ(mDevice->isEnabled(), false);
9611
9612 // Add display on hdmi2, the device should be enabled and can receive touch event.
9613 prepareSecondaryDisplay(type, hdmi2);
9614 ASSERT_EQ(mDevice->isEnabled(), true);
9615
9616 // Send a touch event.
9617 processPosition(mapper, 100, 100);
9618 processSync(mapper);
9619
9620 NotifyMotionArgs args;
9621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9622 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9623}
9624
Arthur Hung421eb1c2020-01-16 00:09:42 +08009625TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009626 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009627 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009628 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009629 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009630
9631 NotifyMotionArgs motionArgs;
9632
9633 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9634 // finger down
9635 processId(mapper, 1);
9636 processPosition(mapper, x1, y1);
9637 processSync(mapper);
9638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9639 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009640 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009641
9642 // finger move
9643 processId(mapper, 1);
9644 processPosition(mapper, x2, y2);
9645 processSync(mapper);
9646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9647 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009648 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009649
9650 // finger up.
9651 processId(mapper, -1);
9652 processSync(mapper);
9653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9654 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009655 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009656
9657 // new finger down
9658 processId(mapper, 1);
9659 processPosition(mapper, x3, y3);
9660 processSync(mapper);
9661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9662 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009663 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009664}
9665
9666/**
arthurhungcc7f9802020-04-30 17:55:40 +08009667 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9668 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009669 */
arthurhungcc7f9802020-04-30 17:55:40 +08009670TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009671 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009672 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009673 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009674 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009675
9676 NotifyMotionArgs motionArgs;
9677
9678 // default tool type is finger
9679 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009680 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009681 processPosition(mapper, x1, y1);
9682 processSync(mapper);
9683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9684 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009685 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009686
9687 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9688 processToolType(mapper, MT_TOOL_PALM);
9689 processSync(mapper);
9690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9691 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9692
9693 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009694 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009695 processPosition(mapper, x2, y2);
9696 processSync(mapper);
9697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9698
9699 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009700 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009701 processSync(mapper);
9702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9703
9704 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009705 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009706 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009707 processPosition(mapper, x3, y3);
9708 processSync(mapper);
9709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9710 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009711 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009712}
9713
arthurhungbf89a482020-04-17 17:37:55 +08009714/**
arthurhungcc7f9802020-04-30 17:55:40 +08009715 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9716 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009717 */
arthurhungcc7f9802020-04-30 17:55:40 +08009718TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009719 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009720 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009721 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9722 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9723
9724 NotifyMotionArgs motionArgs;
9725
9726 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009727 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9728 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009729 processPosition(mapper, x1, y1);
9730 processSync(mapper);
9731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9732 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009733 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009734
9735 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009736 processSlot(mapper, SECOND_SLOT);
9737 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009738 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009739 processSync(mapper);
9740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009741 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009742 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009743
9744 // If the tool type of the first finger changes to MT_TOOL_PALM,
9745 // we expect to receive ACTION_POINTER_UP with cancel flag.
9746 processSlot(mapper, FIRST_SLOT);
9747 processId(mapper, FIRST_TRACKING_ID);
9748 processToolType(mapper, MT_TOOL_PALM);
9749 processSync(mapper);
9750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009751 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009752 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9753
9754 // The following MOVE events of second finger should be processed.
9755 processSlot(mapper, SECOND_SLOT);
9756 processId(mapper, SECOND_TRACKING_ID);
9757 processPosition(mapper, x2 + 1, y2 + 1);
9758 processSync(mapper);
9759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9761 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9762
9763 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9764 // it. Second finger receive move.
9765 processSlot(mapper, FIRST_SLOT);
9766 processId(mapper, INVALID_TRACKING_ID);
9767 processSync(mapper);
9768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9769 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9770 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9771
9772 // Second finger keeps moving.
9773 processSlot(mapper, SECOND_SLOT);
9774 processId(mapper, SECOND_TRACKING_ID);
9775 processPosition(mapper, x2 + 2, y2 + 2);
9776 processSync(mapper);
9777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9778 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9779 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9780
9781 // Second finger up.
9782 processId(mapper, INVALID_TRACKING_ID);
9783 processSync(mapper);
9784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9785 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9786 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9787}
9788
9789/**
9790 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9791 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9792 */
9793TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9794 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009795 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009796 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9797 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9798
9799 NotifyMotionArgs motionArgs;
9800
9801 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9802 // First finger down.
9803 processId(mapper, FIRST_TRACKING_ID);
9804 processPosition(mapper, x1, y1);
9805 processSync(mapper);
9806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9807 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009808 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009809
9810 // Second finger down.
9811 processSlot(mapper, SECOND_SLOT);
9812 processId(mapper, SECOND_TRACKING_ID);
9813 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009814 processSync(mapper);
9815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009816 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009817 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009818
arthurhungcc7f9802020-04-30 17:55:40 +08009819 // If the tool type of the first finger changes to MT_TOOL_PALM,
9820 // we expect to receive ACTION_POINTER_UP with cancel flag.
9821 processSlot(mapper, FIRST_SLOT);
9822 processId(mapper, FIRST_TRACKING_ID);
9823 processToolType(mapper, MT_TOOL_PALM);
9824 processSync(mapper);
9825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009826 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009827 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9828
9829 // Second finger keeps moving.
9830 processSlot(mapper, SECOND_SLOT);
9831 processId(mapper, SECOND_TRACKING_ID);
9832 processPosition(mapper, x2 + 1, y2 + 1);
9833 processSync(mapper);
9834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9835 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9836
9837 // second finger becomes palm, receive cancel due to only 1 finger is active.
9838 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009839 processToolType(mapper, MT_TOOL_PALM);
9840 processSync(mapper);
9841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9842 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9843
arthurhungcc7f9802020-04-30 17:55:40 +08009844 // third finger down.
9845 processSlot(mapper, THIRD_SLOT);
9846 processId(mapper, THIRD_TRACKING_ID);
9847 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009848 processPosition(mapper, x3, y3);
9849 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9851 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009852 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009853 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9854
9855 // third finger move
9856 processId(mapper, THIRD_TRACKING_ID);
9857 processPosition(mapper, x3 + 1, y3 + 1);
9858 processSync(mapper);
9859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9860 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9861
9862 // first finger up, third finger receive move.
9863 processSlot(mapper, FIRST_SLOT);
9864 processId(mapper, INVALID_TRACKING_ID);
9865 processSync(mapper);
9866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9867 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9868 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9869
9870 // second finger up, third finger receive move.
9871 processSlot(mapper, SECOND_SLOT);
9872 processId(mapper, INVALID_TRACKING_ID);
9873 processSync(mapper);
9874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9875 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9876 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9877
9878 // third finger up.
9879 processSlot(mapper, THIRD_SLOT);
9880 processId(mapper, INVALID_TRACKING_ID);
9881 processSync(mapper);
9882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9883 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9884 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9885}
9886
9887/**
9888 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9889 * and the active finger could still be allowed to receive the events
9890 */
9891TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9892 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009893 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009894 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9895 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9896
9897 NotifyMotionArgs motionArgs;
9898
9899 // default tool type is finger
9900 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9901 processId(mapper, FIRST_TRACKING_ID);
9902 processPosition(mapper, x1, y1);
9903 processSync(mapper);
9904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9905 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009906 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009907
9908 // Second finger down.
9909 processSlot(mapper, SECOND_SLOT);
9910 processId(mapper, SECOND_TRACKING_ID);
9911 processPosition(mapper, x2, y2);
9912 processSync(mapper);
9913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009914 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009915 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009916
9917 // If the tool type of the second finger changes to MT_TOOL_PALM,
9918 // we expect to receive ACTION_POINTER_UP with cancel flag.
9919 processId(mapper, SECOND_TRACKING_ID);
9920 processToolType(mapper, MT_TOOL_PALM);
9921 processSync(mapper);
9922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009923 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009924 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9925
9926 // The following MOVE event should be processed.
9927 processSlot(mapper, FIRST_SLOT);
9928 processId(mapper, FIRST_TRACKING_ID);
9929 processPosition(mapper, x1 + 1, y1 + 1);
9930 processSync(mapper);
9931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9932 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9933 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9934
9935 // second finger up.
9936 processSlot(mapper, SECOND_SLOT);
9937 processId(mapper, INVALID_TRACKING_ID);
9938 processSync(mapper);
9939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9940 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9941
9942 // first finger keep moving
9943 processSlot(mapper, FIRST_SLOT);
9944 processId(mapper, FIRST_TRACKING_ID);
9945 processPosition(mapper, x1 + 2, y1 + 2);
9946 processSync(mapper);
9947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9948 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9949
9950 // first finger up.
9951 processId(mapper, INVALID_TRACKING_ID);
9952 processSync(mapper);
9953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9954 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9955 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009956}
9957
Arthur Hung9ad18942021-06-19 02:04:46 +00009958/**
9959 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9960 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9961 * cause slot be valid again.
9962 */
9963TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9964 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009965 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009966 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9967 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9968
9969 NotifyMotionArgs motionArgs;
9970
9971 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9972 // First finger down.
9973 processId(mapper, FIRST_TRACKING_ID);
9974 processPosition(mapper, x1, y1);
9975 processPressure(mapper, RAW_PRESSURE_MAX);
9976 processSync(mapper);
9977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9978 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9979 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9980
9981 // First finger move.
9982 processId(mapper, FIRST_TRACKING_ID);
9983 processPosition(mapper, x1 + 1, y1 + 1);
9984 processPressure(mapper, RAW_PRESSURE_MAX);
9985 processSync(mapper);
9986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9987 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9988 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9989
9990 // Second finger down.
9991 processSlot(mapper, SECOND_SLOT);
9992 processId(mapper, SECOND_TRACKING_ID);
9993 processPosition(mapper, x2, y2);
9994 processPressure(mapper, RAW_PRESSURE_MAX);
9995 processSync(mapper);
9996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009997 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009998 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9999
10000 // second finger up with some unexpected data.
10001 processSlot(mapper, SECOND_SLOT);
10002 processId(mapper, INVALID_TRACKING_ID);
10003 processPosition(mapper, x2, y2);
10004 processSync(mapper);
10005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010006 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010007 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10008
10009 // first finger up with some unexpected data.
10010 processSlot(mapper, FIRST_SLOT);
10011 processId(mapper, INVALID_TRACKING_ID);
10012 processPosition(mapper, x2, y2);
10013 processPressure(mapper, RAW_PRESSURE_MAX);
10014 processSync(mapper);
10015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10016 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10017 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10018}
10019
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010020TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10021 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010022 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010023 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10024 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10025
10026 // First finger down.
10027 processId(mapper, FIRST_TRACKING_ID);
10028 processPosition(mapper, 100, 200);
10029 processPressure(mapper, RAW_PRESSURE_MAX);
10030 processSync(mapper);
10031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10032 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10033
10034 // Second finger down.
10035 processSlot(mapper, SECOND_SLOT);
10036 processId(mapper, SECOND_TRACKING_ID);
10037 processPosition(mapper, 300, 400);
10038 processPressure(mapper, RAW_PRESSURE_MAX);
10039 processSync(mapper);
10040 ASSERT_NO_FATAL_FAILURE(
10041 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10042
10043 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010044 // preserved. Resetting should cancel the ongoing gesture.
10045 resetMapper(mapper, ARBITRARY_TIME);
10046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10047 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010048
10049 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10050 // the existing touch state to generate a down event.
10051 processPosition(mapper, 301, 302);
10052 processSync(mapper);
10053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10054 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10056 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10057
10058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10059}
10060
10061TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10062 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010063 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010064 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10065 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10066
10067 // First finger touches down and releases.
10068 processId(mapper, FIRST_TRACKING_ID);
10069 processPosition(mapper, 100, 200);
10070 processPressure(mapper, RAW_PRESSURE_MAX);
10071 processSync(mapper);
10072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10073 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10074 processId(mapper, INVALID_TRACKING_ID);
10075 processSync(mapper);
10076 ASSERT_NO_FATAL_FAILURE(
10077 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10078
10079 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10080 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010081 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10083
10084 // Send an empty sync frame. Since there are no pointers, no events are generated.
10085 processSync(mapper);
10086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10087}
10088
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010089TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010090 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010091 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010092 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
10093 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010095
10096 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10097 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10098 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10099 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10100 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10101
10102 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010103 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010104 processId(mapper, FIRST_TRACKING_ID);
10105 processToolType(mapper, MT_TOOL_PEN);
10106 processPosition(mapper, 100, 200);
10107 processPressure(mapper, RAW_PRESSURE_MAX);
10108 processSync(mapper);
10109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10110 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10111 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010112 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010113
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010114 // Now that we know the device supports styluses, ensure that the device is re-configured with
10115 // the stylus source.
10116 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10117 {
10118 const auto& devices = mReader->getInputDevices();
10119 auto deviceInfo =
10120 std::find_if(devices.begin(), devices.end(),
10121 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10122 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10123 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10124 }
10125
10126 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10128
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010129 processId(mapper, INVALID_TRACKING_ID);
10130 processSync(mapper);
10131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10132 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10133 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010134 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010135}
10136
Seunghwan Choi356026c2023-02-01 14:37:25 +090010137TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10138 addConfigurationProperty("touch.deviceType", "touchScreen");
10139 prepareDisplay(ui::ROTATION_0);
10140 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10141 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10142 // indicate stylus presence dynamically.
10143 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10144 std::shared_ptr<FakePointerController> fakePointerController =
10145 std::make_shared<FakePointerController>();
10146 mFakePolicy->setPointerController(fakePointerController);
10147 mFakePolicy->setStylusPointerIconEnabled(true);
10148 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10149
10150 processId(mapper, FIRST_TRACKING_ID);
10151 processPressure(mapper, RAW_PRESSURE_MIN);
10152 processPosition(mapper, 100, 200);
10153 processToolType(mapper, MT_TOOL_PEN);
10154 processSync(mapper);
10155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10156 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010157 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010158 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10159 ASSERT_TRUE(fakePointerController->isPointerShown());
10160 ASSERT_NO_FATAL_FAILURE(
10161 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10162}
10163
10164TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10165 addConfigurationProperty("touch.deviceType", "touchScreen");
10166 prepareDisplay(ui::ROTATION_0);
10167 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10168 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10169 // indicate stylus presence dynamically.
10170 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10171 std::shared_ptr<FakePointerController> fakePointerController =
10172 std::make_shared<FakePointerController>();
10173 mFakePolicy->setPointerController(fakePointerController);
10174 mFakePolicy->setStylusPointerIconEnabled(false);
10175 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10176
10177 processId(mapper, FIRST_TRACKING_ID);
10178 processPressure(mapper, RAW_PRESSURE_MIN);
10179 processPosition(mapper, 100, 200);
10180 processToolType(mapper, MT_TOOL_PEN);
10181 processSync(mapper);
10182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10183 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010184 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010185 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10186 ASSERT_FALSE(fakePointerController->isPointerShown());
10187}
10188
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010189// --- MultiTouchInputMapperTest_ExternalDevice ---
10190
10191class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10192protected:
Chris Yea52ade12020-08-27 16:49:20 -070010193 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010194};
10195
10196/**
10197 * Expect fallback to internal viewport if device is external and external viewport is not present.
10198 */
10199TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10200 prepareAxes(POSITION);
10201 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010202 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010203 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10204
10205 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10206
10207 NotifyMotionArgs motionArgs;
10208
10209 // Expect the event to be sent to the internal viewport,
10210 // because an external viewport is not present.
10211 processPosition(mapper, 100, 100);
10212 processSync(mapper);
10213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10214 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10215
10216 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010217 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010218 processPosition(mapper, 100, 100);
10219 processSync(mapper);
10220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10221 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10222}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010223
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010224TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10225 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10226 std::shared_ptr<FakePointerController> fakePointerController =
10227 std::make_shared<FakePointerController>();
10228 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10229 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010230
10231 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010232 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010233 prepareAxes(POSITION | ID | SLOT);
10234 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10235 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10236 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010237 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010238 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10239
10240 // captured touchpad should be a touchpad source
10241 NotifyDeviceResetArgs resetArgs;
10242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10243 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10244
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010245 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010246
10247 const InputDeviceInfo::MotionRange* relRangeX =
10248 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10249 ASSERT_NE(relRangeX, nullptr);
10250 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10251 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10252 const InputDeviceInfo::MotionRange* relRangeY =
10253 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10254 ASSERT_NE(relRangeY, nullptr);
10255 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10256 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10257
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010258 // run captured pointer tests - note that this is unscaled, so input listener events should be
10259 // identical to what the hardware sends (accounting for any
10260 // calibration).
10261 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010262 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010263 processId(mapper, 1);
10264 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10265 processKey(mapper, BTN_TOUCH, 1);
10266 processSync(mapper);
10267
10268 // expect coord[0] to contain initial location of touch 0
10269 NotifyMotionArgs args;
10270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10271 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10272 ASSERT_EQ(1U, args.pointerCount);
10273 ASSERT_EQ(0, args.pointerProperties[0].id);
10274 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10275 ASSERT_NO_FATAL_FAILURE(
10276 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10277
10278 // FINGER 1 DOWN
10279 processSlot(mapper, 1);
10280 processId(mapper, 2);
10281 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10282 processSync(mapper);
10283
10284 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010286 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010287 ASSERT_EQ(2U, args.pointerCount);
10288 ASSERT_EQ(0, args.pointerProperties[0].id);
10289 ASSERT_EQ(1, args.pointerProperties[1].id);
10290 ASSERT_NO_FATAL_FAILURE(
10291 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10292 ASSERT_NO_FATAL_FAILURE(
10293 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10294
10295 // FINGER 1 MOVE
10296 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10297 processSync(mapper);
10298
10299 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10300 // from move
10301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10303 ASSERT_NO_FATAL_FAILURE(
10304 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10305 ASSERT_NO_FATAL_FAILURE(
10306 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10307
10308 // FINGER 0 MOVE
10309 processSlot(mapper, 0);
10310 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10311 processSync(mapper);
10312
10313 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10315 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10316 ASSERT_NO_FATAL_FAILURE(
10317 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10318 ASSERT_NO_FATAL_FAILURE(
10319 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10320
10321 // BUTTON DOWN
10322 processKey(mapper, BTN_LEFT, 1);
10323 processSync(mapper);
10324
10325 // touchinputmapper design sends a move before button press
10326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10330
10331 // BUTTON UP
10332 processKey(mapper, BTN_LEFT, 0);
10333 processSync(mapper);
10334
10335 // touchinputmapper design sends a move after button release
10336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10337 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10340
10341 // FINGER 0 UP
10342 processId(mapper, -1);
10343 processSync(mapper);
10344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10345 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10346
10347 // FINGER 1 MOVE
10348 processSlot(mapper, 1);
10349 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10350 processSync(mapper);
10351
10352 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10355 ASSERT_EQ(1U, args.pointerCount);
10356 ASSERT_EQ(1, args.pointerProperties[0].id);
10357 ASSERT_NO_FATAL_FAILURE(
10358 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10359
10360 // FINGER 1 UP
10361 processId(mapper, -1);
10362 processKey(mapper, BTN_TOUCH, 0);
10363 processSync(mapper);
10364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10365 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10366
Josep del Río2d8c79a2023-01-23 19:33:50 +000010367 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010368 mFakePolicy->setPointerCapture(false);
10369 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010371 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010372}
10373
10374TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10375 std::shared_ptr<FakePointerController> fakePointerController =
10376 std::make_shared<FakePointerController>();
10377 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10378 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010379
10380 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010381 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010382 prepareAxes(POSITION | ID | SLOT);
10383 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10384 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010385 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010386 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10387 // run uncaptured pointer tests - pushes out generic events
10388 // FINGER 0 DOWN
10389 processId(mapper, 3);
10390 processPosition(mapper, 100, 100);
10391 processKey(mapper, BTN_TOUCH, 1);
10392 processSync(mapper);
10393
10394 // start at (100,100), cursor should be at (0,0) * scale
10395 NotifyMotionArgs args;
10396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10397 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10398 ASSERT_NO_FATAL_FAILURE(
10399 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10400
10401 // FINGER 0 MOVE
10402 processPosition(mapper, 200, 200);
10403 processSync(mapper);
10404
10405 // compute scaling to help with touch position checking
10406 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10407 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10408 float scale =
10409 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10410
10411 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10413 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10415 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010416
10417 // BUTTON DOWN
10418 processKey(mapper, BTN_LEFT, 1);
10419 processSync(mapper);
10420
10421 // touchinputmapper design sends a move before button press
10422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10423 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10425 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10426
10427 // BUTTON UP
10428 processKey(mapper, BTN_LEFT, 0);
10429 processSync(mapper);
10430
10431 // touchinputmapper design sends a move after button release
10432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10433 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10435 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010436}
10437
10438TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10439 std::shared_ptr<FakePointerController> fakePointerController =
10440 std::make_shared<FakePointerController>();
10441
Michael Wrighta9cf4192022-12-01 23:46:39 +000010442 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010443 prepareAxes(POSITION | ID | SLOT);
10444 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010445 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010446 mFakePolicy->setPointerCapture(false);
10447 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10448
Josep del Río2d8c79a2023-01-23 19:33:50 +000010449 // uncaptured touchpad should be a pointer device
10450 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010451
Josep del Río2d8c79a2023-01-23 19:33:50 +000010452 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010453 mFakePolicy->setPointerCapture(true);
10454 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10455 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10456}
10457
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010458// --- BluetoothMultiTouchInputMapperTest ---
10459
10460class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10461protected:
10462 void SetUp() override {
10463 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10464 }
10465};
10466
10467TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10468 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010469 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010470 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10471 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10472
10473 nsecs_t kernelEventTime = ARBITRARY_TIME;
10474 nsecs_t expectedEventTime = ARBITRARY_TIME;
10475 // Touch down.
10476 processId(mapper, FIRST_TRACKING_ID);
10477 processPosition(mapper, 100, 200);
10478 processPressure(mapper, RAW_PRESSURE_MAX);
10479 processSync(mapper, ARBITRARY_TIME);
10480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10481 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10482
10483 // Process several events that come in quick succession, according to their timestamps.
10484 for (int i = 0; i < 3; i++) {
10485 constexpr static nsecs_t delta = ms2ns(1);
10486 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10487 kernelEventTime += delta;
10488 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10489
10490 processPosition(mapper, 101 + i, 201 + i);
10491 processSync(mapper, kernelEventTime);
10492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10493 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10494 WithEventTime(expectedEventTime))));
10495 }
10496
10497 // Release the touch.
10498 processId(mapper, INVALID_TRACKING_ID);
10499 processPressure(mapper, RAW_PRESSURE_MIN);
10500 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10502 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10503 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10504}
10505
10506// --- MultiTouchPointerModeTest ---
10507
HQ Liue6983c72022-04-19 22:14:56 +000010508class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10509protected:
10510 float mPointerMovementScale;
10511 float mPointerXZoomScale;
10512 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10513 addConfigurationProperty("touch.deviceType", "pointer");
10514 std::shared_ptr<FakePointerController> fakePointerController =
10515 std::make_shared<FakePointerController>();
10516 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10517 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010518 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010519
10520 prepareAxes(POSITION);
10521 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10522 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10523 // needs to be disabled, and the pointer gesture needs to be enabled.
10524 mFakePolicy->setPointerCapture(false);
10525 mFakePolicy->setPointerGestureEnabled(true);
10526 mFakePolicy->setPointerController(fakePointerController);
10527
10528 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10529 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10530 mPointerMovementScale =
10531 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10532 mPointerXZoomScale =
10533 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10534 }
10535
10536 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10537 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10538 /*flat*/ 0,
10539 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10540 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10541 /*flat*/ 0,
10542 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10543 }
10544};
10545
10546/**
10547 * Two fingers down on a pointer mode touch pad. The width
10548 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10549 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10550 * be greater than the both value to be freeform gesture, so that after two
10551 * fingers start to move downwards, the gesture should be swipe.
10552 */
10553TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10554 // The min freeform gesture width is 25units/mm x 30mm = 750
10555 // which is greater than fraction of the diagnal length of the touchpad (349).
10556 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010557 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010558 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10559 NotifyMotionArgs motionArgs;
10560
10561 // Two fingers down at once.
10562 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10563 // Pointer's initial position is used the [0,0] coordinate.
10564 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10565
10566 processId(mapper, FIRST_TRACKING_ID);
10567 processPosition(mapper, x1, y1);
10568 processMTSync(mapper);
10569 processId(mapper, SECOND_TRACKING_ID);
10570 processPosition(mapper, x2, y2);
10571 processMTSync(mapper);
10572 processSync(mapper);
10573
10574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10575 ASSERT_EQ(1U, motionArgs.pointerCount);
10576 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010577 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010578 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010579 ASSERT_NO_FATAL_FAILURE(
10580 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10581
10582 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10583 // that there should be 1 pointer.
10584 int32_t movingDistance = 200;
10585 y1 += movingDistance;
10586 y2 += movingDistance;
10587
10588 processId(mapper, FIRST_TRACKING_ID);
10589 processPosition(mapper, x1, y1);
10590 processMTSync(mapper);
10591 processId(mapper, SECOND_TRACKING_ID);
10592 processPosition(mapper, x2, y2);
10593 processMTSync(mapper);
10594 processSync(mapper);
10595
10596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10597 ASSERT_EQ(1U, motionArgs.pointerCount);
10598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010599 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010600 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10602 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10603 0, 0, 0, 0));
10604}
10605
10606/**
10607 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10608 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10609 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10610 * value to be freeform gesture, so that after two fingers start to move downwards,
10611 * the gesture should be swipe.
10612 */
10613TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10614 // The min freeform gesture width is 5units/mm x 30mm = 150
10615 // which is greater than fraction of the diagnal length of the touchpad (349).
10616 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010617 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
HQ Liue6983c72022-04-19 22:14:56 +000010618 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10619 NotifyMotionArgs motionArgs;
10620
10621 // Two fingers down at once.
10622 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10623 // Pointer's initial position is used the [0,0] coordinate.
10624 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10625
10626 processId(mapper, FIRST_TRACKING_ID);
10627 processPosition(mapper, x1, y1);
10628 processMTSync(mapper);
10629 processId(mapper, SECOND_TRACKING_ID);
10630 processPosition(mapper, x2, y2);
10631 processMTSync(mapper);
10632 processSync(mapper);
10633
10634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10635 ASSERT_EQ(1U, motionArgs.pointerCount);
10636 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010637 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010638 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010639 ASSERT_NO_FATAL_FAILURE(
10640 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10641
10642 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10643 // and there should be 1 pointer.
10644 int32_t movingDistance = 200;
10645 y1 += movingDistance;
10646 y2 += movingDistance;
10647
10648 processId(mapper, FIRST_TRACKING_ID);
10649 processPosition(mapper, x1, y1);
10650 processMTSync(mapper);
10651 processId(mapper, SECOND_TRACKING_ID);
10652 processPosition(mapper, x2, y2);
10653 processMTSync(mapper);
10654 processSync(mapper);
10655
10656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10657 ASSERT_EQ(1U, motionArgs.pointerCount);
10658 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010659 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010660 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010661 // New coordinate is the scaled relative coordinate from the initial coordinate.
10662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10663 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10664 0, 0, 0, 0));
10665}
10666
10667/**
10668 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10669 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10670 * freeform gestures after two fingers start to move downwards.
10671 */
10672TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010673 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010674 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10675
10676 NotifyMotionArgs motionArgs;
10677
10678 // Two fingers down at once. Wider than the max swipe width.
10679 // The gesture is expected to be PRESS, then transformed to FREEFORM
10680 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10681
10682 processId(mapper, FIRST_TRACKING_ID);
10683 processPosition(mapper, x1, y1);
10684 processMTSync(mapper);
10685 processId(mapper, SECOND_TRACKING_ID);
10686 processPosition(mapper, x2, y2);
10687 processMTSync(mapper);
10688 processSync(mapper);
10689
10690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10691 ASSERT_EQ(1U, motionArgs.pointerCount);
10692 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010693 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010694 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010695 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10696 ASSERT_NO_FATAL_FAILURE(
10697 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10698
10699 int32_t movingDistance = 200;
10700
10701 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10702 // then two down events for two pointers.
10703 y1 += movingDistance;
10704 y2 += movingDistance;
10705
10706 processId(mapper, FIRST_TRACKING_ID);
10707 processPosition(mapper, x1, y1);
10708 processMTSync(mapper);
10709 processId(mapper, SECOND_TRACKING_ID);
10710 processPosition(mapper, x2, y2);
10711 processMTSync(mapper);
10712 processSync(mapper);
10713
10714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10715 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10716 ASSERT_EQ(1U, motionArgs.pointerCount);
10717 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010719 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
HQ Liue6983c72022-04-19 22:14:56 +000010720 ASSERT_EQ(1U, motionArgs.pointerCount);
10721 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010723 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010724 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010725 ASSERT_EQ(2U, motionArgs.pointerCount);
10726 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010727 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010728 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010729 // Two pointers' scaled relative coordinates from their initial centroid.
10730 // Initial y coordinates are 0 as y1 and y2 have the same value.
10731 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10732 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10733 // When pointers move, the new coordinates equal to the initial coordinates plus
10734 // scaled moving distance.
10735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10736 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10737 0, 0, 0, 0));
10738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10739 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10740 0, 0, 0, 0));
10741
10742 // Move two fingers down again, expect one MOVE motion event.
10743 y1 += movingDistance;
10744 y2 += movingDistance;
10745
10746 processId(mapper, FIRST_TRACKING_ID);
10747 processPosition(mapper, x1, y1);
10748 processMTSync(mapper);
10749 processId(mapper, SECOND_TRACKING_ID);
10750 processPosition(mapper, x2, y2);
10751 processMTSync(mapper);
10752 processSync(mapper);
10753
10754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10755 ASSERT_EQ(2U, motionArgs.pointerCount);
10756 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010757 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010758 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10760 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10761 0, 0, 0, 0, 0));
10762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10763 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10764 0, 0, 0, 0, 0));
10765}
10766
Harry Cutts39b7ca22022-10-05 15:55:48 +000010767TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010768 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Harry Cutts39b7ca22022-10-05 15:55:48 +000010769 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10770 NotifyMotionArgs motionArgs;
10771
10772 // Place two fingers down.
10773 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10774
10775 processId(mapper, FIRST_TRACKING_ID);
10776 processPosition(mapper, x1, y1);
10777 processMTSync(mapper);
10778 processId(mapper, SECOND_TRACKING_ID);
10779 processPosition(mapper, x2, y2);
10780 processMTSync(mapper);
10781 processSync(mapper);
10782
10783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10784 ASSERT_EQ(1U, motionArgs.pointerCount);
10785 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10786 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10787 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10788 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10789
10790 // Move the two fingers down and to the left.
10791 int32_t movingDistance = 200;
10792 x1 -= movingDistance;
10793 y1 += movingDistance;
10794 x2 -= movingDistance;
10795 y2 += movingDistance;
10796
10797 processId(mapper, FIRST_TRACKING_ID);
10798 processPosition(mapper, x1, y1);
10799 processMTSync(mapper);
10800 processId(mapper, SECOND_TRACKING_ID);
10801 processPosition(mapper, x2, y2);
10802 processMTSync(mapper);
10803 processSync(mapper);
10804
10805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10806 ASSERT_EQ(1U, motionArgs.pointerCount);
10807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10808 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10809 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10810 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10811}
10812
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010813TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010814 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010815 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10816 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10818
10819 // Start a stylus gesture.
10820 processKey(mapper, BTN_TOOL_PEN, 1);
10821 processId(mapper, FIRST_TRACKING_ID);
10822 processPosition(mapper, 100, 200);
10823 processSync(mapper);
10824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10825 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10826 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010827 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010828 // TODO(b/257078296): Pointer mode generates extra event.
10829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10830 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10831 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010832 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10834
10835 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10836 // gesture should be disabled.
10837 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10838 viewport->isActive = false;
10839 mFakePolicy->updateViewport(*viewport);
10840 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10842 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10843 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010844 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010845 // TODO(b/257078296): Pointer mode generates extra event.
10846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10847 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10848 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010849 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10851}
10852
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010853// --- JoystickInputMapperTest ---
10854
10855class JoystickInputMapperTest : public InputMapperTest {
10856protected:
10857 static const int32_t RAW_X_MIN;
10858 static const int32_t RAW_X_MAX;
10859 static const int32_t RAW_Y_MIN;
10860 static const int32_t RAW_Y_MAX;
10861
10862 void SetUp() override {
10863 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10864 }
10865 void prepareAxes() {
10866 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10867 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10868 }
10869
10870 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10871 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10872 }
10873
10874 void processSync(JoystickInputMapper& mapper) {
10875 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10876 }
10877
Michael Wrighta9cf4192022-12-01 23:46:39 +000010878 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010879 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10880 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10881 NO_PORT, ViewportType::VIRTUAL);
10882 }
10883};
10884
10885const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10886const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10887const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10888const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10889
10890TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10891 prepareAxes();
10892 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10893
10894 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10895
Michael Wrighta9cf4192022-12-01 23:46:39 +000010896 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010897
10898 // Send an axis event
10899 processAxis(mapper, ABS_X, 100);
10900 processSync(mapper);
10901
10902 NotifyMotionArgs args;
10903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10904 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10905
10906 // Send another axis event
10907 processAxis(mapper, ABS_Y, 100);
10908 processSync(mapper);
10909
10910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10911 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10912}
10913
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010914// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010915
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010916class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010917protected:
10918 static const char* DEVICE_NAME;
10919 static const char* DEVICE_LOCATION;
10920 static const int32_t DEVICE_ID;
10921 static const int32_t DEVICE_GENERATION;
10922 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010923 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010924 static const int32_t EVENTHUB_ID;
10925
10926 std::shared_ptr<FakeEventHub> mFakeEventHub;
10927 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010928 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010929 std::unique_ptr<InstrumentedInputReader> mReader;
10930 std::shared_ptr<InputDevice> mDevice;
10931
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010932 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010933 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010934 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010935 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010936 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010937 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010938 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10939 }
10940
10941 void SetUp() override { SetUp(DEVICE_CLASSES); }
10942
10943 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010944 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010945 mFakePolicy.clear();
10946 }
10947
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010948 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010949 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10950 mReader->requestRefreshConfiguration(changes);
10951 mReader->loopOnce();
10952 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010953 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010954 }
10955
10956 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10957 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010958 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010959 InputDeviceIdentifier identifier;
10960 identifier.name = name;
10961 identifier.location = location;
10962 std::shared_ptr<InputDevice> device =
10963 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10964 identifier);
10965 mReader->pushNextDevice(device);
10966 mFakeEventHub->addDevice(eventHubId, name, classes);
10967 mReader->loopOnce();
10968 return device;
10969 }
10970
10971 template <class T, typename... Args>
10972 T& addControllerAndConfigure(Args... args) {
10973 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10974
10975 return controller;
10976 }
10977};
10978
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010979const char* PeripheralControllerTest::DEVICE_NAME = "device";
10980const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10981const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10982const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10983const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010984const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10985 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010986const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010987
10988// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010989class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010990protected:
10991 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010992 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010993 }
10994};
10995
10996TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010997 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010998
Harry Cuttsa5b71292022-11-28 12:56:17 +000010999 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11000 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11001 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011002}
11003
11004TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011005 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011006
Harry Cuttsa5b71292022-11-28 12:56:17 +000011007 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11008 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11009 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011010}
11011
11012// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011013class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011014protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011015 void SetUp() override {
11016 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11017 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011018};
11019
Chris Ye85758332021-05-16 23:05:17 -070011020TEST_F(LightControllerTest, MonoLight) {
11021 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011022 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011023 .maxBrightness = 255,
11024 .flags = InputLightClass::BRIGHTNESS,
11025 .path = ""};
11026 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011027
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011028 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011029 InputDeviceInfo info;
11030 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011031 std::vector<InputDeviceLightInfo> lights = info.getLights();
11032 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011033 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11034 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11035
11036 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11037 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11038}
11039
11040TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11041 RawLightInfo infoMono = {.id = 1,
11042 .name = "mono_keyboard_backlight",
11043 .maxBrightness = 255,
11044 .flags = InputLightClass::BRIGHTNESS |
11045 InputLightClass::KEYBOARD_BACKLIGHT,
11046 .path = ""};
11047 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11048
11049 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11050 InputDeviceInfo info;
11051 controller.populateDeviceInfo(&info);
11052 std::vector<InputDeviceLightInfo> lights = info.getLights();
11053 ASSERT_EQ(1U, lights.size());
11054 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11055 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011056
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011057 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11058 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011059}
11060
11061TEST_F(LightControllerTest, RGBLight) {
11062 RawLightInfo infoRed = {.id = 1,
11063 .name = "red",
11064 .maxBrightness = 255,
11065 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11066 .path = ""};
11067 RawLightInfo infoGreen = {.id = 2,
11068 .name = "green",
11069 .maxBrightness = 255,
11070 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11071 .path = ""};
11072 RawLightInfo infoBlue = {.id = 3,
11073 .name = "blue",
11074 .maxBrightness = 255,
11075 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11076 .path = ""};
11077 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11078 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11079 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11080
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011081 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011082 InputDeviceInfo info;
11083 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011084 std::vector<InputDeviceLightInfo> lights = info.getLights();
11085 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011086 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11087 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11088 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11089
11090 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11091 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11092}
11093
11094TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11095 RawLightInfo infoRed = {.id = 1,
11096 .name = "red_keyboard_backlight",
11097 .maxBrightness = 255,
11098 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11099 InputLightClass::KEYBOARD_BACKLIGHT,
11100 .path = ""};
11101 RawLightInfo infoGreen = {.id = 2,
11102 .name = "green_keyboard_backlight",
11103 .maxBrightness = 255,
11104 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11105 InputLightClass::KEYBOARD_BACKLIGHT,
11106 .path = ""};
11107 RawLightInfo infoBlue = {.id = 3,
11108 .name = "blue_keyboard_backlight",
11109 .maxBrightness = 255,
11110 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11111 InputLightClass::KEYBOARD_BACKLIGHT,
11112 .path = ""};
11113 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11114 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11115 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11116
11117 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11118 InputDeviceInfo info;
11119 controller.populateDeviceInfo(&info);
11120 std::vector<InputDeviceLightInfo> lights = info.getLights();
11121 ASSERT_EQ(1U, lights.size());
11122 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11123 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11124 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11125
11126 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11127 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11128}
11129
11130TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11131 RawLightInfo infoRed = {.id = 1,
11132 .name = "red",
11133 .maxBrightness = 255,
11134 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11135 .path = ""};
11136 RawLightInfo infoGreen = {.id = 2,
11137 .name = "green",
11138 .maxBrightness = 255,
11139 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11140 .path = ""};
11141 RawLightInfo infoBlue = {.id = 3,
11142 .name = "blue",
11143 .maxBrightness = 255,
11144 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11145 .path = ""};
11146 RawLightInfo infoGlobal = {.id = 3,
11147 .name = "global_keyboard_backlight",
11148 .maxBrightness = 255,
11149 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11150 InputLightClass::KEYBOARD_BACKLIGHT,
11151 .path = ""};
11152 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11153 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11154 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11155 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11156
11157 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11158 InputDeviceInfo info;
11159 controller.populateDeviceInfo(&info);
11160 std::vector<InputDeviceLightInfo> lights = info.getLights();
11161 ASSERT_EQ(1U, lights.size());
11162 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11163 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11164 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011165
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011166 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11167 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011168}
11169
11170TEST_F(LightControllerTest, MultiColorRGBLight) {
11171 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011172 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011173 .maxBrightness = 255,
11174 .flags = InputLightClass::BRIGHTNESS |
11175 InputLightClass::MULTI_INTENSITY |
11176 InputLightClass::MULTI_INDEX,
11177 .path = ""};
11178
11179 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11180
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011181 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011182 InputDeviceInfo info;
11183 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011184 std::vector<InputDeviceLightInfo> lights = info.getLights();
11185 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011186 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11187 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11188 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11189
11190 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11191 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11192}
11193
11194TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11195 RawLightInfo infoColor = {.id = 1,
11196 .name = "multi_color_keyboard_backlight",
11197 .maxBrightness = 255,
11198 .flags = InputLightClass::BRIGHTNESS |
11199 InputLightClass::MULTI_INTENSITY |
11200 InputLightClass::MULTI_INDEX |
11201 InputLightClass::KEYBOARD_BACKLIGHT,
11202 .path = ""};
11203
11204 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11205
11206 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11207 InputDeviceInfo info;
11208 controller.populateDeviceInfo(&info);
11209 std::vector<InputDeviceLightInfo> lights = info.getLights();
11210 ASSERT_EQ(1U, lights.size());
11211 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11212 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11213 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011214
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011215 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11216 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011217}
11218
11219TEST_F(LightControllerTest, PlayerIdLight) {
11220 RawLightInfo info1 = {.id = 1,
11221 .name = "player1",
11222 .maxBrightness = 255,
11223 .flags = InputLightClass::BRIGHTNESS,
11224 .path = ""};
11225 RawLightInfo info2 = {.id = 2,
11226 .name = "player2",
11227 .maxBrightness = 255,
11228 .flags = InputLightClass::BRIGHTNESS,
11229 .path = ""};
11230 RawLightInfo info3 = {.id = 3,
11231 .name = "player3",
11232 .maxBrightness = 255,
11233 .flags = InputLightClass::BRIGHTNESS,
11234 .path = ""};
11235 RawLightInfo info4 = {.id = 4,
11236 .name = "player4",
11237 .maxBrightness = 255,
11238 .flags = InputLightClass::BRIGHTNESS,
11239 .path = ""};
11240 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11241 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11242 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11243 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11244
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011245 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011246 InputDeviceInfo info;
11247 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011248 std::vector<InputDeviceLightInfo> lights = info.getLights();
11249 ASSERT_EQ(1U, lights.size());
11250 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011251 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11252 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011253
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011254 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11255 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11256 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011257}
11258
Michael Wrightd02c5b62014-02-10 15:10:22 -080011259} // namespace android