blob: da3fe5bc4f1f2226f2bb29b84c09fe2cb3def808 [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());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700608 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609 }
610
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800611 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700612 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000613 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700614 }
615
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800616 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700617 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000618 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700619 }
620
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800621 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700622 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700623 ftl::Flags<InputDeviceClass> classes,
624 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800625 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800626 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000627 FakeInputMapper& mapper =
628 device->addMapper<FakeInputMapper>(eventHubId,
629 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800630 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800631 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 return mapper;
633 }
634};
635
Chris Ye98d3f532020-10-01 21:48:59 -0700636TEST_F(InputReaderTest, PolicyGetInputDevices) {
637 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700638 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700639 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640
641 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700642 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800644 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100645 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800646 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
647 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000648 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800649}
650
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000651TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
652 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
653 mFakeEventHub->setSysfsRootPath(1, "xyz");
654
655 // Should also have received a notification describing the new input device.
656 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
657 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
658 ASSERT_EQ(0U, inputDevice.getLights().size());
659
660 RawLightInfo infoMonolight = {.id = 123,
661 .name = "mono_keyboard_backlight",
662 .maxBrightness = 255,
663 .flags = InputLightClass::BRIGHTNESS,
664 .path = ""};
665 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
666 mReader->sysfsNodeChanged("xyz");
667 mReader->loopOnce();
668
669 // Should also have received a notification describing the new recreated input device.
670 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
671 inputDevice = mFakePolicy->getInputDevices()[0];
672 ASSERT_EQ(1U, inputDevice.getLights().size());
673}
674
Chris Yee7310032020-09-22 15:36:28 -0700675TEST_F(InputReaderTest, GetMergedInputDevices) {
676 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
677 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
678 // Add two subdevices to device
679 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
680 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000681 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
682 AINPUT_SOURCE_KEYBOARD);
683 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
684 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700685
686 // Push same device instance for next device to be added, so they'll have same identifier.
687 mReader->pushNextDevice(device);
688 mReader->pushNextDevice(device);
689 ASSERT_NO_FATAL_FAILURE(
690 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
691 ASSERT_NO_FATAL_FAILURE(
692 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
693
694 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000695 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700696}
697
Chris Yee14523a2020-12-19 13:46:00 -0800698TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
699 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
700 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
701 // Add two subdevices to device
702 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
703 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000704 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
705 AINPUT_SOURCE_KEYBOARD);
706 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
707 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800708
709 // Push same device instance for next device to be added, so they'll have same identifier.
710 mReader->pushNextDevice(device);
711 mReader->pushNextDevice(device);
712 // Sensor device is initially disabled
713 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
714 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
715 nullptr));
716 // Device is disabled because the only sub device is a sensor device and disabled initially.
717 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
718 ASSERT_FALSE(device->isEnabled());
719 ASSERT_NO_FATAL_FAILURE(
720 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
721 // The merged device is enabled if any sub device is enabled
722 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
723 ASSERT_TRUE(device->isEnabled());
724}
725
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700726TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800727 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700728 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800729 constexpr int32_t eventHubId = 1;
730 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700731 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000732 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
733 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800734 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800735 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700736
Yi Kong9b14ac62018-07-17 13:48:38 -0700737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700738
739 NotifyDeviceResetArgs resetArgs;
740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700741 ASSERT_EQ(deviceId, resetArgs.deviceId);
742
743 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800744 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000745 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700746
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700748 ASSERT_EQ(deviceId, resetArgs.deviceId);
749 ASSERT_EQ(device->isEnabled(), false);
750
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800751 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000752 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700755 ASSERT_EQ(device->isEnabled(), false);
756
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800757 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000758 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700760 ASSERT_EQ(deviceId, resetArgs.deviceId);
761 ASSERT_EQ(device->isEnabled(), true);
762}
763
Michael Wrightd02c5b62014-02-10 15:10:22 -0800764TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800765 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700766 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800767 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800768 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800769 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800770 AINPUT_SOURCE_KEYBOARD, nullptr);
771 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800772
773 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
774 AINPUT_SOURCE_ANY, AKEYCODE_A))
775 << "Should return unknown when the device id is >= 0 but unknown.";
776
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800777 ASSERT_EQ(AKEY_STATE_UNKNOWN,
778 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
779 << "Should return unknown when the device id is valid but the sources are not "
780 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800781
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800782 ASSERT_EQ(AKEY_STATE_DOWN,
783 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
784 AKEYCODE_A))
785 << "Should return value provided by mapper when device id is valid and the device "
786 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787
788 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
789 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
790 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
791
792 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
793 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
794 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
795}
796
Philip Junker4af3b3d2021-12-14 10:36:55 +0100797TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
798 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
799 constexpr int32_t eventHubId = 1;
800 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
801 InputDeviceClass::KEYBOARD,
802 AINPUT_SOURCE_KEYBOARD, nullptr);
803 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
804
805 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
806 << "Should return unknown when the device with the specified id is not found.";
807
808 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
809 << "Should return correct mapping when device id is valid and mapping exists.";
810
811 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
812 << "Should return the location key code when device id is valid and there's no "
813 "mapping.";
814}
815
816TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
817 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
818 constexpr int32_t eventHubId = 1;
819 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
820 InputDeviceClass::JOYSTICK,
821 AINPUT_SOURCE_GAMEPAD, nullptr);
822 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
823
824 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
825 << "Should return unknown when the device id is valid but there is no keyboard mapper";
826}
827
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800829 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700830 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800831 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800832 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800833 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800834 AINPUT_SOURCE_KEYBOARD, nullptr);
835 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836
837 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
838 AINPUT_SOURCE_ANY, KEY_A))
839 << "Should return unknown when the device id is >= 0 but unknown.";
840
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800841 ASSERT_EQ(AKEY_STATE_UNKNOWN,
842 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
843 << "Should return unknown when the device id is valid but the sources are not "
844 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800846 ASSERT_EQ(AKEY_STATE_DOWN,
847 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
848 KEY_A))
849 << "Should return value provided by mapper when device id is valid and the device "
850 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851
852 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
853 AINPUT_SOURCE_TRACKBALL, KEY_A))
854 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
855
856 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
857 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
858 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
859}
860
861TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800862 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700863 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800864 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800865 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800866 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800867 AINPUT_SOURCE_KEYBOARD, nullptr);
868 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869
870 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
871 AINPUT_SOURCE_ANY, SW_LID))
872 << "Should return unknown when the device id is >= 0 but unknown.";
873
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800874 ASSERT_EQ(AKEY_STATE_UNKNOWN,
875 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
876 << "Should return unknown when the device id is valid but the sources are not "
877 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800878
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800879 ASSERT_EQ(AKEY_STATE_DOWN,
880 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
881 SW_LID))
882 << "Should return value provided by mapper when device id is valid and the device "
883 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800884
885 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
886 AINPUT_SOURCE_TRACKBALL, SW_LID))
887 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
888
889 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
890 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
891 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
892}
893
894TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800895 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700896 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800897 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800898 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800899 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800900 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100901
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800902 mapper.addSupportedKeyCode(AKEYCODE_A);
903 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700905 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800906 uint8_t flags[4] = { 0, 0, 0, 1 };
907
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700908 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909 << "Should return false when device id is >= 0 but unknown.";
910 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
911
912 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700913 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800914 << "Should return false when device id is valid but the sources are not supported by "
915 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
917
918 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700919 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800920 keyCodes, flags))
921 << "Should return value provided by mapper when device id is valid and the device "
922 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800923 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
924
925 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700926 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
927 << "Should return false when the device id is < 0 but the sources are not supported by "
928 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
930
931 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700932 ASSERT_TRUE(
933 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
934 << "Should return value provided by mapper when device id is < 0 and one of the "
935 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800936 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
937}
938
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000939TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800940 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700941 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800942
943 NotifyConfigurationChangedArgs args;
944
945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
946 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
947}
948
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000949TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800950 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700951 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000952 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800953 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000954 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800955 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800956 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800957 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000959 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000960 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
962
963 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800964 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000965 ASSERT_EQ(when, event.when);
966 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800967 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968 ASSERT_EQ(EV_KEY, event.type);
969 ASSERT_EQ(KEY_A, event.code);
970 ASSERT_EQ(1, event.value);
971}
972
Garfield Tan1c7bc862020-01-28 13:24:04 -0800973TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800974 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700975 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800976 constexpr int32_t eventHubId = 1;
977 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800978 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000979 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
980 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800981 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800982 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800983
984 NotifyDeviceResetArgs resetArgs;
985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800986 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800987
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800988 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000989 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800991 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800992 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800993
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800994 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000995 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800997 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800998 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800999
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001000 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001001 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001003 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001004 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001005}
1006
Garfield Tan1c7bc862020-01-28 13:24:04 -08001007TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1008 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001009 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001010 constexpr int32_t eventHubId = 1;
1011 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1012 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001013 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1014 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001015 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001016 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1017
1018 NotifyDeviceResetArgs resetArgs;
1019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1020 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1021}
1022
Arthur Hungc23540e2018-11-29 20:42:11 +08001023TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001024 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001025 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001026 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001027 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001028 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1029 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001030 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1031 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001032 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001033
1034 const uint8_t hdmi1 = 1;
1035
1036 // Associated touch screen with second display.
1037 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1038
1039 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001040 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001041 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001042 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001043 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001044 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001045 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001046 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001047 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001048
1049 // Add the device, and make sure all of the callbacks are triggered.
1050 // The device is added after the input port associations are processed since
1051 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001052 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001055 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001056
Arthur Hung2c9a3342019-07-23 14:18:59 +08001057 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001058 ASSERT_EQ(deviceId, device->getId());
1059 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1060 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001061
1062 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001063 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001064 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001065 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001066}
1067
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001068TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1069 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001070 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001071 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1072 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1073 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001074 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1075 AINPUT_SOURCE_KEYBOARD);
1076 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1077 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001078 mReader->pushNextDevice(device);
1079 mReader->pushNextDevice(device);
1080 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1081 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1082
1083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1084
1085 NotifyDeviceResetArgs resetArgs;
1086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1087 ASSERT_EQ(deviceId, resetArgs.deviceId);
1088 ASSERT_TRUE(device->isEnabled());
1089 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1090 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1091
1092 disableDevice(deviceId);
1093 mReader->loopOnce();
1094
1095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1096 ASSERT_EQ(deviceId, resetArgs.deviceId);
1097 ASSERT_FALSE(device->isEnabled());
1098 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1099 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1100
1101 enableDevice(deviceId);
1102 mReader->loopOnce();
1103
1104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1105 ASSERT_EQ(deviceId, resetArgs.deviceId);
1106 ASSERT_TRUE(device->isEnabled());
1107 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1108 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1109}
1110
1111TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1112 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001113 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001114 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1115 // Add two subdevices to device
1116 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1117 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001118 device->addMapper<FakeInputMapper>(eventHubIds[0],
1119 mFakePolicy->getReaderConfiguration(),
1120 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001121 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001122 device->addMapper<FakeInputMapper>(eventHubIds[1],
1123 mFakePolicy->getReaderConfiguration(),
1124 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001125 mReader->pushNextDevice(device);
1126 mReader->pushNextDevice(device);
1127 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1128 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1129
1130 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1131 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1132
1133 ASSERT_EQ(AKEY_STATE_DOWN,
1134 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1135 ASSERT_EQ(AKEY_STATE_DOWN,
1136 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1137 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1138 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1139}
1140
Prabir Pradhan7e186182020-11-10 13:56:45 -08001141TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1142 NotifyPointerCaptureChangedArgs args;
1143
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001144 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001145 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1146 mReader->loopOnce();
1147 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001148 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1149 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001150
1151 mFakePolicy->setPointerCapture(false);
1152 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1153 mReader->loopOnce();
1154 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001155 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001156
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001157 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001158 // does not change.
1159 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1160 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001161 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001162}
1163
Chris Ye87143712020-11-10 05:05:58 +00001164class FakeVibratorInputMapper : public FakeInputMapper {
1165public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001166 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1167 const InputReaderConfiguration& readerConfig, uint32_t sources)
1168 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001169
1170 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1171};
1172
1173TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1174 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001175 ftl::Flags<InputDeviceClass> deviceClass =
1176 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001177 constexpr int32_t eventHubId = 1;
1178 const char* DEVICE_LOCATION = "BLUETOOTH";
1179 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1180 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001181 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1182 mFakePolicy->getReaderConfiguration(),
1183 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001184 mReader->pushNextDevice(device);
1185
1186 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1187 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1188
1189 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1190 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1191}
1192
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001193// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001194
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001195class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001196public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001197 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001198
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001199 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001200
Andy Chenf9f1a022022-08-29 20:07:10 -04001201 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1202
Chris Yee2b1e5c2021-03-10 22:45:12 -08001203 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1204
1205 void dump(std::string& dump) override {}
1206
1207 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1208 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001209 }
1210
Chris Yee2b1e5c2021-03-10 22:45:12 -08001211 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1212 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001213 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001214
1215 bool setLightColor(int32_t lightId, int32_t color) override {
1216 getDeviceContext().setLightBrightness(lightId, color >> 24);
1217 return true;
1218 }
1219
1220 std::optional<int32_t> getLightColor(int32_t lightId) override {
1221 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1222 if (!result.has_value()) {
1223 return std::nullopt;
1224 }
1225 return result.value() << 24;
1226 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001227
1228 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1229
1230 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1231
1232private:
1233 InputDeviceContext& mDeviceContext;
1234 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1235 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001236 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001237};
1238
Chris Yee2b1e5c2021-03-10 22:45:12 -08001239TEST_F(InputReaderTest, BatteryGetCapacity) {
1240 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001241 ftl::Flags<InputDeviceClass> deviceClass =
1242 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001243 constexpr int32_t eventHubId = 1;
1244 const char* DEVICE_LOCATION = "BLUETOOTH";
1245 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001246 FakePeripheralController& controller =
1247 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001248 mReader->pushNextDevice(device);
1249
1250 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1251
Harry Cuttsa5b71292022-11-28 12:56:17 +00001252 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1253 FakeEventHub::BATTERY_CAPACITY);
1254 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001255}
1256
1257TEST_F(InputReaderTest, BatteryGetStatus) {
1258 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001259 ftl::Flags<InputDeviceClass> deviceClass =
1260 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001261 constexpr int32_t eventHubId = 1;
1262 const char* DEVICE_LOCATION = "BLUETOOTH";
1263 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001264 FakePeripheralController& controller =
1265 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001266 mReader->pushNextDevice(device);
1267
1268 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1269
Harry Cuttsa5b71292022-11-28 12:56:17 +00001270 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1271 FakeEventHub::BATTERY_STATUS);
1272 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001273}
1274
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001275TEST_F(InputReaderTest, BatteryGetDevicePath) {
1276 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1277 ftl::Flags<InputDeviceClass> deviceClass =
1278 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1279 constexpr int32_t eventHubId = 1;
1280 const char* DEVICE_LOCATION = "BLUETOOTH";
1281 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1282 device->addController<FakePeripheralController>(eventHubId);
1283 mReader->pushNextDevice(device);
1284
1285 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1286
Harry Cuttsa5b71292022-11-28 12:56:17 +00001287 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001288}
1289
Chris Ye3fdbfef2021-01-06 18:45:18 -08001290TEST_F(InputReaderTest, LightGetColor) {
1291 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001292 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001293 constexpr int32_t eventHubId = 1;
1294 const char* DEVICE_LOCATION = "BLUETOOTH";
1295 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001296 FakePeripheralController& controller =
1297 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001298 mReader->pushNextDevice(device);
1299 RawLightInfo info = {.id = 1,
1300 .name = "Mono",
1301 .maxBrightness = 255,
1302 .flags = InputLightClass::BRIGHTNESS,
1303 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001304 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1305 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001306
1307 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001308
Harry Cutts33476232023-01-30 19:57:29 +00001309 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1310 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1311 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1312 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001313}
1314
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001315// --- InputReaderIntegrationTest ---
1316
1317// These tests create and interact with the InputReader only through its interface.
1318// The InputReader is started during SetUp(), which starts its processing in its own
1319// thread. The tests use linux uinput to emulate input devices.
1320// NOTE: Interacting with the physical device while these tests are running may cause
1321// the tests to fail.
1322class InputReaderIntegrationTest : public testing::Test {
1323protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001324 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001325 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001326 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001327
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001328 std::shared_ptr<FakePointerController> mFakePointerController;
1329
Chris Yea52ade12020-08-27 16:49:20 -07001330 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001331#if !defined(__ANDROID__)
1332 GTEST_SKIP();
1333#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001334 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001335 mFakePointerController = std::make_shared<FakePointerController>();
1336 mFakePolicy->setPointerController(mFakePointerController);
Harry Cutts33476232023-01-30 19:57:29 +00001337 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1338 /*eventDidNotHappenTimeout=*/30ms);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001339
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001340 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1341 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001342 ASSERT_EQ(mReader->start(), OK);
1343
1344 // Since this test is run on a real device, all the input devices connected
1345 // to the test device will show up in mReader. We wait for those input devices to
1346 // show up before beginning the tests.
1347 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +00001348 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001349 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1350 }
1351
Chris Yea52ade12020-08-27 16:49:20 -07001352 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001353#if !defined(__ANDROID__)
1354 return;
1355#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001356 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001357 mReader.reset();
1358 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001359 mFakePolicy.clear();
1360 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001361
1362 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1363 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1364 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1365 [&name](const InputDeviceInfo& info) {
1366 return info.getIdentifier().name == name;
1367 });
1368 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1369 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001370};
1371
1372TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1373 // An invalid input device that is only used for this test.
1374 class InvalidUinputDevice : public UinputDevice {
1375 public:
Harry Cutts33476232023-01-30 19:57:29 +00001376 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001377
1378 private:
1379 void configureDevice(int fd, uinput_user_dev* device) override {}
1380 };
1381
1382 const size_t numDevices = mFakePolicy->getInputDevices().size();
1383
1384 // UinputDevice does not set any event or key bits, so InputReader should not
1385 // consider it as a valid device.
1386 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1387 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1388 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1389 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1390
1391 invalidDevice.reset();
1392 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1393 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1394 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1395}
1396
1397TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1398 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1399
1400 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1401 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1402 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1403 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1404
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001405 const auto device = findDeviceByName(keyboard->getName());
1406 ASSERT_TRUE(device.has_value());
1407 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1408 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1409 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001410
1411 keyboard.reset();
1412 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1413 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1414 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1415}
1416
1417TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1418 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1419 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1420
1421 NotifyConfigurationChangedArgs configChangedArgs;
1422 ASSERT_NO_FATAL_FAILURE(
1423 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001424 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001425 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1426
1427 NotifyKeyArgs keyArgs;
1428 keyboard->pressAndReleaseHomeKey();
1429 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1430 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001431 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001432 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001433 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001434 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001435 prevTimestamp = keyArgs.eventTime;
1436
1437 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1438 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001439 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001440 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001441 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001442}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001443
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001444TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1445 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1446 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1447
1448 const auto device = findDeviceByName(stylus->getName());
1449 ASSERT_TRUE(device.has_value());
1450
Prabir Pradhana3621852022-10-14 18:57:23 +00001451 // An external stylus with buttons should also be recognized as a keyboard.
1452 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001453 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1454 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1455
1456 const auto DOWN =
1457 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1458 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1459
1460 stylus->pressAndReleaseKey(BTN_STYLUS);
1461 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1462 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1463 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1464 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1465
1466 stylus->pressAndReleaseKey(BTN_STYLUS2);
1467 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1468 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1469 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1470 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1471
1472 stylus->pressAndReleaseKey(BTN_STYLUS3);
1473 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1474 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1475 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1476 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1477}
1478
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001479/**
1480 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1481 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1482 * are passed to the listener.
1483 */
1484static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1485TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1486 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1487 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1488 NotifyKeyArgs keyArgs;
1489
1490 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1491 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1492 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1493 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1494
1495 controller->pressAndReleaseKey(BTN_GEAR_UP);
1496 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1497 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1498 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1499}
1500
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001501// --- TouchIntegrationTest ---
1502
Arthur Hungaab25622020-01-16 11:22:11 +08001503class TouchIntegrationTest : public InputReaderIntegrationTest {
1504protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001505 const std::string UNIQUE_ID = "local:0";
1506
Chris Yea52ade12020-08-27 16:49:20 -07001507 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001508#if !defined(__ANDROID__)
1509 GTEST_SKIP();
1510#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001511 InputReaderIntegrationTest::SetUp();
1512 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001513 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1514 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001515
1516 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1517 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1518 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001519 const auto info = findDeviceByName(mDevice->getName());
1520 ASSERT_TRUE(info);
1521 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001522 }
1523
1524 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001525 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001526 std::optional<uint8_t> physicalPort,
1527 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001528 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001529 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08001530 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1531 }
1532
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001533 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1534 NotifyMotionArgs args;
1535 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1536 EXPECT_EQ(action, args.action);
1537 ASSERT_EQ(points.size(), args.pointerCount);
1538 for (size_t i = 0; i < args.pointerCount; i++) {
1539 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1540 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1541 }
1542 }
1543
Arthur Hungaab25622020-01-16 11:22:11 +08001544 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001545 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001546};
1547
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001548TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
1549 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1550 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1551 // presses).
1552 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1553 mDeviceInfo.getSources());
1554}
1555
Arthur Hungaab25622020-01-16 11:22:11 +08001556TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1557 NotifyMotionArgs args;
1558 const Point centerPoint = mDevice->getCenterPoint();
1559
1560 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001561 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001562 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001563 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001564 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1565 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1566
1567 // ACTION_MOVE
1568 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001569 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001570 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1572
1573 // ACTION_UP
1574 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001575 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001576 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1577 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1578}
1579
1580TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1581 NotifyMotionArgs args;
1582 const Point centerPoint = mDevice->getCenterPoint();
1583
1584 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001585 mDevice->sendSlot(FIRST_SLOT);
1586 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001587 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001588 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001589 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1590 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1591
1592 // ACTION_POINTER_DOWN (Second slot)
1593 const Point secondPoint = centerPoint + Point(100, 100);
1594 mDevice->sendSlot(SECOND_SLOT);
1595 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001596 mDevice->sendDown(secondPoint);
1597 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001598 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001599 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001600
1601 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001602 mDevice->sendMove(secondPoint + Point(1, 1));
1603 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001604 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1605 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1606
1607 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001608 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001609 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001610 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001611 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001612
1613 // ACTION_UP
1614 mDevice->sendSlot(FIRST_SLOT);
1615 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001616 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001617 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1618 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1619}
1620
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001621/**
1622 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1623 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1624 * data?
1625 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1626 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1627 * for Pointer 0 only is generated after.
1628 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1629 * events, we will not miss any information.
1630 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1631 * event generated afterwards that contains the newest movement of pointer 0.
1632 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1633 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1634 * losing information about non-palm pointers.
1635 */
1636TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1637 NotifyMotionArgs args;
1638 const Point centerPoint = mDevice->getCenterPoint();
1639
1640 // ACTION_DOWN
1641 mDevice->sendSlot(FIRST_SLOT);
1642 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1643 mDevice->sendDown(centerPoint);
1644 mDevice->sendSync();
1645 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1646
1647 // ACTION_POINTER_DOWN (Second slot)
1648 const Point secondPoint = centerPoint + Point(100, 100);
1649 mDevice->sendSlot(SECOND_SLOT);
1650 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1651 mDevice->sendDown(secondPoint);
1652 mDevice->sendSync();
1653 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1654
1655 // ACTION_MOVE (First slot)
1656 mDevice->sendSlot(FIRST_SLOT);
1657 mDevice->sendMove(centerPoint + Point(5, 5));
1658 // ACTION_POINTER_UP (Second slot)
1659 mDevice->sendSlot(SECOND_SLOT);
1660 mDevice->sendPointerUp();
1661 // Send a single sync for the above 2 pointer updates
1662 mDevice->sendSync();
1663
1664 // First, we should get POINTER_UP for the second pointer
1665 assertReceivedMotion(ACTION_POINTER_1_UP,
1666 {/*first pointer */ centerPoint + Point(5, 5),
1667 /*second pointer*/ secondPoint});
1668
1669 // Next, the MOVE event for the first pointer
1670 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1671}
1672
1673/**
1674 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1675 * move, and then it will go up, all in the same frame.
1676 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1677 * gets sent to the listener.
1678 */
1679TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1680 NotifyMotionArgs args;
1681 const Point centerPoint = mDevice->getCenterPoint();
1682
1683 // ACTION_DOWN
1684 mDevice->sendSlot(FIRST_SLOT);
1685 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1686 mDevice->sendDown(centerPoint);
1687 mDevice->sendSync();
1688 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1689
1690 // ACTION_POINTER_DOWN (Second slot)
1691 const Point secondPoint = centerPoint + Point(100, 100);
1692 mDevice->sendSlot(SECOND_SLOT);
1693 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1694 mDevice->sendDown(secondPoint);
1695 mDevice->sendSync();
1696 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1697
1698 // ACTION_MOVE (First slot)
1699 mDevice->sendSlot(FIRST_SLOT);
1700 mDevice->sendMove(centerPoint + Point(5, 5));
1701 // ACTION_POINTER_UP (Second slot)
1702 mDevice->sendSlot(SECOND_SLOT);
1703 mDevice->sendMove(secondPoint + Point(6, 6));
1704 mDevice->sendPointerUp();
1705 // Send a single sync for the above 2 pointer updates
1706 mDevice->sendSync();
1707
1708 // First, we should get POINTER_UP for the second pointer
1709 // The movement of the second pointer during the liftoff frame is ignored.
1710 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1711 assertReceivedMotion(ACTION_POINTER_1_UP,
1712 {/*first pointer */ centerPoint + Point(5, 5),
1713 /*second pointer*/ secondPoint});
1714
1715 // Next, the MOVE event for the first pointer
1716 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1717}
1718
Arthur Hungaab25622020-01-16 11:22:11 +08001719TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1720 NotifyMotionArgs args;
1721 const Point centerPoint = mDevice->getCenterPoint();
1722
1723 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001724 mDevice->sendSlot(FIRST_SLOT);
1725 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001726 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001727 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001728 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1729 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1730
arthurhungcc7f9802020-04-30 17:55:40 +08001731 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001732 const Point secondPoint = centerPoint + Point(100, 100);
1733 mDevice->sendSlot(SECOND_SLOT);
1734 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1735 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001736 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001737 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001738 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001739
arthurhungcc7f9802020-04-30 17:55:40 +08001740 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001741 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001742 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001743 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1744 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1745
arthurhungcc7f9802020-04-30 17:55:40 +08001746 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1747 // a palm event.
1748 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001749 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001750 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001751 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001752 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001753 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001754
arthurhungcc7f9802020-04-30 17:55:40 +08001755 // Send up to second slot, expect first slot send moving.
1756 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001757 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001758 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001760
arthurhungcc7f9802020-04-30 17:55:40 +08001761 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001762 mDevice->sendSlot(FIRST_SLOT);
1763 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001764 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001765
arthurhungcc7f9802020-04-30 17:55:40 +08001766 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1767 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001768}
1769
Prabir Pradhanda20b172022-09-26 17:01:18 +00001770TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
1771 const Point centerPoint = mDevice->getCenterPoint();
1772
1773 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1774 mDevice->sendSlot(FIRST_SLOT);
1775 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1776 mDevice->sendToolType(MT_TOOL_PEN);
1777 mDevice->sendDown(centerPoint);
1778 mDevice->sendSync();
1779 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1780 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001781 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001782
1783 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1784
1785 // Release the stylus touch.
1786 mDevice->sendUp();
1787 mDevice->sendSync();
1788 ASSERT_NO_FATAL_FAILURE(
1789 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1790
1791 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1792
1793 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1794 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1795 mDevice->sendToolType(MT_TOOL_FINGER);
1796 mDevice->sendDown(centerPoint);
1797 mDevice->sendSync();
1798 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1799 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001800 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001801
1802 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1803
1804 mDevice->sendUp();
1805 mDevice->sendSync();
1806 ASSERT_NO_FATAL_FAILURE(
1807 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1808
1809 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1810 // The policy should be notified of the stylus presence.
1811 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1812 mDevice->sendToolType(MT_TOOL_PEN);
1813 mDevice->sendMove(centerPoint);
1814 mDevice->sendSync();
1815 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1816 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001817 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001818
1819 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1820}
1821
Prabir Pradhan124ea442022-10-28 20:27:44 +00001822// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001823
Prabir Pradhan124ea442022-10-28 20:27:44 +00001824// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1825// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1826// stylus.
1827template <typename UinputStylusDevice>
1828class StylusButtonIntegrationTest : public TouchIntegrationTest {
1829protected:
1830 void SetUp() override {
1831#if !defined(__ANDROID__)
1832 GTEST_SKIP();
1833#endif
1834 TouchIntegrationTest::SetUp();
1835 mTouchscreen = mDevice.get();
1836 mTouchscreenInfo = mDeviceInfo;
1837
1838 setUpStylusDevice();
1839 }
1840
1841 UinputStylusDevice* mStylus{nullptr};
1842 InputDeviceInfo mStylusInfo{};
1843
1844 UinputTouchScreen* mTouchscreen{nullptr};
1845 InputDeviceInfo mTouchscreenInfo{};
1846
1847private:
1848 // When we are attempting to test stylus button events that are sent from the touchscreen,
1849 // use the same Uinput device for the touchscreen and the stylus.
1850 template <typename T = UinputStylusDevice>
1851 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1852 mStylus = mDevice.get();
1853 mStylusInfo = mDeviceInfo;
1854 }
1855
1856 // When we are attempting to stylus buttons from an external stylus being merged with touches
1857 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1858 template <typename T = UinputStylusDevice>
1859 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1860 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1861 mStylus = mStylusDeviceLifecycleTracker.get();
1862 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1863 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1864 const auto info = findDeviceByName(mStylus->getName());
1865 ASSERT_TRUE(info);
1866 mStylusInfo = *info;
1867 }
1868
1869 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1870
1871 // Hide the base class's device to expose it with a different name for readability.
1872 using TouchIntegrationTest::mDevice;
1873 using TouchIntegrationTest::mDeviceInfo;
1874};
1875
1876using StylusButtonIntegrationTestTypes =
1877 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1878TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1879
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001880TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001881 const auto stylusId = TestFixture::mStylusInfo.getId();
1882
1883 TestFixture::mStylus->pressKey(BTN_STYLUS);
1884 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1885 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1886 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1887
1888 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1889 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001890 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001891 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001892}
1893
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001894TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001895 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1896 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1897 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001898
1899 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001900 TestFixture::mStylus->pressKey(BTN_STYLUS);
1901 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001902 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001903 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001904
1905 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001906 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1907 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1908 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1909 TestFixture::mTouchscreen->sendDown(centerPoint);
1910 TestFixture::mTouchscreen->sendSync();
1911 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001912 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001913 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001914 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1915 WithDeviceId(touchscreenId))));
1916 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001917 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001918 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001919 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1920 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001921
Prabir Pradhan124ea442022-10-28 20:27:44 +00001922 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1923 TestFixture::mTouchscreen->sendSync();
1924 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001925 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001926 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001927 WithDeviceId(touchscreenId))));
1928 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001929 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001930 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001931 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001932
1933 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001934 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1935 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001936 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001937 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001938}
1939
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001940TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001941 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1942 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1943 const auto stylusId = TestFixture::mStylusInfo.getId();
1944 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001945 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001946
1947 // Press the stylus button.
1948 TestFixture::mStylus->pressKey(BTN_STYLUS);
1949 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1950 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1951 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1952
1953 // Start hovering with the stylus.
1954 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1955 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1956 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1957 TestFixture::mTouchscreen->sendMove(centerPoint);
1958 TestFixture::mTouchscreen->sendSync();
1959 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1960 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1961 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1962 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1963 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1964 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1965 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1966 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1967 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1968
1969 // Touch down with the stylus.
1970 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1971 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1972 TestFixture::mTouchscreen->sendDown(centerPoint);
1973 TestFixture::mTouchscreen->sendSync();
1974 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1975 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1976 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1977
1978 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1979 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1980 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1981
1982 // Stop touching with the stylus, and start hovering.
1983 TestFixture::mTouchscreen->sendUp();
1984 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1985 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1986 TestFixture::mTouchscreen->sendMove(centerPoint);
1987 TestFixture::mTouchscreen->sendSync();
1988 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1989 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
1990 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1991 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1992 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1993 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1994 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1995 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1996 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1997
1998 // Stop hovering.
1999 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2000 TestFixture::mTouchscreen->sendSync();
2001 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2002 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2003 WithButtonState(0))));
2004 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2005 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2006 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2007 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2008
2009 // Release the stylus button.
2010 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2011 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2012 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2013 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2014}
2015
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002016TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002017 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2018 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2019 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002020
2021 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002022 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2023 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2024 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2025 TestFixture::mTouchscreen->sendDown(centerPoint);
2026 TestFixture::mTouchscreen->sendSync();
2027 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002028 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002029 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002030 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002031
2032 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002033 TestFixture::mStylus->pressKey(BTN_STYLUS);
2034 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002035 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002036 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2037 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002038 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002039 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002040 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2041 WithDeviceId(touchscreenId))));
2042 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002043 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002044 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002045 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2046 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002047
Prabir Pradhan124ea442022-10-28 20:27:44 +00002048 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2049 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002050 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002051 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2052 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002053 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002054 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002055 WithDeviceId(touchscreenId))));
2056 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002057 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002058 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002059 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002060
2061 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002062 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2063 TestFixture::mTouchscreen->sendSync();
2064 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002065 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002066 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002067 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002068}
2069
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002070TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002071 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2072 TestFixture::mReader->requestRefreshConfiguration(
2073 InputReaderConfiguration::CHANGE_STYLUS_BUTTON_REPORTING);
2074
2075 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2076 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2077 const auto stylusId = TestFixture::mStylusInfo.getId();
2078
2079 // Start a stylus gesture. By the time this event is processed, the configuration change that
2080 // was requested is guaranteed to be completed.
2081 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2082 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2083 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2084 TestFixture::mTouchscreen->sendDown(centerPoint);
2085 TestFixture::mTouchscreen->sendSync();
2086 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2087 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002088 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002089 WithDeviceId(touchscreenId))));
2090
2091 // Press and release a stylus button. Each change only generates a MOVE motion event.
2092 // Key events are unaffected.
2093 TestFixture::mStylus->pressKey(BTN_STYLUS);
2094 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2095 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2096 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2097 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2098 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002099 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002100 WithDeviceId(touchscreenId))));
2101
2102 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2103 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2104 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2105 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2106 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2107 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002108 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002109 WithDeviceId(touchscreenId))));
2110
2111 // Finish the stylus gesture.
2112 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2113 TestFixture::mTouchscreen->sendSync();
2114 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2115 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002116 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002117 WithDeviceId(touchscreenId))));
2118}
2119
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002120// --- ExternalStylusIntegrationTest ---
2121
2122// Verify the behavior of an external stylus. An external stylus can report pressure or button
2123// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2124// ongoing stylus gesture that is being emitted by the touchscreen.
2125using ExternalStylusIntegrationTest = TouchIntegrationTest;
2126
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002127TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002128 const Point centerPoint = mDevice->getCenterPoint();
2129
2130 // Create an external stylus capable of reporting pressure data that
2131 // should be fused with a touch pointer.
2132 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2133 createUinputDevice<UinputExternalStylusWithPressure>();
2134 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2135 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2136 const auto stylusInfo = findDeviceByName(stylus->getName());
2137 ASSERT_TRUE(stylusInfo);
2138
2139 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2140
2141 const auto touchscreenId = mDeviceInfo.getId();
2142
2143 // Set a pressure value on the stylus. It doesn't generate any events.
2144 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2145 stylus->setPressure(100);
2146 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2147
2148 // Start a finger gesture, and ensure it shows up as stylus gesture
2149 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002150 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002151 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002152 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002153 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002154 mDevice->sendSync();
2155 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2156 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002157 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002158 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002159
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002160 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2161 // event with the updated pressure.
2162 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002163 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2164 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002165 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002166 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002167
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002168 // The external stylus did not generate any events.
2169 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2170 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2171}
2172
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002173TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002174 const Point centerPoint = mDevice->getCenterPoint();
2175
2176 // Create an external stylus capable of reporting pressure data that
2177 // should be fused with a touch pointer.
2178 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2179 createUinputDevice<UinputExternalStylusWithPressure>();
2180 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2181 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2182 const auto stylusInfo = findDeviceByName(stylus->getName());
2183 ASSERT_TRUE(stylusInfo);
2184
2185 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2186
2187 const auto touchscreenId = mDeviceInfo.getId();
2188
2189 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2190 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002191 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2192 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002193 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002194 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002195
2196 // Start a finger gesture. The touch device will withhold generating any touches for
2197 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2198 mDevice->sendSlot(FIRST_SLOT);
2199 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2200 mDevice->sendToolType(MT_TOOL_FINGER);
2201 mDevice->sendDown(centerPoint);
2202 auto waitUntil = std::chrono::system_clock::now() +
2203 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002204 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002205 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002206
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002207 // Since the external stylus did not report a pressure value within the timeout,
2208 // it shows up as a finger pointer.
2209 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2210 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002211 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002212 WithPressure(1.f))));
2213
2214 // Change the pressure on the external stylus. Since the pressure was not present at the start
2215 // of the gesture, it is ignored for now.
2216 stylus->setPressure(200);
2217 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2218
2219 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002220 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2221 mDevice->sendSync();
2222 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2223 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002224 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002225
2226 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2227 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2228 mDevice->sendToolType(MT_TOOL_FINGER);
2229 mDevice->sendDown(centerPoint);
2230 mDevice->sendSync();
2231 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2232 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002233 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002234 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2235
2236 // The external stylus did not generate any events.
2237 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2238 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002239}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002240
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002241TEST_F(ExternalStylusIntegrationTest, DISABLED_UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002242 const Point centerPoint = mDevice->getCenterPoint();
2243
2244 // Create an external stylus device that does not support pressure. It should not affect any
2245 // touch pointers.
2246 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2247 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2248 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2249 const auto stylusInfo = findDeviceByName(stylus->getName());
2250 ASSERT_TRUE(stylusInfo);
2251
2252 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2253
2254 const auto touchscreenId = mDeviceInfo.getId();
2255
2256 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2257 // pressure data from the external stylus.
2258 mDevice->sendSlot(FIRST_SLOT);
2259 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2260 mDevice->sendToolType(MT_TOOL_FINGER);
2261 mDevice->sendDown(centerPoint);
2262 auto waitUntil = std::chrono::system_clock::now() +
2263 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2264 mDevice->sendSync();
2265 ASSERT_NO_FATAL_FAILURE(
2266 mTestListener
2267 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2268 WithToolType(
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002269 ToolType::FINGER),
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002270 WithButtonState(0),
2271 WithDeviceId(touchscreenId),
2272 WithPressure(1.f)),
2273 waitUntil));
2274
2275 // The external stylus did not generate any events.
2276 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2277 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2278}
2279
Michael Wrightd02c5b62014-02-10 15:10:22 -08002280// --- InputDeviceTest ---
2281class InputDeviceTest : public testing::Test {
2282protected:
2283 static const char* DEVICE_NAME;
2284 static const char* DEVICE_LOCATION;
2285 static const int32_t DEVICE_ID;
2286 static const int32_t DEVICE_GENERATION;
2287 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002288 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002289 static const int32_t EVENTHUB_ID;
2290 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2291
2292 std::shared_ptr<FakeEventHub> mFakeEventHub;
2293 sp<FakeInputReaderPolicy> mFakePolicy;
2294 std::unique_ptr<TestInputListener> mFakeListener;
2295 std::unique_ptr<InstrumentedInputReader> mReader;
2296 std::shared_ptr<InputDevice> mDevice;
2297
2298 void SetUp() override {
2299 mFakeEventHub = std::make_unique<FakeEventHub>();
2300 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2301 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002302 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002303 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002304 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002305 identifier.name = DEVICE_NAME;
2306 identifier.location = DEVICE_LOCATION;
2307 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2308 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2309 identifier);
2310 mReader->pushNextDevice(mDevice);
2311 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002312 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002313 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002314
2315 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002316 mFakeListener.reset();
2317 mFakePolicy.clear();
2318 }
2319};
2320
2321const char* InputDeviceTest::DEVICE_NAME = "device";
2322const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2323const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2324const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002325const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002326const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2327 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002328const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002329const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2330
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002331TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002332 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002333 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2334 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002335}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002336
Michael Wrightd02c5b62014-02-10 15:10:22 -08002337TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2338 ASSERT_EQ(mDevice->isEnabled(), false);
2339}
2340
2341TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2342 // Configuration.
2343 InputReaderConfiguration config;
Arpit Singhed6c3de2023-04-05 19:24:37 +00002344 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002345
2346 // Reset.
2347 unused += mDevice->reset(ARBITRARY_TIME);
2348
2349 NotifyDeviceResetArgs resetArgs;
2350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2351 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2352 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2353
2354 // Metadata.
2355 ASSERT_TRUE(mDevice->isIgnored());
2356 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2357
2358 InputDeviceInfo info = mDevice->getDeviceInfo();
2359 ASSERT_EQ(DEVICE_ID, info.getId());
2360 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2361 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2362 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2363
2364 // State queries.
2365 ASSERT_EQ(0, mDevice->getMetaState());
2366
2367 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2368 << "Ignored device should return unknown key code state.";
2369 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2370 << "Ignored device should return unknown scan code state.";
2371 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2372 << "Ignored device should return unknown switch state.";
2373
2374 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2375 uint8_t flags[2] = { 0, 1 };
2376 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2377 << "Ignored device should never mark any key codes.";
2378 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2379 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2380}
2381
2382TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2383 // Configuration.
2384 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2385
2386 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002387 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2388 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002389 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2390 mapper1.setMetaState(AMETA_ALT_ON);
2391 mapper1.addSupportedKeyCode(AKEYCODE_A);
2392 mapper1.addSupportedKeyCode(AKEYCODE_B);
2393 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2394 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2395 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2396 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2397 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2398
2399 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002400 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2401 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002402 mapper2.setMetaState(AMETA_SHIFT_ON);
2403
2404 InputReaderConfiguration config;
Arpit Singhed6c3de2023-04-05 19:24:37 +00002405 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002406
Harry Cuttsf13161a2023-03-08 14:15:49 +00002407 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2408 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002409 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002410 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002411
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002412 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2413 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002414
2415 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002416 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002417 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2418 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002419
2420 NotifyDeviceResetArgs resetArgs;
2421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2422 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2423 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2424
2425 // Metadata.
2426 ASSERT_FALSE(mDevice->isIgnored());
2427 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2428
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002429 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002431 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002432 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2433 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2434
2435 // State queries.
2436 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2437 << "Should query mappers and combine meta states.";
2438
2439 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2440 << "Should return unknown key code state when source not supported.";
2441 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2442 << "Should return unknown scan code state when source not supported.";
2443 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2444 << "Should return unknown switch state when source not supported.";
2445
2446 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2447 << "Should query mapper when source is supported.";
2448 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2449 << "Should query mapper when source is supported.";
2450 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2451 << "Should query mapper when source is supported.";
2452
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002453 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002454 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002455 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002456 << "Should do nothing when source is unsupported.";
2457 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2458 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2459 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2460 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2461
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002462 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002463 << "Should query mapper when source is supported.";
2464 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2465 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2466 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2467 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2468
2469 // Event handling.
2470 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002471 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002472 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002473
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002474 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2475 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002476}
2477
Arthur Hung2c9a3342019-07-23 14:18:59 +08002478// A single input device is associated with a specific display. Check that:
2479// 1. Device is disabled if the viewport corresponding to the associated display is not found
2480// 2. Device is disabled when setEnabled API is called
2481TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002482 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2483 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002484
2485 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002486 std::list<NotifyArgs> unused =
2487 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002488
2489 // Device should be enabled by default.
2490 ASSERT_TRUE(mDevice->isEnabled());
2491
2492 // Prepare associated info.
2493 constexpr uint8_t hdmi = 1;
2494 const std::string UNIQUE_ID = "local:1";
2495
2496 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002497 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2498 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002499 // Device should be disabled because it is associated with a specific display via
2500 // input port <-> display port association, but the corresponding display is not found
2501 ASSERT_FALSE(mDevice->isEnabled());
2502
2503 // Prepare displays.
2504 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002505 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002506 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002507 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2508 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002509 ASSERT_TRUE(mDevice->isEnabled());
2510
2511 // Device should be disabled after set disable.
2512 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002513 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2514 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002515 ASSERT_FALSE(mDevice->isEnabled());
2516
2517 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002518 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2519 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002520 ASSERT_FALSE(mDevice->isEnabled());
2521}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002522
Christine Franks1ba71cc2021-04-07 14:37:42 -07002523TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2524 // Device should be enabled by default.
2525 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002526 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2527 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002528 std::list<NotifyArgs> unused =
2529 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002530 ASSERT_TRUE(mDevice->isEnabled());
2531
2532 // Device should be disabled because it is associated with a specific display, but the
2533 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002534 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002535 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2536 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002537 ASSERT_FALSE(mDevice->isEnabled());
2538
2539 // Device should be enabled when a display is found.
2540 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002541 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002542 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002543 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2544 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002545 ASSERT_TRUE(mDevice->isEnabled());
2546
2547 // Device should be disabled after set disable.
2548 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002549 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2550 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002551 ASSERT_FALSE(mDevice->isEnabled());
2552
2553 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002554 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2555 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002556 ASSERT_FALSE(mDevice->isEnabled());
2557}
2558
Christine Franks2a2293c2022-01-18 11:51:16 -08002559TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2560 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002561 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2562 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002563 std::list<NotifyArgs> unused =
2564 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08002565
Christine Franks2a2293c2022-01-18 11:51:16 -08002566 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2567 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002568 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002569 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002570 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2571 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002572 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2573}
2574
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002575/**
2576 * This test reproduces a crash caused by a dangling reference that remains after device is added
2577 * and removed. The reference is accessed in InputDevice::dump(..);
2578 */
2579TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2580 constexpr int32_t TEST_EVENTHUB_ID = 10;
2581 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2582
Harry Cutts33476232023-01-30 19:57:29 +00002583 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh8e6fb252023-04-06 11:49:17 +00002584 device.addEventHubDevice(TEST_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002585 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2586 std::string dumpStr, eventHubDevStr;
2587 device.dump(dumpStr, eventHubDevStr);
2588}
2589
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002590TEST_F(InputDeviceTest, GetBluetoothAddress) {
2591 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2592 ASSERT_TRUE(address);
2593 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2594}
2595
Michael Wrightd02c5b62014-02-10 15:10:22 -08002596// --- SwitchInputMapperTest ---
2597
2598class SwitchInputMapperTest : public InputMapperTest {
2599protected:
2600};
2601
2602TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002603 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002604
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002605 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002606}
2607
2608TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002609 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002610
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002611 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002612 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002613
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002614 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002615 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616}
2617
2618TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002619 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002620 std::list<NotifyArgs> out;
2621 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2622 ASSERT_TRUE(out.empty());
2623 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2624 ASSERT_TRUE(out.empty());
2625 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2626 ASSERT_TRUE(out.empty());
2627 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002628
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002629 ASSERT_EQ(1u, out.size());
2630 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002631 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002632 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2633 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002634 args.switchMask);
2635 ASSERT_EQ(uint32_t(0), args.policyFlags);
2636}
2637
Chris Ye87143712020-11-10 05:05:58 +00002638// --- VibratorInputMapperTest ---
2639class VibratorInputMapperTest : public InputMapperTest {
2640protected:
2641 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2642};
2643
2644TEST_F(VibratorInputMapperTest, GetSources) {
2645 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2646
2647 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2648}
2649
2650TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2651 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2652
2653 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2654}
2655
2656TEST_F(VibratorInputMapperTest, Vibrate) {
2657 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002658 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002659 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2660
2661 VibrationElement pattern(2);
2662 VibrationSequence sequence(2);
2663 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002664 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2665 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002666 sequence.addElement(pattern);
2667 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002668 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2669 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002670 sequence.addElement(pattern);
2671
2672 std::vector<int64_t> timings = {0, 1};
2673 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2674
2675 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002676 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002677 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002678 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002679 // Verify vibrator state listener was notified.
2680 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002681 ASSERT_EQ(1u, out.size());
2682 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2683 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2684 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002685 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002686 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002687 ASSERT_FALSE(mapper.isVibrating());
2688 // Verify vibrator state listener was notified.
2689 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002690 ASSERT_EQ(1u, out.size());
2691 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2692 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2693 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002694}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002695
Chris Yef59a2f42020-10-16 12:55:26 -07002696// --- SensorInputMapperTest ---
2697
2698class SensorInputMapperTest : public InputMapperTest {
2699protected:
2700 static const int32_t ACCEL_RAW_MIN;
2701 static const int32_t ACCEL_RAW_MAX;
2702 static const int32_t ACCEL_RAW_FUZZ;
2703 static const int32_t ACCEL_RAW_FLAT;
2704 static const int32_t ACCEL_RAW_RESOLUTION;
2705
2706 static const int32_t GYRO_RAW_MIN;
2707 static const int32_t GYRO_RAW_MAX;
2708 static const int32_t GYRO_RAW_FUZZ;
2709 static const int32_t GYRO_RAW_FLAT;
2710 static const int32_t GYRO_RAW_RESOLUTION;
2711
2712 static const float GRAVITY_MS2_UNIT;
2713 static const float DEGREE_RADIAN_UNIT;
2714
2715 void prepareAccelAxes();
2716 void prepareGyroAxes();
2717 void setAccelProperties();
2718 void setGyroProperties();
2719 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2720};
2721
2722const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2723const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2724const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2725const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2726const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2727
2728const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2729const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2730const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2731const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2732const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2733
2734const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2735const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2736
2737void SensorInputMapperTest::prepareAccelAxes() {
2738 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2739 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2740 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2741 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2742 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2743 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2744}
2745
2746void SensorInputMapperTest::prepareGyroAxes() {
2747 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2748 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2749 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2750 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2751 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2752 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2753}
2754
2755void SensorInputMapperTest::setAccelProperties() {
2756 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2757 /* sensorDataIndex */ 0);
2758 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2759 /* sensorDataIndex */ 1);
2760 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2761 /* sensorDataIndex */ 2);
2762 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2763 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2764 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2765 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2766 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2767}
2768
2769void SensorInputMapperTest::setGyroProperties() {
2770 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2771 /* sensorDataIndex */ 0);
2772 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2773 /* sensorDataIndex */ 1);
2774 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2775 /* sensorDataIndex */ 2);
2776 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2777 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2778 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2779 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2780 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2781}
2782
2783TEST_F(SensorInputMapperTest, GetSources) {
2784 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2785
2786 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2787}
2788
2789TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2790 setAccelProperties();
2791 prepareAccelAxes();
2792 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2793
2794 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2795 std::chrono::microseconds(10000),
2796 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002797 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002798 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002803
2804 NotifySensorArgs args;
2805 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2806 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2807 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2808
2809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2810 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2811 ASSERT_EQ(args.deviceId, DEVICE_ID);
2812 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2813 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2814 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2815 ASSERT_EQ(args.values, values);
2816 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2817}
2818
2819TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2820 setGyroProperties();
2821 prepareGyroAxes();
2822 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2823
2824 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2825 std::chrono::microseconds(10000),
2826 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002827 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002833
2834 NotifySensorArgs args;
2835 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2836 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2837 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2838
2839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2840 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2841 ASSERT_EQ(args.deviceId, DEVICE_ID);
2842 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2843 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2844 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2845 ASSERT_EQ(args.values, values);
2846 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2847}
2848
Michael Wrightd02c5b62014-02-10 15:10:22 -08002849// --- KeyboardInputMapperTest ---
2850
2851class KeyboardInputMapperTest : public InputMapperTest {
2852protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002853 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002854 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002855 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002856
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002857 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002858 int32_t originalKeyCode, int32_t rotatedKeyCode,
2859 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002860};
2861
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002862/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2863 * orientation.
2864 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002865void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002866 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2867 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002868}
2869
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002870void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002871 int32_t originalScanCode, int32_t originalKeyCode,
2872 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002873 NotifyKeyArgs args;
2874
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002875 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2877 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2878 ASSERT_EQ(originalScanCode, args.scanCode);
2879 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002880 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002881
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002882 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2884 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2885 ASSERT_EQ(originalScanCode, args.scanCode);
2886 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002887 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002888}
2889
Michael Wrightd02c5b62014-02-10 15:10:22 -08002890TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002891 KeyboardInputMapper& mapper =
2892 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2893 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002894
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002895 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002896}
2897
2898TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2899 const int32_t USAGE_A = 0x070004;
2900 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002901 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2902 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002903 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2904 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2905 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002906
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002907 KeyboardInputMapper& mapper =
2908 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2909 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002910 // Initial metastate is AMETA_NONE.
2911 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002912
2913 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002914 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002915 NotifyKeyArgs args;
2916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2917 ASSERT_EQ(DEVICE_ID, args.deviceId);
2918 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2919 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2920 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2921 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2922 ASSERT_EQ(KEY_HOME, args.scanCode);
2923 ASSERT_EQ(AMETA_NONE, args.metaState);
2924 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2925 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2926 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2927
2928 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002929 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2931 ASSERT_EQ(DEVICE_ID, args.deviceId);
2932 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2933 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2934 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2935 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2936 ASSERT_EQ(KEY_HOME, args.scanCode);
2937 ASSERT_EQ(AMETA_NONE, args.metaState);
2938 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2939 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2940 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2941
2942 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2944 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2946 ASSERT_EQ(DEVICE_ID, args.deviceId);
2947 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2948 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2949 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2950 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2951 ASSERT_EQ(0, args.scanCode);
2952 ASSERT_EQ(AMETA_NONE, args.metaState);
2953 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2954 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2955 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2956
2957 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002958 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2959 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2961 ASSERT_EQ(DEVICE_ID, args.deviceId);
2962 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2963 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2964 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2965 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2966 ASSERT_EQ(0, args.scanCode);
2967 ASSERT_EQ(AMETA_NONE, args.metaState);
2968 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2969 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2970 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2971
2972 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2974 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2976 ASSERT_EQ(DEVICE_ID, args.deviceId);
2977 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2978 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2979 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2980 ASSERT_EQ(0, args.keyCode);
2981 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2982 ASSERT_EQ(AMETA_NONE, args.metaState);
2983 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2984 ASSERT_EQ(0U, args.policyFlags);
2985 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2986
2987 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002988 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2989 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2991 ASSERT_EQ(DEVICE_ID, args.deviceId);
2992 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2993 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2994 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2995 ASSERT_EQ(0, args.keyCode);
2996 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2997 ASSERT_EQ(AMETA_NONE, args.metaState);
2998 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2999 ASSERT_EQ(0U, args.policyFlags);
3000 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3001}
3002
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003003TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3004 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3005 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3006 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3007
3008 KeyboardInputMapper& mapper =
3009 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3010 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3011
3012 // Key down by scan code.
3013 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3014 NotifyKeyArgs args;
3015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3016 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3017
3018 // Key up by scan code.
3019 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3021 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3022}
3023
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003024/**
3025 * Ensure that the readTime is set to the time when the EV_KEY is received.
3026 */
3027TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3028 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3029
3030 KeyboardInputMapper& mapper =
3031 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3032 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3033 NotifyKeyArgs args;
3034
3035 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003036 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3038 ASSERT_EQ(12, args.readTime);
3039
3040 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003041 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3043 ASSERT_EQ(15, args.readTime);
3044}
3045
Michael Wrightd02c5b62014-02-10 15:10:22 -08003046TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003047 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3048 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003049 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3050 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3051 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003052
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003053 KeyboardInputMapper& mapper =
3054 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3055 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003056
Arthur Hung95f68612022-04-07 14:08:22 +08003057 // Initial metastate is AMETA_NONE.
3058 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059
3060 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003062 NotifyKeyArgs args;
3063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3064 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003065 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003066 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067
3068 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003069 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3071 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003072 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073
3074 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003075 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3077 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003078 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079
3080 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003081 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3083 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003084 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003085 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086}
3087
3088TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003089 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3090 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3091 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3092 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003093
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003094 KeyboardInputMapper& mapper =
3095 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3096 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003097
Michael Wrighta9cf4192022-12-01 23:46:39 +00003098 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3100 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3101 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3102 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3103 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3104 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3105 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3106 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3107}
3108
3109TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003110 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3111 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3112 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3113 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003114
Michael Wrightd02c5b62014-02-10 15:10:22 -08003115 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003116 KeyboardInputMapper& mapper =
3117 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3118 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003119
Michael Wrighta9cf4192022-12-01 23:46:39 +00003120 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003121 ASSERT_NO_FATAL_FAILURE(
3122 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3123 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3124 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3125 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3126 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3127 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3128 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003129
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003130 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003131 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003132 ASSERT_NO_FATAL_FAILURE(
3133 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3134 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3135 AKEYCODE_DPAD_UP, DISPLAY_ID));
3136 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3137 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3138 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3139 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003140
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003141 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003142 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003143 ASSERT_NO_FATAL_FAILURE(
3144 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3145 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3146 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3147 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3148 AKEYCODE_DPAD_UP, DISPLAY_ID));
3149 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3150 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003151
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003152 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003153 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003154 ASSERT_NO_FATAL_FAILURE(
3155 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3156 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3157 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3158 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3159 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3160 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3161 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003162
3163 // Special case: if orientation changes while key is down, we still emit the same keycode
3164 // in the key up as we did in the key down.
3165 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003166 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003167 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3170 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3171 ASSERT_EQ(KEY_UP, args.scanCode);
3172 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3173
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003174 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003175 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3178 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3179 ASSERT_EQ(KEY_UP, args.scanCode);
3180 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3181}
3182
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003183TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3184 // If the keyboard is not orientation aware,
3185 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003186 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003187
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003188 KeyboardInputMapper& mapper =
3189 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3190 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003191 NotifyKeyArgs args;
3192
3193 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003194 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003196 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3198 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3199
Michael Wrighta9cf4192022-12-01 23:46:39 +00003200 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003203 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3205 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3206}
3207
3208TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3209 // If the keyboard is orientation aware,
3210 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003211 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003212
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003213 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003214 KeyboardInputMapper& mapper =
3215 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3216 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003217 NotifyKeyArgs args;
3218
3219 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3220 // ^--- already checked by the previous test
3221
Michael Wrighta9cf4192022-12-01 23:46:39 +00003222 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003223 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003224 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3228 ASSERT_EQ(DISPLAY_ID, args.displayId);
3229
3230 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003231 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003232 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003233 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3238 ASSERT_EQ(newDisplayId, args.displayId);
3239}
3240
Michael Wrightd02c5b62014-02-10 15:10:22 -08003241TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003242 KeyboardInputMapper& mapper =
3243 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3244 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003245
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003246 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003247 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003249 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003250 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251}
3252
Philip Junker4af3b3d2021-12-14 10:36:55 +01003253TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3254 KeyboardInputMapper& mapper =
3255 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3256 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3257
3258 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3259 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3260 << "If a mapping is available, the result is equal to the mapping";
3261
3262 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3263 << "If no mapping is available, the result is the key location";
3264}
3265
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003267 KeyboardInputMapper& mapper =
3268 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3269 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003270
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003271 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003272 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003274 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003275 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276}
3277
3278TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003279 KeyboardInputMapper& mapper =
3280 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3281 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003283 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003284
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003286 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003287 ASSERT_TRUE(flags[0]);
3288 ASSERT_FALSE(flags[1]);
3289}
3290
3291TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003292 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3293 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3294 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3295 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3296 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3297 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003298
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003299 KeyboardInputMapper& mapper =
3300 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3301 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003302 // Initial metastate is AMETA_NONE.
3303 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003304
3305 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003306 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3307 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3308 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003309
3310 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3312 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003313 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3314 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3315 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003316 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003317
3318 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003319 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3320 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003321 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3322 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3323 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003324 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325
3326 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003327 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3328 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003329 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3330 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3331 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003332 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333
3334 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003337 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3338 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3339 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003340 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003341
3342 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003343 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003345 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3346 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3347 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003348 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003349
3350 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3352 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003353 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3354 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3355 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003356 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003357}
3358
Chris Yea52ade12020-08-27 16:49:20 -07003359TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3360 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3361 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3362 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3363 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3364
3365 KeyboardInputMapper& mapper =
3366 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3367 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3368
Chris Yea52ade12020-08-27 16:49:20 -07003369 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003370 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003371 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3372 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3373 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3374 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3375
3376 NotifyKeyArgs args;
3377 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3380 ASSERT_EQ(AMETA_NONE, args.metaState);
3381 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3382 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3383 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3384
3385 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003386 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3388 ASSERT_EQ(AMETA_NONE, args.metaState);
3389 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3390 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3391 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3392}
3393
Arthur Hung2c9a3342019-07-23 14:18:59 +08003394TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3395 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003396 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3397 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3398 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3399 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003400
3401 // keyboard 2.
3402 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003403 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003404 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003405 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003406 std::shared_ptr<InputDevice> device2 =
3407 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003408 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003409
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003410 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3411 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3412 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3413 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003414
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003415 KeyboardInputMapper& mapper =
3416 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3417 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003418
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003419 KeyboardInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00003420 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3421 mFakePolicy->getReaderConfiguration(),
3422 AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003423 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003424 std::list<NotifyArgs> unused =
3425 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003426 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003427 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003428
3429 // Prepared displays and associated info.
3430 constexpr uint8_t hdmi1 = 0;
3431 constexpr uint8_t hdmi2 = 1;
3432 const std::string SECONDARY_UNIQUE_ID = "local:1";
3433
3434 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3435 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3436
3437 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003438 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3439 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003440 ASSERT_FALSE(device2->isEnabled());
3441
3442 // Prepare second display.
3443 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003444 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003445 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003446 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003447 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003448 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003449 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3450 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003451
3452 // Device should be enabled after the associated display is found.
3453 ASSERT_TRUE(mDevice->isEnabled());
3454 ASSERT_TRUE(device2->isEnabled());
3455
3456 // Test pad key events
3457 ASSERT_NO_FATAL_FAILURE(
3458 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3459 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3460 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3461 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3462 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3463 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3464 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3465
3466 ASSERT_NO_FATAL_FAILURE(
3467 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3468 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3469 AKEYCODE_DPAD_RIGHT, newDisplayId));
3470 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3471 AKEYCODE_DPAD_DOWN, newDisplayId));
3472 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3473 AKEYCODE_DPAD_LEFT, newDisplayId));
3474}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003475
arthurhungc903df12020-08-11 15:08:42 +08003476TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3477 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3478 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3479 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3480 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3481 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3482 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3483
3484 KeyboardInputMapper& mapper =
3485 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3486 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003487 // Initial metastate is AMETA_NONE.
3488 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003489
3490 // Initialization should have turned all of the lights off.
3491 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3492 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3493 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3494
3495 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3497 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003498 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3499 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3500
3501 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3503 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003504 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3505 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3506
3507 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003510 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3511 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3512
3513 mFakeEventHub->removeDevice(EVENTHUB_ID);
3514 mReader->loopOnce();
3515
3516 // keyboard 2 should default toggle keys.
3517 const std::string USB2 = "USB2";
3518 const std::string DEVICE_NAME2 = "KEYBOARD2";
3519 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3520 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3521 std::shared_ptr<InputDevice> device2 =
3522 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003523 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003524 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3525 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3526 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3527 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3528 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3529 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3530
arthurhung6fe95782020-10-05 22:41:16 +08003531 KeyboardInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00003532 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3533 mFakePolicy->getReaderConfiguration(),
3534 AINPUT_SOURCE_KEYBOARD,
arthurhung6fe95782020-10-05 22:41:16 +08003535 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003536 std::list<NotifyArgs> unused =
3537 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003538 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003539 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003540
3541 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3542 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3543 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003544 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3545 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003546}
3547
Arthur Hungcb40a002021-08-03 14:31:01 +00003548TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3549 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3550 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3551 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3552
3553 // Suppose we have two mappers. (DPAD + KEYBOARD)
3554 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3555 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3556 KeyboardInputMapper& mapper =
3557 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3558 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003559 // Initial metastate is AMETA_NONE.
3560 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003561
3562 mReader->toggleCapsLockState(DEVICE_ID);
3563 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3564}
3565
Arthur Hungfb3cc112022-04-13 07:39:50 +00003566TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3567 // keyboard 1.
3568 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3569 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3570 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3571 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3572 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3573 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3574
3575 KeyboardInputMapper& mapper1 =
3576 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3577 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3578
3579 // keyboard 2.
3580 const std::string USB2 = "USB2";
3581 const std::string DEVICE_NAME2 = "KEYBOARD2";
3582 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3583 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3584 std::shared_ptr<InputDevice> device2 =
3585 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3586 ftl::Flags<InputDeviceClass>(0));
3587 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3588 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3589 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3590 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3591 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3592 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3593
3594 KeyboardInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00003595 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3596 mFakePolicy->getReaderConfiguration(),
3597 AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003598 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003599 std::list<NotifyArgs> unused =
3600 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003601 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003602 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003603
Arthur Hung95f68612022-04-07 14:08:22 +08003604 // Initial metastate is AMETA_NONE.
3605 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3606 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3607
3608 // Toggle num lock on and off.
3609 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3610 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003611 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3612 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3613 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3614
3615 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3616 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3617 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3618 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3619 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3620
3621 // Toggle caps lock on and off.
3622 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3623 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3624 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3625 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3626 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3627
3628 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3629 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3630 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3631 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3632 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3633
3634 // Toggle scroll lock on and off.
3635 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3636 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3637 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3638 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3639 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3640
3641 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3642 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3643 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3644 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3645 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3646}
3647
Arthur Hung2141d542022-08-23 07:45:21 +00003648TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3649 const int32_t USAGE_A = 0x070004;
3650 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3651 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3652
3653 KeyboardInputMapper& mapper =
3654 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3655 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3656 // Key down by scan code.
3657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3658 NotifyKeyArgs args;
3659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3660 ASSERT_EQ(DEVICE_ID, args.deviceId);
3661 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3662 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3663 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3664 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3665 ASSERT_EQ(KEY_HOME, args.scanCode);
3666 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3667
3668 // Disable device, it should synthesize cancellation events for down events.
3669 mFakePolicy->addDisabledDevice(DEVICE_ID);
3670 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
3671
3672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3673 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3674 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3675 ASSERT_EQ(KEY_HOME, args.scanCode);
3676 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3677}
3678
Zixuan Qufecb6062022-11-12 04:44:31 +00003679TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00003680 mDevice->addMapper<KeyboardInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
3681 AINPUT_SOURCE_KEYBOARD,
Zixuan Qufecb6062022-11-12 04:44:31 +00003682 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3683 std::list<NotifyArgs> unused =
3684 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
3685
3686 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3687
3688 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3689 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUT_ASSOCIATION);
3690
3691 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3692 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3693 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3694 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3695 deviceInfo.getKeyboardLayoutInfo()->layoutType);
3696}
3697
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003698TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3699 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3700 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3701
3702 // Configuration
3703 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3704 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3705 InputReaderConfiguration config;
Arpit Singhed6c3de2023-04-05 19:24:37 +00003706 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, 0);
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003707
3708 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3709 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3710}
3711
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003712// --- KeyboardInputMapperTest_ExternalDevice ---
3713
3714class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3715protected:
Chris Yea52ade12020-08-27 16:49:20 -07003716 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003717};
3718
3719TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003720 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3721 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003722
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003723 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3724 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3725 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3726 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003727
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003728 KeyboardInputMapper& mapper =
3729 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3730 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003731
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003732 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003733 NotifyKeyArgs args;
3734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3735 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3736
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003737 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3739 ASSERT_EQ(uint32_t(0), args.policyFlags);
3740
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003741 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003743 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003744
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003745 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3747 ASSERT_EQ(uint32_t(0), args.policyFlags);
3748
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003749 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3751 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3752
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003753 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3755 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3756}
3757
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003758TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003759 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003760
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003761 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3762 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3763 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003764
Powei Fengd041c5d2019-05-03 17:11:33 -07003765 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003766 KeyboardInputMapper& mapper =
3767 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3768 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003769
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003771 NotifyKeyArgs args;
3772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3773 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3774
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003775 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3777 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3778
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3781 ASSERT_EQ(uint32_t(0), args.policyFlags);
3782
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003783 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3785 ASSERT_EQ(uint32_t(0), args.policyFlags);
3786
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3789 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3790
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003791 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3793 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3794}
3795
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796// --- CursorInputMapperTest ---
3797
3798class CursorInputMapperTest : public InputMapperTest {
3799protected:
3800 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3801
Michael Wright17db18e2020-06-26 20:51:44 +01003802 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003803
Chris Yea52ade12020-08-27 16:49:20 -07003804 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003805 InputMapperTest::SetUp();
3806
Michael Wright17db18e2020-06-26 20:51:44 +01003807 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003808 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003809 }
3810
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003811 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3812 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003813
Michael Wrighta9cf4192022-12-01 23:46:39 +00003814 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003815 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3816 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3817 }
3818
3819 void prepareSecondaryDisplay() {
3820 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003821 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003822 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003823 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003824
3825 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3826 float pressure) {
3827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3828 0.0f, 0.0f, 0.0f, EPSILON));
3829 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003830};
3831
3832const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3833
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003834void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3835 int32_t originalY, int32_t rotatedX,
3836 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003837 NotifyMotionArgs args;
3838
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3841 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3843 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003844 ASSERT_NO_FATAL_FAILURE(
3845 assertCursorPointerCoords(args.pointerCoords[0],
3846 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3847 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003848}
3849
3850TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003851 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003852 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003853
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003854 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003855}
3856
3857TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003859 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003860
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003861 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003862}
3863
3864TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003865 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003866 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003867
3868 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003869 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003870
3871 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003872 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3873 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003874 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3875 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3876
3877 // When the bounds are set, then there should be a valid motion range.
3878 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3879
3880 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003881 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003882
3883 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3884 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3885 1, 800 - 1, 0.0f, 0.0f));
3886 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3887 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3888 2, 480 - 1, 0.0f, 0.0f));
3889 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3890 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3891 0.0f, 1.0f, 0.0f, 0.0f));
3892}
3893
3894TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003895 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003896 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003897
3898 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003899 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003900
3901 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3902 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3903 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3904 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3905 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3906 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3907 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3908 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3909 0.0f, 1.0f, 0.0f, 0.0f));
3910}
3911
3912TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003914 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003915
arthurhungdcef2dc2020-08-11 14:47:50 +08003916 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003917
3918 NotifyMotionArgs args;
3919
3920 // Button press.
3921 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003922 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3923 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3925 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3926 ASSERT_EQ(DEVICE_ID, args.deviceId);
3927 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3928 ASSERT_EQ(uint32_t(0), args.policyFlags);
3929 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3930 ASSERT_EQ(0, args.flags);
3931 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3932 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3933 ASSERT_EQ(0, args.edgeFlags);
3934 ASSERT_EQ(uint32_t(1), args.pointerCount);
3935 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003936 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003937 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003938 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3939 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3940 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3941
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3943 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3944 ASSERT_EQ(DEVICE_ID, args.deviceId);
3945 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3946 ASSERT_EQ(uint32_t(0), args.policyFlags);
3947 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3948 ASSERT_EQ(0, args.flags);
3949 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3950 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3951 ASSERT_EQ(0, args.edgeFlags);
3952 ASSERT_EQ(uint32_t(1), args.pointerCount);
3953 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003954 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003955 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003956 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3957 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3958 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3959
Michael Wrightd02c5b62014-02-10 15:10:22 -08003960 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003961 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3962 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3964 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3965 ASSERT_EQ(DEVICE_ID, args.deviceId);
3966 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3967 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003968 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3969 ASSERT_EQ(0, args.flags);
3970 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3971 ASSERT_EQ(0, args.buttonState);
3972 ASSERT_EQ(0, args.edgeFlags);
3973 ASSERT_EQ(uint32_t(1), args.pointerCount);
3974 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003975 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003976 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003977 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3978 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3979 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3980
3981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3982 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3983 ASSERT_EQ(DEVICE_ID, args.deviceId);
3984 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3985 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003986 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3987 ASSERT_EQ(0, args.flags);
3988 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3989 ASSERT_EQ(0, args.buttonState);
3990 ASSERT_EQ(0, args.edgeFlags);
3991 ASSERT_EQ(uint32_t(1), args.pointerCount);
3992 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003993 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003994 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003995 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3996 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3997 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3998}
3999
4000TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004001 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004002 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004003
4004 NotifyMotionArgs args;
4005
4006 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004007 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4008 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4010 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004011 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4012 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4013 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004014
4015 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004020 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4021 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004022}
4023
4024TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004025 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004026 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004027
4028 NotifyMotionArgs args;
4029
4030 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4032 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4034 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004035 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004036
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4038 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004039 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004040
Michael Wrightd02c5b62014-02-10 15:10:22 -08004041 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004042 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004045 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004046 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004047
4048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004050 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004051}
4052
4053TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004055 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056
4057 NotifyMotionArgs args;
4058
4059 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004060 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4062 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4063 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4065 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004066 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4067 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4068 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004069
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4071 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004072 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4073 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4074 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004075
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4078 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004082 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4083 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4084 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004085
4086 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004087 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4088 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004090 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004091 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004092
4093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004095 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004096}
4097
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004098TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004099 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004100 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004101 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4102 // need to be rotated.
4103 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004104 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105
Michael Wrighta9cf4192022-12-01 23:46:39 +00004106 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4108 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4109 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4110 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4111 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4112 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4113 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4114 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4115}
4116
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004117TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004118 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004119 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004120 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4121 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004122 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004123
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004124 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004125 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004126 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4127 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4128 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4129 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4130 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4131 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4132 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4133 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4134
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004135 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004136 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004137 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4138 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4139 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4140 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4141 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4142 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4143 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4144 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004145
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004146 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004147 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004148 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4149 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4150 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4151 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4152 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4153 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4154 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4155 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4156
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004157 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004158 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004159 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4160 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4161 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4162 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4163 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4164 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4165 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4166 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004167}
4168
4169TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004171 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172
4173 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4174 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175
4176 NotifyMotionArgs motionArgs;
4177 NotifyKeyArgs keyArgs;
4178
4179 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4181 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4183 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4184 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004185 ASSERT_NO_FATAL_FAILURE(
4186 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4189 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4190 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004191 ASSERT_NO_FATAL_FAILURE(
4192 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004193
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004194 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4195 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004197 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004198 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004199 ASSERT_NO_FATAL_FAILURE(
4200 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201
4202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004203 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004204 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004205 ASSERT_NO_FATAL_FAILURE(
4206 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004207
4208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004209 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004210 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004211 ASSERT_NO_FATAL_FAILURE(
4212 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213
4214 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4219 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4220 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004221 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004222 ASSERT_NO_FATAL_FAILURE(
4223 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004224
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4226 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4227 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004228 ASSERT_NO_FATAL_FAILURE(
4229 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004230
4231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4232 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4233 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004234 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004235 ASSERT_NO_FATAL_FAILURE(
4236 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004237
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004238 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4239 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004241 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004243 ASSERT_NO_FATAL_FAILURE(
4244 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004245
4246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004248 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004249 ASSERT_NO_FATAL_FAILURE(
4250 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004251
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004252 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004255 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4256 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004257 ASSERT_NO_FATAL_FAILURE(
4258 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4260 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004261
4262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004265 ASSERT_NO_FATAL_FAILURE(
4266 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004267
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4269 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004271 ASSERT_NO_FATAL_FAILURE(
4272 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273
4274 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4278 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4279 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004280
Michael Wrightd02c5b62014-02-10 15:10:22 -08004281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004282 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004284 ASSERT_NO_FATAL_FAILURE(
4285 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004286
4287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4288 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4289 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004290 ASSERT_NO_FATAL_FAILURE(
4291 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004292
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004296 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004297 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004298 ASSERT_NO_FATAL_FAILURE(
4299 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004300
4301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004303 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004304
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004305 ASSERT_NO_FATAL_FAILURE(
4306 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4308 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4309 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4310
4311 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004312 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4315 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4316 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004317
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004319 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004321 ASSERT_NO_FATAL_FAILURE(
4322 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004323
4324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4325 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4326 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004327 ASSERT_NO_FATAL_FAILURE(
4328 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004329
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004330 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004333 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004335 ASSERT_NO_FATAL_FAILURE(
4336 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004337
4338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4339 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4340 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004341 ASSERT_NO_FATAL_FAILURE(
4342 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004343
Michael Wrightd02c5b62014-02-10 15:10:22 -08004344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4345 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4346 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4347
4348 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004349 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4350 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4352 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4353 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004354
Michael Wrightd02c5b62014-02-10 15:10:22 -08004355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004356 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004357 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004358 ASSERT_NO_FATAL_FAILURE(
4359 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004360
4361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4362 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4363 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004364 ASSERT_NO_FATAL_FAILURE(
4365 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004370 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004372 ASSERT_NO_FATAL_FAILURE(
4373 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004374
4375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4376 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4377 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004378 ASSERT_NO_FATAL_FAILURE(
4379 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004380
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4382 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4383 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4384
4385 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4387 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4389 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4390 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004391
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004393 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004394 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004395 ASSERT_NO_FATAL_FAILURE(
4396 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004397
4398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4399 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4400 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004401 ASSERT_NO_FATAL_FAILURE(
4402 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004404 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4405 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004407 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004408 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004409 ASSERT_NO_FATAL_FAILURE(
4410 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004411
4412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4413 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4414 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004415 ASSERT_NO_FATAL_FAILURE(
4416 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004417
Michael Wrightd02c5b62014-02-10 15:10:22 -08004418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4419 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4420 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4421}
4422
4423TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004425 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004426
4427 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4428 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429
4430 NotifyMotionArgs args;
4431
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004432 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4433 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4434 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004436 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4437 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4439 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 +00004440 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004441}
4442
4443TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004444 addConfigurationProperty("cursor.mode", "pointer");
4445 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004446 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004447
4448 NotifyDeviceResetArgs resetArgs;
4449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4450 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4451 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4452
4453 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4454 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004455
4456 NotifyMotionArgs args;
4457
4458 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4460 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4461 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4463 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4464 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4466 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 +00004467 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004468
4469 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004470 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4471 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4473 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4474 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4476 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4478 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4479 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4481 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4482
4483 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004484 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4485 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4487 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4488 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4490 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4492 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4493 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4495 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4496
4497 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004498 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4499 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4502 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4503 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4504 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4505 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 +00004506 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004507
4508 // Disable pointer capture and check that the device generation got bumped
4509 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004510 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004511 mFakePolicy->setPointerCapture(false);
4512 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004513 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004514
4515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004516 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4517
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4522 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4525 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 +00004526 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527}
4528
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004529/**
4530 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4531 * pointer acceleration or speed processing should not be applied.
4532 */
4533TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4534 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004535 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4536 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004537 mFakePolicy->setVelocityControlParams(testParams);
4538 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4539
4540 NotifyDeviceResetArgs resetArgs;
4541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4542 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4543 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4544
4545 NotifyMotionArgs args;
4546
4547 // Move and verify scale is applied.
4548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4550 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4552 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4553 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4554 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4555 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4556 ASSERT_GT(relX, 10);
4557 ASSERT_GT(relY, 20);
4558
4559 // Enable Pointer Capture
4560 mFakePolicy->setPointerCapture(true);
4561 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4562 NotifyPointerCaptureChangedArgs captureArgs;
4563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4564 ASSERT_TRUE(captureArgs.request.enable);
4565
4566 // Move and verify scale is not applied.
4567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4571 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4572 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4573 ASSERT_EQ(10, args.pointerCoords[0].getX());
4574 ASSERT_EQ(20, args.pointerCoords[0].getY());
4575}
4576
Prabir Pradhan208360b2022-06-24 18:37:04 +00004577TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4578 addConfigurationProperty("cursor.mode", "pointer");
4579 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4580
4581 NotifyDeviceResetArgs resetArgs;
4582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4583 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4584 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4585
4586 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004587 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004588
4589 NotifyMotionArgs args;
4590
4591 // Verify that the coordinates are rotated.
4592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4594 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4596 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4597 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4598 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4599 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4600
4601 // Enable Pointer Capture.
4602 mFakePolicy->setPointerCapture(true);
4603 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4604 NotifyPointerCaptureChangedArgs captureArgs;
4605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4606 ASSERT_TRUE(captureArgs.request.enable);
4607
4608 // Move and verify rotation is not applied.
4609 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4610 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4611 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4613 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4615 ASSERT_EQ(10, args.pointerCoords[0].getX());
4616 ASSERT_EQ(20, args.pointerCoords[0].getY());
4617}
4618
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004619TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004620 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004621
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004622 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004623 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004624
4625 // Set up the secondary display as the display on which the pointer should be shown.
4626 // The InputDevice is not associated with any display.
4627 prepareSecondaryDisplay();
4628 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08004629 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4630
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004631 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004632 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004633
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004634 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4637 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004639 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4640 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4641 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004642 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004643}
4644
4645TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
4646 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4647
4648 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004649 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004650
4651 // Set up the secondary display as the display on which the pointer should be shown,
4652 // and associate the InputDevice with the secondary display.
4653 prepareSecondaryDisplay();
4654 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4655 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4656 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4657
4658 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4659 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004660
4661 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4662 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004665 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4666 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4667 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004668 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004669}
4670
4671TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
4672 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4673
4674 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004675 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004676 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4677
4678 // Associate the InputDevice with the secondary display.
4679 prepareSecondaryDisplay();
4680 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4681 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4682
4683 // The mapper should not generate any events because it is associated with a display that is
4684 // different from the pointer display.
4685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004689}
4690
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004691// --- BluetoothCursorInputMapperTest ---
4692
4693class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4694protected:
4695 void SetUp() override {
4696 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4697
4698 mFakePointerController = std::make_shared<FakePointerController>();
4699 mFakePolicy->setPointerController(mFakePointerController);
4700 }
4701};
4702
4703TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4704 addConfigurationProperty("cursor.mode", "pointer");
4705 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4706
4707 nsecs_t kernelEventTime = ARBITRARY_TIME;
4708 nsecs_t expectedEventTime = ARBITRARY_TIME;
4709 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4710 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4712 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4713 WithEventTime(expectedEventTime))));
4714
4715 // Process several events that come in quick succession, according to their timestamps.
4716 for (int i = 0; i < 3; i++) {
4717 constexpr static nsecs_t delta = ms2ns(1);
4718 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4719 kernelEventTime += delta;
4720 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4721
4722 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4723 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4725 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4726 WithEventTime(expectedEventTime))));
4727 }
4728}
4729
4730TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4731 addConfigurationProperty("cursor.mode", "pointer");
4732 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4733
4734 nsecs_t expectedEventTime = ARBITRARY_TIME;
4735 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4736 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4738 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4739 WithEventTime(expectedEventTime))));
4740
4741 // Process several events with the same timestamp from the kernel.
4742 // Ensure that we do not generate events too far into the future.
4743 constexpr static int32_t numEvents =
4744 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4745 for (int i = 0; i < numEvents; i++) {
4746 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4747
4748 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4749 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4751 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4752 WithEventTime(expectedEventTime))));
4753 }
4754
4755 // By processing more events with the same timestamp, we should not generate events with a
4756 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4757 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4758 for (int i = 0; i < 3; i++) {
4759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4760 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4762 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4763 WithEventTime(cappedEventTime))));
4764 }
4765}
4766
4767TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4768 addConfigurationProperty("cursor.mode", "pointer");
4769 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4770
4771 nsecs_t kernelEventTime = ARBITRARY_TIME;
4772 nsecs_t expectedEventTime = ARBITRARY_TIME;
4773 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4774 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4776 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4777 WithEventTime(expectedEventTime))));
4778
4779 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4780 // smoothening is not needed, its timestamp is not affected.
4781 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4782 expectedEventTime = kernelEventTime;
4783
4784 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4785 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4787 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4788 WithEventTime(expectedEventTime))));
4789}
4790
Michael Wrightd02c5b62014-02-10 15:10:22 -08004791// --- TouchInputMapperTest ---
4792
4793class TouchInputMapperTest : public InputMapperTest {
4794protected:
4795 static const int32_t RAW_X_MIN;
4796 static const int32_t RAW_X_MAX;
4797 static const int32_t RAW_Y_MIN;
4798 static const int32_t RAW_Y_MAX;
4799 static const int32_t RAW_TOUCH_MIN;
4800 static const int32_t RAW_TOUCH_MAX;
4801 static const int32_t RAW_TOOL_MIN;
4802 static const int32_t RAW_TOOL_MAX;
4803 static const int32_t RAW_PRESSURE_MIN;
4804 static const int32_t RAW_PRESSURE_MAX;
4805 static const int32_t RAW_ORIENTATION_MIN;
4806 static const int32_t RAW_ORIENTATION_MAX;
4807 static const int32_t RAW_DISTANCE_MIN;
4808 static const int32_t RAW_DISTANCE_MAX;
4809 static const int32_t RAW_TILT_MIN;
4810 static const int32_t RAW_TILT_MAX;
4811 static const int32_t RAW_ID_MIN;
4812 static const int32_t RAW_ID_MAX;
4813 static const int32_t RAW_SLOT_MIN;
4814 static const int32_t RAW_SLOT_MAX;
4815 static const float X_PRECISION;
4816 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004817 static const float X_PRECISION_VIRTUAL;
4818 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004819
4820 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004821 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004822
4823 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4824
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004825 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004826 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004827
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828 enum Axes {
4829 POSITION = 1 << 0,
4830 TOUCH = 1 << 1,
4831 TOOL = 1 << 2,
4832 PRESSURE = 1 << 3,
4833 ORIENTATION = 1 << 4,
4834 MINOR = 1 << 5,
4835 ID = 1 << 6,
4836 DISTANCE = 1 << 7,
4837 TILT = 1 << 8,
4838 SLOT = 1 << 9,
4839 TOOL_TYPE = 1 << 10,
4840 };
4841
Michael Wrighta9cf4192022-12-01 23:46:39 +00004842 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004843 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004844 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004845 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004846 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004847 int32_t toRawX(float displayX);
4848 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004849 int32_t toRotatedRawX(float displayX);
4850 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004851 float toCookedX(float rawX, float rawY);
4852 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004854 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004855 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004856 float toDisplayY(int32_t rawY, int32_t displayHeight);
4857
Michael Wrightd02c5b62014-02-10 15:10:22 -08004858};
4859
4860const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4861const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4862const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4863const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4864const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4865const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4866const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4867const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004868const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4869const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004870const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4871const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4872const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4873const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4874const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4875const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4876const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4877const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4878const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4879const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4880const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4881const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004882const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4883 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4884const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4885 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004886const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4887 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004888
4889const float TouchInputMapperTest::GEOMETRIC_SCALE =
4890 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4891 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4892
4893const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4894 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4895 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4896};
4897
Michael Wrighta9cf4192022-12-01 23:46:39 +00004898void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004899 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4900 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004901}
4902
4903void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4904 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004905 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004906}
4907
Michael Wrighta9cf4192022-12-01 23:46:39 +00004908void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004909 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4910 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4911 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004912}
4913
Michael Wrightd02c5b62014-02-10 15:10:22 -08004914void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004915 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4916 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4917 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4918 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919}
4920
Jason Gerecke489fda82012-09-07 17:19:40 -07004921void TouchInputMapperTest::prepareLocationCalibration() {
4922 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4923}
4924
Michael Wrightd02c5b62014-02-10 15:10:22 -08004925int32_t TouchInputMapperTest::toRawX(float displayX) {
4926 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4927}
4928
4929int32_t TouchInputMapperTest::toRawY(float displayY) {
4930 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4931}
4932
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004933int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4934 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4935}
4936
4937int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4938 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4939}
4940
Jason Gerecke489fda82012-09-07 17:19:40 -07004941float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4942 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4943 return rawX;
4944}
4945
4946float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4947 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4948 return rawY;
4949}
4950
Michael Wrightd02c5b62014-02-10 15:10:22 -08004951float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004952 return toDisplayX(rawX, DISPLAY_WIDTH);
4953}
4954
4955float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4956 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004957}
4958
4959float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004960 return toDisplayY(rawY, DISPLAY_HEIGHT);
4961}
4962
4963float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4964 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004965}
4966
4967
4968// --- SingleTouchInputMapperTest ---
4969
4970class SingleTouchInputMapperTest : public TouchInputMapperTest {
4971protected:
4972 void prepareButtons();
4973 void prepareAxes(int axes);
4974
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004975 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4976 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4977 void processUp(SingleTouchInputMapper& mappery);
4978 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4979 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4980 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4981 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4982 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4983 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984};
4985
4986void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004987 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004988}
4989
4990void SingleTouchInputMapperTest::prepareAxes(int axes) {
4991 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004992 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4993 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004994 }
4995 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004996 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4997 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004998 }
4999 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005000 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5001 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002 }
5003 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005004 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5005 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005006 }
5007 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005008 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5009 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005010 }
5011}
5012
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005013void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5015 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005017}
5018
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005019void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005022}
5023
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005024void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005025 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005026}
5027
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005028void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030}
5031
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005032void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5033 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035}
5036
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005037void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005039}
5040
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005041void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5042 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005045}
5046
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005047void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5048 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005049 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005050}
5051
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005052void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005054}
5055
Michael Wrightd02c5b62014-02-10 15:10:22 -08005056TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005057 prepareButtons();
5058 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005059 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060
Josep del Río2d8c79a2023-01-23 19:33:50 +00005061 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062}
5063
Michael Wrightd02c5b62014-02-10 15:10:22 -08005064TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005065 prepareButtons();
5066 prepareAxes(POSITION);
5067 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005068 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005069
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005070 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005071}
5072
5073TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005074 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005075 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005076 prepareButtons();
5077 prepareAxes(POSITION);
5078 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005079 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080
5081 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005082 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005083
5084 // Virtual key is down.
5085 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5086 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5087 processDown(mapper, x, y);
5088 processSync(mapper);
5089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5090
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005091 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005092
5093 // Virtual key is up.
5094 processUp(mapper);
5095 processSync(mapper);
5096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5097
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005098 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005099}
5100
5101TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005102 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005103 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005104 prepareButtons();
5105 prepareAxes(POSITION);
5106 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005107 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005108
5109 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005110 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111
5112 // Virtual key is down.
5113 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5114 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5115 processDown(mapper, x, y);
5116 processSync(mapper);
5117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5118
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005119 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005120
5121 // Virtual key is up.
5122 processUp(mapper);
5123 processSync(mapper);
5124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5125
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005126 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005127}
5128
5129TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005131 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005132 prepareButtons();
5133 prepareAxes(POSITION);
5134 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005135 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005136
Michael Wrightd02c5b62014-02-10 15:10:22 -08005137 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005138 ASSERT_TRUE(
5139 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005140 ASSERT_TRUE(flags[0]);
5141 ASSERT_FALSE(flags[1]);
5142}
5143
5144TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005145 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005146 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005147 prepareButtons();
5148 prepareAxes(POSITION);
5149 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005150 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005151
arthurhungdcef2dc2020-08-11 14:47:50 +08005152 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005153
5154 NotifyKeyArgs args;
5155
5156 // Press virtual key.
5157 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5158 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5159 processDown(mapper, x, y);
5160 processSync(mapper);
5161
5162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5163 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5164 ASSERT_EQ(DEVICE_ID, args.deviceId);
5165 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5166 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5167 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5168 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5169 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5170 ASSERT_EQ(KEY_HOME, args.scanCode);
5171 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5172 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5173
5174 // Release virtual key.
5175 processUp(mapper);
5176 processSync(mapper);
5177
5178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5179 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5180 ASSERT_EQ(DEVICE_ID, args.deviceId);
5181 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5182 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5183 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5184 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5185 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5186 ASSERT_EQ(KEY_HOME, args.scanCode);
5187 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5188 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5189
5190 // Should not have sent any motions.
5191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5192}
5193
5194TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005195 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005196 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005197 prepareButtons();
5198 prepareAxes(POSITION);
5199 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005200 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005201
arthurhungdcef2dc2020-08-11 14:47:50 +08005202 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005203
5204 NotifyKeyArgs keyArgs;
5205
5206 // Press virtual key.
5207 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5208 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5209 processDown(mapper, x, y);
5210 processSync(mapper);
5211
5212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5213 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5214 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5215 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5216 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5217 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5218 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5219 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5220 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5221 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5222 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5223
5224 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5225 // into the display area.
5226 y -= 100;
5227 processMove(mapper, x, y);
5228 processSync(mapper);
5229
5230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5231 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5232 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5233 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5234 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5235 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5236 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5237 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5238 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5239 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5240 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5241 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5242
5243 NotifyMotionArgs motionArgs;
5244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5245 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5246 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5247 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5248 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5249 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5250 ASSERT_EQ(0, motionArgs.flags);
5251 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5252 ASSERT_EQ(0, motionArgs.buttonState);
5253 ASSERT_EQ(0, motionArgs.edgeFlags);
5254 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5255 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005256 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5258 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5259 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5260 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5261 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5262
5263 // Keep moving out of bounds. Should generate a pointer move.
5264 y -= 50;
5265 processMove(mapper, x, y);
5266 processSync(mapper);
5267
5268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5269 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5270 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5271 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5272 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5274 ASSERT_EQ(0, motionArgs.flags);
5275 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5276 ASSERT_EQ(0, motionArgs.buttonState);
5277 ASSERT_EQ(0, motionArgs.edgeFlags);
5278 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5279 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005280 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5282 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5283 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5284 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5285 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5286
5287 // Release out of bounds. Should generate a pointer up.
5288 processUp(mapper);
5289 processSync(mapper);
5290
5291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5292 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5293 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5294 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5295 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5296 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5297 ASSERT_EQ(0, motionArgs.flags);
5298 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5299 ASSERT_EQ(0, motionArgs.buttonState);
5300 ASSERT_EQ(0, motionArgs.edgeFlags);
5301 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5302 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005303 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5305 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5306 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5307 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5308 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5309
5310 // Should not have sent any more keys or motions.
5311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5313}
5314
5315TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005316 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005317 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005318 prepareButtons();
5319 prepareAxes(POSITION);
5320 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005321 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322
arthurhungdcef2dc2020-08-11 14:47:50 +08005323 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005324
5325 NotifyMotionArgs motionArgs;
5326
5327 // Initially go down out of bounds.
5328 int32_t x = -10;
5329 int32_t y = -10;
5330 processDown(mapper, x, y);
5331 processSync(mapper);
5332
5333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5334
5335 // Move into the display area. Should generate a pointer down.
5336 x = 50;
5337 y = 75;
5338 processMove(mapper, x, y);
5339 processSync(mapper);
5340
5341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5342 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5343 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5344 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5345 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5346 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5347 ASSERT_EQ(0, motionArgs.flags);
5348 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5349 ASSERT_EQ(0, motionArgs.buttonState);
5350 ASSERT_EQ(0, motionArgs.edgeFlags);
5351 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5352 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005353 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5355 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5356 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5357 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5358 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5359
5360 // Release. Should generate a pointer up.
5361 processUp(mapper);
5362 processSync(mapper);
5363
5364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5365 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5366 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5367 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5368 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5369 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5370 ASSERT_EQ(0, motionArgs.flags);
5371 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5372 ASSERT_EQ(0, motionArgs.buttonState);
5373 ASSERT_EQ(0, motionArgs.edgeFlags);
5374 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5375 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005376 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5378 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5379 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5380 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5381 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5382
5383 // Should not have sent any more keys or motions.
5384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5386}
5387
Santos Cordonfa5cf462017-04-05 10:37:00 -07005388TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005389 addConfigurationProperty("touch.deviceType", "touchScreen");
5390 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5391
Michael Wrighta9cf4192022-12-01 23:46:39 +00005392 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005393 prepareButtons();
5394 prepareAxes(POSITION);
5395 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005396 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005397
arthurhungdcef2dc2020-08-11 14:47:50 +08005398 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005399
5400 NotifyMotionArgs motionArgs;
5401
5402 // Down.
5403 int32_t x = 100;
5404 int32_t y = 125;
5405 processDown(mapper, x, y);
5406 processSync(mapper);
5407
5408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5409 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5410 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5411 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5412 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5413 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5414 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5415 ASSERT_EQ(0, motionArgs.flags);
5416 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5417 ASSERT_EQ(0, motionArgs.buttonState);
5418 ASSERT_EQ(0, motionArgs.edgeFlags);
5419 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5420 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005421 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5423 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5424 1, 0, 0, 0, 0, 0, 0, 0));
5425 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5426 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5427 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5428
5429 // Move.
5430 x += 50;
5431 y += 75;
5432 processMove(mapper, x, y);
5433 processSync(mapper);
5434
5435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5436 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5437 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5438 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5439 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5440 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5441 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5442 ASSERT_EQ(0, motionArgs.flags);
5443 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5444 ASSERT_EQ(0, motionArgs.buttonState);
5445 ASSERT_EQ(0, motionArgs.edgeFlags);
5446 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5447 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005448 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5450 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5451 1, 0, 0, 0, 0, 0, 0, 0));
5452 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5453 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5454 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5455
5456 // Up.
5457 processUp(mapper);
5458 processSync(mapper);
5459
5460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5461 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5462 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5463 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5464 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5465 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5466 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5467 ASSERT_EQ(0, motionArgs.flags);
5468 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5469 ASSERT_EQ(0, motionArgs.buttonState);
5470 ASSERT_EQ(0, motionArgs.edgeFlags);
5471 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5472 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005473 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5475 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5476 1, 0, 0, 0, 0, 0, 0, 0));
5477 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5478 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5479 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5480
5481 // Should not have sent any more keys or motions.
5482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5484}
5485
Michael Wrightd02c5b62014-02-10 15:10:22 -08005486TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005487 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005488 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005489 prepareButtons();
5490 prepareAxes(POSITION);
5491 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005492 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493
arthurhungdcef2dc2020-08-11 14:47:50 +08005494 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005495
5496 NotifyMotionArgs motionArgs;
5497
5498 // Down.
5499 int32_t x = 100;
5500 int32_t y = 125;
5501 processDown(mapper, x, y);
5502 processSync(mapper);
5503
5504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5505 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5506 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5507 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5508 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5509 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5510 ASSERT_EQ(0, motionArgs.flags);
5511 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5512 ASSERT_EQ(0, motionArgs.buttonState);
5513 ASSERT_EQ(0, motionArgs.edgeFlags);
5514 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5515 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005516 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5518 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5519 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5520 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5521 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5522
5523 // Move.
5524 x += 50;
5525 y += 75;
5526 processMove(mapper, x, y);
5527 processSync(mapper);
5528
5529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5530 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5531 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5532 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5533 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5535 ASSERT_EQ(0, motionArgs.flags);
5536 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5537 ASSERT_EQ(0, motionArgs.buttonState);
5538 ASSERT_EQ(0, motionArgs.edgeFlags);
5539 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5540 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005541 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5543 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5544 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5545 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5546 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5547
5548 // Up.
5549 processUp(mapper);
5550 processSync(mapper);
5551
5552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5553 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5554 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5555 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5556 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5557 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5558 ASSERT_EQ(0, motionArgs.flags);
5559 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5560 ASSERT_EQ(0, motionArgs.buttonState);
5561 ASSERT_EQ(0, motionArgs.edgeFlags);
5562 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5563 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005564 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5566 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5567 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5568 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5569 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5570
5571 // Should not have sent any more keys or motions.
5572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5574}
5575
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005576TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005577 addConfigurationProperty("touch.deviceType", "touchScreen");
5578 prepareButtons();
5579 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005580 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5581 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005582 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005583
5584 NotifyMotionArgs args;
5585
5586 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005587 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005588 processDown(mapper, toRawX(50), toRawY(75));
5589 processSync(mapper);
5590
5591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5592 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5593 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5594
5595 processUp(mapper);
5596 processSync(mapper);
5597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5598}
5599
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005600TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005601 addConfigurationProperty("touch.deviceType", "touchScreen");
5602 prepareButtons();
5603 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005604 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5605 // orientation-aware are affected by display rotation.
5606 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005607 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005608
5609 NotifyMotionArgs args;
5610
5611 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005612 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005613 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005614 processDown(mapper, toRawX(50), toRawY(75));
5615 processSync(mapper);
5616
5617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5618 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5619 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5620
5621 processUp(mapper);
5622 processSync(mapper);
5623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5624
5625 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005626 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005627 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005628 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005629 processSync(mapper);
5630
5631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5632 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5633 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5634
5635 processUp(mapper);
5636 processSync(mapper);
5637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5638
5639 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005640 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005641 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005642 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5643 processSync(mapper);
5644
5645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5646 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5647 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5648
5649 processUp(mapper);
5650 processSync(mapper);
5651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5652
5653 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005654 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005655 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005656 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005657 processSync(mapper);
5658
5659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5660 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5661 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5662
5663 processUp(mapper);
5664 processSync(mapper);
5665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5666}
5667
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005668TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5669 addConfigurationProperty("touch.deviceType", "touchScreen");
5670 prepareButtons();
5671 prepareAxes(POSITION);
5672 addConfigurationProperty("touch.orientationAware", "1");
5673 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5674 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005675 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005676 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5677 NotifyMotionArgs args;
5678
5679 // Orientation 0.
5680 processDown(mapper, toRawX(50), toRawY(75));
5681 processSync(mapper);
5682
5683 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5684 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5685 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5686
5687 processUp(mapper);
5688 processSync(mapper);
5689 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5690}
5691
5692TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5693 addConfigurationProperty("touch.deviceType", "touchScreen");
5694 prepareButtons();
5695 prepareAxes(POSITION);
5696 addConfigurationProperty("touch.orientationAware", "1");
5697 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5698 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005699 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005700 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5701 NotifyMotionArgs args;
5702
5703 // Orientation 90.
5704 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5705 processSync(mapper);
5706
5707 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5708 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5709 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5710
5711 processUp(mapper);
5712 processSync(mapper);
5713 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5714}
5715
5716TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5717 addConfigurationProperty("touch.deviceType", "touchScreen");
5718 prepareButtons();
5719 prepareAxes(POSITION);
5720 addConfigurationProperty("touch.orientationAware", "1");
5721 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5722 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005723 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005724 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5725 NotifyMotionArgs args;
5726
5727 // Orientation 180.
5728 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5729 processSync(mapper);
5730
5731 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5732 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5733 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5734
5735 processUp(mapper);
5736 processSync(mapper);
5737 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5738}
5739
5740TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5741 addConfigurationProperty("touch.deviceType", "touchScreen");
5742 prepareButtons();
5743 prepareAxes(POSITION);
5744 addConfigurationProperty("touch.orientationAware", "1");
5745 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5746 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005747 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005748 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5749 NotifyMotionArgs args;
5750
5751 // Orientation 270.
5752 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5753 processSync(mapper);
5754
5755 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5756 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5757 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5758
5759 processUp(mapper);
5760 processSync(mapper);
5761 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5762}
5763
5764TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5765 addConfigurationProperty("touch.deviceType", "touchScreen");
5766 prepareButtons();
5767 prepareAxes(POSITION);
5768 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5769 // orientation-aware are affected by display rotation.
5770 addConfigurationProperty("touch.orientationAware", "0");
5771 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5772 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5773
5774 NotifyMotionArgs args;
5775
5776 // Orientation 90, Rotation 0.
5777 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005778 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005779 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5780 processSync(mapper);
5781
5782 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5783 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5784 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5785
5786 processUp(mapper);
5787 processSync(mapper);
5788 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5789
5790 // Orientation 90, Rotation 90.
5791 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005792 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005793 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005794 processSync(mapper);
5795
5796 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5797 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5798 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5799
5800 processUp(mapper);
5801 processSync(mapper);
5802 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5803
5804 // Orientation 90, Rotation 180.
5805 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005806 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005807 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5808 processSync(mapper);
5809
5810 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5811 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5812 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5813
5814 processUp(mapper);
5815 processSync(mapper);
5816 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5817
5818 // Orientation 90, Rotation 270.
5819 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005820 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005821 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 -07005822 processSync(mapper);
5823
5824 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5825 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5826 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5827
5828 processUp(mapper);
5829 processSync(mapper);
5830 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5831}
5832
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005833TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5834 addConfigurationProperty("touch.deviceType", "touchScreen");
5835 prepareButtons();
5836 prepareAxes(POSITION);
5837 addConfigurationProperty("touch.orientationAware", "1");
5838 prepareDisplay(ui::ROTATION_0);
5839 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5840
5841 // Set a physical frame in the display viewport.
5842 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5843 viewport->physicalLeft = 20;
5844 viewport->physicalTop = 600;
5845 viewport->physicalRight = 30;
5846 viewport->physicalBottom = 610;
5847 mFakePolicy->updateViewport(*viewport);
5848 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5849
5850 // Start the touch.
5851 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5852 processSync(mapper);
5853
5854 // Expect all input starting outside the physical frame to be ignored.
5855 const std::array<Point, 6> outsidePoints = {
5856 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5857 for (const auto& p : outsidePoints) {
5858 processMove(mapper, toRawX(p.x), toRawY(p.y));
5859 processSync(mapper);
5860 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5861 }
5862
5863 // Move the touch into the physical frame.
5864 processMove(mapper, toRawX(25), toRawY(605));
5865 processSync(mapper);
5866 NotifyMotionArgs args;
5867 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5868 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5869 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5870 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5871
5872 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5873 for (const auto& p : outsidePoints) {
5874 processMove(mapper, toRawX(p.x), toRawY(p.y));
5875 processSync(mapper);
5876 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5877 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5878 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5879 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5880 }
5881
5882 processUp(mapper);
5883 processSync(mapper);
5884 EXPECT_NO_FATAL_FAILURE(
5885 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5886}
5887
Michael Wrightd02c5b62014-02-10 15:10:22 -08005888TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005889 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005890 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005891 prepareButtons();
5892 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005893 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005894
5895 // These calculations are based on the input device calibration documentation.
5896 int32_t rawX = 100;
5897 int32_t rawY = 200;
5898 int32_t rawPressure = 10;
5899 int32_t rawToolMajor = 12;
5900 int32_t rawDistance = 2;
5901 int32_t rawTiltX = 30;
5902 int32_t rawTiltY = 110;
5903
5904 float x = toDisplayX(rawX);
5905 float y = toDisplayY(rawY);
5906 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5907 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5908 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5909 float distance = float(rawDistance);
5910
5911 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5912 float tiltScale = M_PI / 180;
5913 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5914 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5915 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5916 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5917
5918 processDown(mapper, rawX, rawY);
5919 processPressure(mapper, rawPressure);
5920 processToolMajor(mapper, rawToolMajor);
5921 processDistance(mapper, rawDistance);
5922 processTilt(mapper, rawTiltX, rawTiltY);
5923 processSync(mapper);
5924
5925 NotifyMotionArgs args;
5926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5928 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5929 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5930}
5931
Jason Gerecke489fda82012-09-07 17:19:40 -07005932TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005933 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005934 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005935 prepareLocationCalibration();
5936 prepareButtons();
5937 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005938 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005939
5940 int32_t rawX = 100;
5941 int32_t rawY = 200;
5942
5943 float x = toDisplayX(toCookedX(rawX, rawY));
5944 float y = toDisplayY(toCookedY(rawX, rawY));
5945
5946 processDown(mapper, rawX, rawY);
5947 processSync(mapper);
5948
5949 NotifyMotionArgs args;
5950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5952 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5953}
5954
Michael Wrightd02c5b62014-02-10 15:10:22 -08005955TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005956 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005957 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005958 prepareButtons();
5959 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005960 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005961
5962 NotifyMotionArgs motionArgs;
5963 NotifyKeyArgs keyArgs;
5964
5965 processDown(mapper, 100, 200);
5966 processSync(mapper);
5967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5968 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5969 ASSERT_EQ(0, motionArgs.buttonState);
5970
5971 // press BTN_LEFT, release BTN_LEFT
5972 processKey(mapper, BTN_LEFT, 1);
5973 processSync(mapper);
5974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5975 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5976 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5977
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5979 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5980 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5981
Michael Wrightd02c5b62014-02-10 15:10:22 -08005982 processKey(mapper, BTN_LEFT, 0);
5983 processSync(mapper);
5984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005985 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005986 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005987
5988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005989 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005990 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005991
5992 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5993 processKey(mapper, BTN_RIGHT, 1);
5994 processKey(mapper, BTN_MIDDLE, 1);
5995 processSync(mapper);
5996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5997 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5998 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5999 motionArgs.buttonState);
6000
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6002 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6003 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6004
6005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6006 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6007 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6008 motionArgs.buttonState);
6009
Michael Wrightd02c5b62014-02-10 15:10:22 -08006010 processKey(mapper, BTN_RIGHT, 0);
6011 processSync(mapper);
6012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006013 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006014 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006015
6016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006017 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006018 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006019
6020 processKey(mapper, BTN_MIDDLE, 0);
6021 processSync(mapper);
6022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006023 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006024 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006025
6026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006027 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006028 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006029
6030 // press BTN_BACK, release BTN_BACK
6031 processKey(mapper, BTN_BACK, 1);
6032 processSync(mapper);
6033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6034 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6035 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006036
Michael Wrightd02c5b62014-02-10 15:10:22 -08006037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006039 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6040
6041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6042 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6043 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006044
6045 processKey(mapper, BTN_BACK, 0);
6046 processSync(mapper);
6047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006048 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006049 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006050
6051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006052 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006053 ASSERT_EQ(0, motionArgs.buttonState);
6054
Michael Wrightd02c5b62014-02-10 15:10:22 -08006055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6056 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6057 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6058
6059 // press BTN_SIDE, release BTN_SIDE
6060 processKey(mapper, BTN_SIDE, 1);
6061 processSync(mapper);
6062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6063 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6064 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006065
Michael Wrightd02c5b62014-02-10 15:10:22 -08006066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006068 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6069
6070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6071 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6072 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006073
6074 processKey(mapper, BTN_SIDE, 0);
6075 processSync(mapper);
6076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006077 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006078 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006079
6080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006082 ASSERT_EQ(0, motionArgs.buttonState);
6083
Michael Wrightd02c5b62014-02-10 15:10:22 -08006084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6085 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6086 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6087
6088 // press BTN_FORWARD, release BTN_FORWARD
6089 processKey(mapper, BTN_FORWARD, 1);
6090 processSync(mapper);
6091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6092 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6093 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006094
Michael Wrightd02c5b62014-02-10 15:10:22 -08006095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006096 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006097 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6098
6099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6100 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6101 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006102
6103 processKey(mapper, BTN_FORWARD, 0);
6104 processSync(mapper);
6105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006106 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006107 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006108
6109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006111 ASSERT_EQ(0, motionArgs.buttonState);
6112
Michael Wrightd02c5b62014-02-10 15:10:22 -08006113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6114 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6115 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6116
6117 // press BTN_EXTRA, release BTN_EXTRA
6118 processKey(mapper, BTN_EXTRA, 1);
6119 processSync(mapper);
6120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6121 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6122 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006123
Michael Wrightd02c5b62014-02-10 15:10:22 -08006124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006125 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006126 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6127
6128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6129 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6130 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006131
6132 processKey(mapper, BTN_EXTRA, 0);
6133 processSync(mapper);
6134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006135 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006136 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006137
6138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006140 ASSERT_EQ(0, motionArgs.buttonState);
6141
Michael Wrightd02c5b62014-02-10 15:10:22 -08006142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6143 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6144 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6145
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6147
Michael Wrightd02c5b62014-02-10 15:10:22 -08006148 // press BTN_STYLUS, release BTN_STYLUS
6149 processKey(mapper, BTN_STYLUS, 1);
6150 processSync(mapper);
6151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006153 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6154
6155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6156 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6157 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006158
6159 processKey(mapper, BTN_STYLUS, 0);
6160 processSync(mapper);
6161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006162 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006163 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006164
6165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006167 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006168
6169 // press BTN_STYLUS2, release BTN_STYLUS2
6170 processKey(mapper, BTN_STYLUS2, 1);
6171 processSync(mapper);
6172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006174 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6175
6176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6177 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6178 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179
6180 processKey(mapper, BTN_STYLUS2, 0);
6181 processSync(mapper);
6182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006184 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006185
6186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006187 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006188 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006189
6190 // release touch
6191 processUp(mapper);
6192 processSync(mapper);
6193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6194 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6195 ASSERT_EQ(0, motionArgs.buttonState);
6196}
6197
6198TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006199 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006200 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006201 prepareButtons();
6202 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006203 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006204
6205 NotifyMotionArgs motionArgs;
6206
6207 // default tool type is finger
6208 processDown(mapper, 100, 200);
6209 processSync(mapper);
6210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6211 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006212 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006213
6214 // eraser
6215 processKey(mapper, BTN_TOOL_RUBBER, 1);
6216 processSync(mapper);
6217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006219 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006220
6221 // stylus
6222 processKey(mapper, BTN_TOOL_RUBBER, 0);
6223 processKey(mapper, BTN_TOOL_PEN, 1);
6224 processSync(mapper);
6225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6226 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006227 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006228
6229 // brush
6230 processKey(mapper, BTN_TOOL_PEN, 0);
6231 processKey(mapper, BTN_TOOL_BRUSH, 1);
6232 processSync(mapper);
6233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006235 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236
6237 // pencil
6238 processKey(mapper, BTN_TOOL_BRUSH, 0);
6239 processKey(mapper, BTN_TOOL_PENCIL, 1);
6240 processSync(mapper);
6241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006243 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006244
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006245 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006246 processKey(mapper, BTN_TOOL_PENCIL, 0);
6247 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6248 processSync(mapper);
6249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006251 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252
6253 // mouse
6254 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6255 processKey(mapper, BTN_TOOL_MOUSE, 1);
6256 processSync(mapper);
6257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006259 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006260
6261 // lens
6262 processKey(mapper, BTN_TOOL_MOUSE, 0);
6263 processKey(mapper, BTN_TOOL_LENS, 1);
6264 processSync(mapper);
6265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6266 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006267 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006268
6269 // double-tap
6270 processKey(mapper, BTN_TOOL_LENS, 0);
6271 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6272 processSync(mapper);
6273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006275 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006276
6277 // triple-tap
6278 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6279 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6280 processSync(mapper);
6281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006283 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006284
6285 // quad-tap
6286 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6287 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6288 processSync(mapper);
6289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006291 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006292
6293 // finger
6294 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6295 processKey(mapper, BTN_TOOL_FINGER, 1);
6296 processSync(mapper);
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006299 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006300
6301 // stylus trumps finger
6302 processKey(mapper, BTN_TOOL_PEN, 1);
6303 processSync(mapper);
6304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006306 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006307
6308 // eraser trumps stylus
6309 processKey(mapper, BTN_TOOL_RUBBER, 1);
6310 processSync(mapper);
6311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6312 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006313 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006314
6315 // mouse trumps eraser
6316 processKey(mapper, BTN_TOOL_MOUSE, 1);
6317 processSync(mapper);
6318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006320 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006321
6322 // back to default tool type
6323 processKey(mapper, BTN_TOOL_MOUSE, 0);
6324 processKey(mapper, BTN_TOOL_RUBBER, 0);
6325 processKey(mapper, BTN_TOOL_PEN, 0);
6326 processKey(mapper, BTN_TOOL_FINGER, 0);
6327 processSync(mapper);
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006330 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331}
6332
6333TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006334 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006335 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336 prepareButtons();
6337 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006338 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006339 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006340
6341 NotifyMotionArgs motionArgs;
6342
6343 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6344 processKey(mapper, BTN_TOOL_FINGER, 1);
6345 processMove(mapper, 100, 200);
6346 processSync(mapper);
6347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6348 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6350 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6351
6352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6353 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6355 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6356
6357 // move a little
6358 processMove(mapper, 150, 250);
6359 processSync(mapper);
6360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6361 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6363 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6364
6365 // down when BTN_TOUCH is pressed, pressure defaults to 1
6366 processKey(mapper, BTN_TOUCH, 1);
6367 processSync(mapper);
6368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6369 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6371 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6372
6373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6374 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6376 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6377
6378 // up when BTN_TOUCH is released, hover restored
6379 processKey(mapper, BTN_TOUCH, 0);
6380 processSync(mapper);
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6382 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6384 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6385
6386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6387 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6389 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6390
6391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6392 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6394 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6395
6396 // exit hover when pointer goes away
6397 processKey(mapper, BTN_TOOL_FINGER, 0);
6398 processSync(mapper);
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6400 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6402 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6403}
6404
6405TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006406 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006407 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006408 prepareButtons();
6409 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006410 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006411
6412 NotifyMotionArgs motionArgs;
6413
6414 // initially hovering because pressure is 0
6415 processDown(mapper, 100, 200);
6416 processPressure(mapper, 0);
6417 processSync(mapper);
6418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6419 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6421 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6422
6423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6424 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6426 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6427
6428 // move a little
6429 processMove(mapper, 150, 250);
6430 processSync(mapper);
6431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6432 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6434 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6435
6436 // down when pressure is non-zero
6437 processPressure(mapper, RAW_PRESSURE_MAX);
6438 processSync(mapper);
6439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6440 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6442 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6443
6444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6445 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6446 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6447 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6448
6449 // up when pressure becomes 0, hover restored
6450 processPressure(mapper, 0);
6451 processSync(mapper);
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6453 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6455 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6456
6457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6458 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6459 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6460 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6461
6462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6463 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6465 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6466
6467 // exit hover when pointer goes away
6468 processUp(mapper);
6469 processSync(mapper);
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6471 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6473 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6474}
6475
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006476TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6477 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006478 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006479 prepareButtons();
6480 prepareAxes(POSITION | PRESSURE);
6481 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6482
6483 // Touch down.
6484 processDown(mapper, 100, 200);
6485 processPressure(mapper, 1);
6486 processSync(mapper);
6487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6488 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6489
6490 // Reset the mapper. This should cancel the ongoing gesture.
6491 resetMapper(mapper, ARBITRARY_TIME);
6492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6493 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6494
6495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6496}
6497
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006498TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6499 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006500 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006501 prepareButtons();
6502 prepareAxes(POSITION | PRESSURE);
6503 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6504
6505 // Set the initial state for the touch pointer.
6506 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6507 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6508 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6509 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6510
6511 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006512 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6513 // does not generate any events.
6514 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006515
6516 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6517 // the recreated touch state to generate a down event.
6518 processSync(mapper);
6519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6520 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6521
6522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6523}
6524
lilinnan687e58f2022-07-19 16:00:50 +08006525TEST_F(SingleTouchInputMapperTest,
6526 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6527 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006528 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006529 prepareButtons();
6530 prepareAxes(POSITION);
6531 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6532 NotifyMotionArgs motionArgs;
6533
6534 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006535 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006536 processSync(mapper);
6537
6538 // We should receive a down event
6539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6540 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6541
6542 // Change display id
6543 clearViewports();
6544 prepareSecondaryDisplay(ViewportType::INTERNAL);
6545
6546 // We should receive a cancel event
6547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6548 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6549 // Then receive reset called
6550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6551}
6552
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006553TEST_F(SingleTouchInputMapperTest,
6554 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6555 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006556 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006557 prepareButtons();
6558 prepareAxes(POSITION);
6559 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6561 NotifyMotionArgs motionArgs;
6562
6563 // Start a new gesture.
6564 processDown(mapper, 100, 200);
6565 processSync(mapper);
6566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6567 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6568
6569 // Make the viewport inactive. This will put the device in disabled mode.
6570 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6571 viewport->isActive = false;
6572 mFakePolicy->updateViewport(*viewport);
6573 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6574
6575 // We should receive a cancel event for the ongoing gesture.
6576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6577 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6578 // Then we should be notified that the device was reset.
6579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6580
6581 // No events are generated while the viewport is inactive.
6582 processMove(mapper, 101, 201);
6583 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006584 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006585 processSync(mapper);
6586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6587
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006588 // Start a new gesture while the viewport is still inactive.
6589 processDown(mapper, 300, 400);
6590 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6591 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6592 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6593 processSync(mapper);
6594
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006595 // Make the viewport active again. The device should resume processing events.
6596 viewport->isActive = true;
6597 mFakePolicy->updateViewport(*viewport);
6598 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6599
6600 // The device is reset because it changes back to direct mode, without generating any events.
6601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6603
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006604 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006605 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6607 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006608
6609 // No more events.
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6612}
6613
Prabir Pradhan211ba622022-10-31 21:09:21 +00006614TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6615 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006616 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006617 prepareButtons();
6618 prepareAxes(POSITION);
6619 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6621
6622 // Press a stylus button.
6623 processKey(mapper, BTN_STYLUS, 1);
6624 processSync(mapper);
6625
6626 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6627 processDown(mapper, 100, 200);
6628 processSync(mapper);
6629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6630 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6631 WithCoords(toDisplayX(100), toDisplayY(200)),
6632 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6634 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6635 WithCoords(toDisplayX(100), toDisplayY(200)),
6636 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6637
6638 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6639 // the button has not actually been released, since there will be no pointers through which the
6640 // button state can be reported. The event is generated at the location of the pointer before
6641 // it went up.
6642 processUp(mapper);
6643 processSync(mapper);
6644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6645 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6646 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6648 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6649 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6650}
6651
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006652TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6653 addConfigurationProperty("touch.deviceType", "touchScreen");
6654 prepareDisplay(ui::ROTATION_0);
6655 prepareButtons();
6656 prepareAxes(POSITION);
6657
6658 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6659
6660 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6662
6663 // Press a stylus button.
6664 processKey(mapper, BTN_STYLUS, 1);
6665 processSync(mapper);
6666
6667 // Start a touch gesture and ensure that the stylus button is not reported.
6668 processDown(mapper, 100, 200);
6669 processSync(mapper);
6670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6671 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6672
6673 // Release and press the stylus button again.
6674 processKey(mapper, BTN_STYLUS, 0);
6675 processSync(mapper);
6676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6677 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6678 processKey(mapper, BTN_STYLUS, 1);
6679 processSync(mapper);
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6681 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6682
6683 // Release the touch gesture.
6684 processUp(mapper);
6685 processSync(mapper);
6686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6687 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6688
6689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6690}
6691
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006692TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6693 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6694 prepareDisplay(ui::ROTATION_0);
6695 prepareButtons();
6696 prepareAxes(POSITION);
6697 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6699
6700 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6701}
6702
Seunghwan Choi356026c2023-02-01 14:37:25 +09006703TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6704 std::shared_ptr<FakePointerController> fakePointerController =
6705 std::make_shared<FakePointerController>();
6706 addConfigurationProperty("touch.deviceType", "touchScreen");
6707 prepareDisplay(ui::ROTATION_0);
6708 prepareButtons();
6709 prepareAxes(POSITION);
6710 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6711 mFakePolicy->setPointerController(fakePointerController);
6712 mFakePolicy->setStylusPointerIconEnabled(true);
6713 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6714
6715 processKey(mapper, BTN_TOOL_PEN, 1);
6716 processMove(mapper, 100, 200);
6717 processSync(mapper);
6718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6719 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006720 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006721 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6722 ASSERT_TRUE(fakePointerController->isPointerShown());
6723 ASSERT_NO_FATAL_FAILURE(
6724 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6725}
6726
6727TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6728 std::shared_ptr<FakePointerController> fakePointerController =
6729 std::make_shared<FakePointerController>();
6730 addConfigurationProperty("touch.deviceType", "touchScreen");
6731 prepareDisplay(ui::ROTATION_0);
6732 prepareButtons();
6733 prepareAxes(POSITION);
6734 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6735 mFakePolicy->setPointerController(fakePointerController);
6736 mFakePolicy->setStylusPointerIconEnabled(false);
6737 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6738
6739 processKey(mapper, BTN_TOOL_PEN, 1);
6740 processMove(mapper, 100, 200);
6741 processSync(mapper);
6742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6743 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006744 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006745 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6746 ASSERT_FALSE(fakePointerController->isPointerShown());
6747}
6748
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006749TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6750 // Initialize the device without setting device source to touch navigation.
6751 addConfigurationProperty("touch.deviceType", "touchScreen");
6752 prepareDisplay(ui::ROTATION_0);
6753 prepareButtons();
6754 prepareAxes(POSITION);
6755 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6756
6757 // Ensure that the device is created as a touchscreen, not touch navigation.
6758 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6759
6760 // Add device type association after the device was created.
6761 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6762
6763 // Send update to the mapper.
6764 std::list<NotifyArgs> unused2 =
6765 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6766 InputReaderConfiguration::CHANGE_DEVICE_TYPE /*changes*/);
6767
6768 // Check whether device type update was successful.
6769 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6770}
6771
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006772TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6773 // Initialize the device without setting device source to touch navigation.
6774 addConfigurationProperty("touch.deviceType", "touchScreen");
6775 prepareDisplay(ui::ROTATION_0);
6776 prepareButtons();
6777 prepareAxes(POSITION);
6778 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6779
6780 // Set a physical frame in the display viewport.
6781 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6782 viewport->physicalLeft = 0;
6783 viewport->physicalTop = 0;
6784 viewport->physicalRight = DISPLAY_WIDTH / 2;
6785 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6786 mFakePolicy->updateViewport(*viewport);
6787 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6788
6789 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6790
6791 // Hovering inside the physical frame produces events.
6792 processKey(mapper, BTN_TOOL_PEN, 1);
6793 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6794 processSync(mapper);
6795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6796 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6798 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6799
6800 // Leaving the physical frame ends the hovering gesture.
6801 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6802 processSync(mapper);
6803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6804 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6805
6806 // Moving outside the physical frame does not produce events.
6807 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6808 processSync(mapper);
6809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6810
6811 // Re-entering the physical frame produces events.
6812 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6813 processSync(mapper);
6814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6815 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6817 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6818}
6819
Prabir Pradhan5632d622021-09-06 07:57:20 -07006820// --- TouchDisplayProjectionTest ---
6821
6822class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6823public:
6824 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6825 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6826 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006827 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6828 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6829 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006830 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006831 auto rotatedWidth = naturalDisplayWidth;
6832 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006833 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006834 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006835 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006836 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006837 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006838 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006839 inverseRotationFlags = ui::Transform::ROT_180;
6840 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006841 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006842 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006843 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006844 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006845 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006846 inverseRotationFlags = ui::Transform::ROT_0;
6847 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006848 }
6849
Prabir Pradhana9df3162022-12-05 23:57:27 +00006850 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006851 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6852
6853 std::optional<DisplayViewport> internalViewport =
6854 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6855 DisplayViewport& v = *internalViewport;
6856 v.displayId = DISPLAY_ID;
6857 v.orientation = orientation;
6858
6859 v.logicalLeft = 0;
6860 v.logicalTop = 0;
6861 v.logicalRight = 100;
6862 v.logicalBottom = 100;
6863
6864 v.physicalLeft = rotatedPhysicalDisplay.left;
6865 v.physicalTop = rotatedPhysicalDisplay.top;
6866 v.physicalRight = rotatedPhysicalDisplay.right;
6867 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6868
Prabir Pradhana9df3162022-12-05 23:57:27 +00006869 v.deviceWidth = rotatedWidth;
6870 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006871
6872 v.isActive = true;
6873 v.uniqueId = UNIQUE_ID;
6874 v.type = ViewportType::INTERNAL;
6875 mFakePolicy->updateViewport(v);
6876 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6877 }
6878
6879 void assertReceivedMove(const Point& point) {
6880 NotifyMotionArgs motionArgs;
6881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6882 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6883 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6885 1, 0, 0, 0, 0, 0, 0, 0));
6886 }
6887};
6888
6889TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6890 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006891 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006892
6893 prepareButtons();
6894 prepareAxes(POSITION);
6895 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6896
6897 NotifyMotionArgs motionArgs;
6898
6899 // Configure the DisplayViewport such that the logical display maps to a subsection of
6900 // the display panel called the physical display. Here, the physical display is bounded by the
6901 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6902 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6903 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6904 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6905
Michael Wrighta9cf4192022-12-01 23:46:39 +00006906 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006907 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6908
6909 // Touches outside the physical display should be ignored, and should not generate any
6910 // events. Ensure touches at the following points that lie outside of the physical display
6911 // area do not generate any events.
6912 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6913 processDown(mapper, toRawX(point.x), toRawY(point.y));
6914 processSync(mapper);
6915 processUp(mapper);
6916 processSync(mapper);
6917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6918 << "Unexpected event generated for touch outside physical display at point: "
6919 << point.x << ", " << point.y;
6920 }
6921 }
6922}
6923
6924TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6925 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006926 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006927
6928 prepareButtons();
6929 prepareAxes(POSITION);
6930 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6931
6932 NotifyMotionArgs motionArgs;
6933
6934 // Configure the DisplayViewport such that the logical display maps to a subsection of
6935 // the display panel called the physical display. Here, the physical display is bounded by the
6936 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6937 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6938
Michael Wrighta9cf4192022-12-01 23:46:39 +00006939 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006940 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6941
6942 // Touches that start outside the physical display should be ignored until it enters the
6943 // physical display bounds, at which point it should generate a down event. Start a touch at
6944 // the point (5, 100), which is outside the physical display bounds.
6945 static const Point kOutsidePoint{5, 100};
6946 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6947 processSync(mapper);
6948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6949
6950 // Move the touch into the physical display area. This should generate a pointer down.
6951 processMove(mapper, toRawX(11), toRawY(21));
6952 processSync(mapper);
6953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6954 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6955 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6956 ASSERT_NO_FATAL_FAILURE(
6957 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6958
6959 // Move the touch inside the physical display area. This should generate a pointer move.
6960 processMove(mapper, toRawX(69), toRawY(159));
6961 processSync(mapper);
6962 assertReceivedMove({69, 159});
6963
6964 // Move outside the physical display area. Since the pointer is already down, this should
6965 // now continue generating events.
6966 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6967 processSync(mapper);
6968 assertReceivedMove(kOutsidePoint);
6969
6970 // Release. This should generate a pointer up.
6971 processUp(mapper);
6972 processSync(mapper);
6973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6974 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6976 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6977
6978 // Ensure no more events were generated.
6979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6981 }
6982}
6983
Prabir Pradhana9df3162022-12-05 23:57:27 +00006984// --- TouchscreenPrecisionTests ---
6985
6986// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6987// in various orientations and with different display rotations. We configure the touchscreen to
6988// have a higher resolution than that of the display by an integer scale factor in each axis so that
6989// we can enforce that coordinates match precisely as expected.
6990class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6991 public ::testing::WithParamInterface<ui::Rotation> {
6992public:
6993 void SetUp() override {
6994 SingleTouchInputMapperTest::SetUp();
6995
6996 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6997 // four times the resolution of the display in the Y axis.
6998 prepareButtons();
6999 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007000 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7001 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007002 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007003 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7004 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007005 }
7006
7007 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7008 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7009 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7010 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7011
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007012 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7013 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7014
7015 static const int32_t PRECISION_RAW_X_FLAT = 16;
7016 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7017
7018 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7019 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7020
Prabir Pradhana9df3162022-12-05 23:57:27 +00007021 static const std::array<Point, 4> kRawCorners;
7022};
7023
7024const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7025 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7026 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7027 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7028 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7029}};
7030
7031// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7032// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7033// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7034TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7035 enum class Orientation {
7036 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7037 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7038 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7039 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7040 ftl_last = ORIENTATION_270,
7041 };
7042 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7043 Orientation::ORIENTATION_270;
7044 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7045 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7046 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7047 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7048 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7049 };
7050
7051 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7052
7053 // Configure the touchscreen as being installed in the one of the four different orientations
7054 // relative to the display.
7055 addConfigurationProperty("touch.deviceType", "touchScreen");
7056 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7057 prepareDisplay(ui::ROTATION_0);
7058
7059 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7060
7061 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7062 // orientations of either 90 or 270) this means the display's natural resolution will be
7063 // flipped.
7064 const bool displayRotated =
7065 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7066 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7067 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7068 const Rect physicalFrame{0, 0, width, height};
7069 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7070
7071 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7072 const float expectedPrecisionX = displayRotated ? 4 : 2;
7073 const float expectedPrecisionY = displayRotated ? 2 : 4;
7074
7075 // Test all four corners.
7076 for (int i = 0; i < 4; i++) {
7077 const auto& raw = kRawCorners[i];
7078 processDown(mapper, raw.x, raw.y);
7079 processSync(mapper);
7080 const auto& expected = expectedPoints[i];
7081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7082 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7083 WithCoords(expected.x, expected.y),
7084 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7085 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7086 << "with touchscreen orientation "
7087 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7088 << expected.x << ", " << expected.y << ").";
7089 processUp(mapper);
7090 processSync(mapper);
7091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7092 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7093 WithCoords(expected.x, expected.y))));
7094 }
7095}
7096
Prabir Pradhan82687402022-12-06 01:32:53 +00007097TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7098 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7099 kMappedCorners = {
7100 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7101 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7102 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7103 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7104 };
7105
7106 const ui::Rotation displayRotation = GetParam();
7107
7108 addConfigurationProperty("touch.deviceType", "touchScreen");
7109 prepareDisplay(displayRotation);
7110
7111 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7112
7113 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7114
7115 // Test all four corners.
7116 for (int i = 0; i < 4; i++) {
7117 const auto& expected = expectedPoints[i];
7118 const auto& raw = kRawCorners[i];
7119 processDown(mapper, raw.x, raw.y);
7120 processSync(mapper);
7121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7122 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7123 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7124 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7125 << "with display rotation " << ui::toCString(displayRotation)
7126 << ", expected point (" << expected.x << ", " << expected.y << ").";
7127 processUp(mapper);
7128 processSync(mapper);
7129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7130 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7131 WithCoords(expected.x, expected.y))));
7132 }
7133}
7134
Prabir Pradhan3e798762022-12-02 21:02:11 +00007135TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7136 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7137 kMappedCorners = {
7138 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7139 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7140 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7141 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7142 };
7143
7144 const ui::Rotation displayRotation = GetParam();
7145
7146 addConfigurationProperty("touch.deviceType", "touchScreen");
7147 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7148
7149 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7150
7151 // Ori 270, so width and height swapped
7152 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7153 prepareDisplay(displayRotation);
7154 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7155
7156 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7157
7158 // Test all four corners.
7159 for (int i = 0; i < 4; i++) {
7160 const auto& expected = expectedPoints[i];
7161 const auto& raw = kRawCorners[i];
7162 processDown(mapper, raw.x, raw.y);
7163 processSync(mapper);
7164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7165 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7166 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7167 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7168 << "with display rotation " << ui::toCString(displayRotation)
7169 << ", expected point (" << expected.x << ", " << expected.y << ").";
7170 processUp(mapper);
7171 processSync(mapper);
7172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7173 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7174 WithCoords(expected.x, expected.y))));
7175 }
7176}
7177
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007178TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7179 const ui::Rotation displayRotation = GetParam();
7180
7181 addConfigurationProperty("touch.deviceType", "touchScreen");
7182 prepareDisplay(displayRotation);
7183
7184 __attribute__((unused)) SingleTouchInputMapper& mapper =
7185 addMapperAndConfigure<SingleTouchInputMapper>();
7186
7187 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7188 // MotionRanges use display pixels as their units
7189 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7190 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7191
7192 // The MotionRanges should be oriented in the rotated display's coordinate space
7193 const bool displayRotated =
7194 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7195
7196 constexpr float MAX_X = 479.5;
7197 constexpr float MAX_Y = 799.75;
7198 EXPECT_EQ(xRange->min, 0.f);
7199 EXPECT_EQ(yRange->min, 0.f);
7200 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7201 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7202
7203 EXPECT_EQ(xRange->flat, 8.f);
7204 EXPECT_EQ(yRange->flat, 8.f);
7205
7206 EXPECT_EQ(xRange->fuzz, 2.f);
7207 EXPECT_EQ(yRange->fuzz, 2.f);
7208
7209 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7210 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7211}
7212
Prabir Pradhana9df3162022-12-05 23:57:27 +00007213// Run the precision tests for all rotations.
7214INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7215 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7216 ui::ROTATION_270),
7217 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7218 return ftl::enum_string(testParamInfo.param);
7219 });
7220
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007221// --- ExternalStylusFusionTest ---
7222
7223class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7224public:
7225 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7226 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007227 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007228 prepareButtons();
7229 prepareAxes(POSITION);
7230 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7231
7232 mStylusState.when = ARBITRARY_TIME;
7233 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007234 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007235 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
7236 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
7237 processExternalStylusState(mapper);
7238 return mapper;
7239 }
7240
7241 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7242 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7243 for (const NotifyArgs& args : generatedArgs) {
7244 mFakeListener->notify(args);
7245 }
7246 // Loop the reader to flush the input listener queue.
7247 mReader->loopOnce();
7248 return generatedArgs;
7249 }
7250
7251protected:
7252 StylusState mStylusState{};
7253 static constexpr uint32_t EXPECTED_SOURCE =
7254 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7255
7256 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7257 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007258 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007259
7260 // The first pointer is withheld.
7261 processDown(mapper, 100, 200);
7262 processSync(mapper);
7263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7264 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7265 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7266
7267 // The external stylus reports pressure. The withheld finger pointer is released as a
7268 // stylus.
7269 mStylusState.pressure = 1.f;
7270 processExternalStylusState(mapper);
7271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7272 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7273 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7274
7275 // Subsequent pointer events are not withheld.
7276 processMove(mapper, 101, 201);
7277 processSync(mapper);
7278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7279 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7280
7281 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7283 }
7284
7285 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7286 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7287
7288 // Releasing the touch pointer ends the gesture.
7289 processUp(mapper);
7290 processSync(mapper);
7291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7292 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007293 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007294
7295 mStylusState.pressure = 0.f;
7296 processExternalStylusState(mapper);
7297 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7299 }
7300
7301 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7302 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007303 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007304
7305 // The first pointer is withheld when an external stylus is connected,
7306 // and a timeout is requested.
7307 processDown(mapper, 100, 200);
7308 processSync(mapper);
7309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7310 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7311 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7312
7313 // If the timeout expires early, it is requested again.
7314 handleTimeout(mapper, ARBITRARY_TIME + 1);
7315 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7316 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7317
7318 // When the timeout expires, the withheld touch is released as a finger pointer.
7319 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7321 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7322
7323 // Subsequent pointer events are not withheld.
7324 processMove(mapper, 101, 201);
7325 processSync(mapper);
7326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7327 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7328 processUp(mapper);
7329 processSync(mapper);
7330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7331 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7332
7333 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7335 }
7336
7337private:
7338 InputDeviceInfo mExternalStylusDeviceInfo{};
7339};
7340
7341TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7342 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7343 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7344}
7345
7346TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7347 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7348 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7349}
7350
7351TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7352 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7353 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7354}
7355
7356// Test a successful stylus fusion gesture where the pressure is reported by the external
7357// before the touch is reported by the touchscreen.
7358TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7359 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7360 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007361 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007362
7363 // The external stylus reports pressure first. It is ignored for now.
7364 mStylusState.pressure = 1.f;
7365 processExternalStylusState(mapper);
7366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7367 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7368
7369 // When the touch goes down afterwards, it is reported as a stylus pointer.
7370 processDown(mapper, 100, 200);
7371 processSync(mapper);
7372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7373 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7374 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7375
7376 processMove(mapper, 101, 201);
7377 processSync(mapper);
7378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7379 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7380 processUp(mapper);
7381 processSync(mapper);
7382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7383 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7384
7385 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7387}
7388
7389TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7390 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7391
7392 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7393 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7394
7395 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7396 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7397 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7398 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7399}
7400
7401TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7402 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7403 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007404 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007405
7406 mStylusState.pressure = 0.8f;
7407 processExternalStylusState(mapper);
7408 processDown(mapper, 100, 200);
7409 processSync(mapper);
7410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7411 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7412 WithPressure(0.8f))));
7413 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7414
7415 // The external stylus reports a pressure change. We wait for some time for a touch event.
7416 mStylusState.pressure = 0.6f;
7417 processExternalStylusState(mapper);
7418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7419 ASSERT_NO_FATAL_FAILURE(
7420 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7421
7422 // If a touch is reported within the timeout, it reports the updated pressure.
7423 processMove(mapper, 101, 201);
7424 processSync(mapper);
7425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7426 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7427 WithPressure(0.6f))));
7428 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7429
7430 // There is another pressure change.
7431 mStylusState.pressure = 0.5f;
7432 processExternalStylusState(mapper);
7433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7434 ASSERT_NO_FATAL_FAILURE(
7435 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7436
7437 // If a touch is not reported within the timeout, a move event is generated to report
7438 // the new pressure.
7439 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7441 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7442 WithPressure(0.5f))));
7443
7444 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7445 // repeated indefinitely.
7446 mStylusState.pressure = 0.0f;
7447 processExternalStylusState(mapper);
7448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7449 ASSERT_NO_FATAL_FAILURE(
7450 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7451 processMove(mapper, 102, 202);
7452 processSync(mapper);
7453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7454 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7455 WithPressure(0.5f))));
7456 processMove(mapper, 103, 203);
7457 processSync(mapper);
7458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7459 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7460 WithPressure(0.5f))));
7461
7462 processUp(mapper);
7463 processSync(mapper);
7464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7465 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007466 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007467
7468 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7470}
7471
7472TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7473 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7474 auto source = WithSource(EXPECTED_SOURCE);
7475
7476 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007477 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007478 processExternalStylusState(mapper);
7479 processDown(mapper, 100, 200);
7480 processSync(mapper);
7481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7482 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007483 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007484 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7485
7486 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007487 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007488 processExternalStylusState(mapper);
7489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7490 ASSERT_NO_FATAL_FAILURE(
7491 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7492
7493 // If a touch is reported within the timeout, it reports the updated pressure.
7494 processMove(mapper, 101, 201);
7495 processSync(mapper);
7496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7497 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007498 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007499 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7500
7501 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007502 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007503 processExternalStylusState(mapper);
7504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7505 ASSERT_NO_FATAL_FAILURE(
7506 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7507
7508 // If a touch is not reported within the timeout, a move event is generated to report
7509 // the new tool type.
7510 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7512 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007513 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007514
7515 processUp(mapper);
7516 processSync(mapper);
7517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7518 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007519 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007520
7521 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7523}
7524
7525TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7526 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7527 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007528 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007529
7530 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7531
7532 // The external stylus reports a button change. We wait for some time for a touch event.
7533 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7534 processExternalStylusState(mapper);
7535 ASSERT_NO_FATAL_FAILURE(
7536 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7537
7538 // If a touch is reported within the timeout, it reports the updated button state.
7539 processMove(mapper, 101, 201);
7540 processSync(mapper);
7541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7542 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7543 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7545 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7546 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7547 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7548
7549 // The button is now released.
7550 mStylusState.buttons = 0;
7551 processExternalStylusState(mapper);
7552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7553 ASSERT_NO_FATAL_FAILURE(
7554 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7555
7556 // If a touch is not reported within the timeout, a move event is generated to report
7557 // the new button state.
7558 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7560 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7561 WithButtonState(0))));
7562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007563 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7564 WithButtonState(0))));
7565
7566 processUp(mapper);
7567 processSync(mapper);
7568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007569 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7570
7571 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7573}
7574
Michael Wrightd02c5b62014-02-10 15:10:22 -08007575// --- MultiTouchInputMapperTest ---
7576
7577class MultiTouchInputMapperTest : public TouchInputMapperTest {
7578protected:
7579 void prepareAxes(int axes);
7580
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007581 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7582 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7583 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7584 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7585 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7586 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7587 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7588 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7589 void processId(MultiTouchInputMapper& mapper, int32_t id);
7590 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7591 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7592 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007593 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007594 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007595 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7596 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007597};
7598
7599void MultiTouchInputMapperTest::prepareAxes(int axes) {
7600 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007601 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7602 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007603 }
7604 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007605 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7606 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007607 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007608 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7609 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007610 }
7611 }
7612 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007613 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7614 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007615 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007616 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007617 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007618 }
7619 }
7620 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007621 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7622 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007623 }
7624 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007625 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7626 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007627 }
7628 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007629 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7630 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007631 }
7632 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007633 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7634 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007635 }
7636 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007637 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7638 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007639 }
7640 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007641 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007642 }
7643}
7644
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007645void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7646 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7648 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007649}
7650
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007651void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7652 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007653 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007654}
7655
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007656void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7657 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007658 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007659}
7660
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007661void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007662 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007663}
7664
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007665void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007666 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007667}
7668
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007669void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7670 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007671 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007672}
7673
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007674void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007675 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007676}
7677
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007678void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007679 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007680}
7681
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007682void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007684}
7685
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007686void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007688}
7689
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007690void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007692}
7693
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007694void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7695 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007696 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007697}
7698
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007699void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7700 int32_t value) {
7701 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7702 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7703}
7704
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007705void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007707}
7708
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007709void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7710 nsecs_t readTime) {
7711 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007712}
7713
Michael Wrightd02c5b62014-02-10 15:10:22 -08007714TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007715 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007716 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007717 prepareAxes(POSITION);
7718 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007719 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007720
arthurhungdcef2dc2020-08-11 14:47:50 +08007721 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007722
7723 NotifyMotionArgs motionArgs;
7724
7725 // Two fingers down at once.
7726 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7727 processPosition(mapper, x1, y1);
7728 processMTSync(mapper);
7729 processPosition(mapper, x2, y2);
7730 processMTSync(mapper);
7731 processSync(mapper);
7732
7733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7734 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7735 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7736 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7737 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7738 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7739 ASSERT_EQ(0, motionArgs.flags);
7740 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7741 ASSERT_EQ(0, motionArgs.buttonState);
7742 ASSERT_EQ(0, motionArgs.edgeFlags);
7743 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7744 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007745 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7747 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7748 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7749 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7750 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7751
7752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7753 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7754 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7755 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7756 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007757 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007758 ASSERT_EQ(0, motionArgs.flags);
7759 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7760 ASSERT_EQ(0, motionArgs.buttonState);
7761 ASSERT_EQ(0, motionArgs.edgeFlags);
7762 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7763 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007764 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007765 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007766 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7768 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7770 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7771 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7772 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7773 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7774
7775 // Move.
7776 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7777 processPosition(mapper, x1, y1);
7778 processMTSync(mapper);
7779 processPosition(mapper, x2, y2);
7780 processMTSync(mapper);
7781 processSync(mapper);
7782
7783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7784 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7785 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7786 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7787 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7788 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7789 ASSERT_EQ(0, motionArgs.flags);
7790 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7791 ASSERT_EQ(0, motionArgs.buttonState);
7792 ASSERT_EQ(0, motionArgs.edgeFlags);
7793 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7794 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007795 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007796 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007797 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7799 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7800 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7801 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7802 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7803 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7804 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7805
7806 // First finger up.
7807 x2 += 15; y2 -= 20;
7808 processPosition(mapper, x2, y2);
7809 processMTSync(mapper);
7810 processSync(mapper);
7811
7812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7813 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7814 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7815 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7816 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007817 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007818 ASSERT_EQ(0, motionArgs.flags);
7819 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7820 ASSERT_EQ(0, motionArgs.buttonState);
7821 ASSERT_EQ(0, motionArgs.edgeFlags);
7822 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7823 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007824 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007825 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007826 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7828 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7829 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7830 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7831 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7832 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7833 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7834
7835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7836 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7837 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7838 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7839 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7840 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7841 ASSERT_EQ(0, motionArgs.flags);
7842 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7843 ASSERT_EQ(0, motionArgs.buttonState);
7844 ASSERT_EQ(0, motionArgs.edgeFlags);
7845 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7846 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007847 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7849 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7850 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7851 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7852 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7853
7854 // Move.
7855 x2 += 20; y2 -= 25;
7856 processPosition(mapper, x2, y2);
7857 processMTSync(mapper);
7858 processSync(mapper);
7859
7860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7861 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7862 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7863 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7864 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7865 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7866 ASSERT_EQ(0, motionArgs.flags);
7867 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7868 ASSERT_EQ(0, motionArgs.buttonState);
7869 ASSERT_EQ(0, motionArgs.edgeFlags);
7870 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7871 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007872 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007873 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7874 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7875 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7876 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7877 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7878
7879 // New finger down.
7880 int32_t x3 = 700, y3 = 300;
7881 processPosition(mapper, x2, y2);
7882 processMTSync(mapper);
7883 processPosition(mapper, x3, y3);
7884 processMTSync(mapper);
7885 processSync(mapper);
7886
7887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7888 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7889 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7890 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7891 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007892 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007893 ASSERT_EQ(0, motionArgs.flags);
7894 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7895 ASSERT_EQ(0, motionArgs.buttonState);
7896 ASSERT_EQ(0, motionArgs.edgeFlags);
7897 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7898 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007899 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007900 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007901 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007902 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7903 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7904 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7905 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7906 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7907 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7908 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7909
7910 // Second finger up.
7911 x3 += 30; y3 -= 20;
7912 processPosition(mapper, x3, y3);
7913 processMTSync(mapper);
7914 processSync(mapper);
7915
7916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7917 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7918 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7919 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7920 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007921 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007922 ASSERT_EQ(0, motionArgs.flags);
7923 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7924 ASSERT_EQ(0, motionArgs.buttonState);
7925 ASSERT_EQ(0, motionArgs.edgeFlags);
7926 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7927 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007928 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007929 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007930 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7932 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7934 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7935 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7936 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7937 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7938
7939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7940 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7941 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7942 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7943 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7944 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7945 ASSERT_EQ(0, motionArgs.flags);
7946 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7947 ASSERT_EQ(0, motionArgs.buttonState);
7948 ASSERT_EQ(0, motionArgs.edgeFlags);
7949 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7950 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007951 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007952 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7953 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7954 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7955 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7956 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7957
7958 // Last finger up.
7959 processMTSync(mapper);
7960 processSync(mapper);
7961
7962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7963 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7964 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7965 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7966 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7967 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7968 ASSERT_EQ(0, motionArgs.flags);
7969 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7970 ASSERT_EQ(0, motionArgs.buttonState);
7971 ASSERT_EQ(0, motionArgs.edgeFlags);
7972 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7973 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007974 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7976 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7977 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7978 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7979 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7980
7981 // Should not have sent any more keys or motions.
7982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7984}
7985
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007986TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7987 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007988 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007989
7990 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7991 /*fuzz*/ 0, /*resolution*/ 10);
7992 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7993 /*fuzz*/ 0, /*resolution*/ 11);
7994 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7995 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7996 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7997 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7998 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7999 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8000 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8001 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8002
8003 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8004
8005 // X and Y axes
8006 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8007 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8008 // Touch major and minor
8009 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8010 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8011 // Tool major and minor
8012 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8013 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8014}
8015
8016TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8017 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008018 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008019
8020 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8021 /*fuzz*/ 0, /*resolution*/ 10);
8022 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8023 /*fuzz*/ 0, /*resolution*/ 11);
8024
8025 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8026
8027 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8028
8029 // Touch major and minor
8030 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8031 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8032 // Tool major and minor
8033 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8034 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8035}
8036
Michael Wrightd02c5b62014-02-10 15:10:22 -08008037TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008038 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008039 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008040 prepareAxes(POSITION | ID);
8041 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008042 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008043
arthurhungdcef2dc2020-08-11 14:47:50 +08008044 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008045
8046 NotifyMotionArgs motionArgs;
8047
8048 // Two fingers down at once.
8049 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8050 processPosition(mapper, x1, y1);
8051 processId(mapper, 1);
8052 processMTSync(mapper);
8053 processPosition(mapper, x2, y2);
8054 processId(mapper, 2);
8055 processMTSync(mapper);
8056 processSync(mapper);
8057
8058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8059 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8060 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8061 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008062 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8064 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8065
8066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008067 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008068 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8069 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008070 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008071 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008072 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008073 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8074 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8076 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8077
8078 // Move.
8079 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8080 processPosition(mapper, x1, y1);
8081 processId(mapper, 1);
8082 processMTSync(mapper);
8083 processPosition(mapper, x2, y2);
8084 processId(mapper, 2);
8085 processMTSync(mapper);
8086 processSync(mapper);
8087
8088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8089 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8090 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8091 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008092 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008093 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008094 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008095 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8096 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8098 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8099
8100 // First finger up.
8101 x2 += 15; y2 -= 20;
8102 processPosition(mapper, x2, y2);
8103 processId(mapper, 2);
8104 processMTSync(mapper);
8105 processSync(mapper);
8106
8107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008108 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008109 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8110 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008111 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008112 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008113 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008114 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8115 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8117 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8118
8119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8120 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8121 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8122 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008123 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008124 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8125 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8126
8127 // Move.
8128 x2 += 20; y2 -= 25;
8129 processPosition(mapper, x2, y2);
8130 processId(mapper, 2);
8131 processMTSync(mapper);
8132 processSync(mapper);
8133
8134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8135 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8136 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8137 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008138 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8140 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8141
8142 // New finger down.
8143 int32_t x3 = 700, y3 = 300;
8144 processPosition(mapper, x2, y2);
8145 processId(mapper, 2);
8146 processMTSync(mapper);
8147 processPosition(mapper, x3, y3);
8148 processId(mapper, 3);
8149 processMTSync(mapper);
8150 processSync(mapper);
8151
8152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008153 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008154 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8155 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008156 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008157 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008158 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008159 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8160 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8161 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8162 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8163
8164 // Second finger up.
8165 x3 += 30; y3 -= 20;
8166 processPosition(mapper, x3, y3);
8167 processId(mapper, 3);
8168 processMTSync(mapper);
8169 processSync(mapper);
8170
8171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008172 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008173 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8174 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008175 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008176 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008177 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008178 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8179 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8181 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8182
8183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8185 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8186 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008187 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008188 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8189 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8190
8191 // Last finger up.
8192 processMTSync(mapper);
8193 processSync(mapper);
8194
8195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8196 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8197 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8198 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008199 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008200 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8201 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8202
8203 // Should not have sent any more keys or motions.
8204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8206}
8207
8208TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008209 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008210 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008211 prepareAxes(POSITION | ID | SLOT);
8212 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008213 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008214
arthurhungdcef2dc2020-08-11 14:47:50 +08008215 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008216
8217 NotifyMotionArgs motionArgs;
8218
8219 // Two fingers down at once.
8220 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8221 processPosition(mapper, x1, y1);
8222 processId(mapper, 1);
8223 processSlot(mapper, 1);
8224 processPosition(mapper, x2, y2);
8225 processId(mapper, 2);
8226 processSync(mapper);
8227
8228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8229 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8230 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8231 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008232 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8234 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8235
8236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008237 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008238 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8239 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008240 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008241 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008242 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8244 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8246 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8247
8248 // Move.
8249 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8250 processSlot(mapper, 0);
8251 processPosition(mapper, x1, y1);
8252 processSlot(mapper, 1);
8253 processPosition(mapper, x2, y2);
8254 processSync(mapper);
8255
8256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8258 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8259 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008260 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008261 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008262 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8264 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8265 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8266 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8267
8268 // First finger up.
8269 x2 += 15; y2 -= 20;
8270 processSlot(mapper, 0);
8271 processId(mapper, -1);
8272 processSlot(mapper, 1);
8273 processPosition(mapper, x2, y2);
8274 processSync(mapper);
8275
8276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008277 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008278 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8279 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008280 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008281 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008282 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008283 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8284 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8286 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8287
8288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8290 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8291 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008292 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8294 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8295
8296 // Move.
8297 x2 += 20; y2 -= 25;
8298 processPosition(mapper, x2, y2);
8299 processSync(mapper);
8300
8301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8303 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8304 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008305 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8307 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8308
8309 // New finger down.
8310 int32_t x3 = 700, y3 = 300;
8311 processPosition(mapper, x2, y2);
8312 processSlot(mapper, 0);
8313 processId(mapper, 3);
8314 processPosition(mapper, x3, y3);
8315 processSync(mapper);
8316
8317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008318 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008319 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8320 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008321 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008322 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008323 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008324 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8325 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8327 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8328
8329 // Second finger up.
8330 x3 += 30; y3 -= 20;
8331 processSlot(mapper, 1);
8332 processId(mapper, -1);
8333 processSlot(mapper, 0);
8334 processPosition(mapper, x3, y3);
8335 processSync(mapper);
8336
8337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008338 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008339 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8340 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008341 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008342 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008343 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8345 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8347 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8348
8349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8351 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8352 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008353 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8355 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8356
8357 // Last finger up.
8358 processId(mapper, -1);
8359 processSync(mapper);
8360
8361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8362 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8363 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8364 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008365 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8367 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8368
8369 // Should not have sent any more keys or motions.
8370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8372}
8373
8374TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008375 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008376 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008377 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008378 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008379
8380 // These calculations are based on the input device calibration documentation.
8381 int32_t rawX = 100;
8382 int32_t rawY = 200;
8383 int32_t rawTouchMajor = 7;
8384 int32_t rawTouchMinor = 6;
8385 int32_t rawToolMajor = 9;
8386 int32_t rawToolMinor = 8;
8387 int32_t rawPressure = 11;
8388 int32_t rawDistance = 0;
8389 int32_t rawOrientation = 3;
8390 int32_t id = 5;
8391
8392 float x = toDisplayX(rawX);
8393 float y = toDisplayY(rawY);
8394 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8395 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8396 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8397 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8398 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8399 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8400 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8401 float distance = float(rawDistance);
8402
8403 processPosition(mapper, rawX, rawY);
8404 processTouchMajor(mapper, rawTouchMajor);
8405 processTouchMinor(mapper, rawTouchMinor);
8406 processToolMajor(mapper, rawToolMajor);
8407 processToolMinor(mapper, rawToolMinor);
8408 processPressure(mapper, rawPressure);
8409 processOrientation(mapper, rawOrientation);
8410 processDistance(mapper, rawDistance);
8411 processId(mapper, id);
8412 processMTSync(mapper);
8413 processSync(mapper);
8414
8415 NotifyMotionArgs args;
8416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8417 ASSERT_EQ(0, args.pointerProperties[0].id);
8418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8419 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8420 orientation, distance));
8421}
8422
8423TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008424 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008425 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008426 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8427 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008428 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008429
8430 // These calculations are based on the input device calibration documentation.
8431 int32_t rawX = 100;
8432 int32_t rawY = 200;
8433 int32_t rawTouchMajor = 140;
8434 int32_t rawTouchMinor = 120;
8435 int32_t rawToolMajor = 180;
8436 int32_t rawToolMinor = 160;
8437
8438 float x = toDisplayX(rawX);
8439 float y = toDisplayY(rawY);
8440 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8441 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8442 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8443 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8444 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8445
8446 processPosition(mapper, rawX, rawY);
8447 processTouchMajor(mapper, rawTouchMajor);
8448 processTouchMinor(mapper, rawTouchMinor);
8449 processToolMajor(mapper, rawToolMajor);
8450 processToolMinor(mapper, rawToolMinor);
8451 processMTSync(mapper);
8452 processSync(mapper);
8453
8454 NotifyMotionArgs args;
8455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8457 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8458}
8459
8460TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008461 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008462 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008463 prepareAxes(POSITION | TOUCH | TOOL);
8464 addConfigurationProperty("touch.size.calibration", "diameter");
8465 addConfigurationProperty("touch.size.scale", "10");
8466 addConfigurationProperty("touch.size.bias", "160");
8467 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008468 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008469
8470 // These calculations are based on the input device calibration documentation.
8471 // Note: We only provide a single common touch/tool value because the device is assumed
8472 // not to emit separate values for each pointer (isSummed = 1).
8473 int32_t rawX = 100;
8474 int32_t rawY = 200;
8475 int32_t rawX2 = 150;
8476 int32_t rawY2 = 250;
8477 int32_t rawTouchMajor = 5;
8478 int32_t rawToolMajor = 8;
8479
8480 float x = toDisplayX(rawX);
8481 float y = toDisplayY(rawY);
8482 float x2 = toDisplayX(rawX2);
8483 float y2 = toDisplayY(rawY2);
8484 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8485 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8486 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8487
8488 processPosition(mapper, rawX, rawY);
8489 processTouchMajor(mapper, rawTouchMajor);
8490 processToolMajor(mapper, rawToolMajor);
8491 processMTSync(mapper);
8492 processPosition(mapper, rawX2, rawY2);
8493 processTouchMajor(mapper, rawTouchMajor);
8494 processToolMajor(mapper, rawToolMajor);
8495 processMTSync(mapper);
8496 processSync(mapper);
8497
8498 NotifyMotionArgs args;
8499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8500 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8501
8502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008503 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008504 ASSERT_EQ(size_t(2), args.pointerCount);
8505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8506 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8508 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8509}
8510
8511TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008512 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008513 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008514 prepareAxes(POSITION | TOUCH | TOOL);
8515 addConfigurationProperty("touch.size.calibration", "area");
8516 addConfigurationProperty("touch.size.scale", "43");
8517 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008518 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008519
8520 // These calculations are based on the input device calibration documentation.
8521 int32_t rawX = 100;
8522 int32_t rawY = 200;
8523 int32_t rawTouchMajor = 5;
8524 int32_t rawToolMajor = 8;
8525
8526 float x = toDisplayX(rawX);
8527 float y = toDisplayY(rawY);
8528 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8529 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8530 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8531
8532 processPosition(mapper, rawX, rawY);
8533 processTouchMajor(mapper, rawTouchMajor);
8534 processToolMajor(mapper, rawToolMajor);
8535 processMTSync(mapper);
8536 processSync(mapper);
8537
8538 NotifyMotionArgs args;
8539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8541 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8542}
8543
8544TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008545 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008546 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008547 prepareAxes(POSITION | PRESSURE);
8548 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8549 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008550 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008551
Michael Wrightaa449c92017-12-13 21:21:43 +00008552 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008553 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008554 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8555 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8556 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8557
Michael Wrightd02c5b62014-02-10 15:10:22 -08008558 // These calculations are based on the input device calibration documentation.
8559 int32_t rawX = 100;
8560 int32_t rawY = 200;
8561 int32_t rawPressure = 60;
8562
8563 float x = toDisplayX(rawX);
8564 float y = toDisplayY(rawY);
8565 float pressure = float(rawPressure) * 0.01f;
8566
8567 processPosition(mapper, rawX, rawY);
8568 processPressure(mapper, rawPressure);
8569 processMTSync(mapper);
8570 processSync(mapper);
8571
8572 NotifyMotionArgs args;
8573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8575 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8576}
8577
8578TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008579 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008580 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008581 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008582 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008583
8584 NotifyMotionArgs motionArgs;
8585 NotifyKeyArgs keyArgs;
8586
8587 processId(mapper, 1);
8588 processPosition(mapper, 100, 200);
8589 processSync(mapper);
8590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8591 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8592 ASSERT_EQ(0, motionArgs.buttonState);
8593
8594 // press BTN_LEFT, release BTN_LEFT
8595 processKey(mapper, BTN_LEFT, 1);
8596 processSync(mapper);
8597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8599 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8600
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8602 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8603 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8604
Michael Wrightd02c5b62014-02-10 15:10:22 -08008605 processKey(mapper, BTN_LEFT, 0);
8606 processSync(mapper);
8607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008608 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008609 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008610
8611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008612 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008613 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008614
8615 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8616 processKey(mapper, BTN_RIGHT, 1);
8617 processKey(mapper, BTN_MIDDLE, 1);
8618 processSync(mapper);
8619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8620 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8621 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8622 motionArgs.buttonState);
8623
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8625 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8626 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8627
8628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8629 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8630 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8631 motionArgs.buttonState);
8632
Michael Wrightd02c5b62014-02-10 15:10:22 -08008633 processKey(mapper, BTN_RIGHT, 0);
8634 processSync(mapper);
8635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008636 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008637 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008638
8639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008640 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008641 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008642
8643 processKey(mapper, BTN_MIDDLE, 0);
8644 processSync(mapper);
8645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008646 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008647 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008648
8649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008651 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008652
8653 // press BTN_BACK, release BTN_BACK
8654 processKey(mapper, BTN_BACK, 1);
8655 processSync(mapper);
8656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8657 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8658 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008659
Michael Wrightd02c5b62014-02-10 15:10:22 -08008660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008661 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008662 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8663
8664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8665 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8666 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008667
8668 processKey(mapper, BTN_BACK, 0);
8669 processSync(mapper);
8670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008671 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008672 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008673
8674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008675 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008676 ASSERT_EQ(0, motionArgs.buttonState);
8677
Michael Wrightd02c5b62014-02-10 15:10:22 -08008678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8679 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8680 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8681
8682 // press BTN_SIDE, release BTN_SIDE
8683 processKey(mapper, BTN_SIDE, 1);
8684 processSync(mapper);
8685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8686 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8687 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008688
Michael Wrightd02c5b62014-02-10 15:10:22 -08008689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008690 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008691 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8692
8693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8694 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8695 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008696
8697 processKey(mapper, BTN_SIDE, 0);
8698 processSync(mapper);
8699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008700 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008701 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008702
8703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008704 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008705 ASSERT_EQ(0, motionArgs.buttonState);
8706
Michael Wrightd02c5b62014-02-10 15:10:22 -08008707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8708 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8709 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8710
8711 // press BTN_FORWARD, release BTN_FORWARD
8712 processKey(mapper, BTN_FORWARD, 1);
8713 processSync(mapper);
8714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8715 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8716 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008717
Michael Wrightd02c5b62014-02-10 15:10:22 -08008718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008719 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008720 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8721
8722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8723 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8724 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008725
8726 processKey(mapper, BTN_FORWARD, 0);
8727 processSync(mapper);
8728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008729 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008730 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008731
8732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008733 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008734 ASSERT_EQ(0, motionArgs.buttonState);
8735
Michael Wrightd02c5b62014-02-10 15:10:22 -08008736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8737 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8738 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8739
8740 // press BTN_EXTRA, release BTN_EXTRA
8741 processKey(mapper, BTN_EXTRA, 1);
8742 processSync(mapper);
8743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8744 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8745 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008746
Michael Wrightd02c5b62014-02-10 15:10:22 -08008747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008749 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8750
8751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8752 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8753 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008754
8755 processKey(mapper, BTN_EXTRA, 0);
8756 processSync(mapper);
8757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008758 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008759 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008760
8761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008762 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008763 ASSERT_EQ(0, motionArgs.buttonState);
8764
Michael Wrightd02c5b62014-02-10 15:10:22 -08008765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8766 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8767 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8768
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8770
Michael Wrightd02c5b62014-02-10 15:10:22 -08008771 // press BTN_STYLUS, release BTN_STYLUS
8772 processKey(mapper, BTN_STYLUS, 1);
8773 processSync(mapper);
8774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008776 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8777
8778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8779 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8780 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008781
8782 processKey(mapper, BTN_STYLUS, 0);
8783 processSync(mapper);
8784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008785 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008786 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008787
8788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008790 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008791
8792 // press BTN_STYLUS2, release BTN_STYLUS2
8793 processKey(mapper, BTN_STYLUS2, 1);
8794 processSync(mapper);
8795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8796 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008797 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8798
8799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8800 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8801 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008802
8803 processKey(mapper, BTN_STYLUS2, 0);
8804 processSync(mapper);
8805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008806 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008807 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008808
8809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008811 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008812
8813 // release touch
8814 processId(mapper, -1);
8815 processSync(mapper);
8816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8817 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8818 ASSERT_EQ(0, motionArgs.buttonState);
8819}
8820
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008821TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8822 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008823 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008824 prepareAxes(POSITION | ID | SLOT);
8825 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8826
8827 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8828 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8829
8830 // Touch down.
8831 processId(mapper, 1);
8832 processPosition(mapper, 100, 200);
8833 processSync(mapper);
8834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8835 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8836
8837 // Press and release button mapped to the primary stylus button.
8838 processKey(mapper, BTN_A, 1);
8839 processSync(mapper);
8840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8841 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8842 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8844 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8845 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8846
8847 processKey(mapper, BTN_A, 0);
8848 processSync(mapper);
8849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8850 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8852 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8853
8854 // Press and release the HID usage mapped to the secondary stylus button.
8855 processHidUsage(mapper, 0xabcd, 1);
8856 processSync(mapper);
8857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8858 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8859 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8861 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8862 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8863
8864 processHidUsage(mapper, 0xabcd, 0);
8865 processSync(mapper);
8866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8867 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8869 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8870
8871 // Release touch.
8872 processId(mapper, -1);
8873 processSync(mapper);
8874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8875 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8876}
8877
Michael Wrightd02c5b62014-02-10 15:10:22 -08008878TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008879 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008880 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008881 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008882 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008883
8884 NotifyMotionArgs motionArgs;
8885
8886 // default tool type is finger
8887 processId(mapper, 1);
8888 processPosition(mapper, 100, 200);
8889 processSync(mapper);
8890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8891 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008892 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008893
8894 // eraser
8895 processKey(mapper, BTN_TOOL_RUBBER, 1);
8896 processSync(mapper);
8897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8898 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008899 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008900
8901 // stylus
8902 processKey(mapper, BTN_TOOL_RUBBER, 0);
8903 processKey(mapper, BTN_TOOL_PEN, 1);
8904 processSync(mapper);
8905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8906 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008907 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008908
8909 // brush
8910 processKey(mapper, BTN_TOOL_PEN, 0);
8911 processKey(mapper, BTN_TOOL_BRUSH, 1);
8912 processSync(mapper);
8913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8914 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008915 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008916
8917 // pencil
8918 processKey(mapper, BTN_TOOL_BRUSH, 0);
8919 processKey(mapper, BTN_TOOL_PENCIL, 1);
8920 processSync(mapper);
8921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8922 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008923 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008924
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008925 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008926 processKey(mapper, BTN_TOOL_PENCIL, 0);
8927 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8928 processSync(mapper);
8929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8930 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008931 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008932
8933 // mouse
8934 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8935 processKey(mapper, BTN_TOOL_MOUSE, 1);
8936 processSync(mapper);
8937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8938 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008939 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008940
8941 // lens
8942 processKey(mapper, BTN_TOOL_MOUSE, 0);
8943 processKey(mapper, BTN_TOOL_LENS, 1);
8944 processSync(mapper);
8945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8946 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008947 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008948
8949 // double-tap
8950 processKey(mapper, BTN_TOOL_LENS, 0);
8951 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8952 processSync(mapper);
8953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8954 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008955 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008956
8957 // triple-tap
8958 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8959 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8960 processSync(mapper);
8961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8962 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008963 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008964
8965 // quad-tap
8966 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8967 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8968 processSync(mapper);
8969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8970 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008971 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008972
8973 // finger
8974 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8975 processKey(mapper, BTN_TOOL_FINGER, 1);
8976 processSync(mapper);
8977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8978 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008979 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008980
8981 // stylus trumps finger
8982 processKey(mapper, BTN_TOOL_PEN, 1);
8983 processSync(mapper);
8984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8985 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008986 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008987
8988 // eraser trumps stylus
8989 processKey(mapper, BTN_TOOL_RUBBER, 1);
8990 processSync(mapper);
8991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8992 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008993 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008994
8995 // mouse trumps eraser
8996 processKey(mapper, BTN_TOOL_MOUSE, 1);
8997 processSync(mapper);
8998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8999 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009000 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009001
9002 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9003 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9004 processSync(mapper);
9005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009007 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009008
9009 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9010 processToolType(mapper, MT_TOOL_PEN);
9011 processSync(mapper);
9012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9013 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009014 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009015
9016 // back to default tool type
9017 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9018 processKey(mapper, BTN_TOOL_MOUSE, 0);
9019 processKey(mapper, BTN_TOOL_RUBBER, 0);
9020 processKey(mapper, BTN_TOOL_PEN, 0);
9021 processKey(mapper, BTN_TOOL_FINGER, 0);
9022 processSync(mapper);
9023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009025 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009026}
9027
9028TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009029 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009030 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009031 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009032 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009033 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009034
9035 NotifyMotionArgs motionArgs;
9036
9037 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9038 processId(mapper, 1);
9039 processPosition(mapper, 100, 200);
9040 processSync(mapper);
9041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9042 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9044 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9045
9046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9047 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9048 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9049 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9050
9051 // move a little
9052 processPosition(mapper, 150, 250);
9053 processSync(mapper);
9054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9055 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9056 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9057 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9058
9059 // down when BTN_TOUCH is pressed, pressure defaults to 1
9060 processKey(mapper, BTN_TOUCH, 1);
9061 processSync(mapper);
9062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9063 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9065 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9066
9067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9068 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9069 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9070 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9071
9072 // up when BTN_TOUCH is released, hover restored
9073 processKey(mapper, BTN_TOUCH, 0);
9074 processSync(mapper);
9075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9076 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9078 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9079
9080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9081 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9082 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9083 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9084
9085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9086 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9087 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9088 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9089
9090 // exit hover when pointer goes away
9091 processId(mapper, -1);
9092 processSync(mapper);
9093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9094 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9095 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9096 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9097}
9098
9099TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009100 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009101 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009102 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009103 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009104
9105 NotifyMotionArgs motionArgs;
9106
9107 // initially hovering because pressure is 0
9108 processId(mapper, 1);
9109 processPosition(mapper, 100, 200);
9110 processPressure(mapper, 0);
9111 processSync(mapper);
9112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9113 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9114 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9115 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9116
9117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9118 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9119 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9120 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9121
9122 // move a little
9123 processPosition(mapper, 150, 250);
9124 processSync(mapper);
9125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9126 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9128 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9129
9130 // down when pressure becomes non-zero
9131 processPressure(mapper, RAW_PRESSURE_MAX);
9132 processSync(mapper);
9133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9134 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9135 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9136 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9137
9138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9139 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9140 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9141 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9142
9143 // up when pressure becomes 0, hover restored
9144 processPressure(mapper, 0);
9145 processSync(mapper);
9146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9147 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9149 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9150
9151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9152 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9154 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9155
9156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9157 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9159 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9160
9161 // exit hover when pointer goes away
9162 processId(mapper, -1);
9163 processSync(mapper);
9164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9165 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9166 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9167 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9168}
9169
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009170/**
9171 * Set the input device port <--> display port associations, and check that the
9172 * events are routed to the display that matches the display port.
9173 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9174 */
9175TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009176 const std::string usb2 = "USB2";
9177 const uint8_t hdmi1 = 0;
9178 const uint8_t hdmi2 = 1;
9179 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009180 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009181
9182 addConfigurationProperty("touch.deviceType", "touchScreen");
9183 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009184 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009185
9186 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9187 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9188
9189 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9190 // for this input device is specified, and the matching viewport is not present,
9191 // the input device should be disabled (at the mapper level).
9192
9193 // Add viewport for display 2 on hdmi2
9194 prepareSecondaryDisplay(type, hdmi2);
9195 // Send a touch event
9196 processPosition(mapper, 100, 100);
9197 processSync(mapper);
9198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9199
9200 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009201 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009202 // Send a touch event again
9203 processPosition(mapper, 100, 100);
9204 processSync(mapper);
9205
9206 NotifyMotionArgs args;
9207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9208 ASSERT_EQ(DISPLAY_ID, args.displayId);
9209}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009210
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009211TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9212 addConfigurationProperty("touch.deviceType", "touchScreen");
9213 prepareAxes(POSITION);
9214 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9215
9216 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9217
Michael Wrighta9cf4192022-12-01 23:46:39 +00009218 prepareDisplay(ui::ROTATION_0);
9219 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009220
9221 // Send a touch event
9222 processPosition(mapper, 100, 100);
9223 processSync(mapper);
9224
9225 NotifyMotionArgs args;
9226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9227 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9228}
9229
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009230TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009231 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009232 std::shared_ptr<FakePointerController> fakePointerController =
9233 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009234 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009235 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009236 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009237
Garfield Tan888a6a42020-01-09 11:39:16 -08009238 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009239 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009240
Michael Wrighta9cf4192022-12-01 23:46:39 +00009241 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009242 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009243 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009244
Josep del Río2d8c79a2023-01-23 19:33:50 +00009245 // Check source is mouse that would obtain the PointerController.
9246 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009247
9248 NotifyMotionArgs motionArgs;
9249 processPosition(mapper, 100, 100);
9250 processSync(mapper);
9251
9252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9253 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9254 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9255}
9256
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009257/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009258 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9259 */
9260TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9261 addConfigurationProperty("touch.deviceType", "touchScreen");
9262 prepareAxes(POSITION);
9263 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9264
Michael Wrighta9cf4192022-12-01 23:46:39 +00009265 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009266 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9267 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9268 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9269 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009270
9271 NotifyMotionArgs args;
9272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9273 ASSERT_EQ(26, args.readTime);
9274
Harry Cutts33476232023-01-30 19:57:29 +00009275 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9276 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9277 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009278
9279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9280 ASSERT_EQ(33, args.readTime);
9281}
9282
9283/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009284 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9285 * events should not be delivered to the listener.
9286 */
9287TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9288 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009289 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009290 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009291 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009292 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9293 prepareAxes(POSITION);
9294 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9295
9296 NotifyMotionArgs motionArgs;
9297 processPosition(mapper, 100, 100);
9298 processSync(mapper);
9299
9300 mFakeListener->assertNotifyMotionWasNotCalled();
9301}
9302
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009303/**
9304 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9305 * the touch mapper can process the events and the events can be delivered to the listener.
9306 */
9307TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9308 addConfigurationProperty("touch.deviceType", "touchScreen");
9309 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009310 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009311 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009312 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9313 prepareAxes(POSITION);
9314 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9315
9316 NotifyMotionArgs motionArgs;
9317 processPosition(mapper, 100, 100);
9318 processSync(mapper);
9319
9320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9321 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9322}
9323
Garfield Tanc734e4f2021-01-15 20:01:39 -08009324TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9325 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009326 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009327 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009328 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009329 std::optional<DisplayViewport> optionalDisplayViewport =
9330 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9331 ASSERT_TRUE(optionalDisplayViewport.has_value());
9332 DisplayViewport displayViewport = *optionalDisplayViewport;
9333
9334 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9335 prepareAxes(POSITION);
9336 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9337
9338 // Finger down
9339 int32_t x = 100, y = 100;
9340 processPosition(mapper, x, y);
9341 processSync(mapper);
9342
9343 NotifyMotionArgs motionArgs;
9344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9345 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9346
9347 // Deactivate display viewport
9348 displayViewport.isActive = false;
9349 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9350 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9351
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009352 // The ongoing touch should be canceled immediately
9353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9354 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9355
9356 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009357 x += 10, y += 10;
9358 processPosition(mapper, x, y);
9359 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009361
9362 // Reactivate display viewport
9363 displayViewport.isActive = true;
9364 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9365 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9366
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009367 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009368 x += 10, y += 10;
9369 processPosition(mapper, x, y);
9370 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9372 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009373}
9374
Arthur Hung7c645402019-01-25 17:45:42 +08009375TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9376 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009377 prepareAxes(POSITION | ID | SLOT);
9378 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009379 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009380
9381 // Create the second touch screen device, and enable multi fingers.
9382 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009383 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009384 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009385 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009386 std::shared_ptr<InputDevice> device2 =
9387 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009388 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009389
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009390 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_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_POSITION_Y, RAW_Y_MIN, RAW_Y_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_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009395 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009396 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009397 /*flat=*/0, /*fuzz=*/0);
9398 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009399 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9400 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009401
9402 // Setup the second touch screen device.
Arpit Singh8e6fb252023-04-06 11:49:17 +00009403 MultiTouchInputMapper& mapper2 =
9404 device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID,
9405 mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009406 std::list<NotifyArgs> unused =
9407 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00009408 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009409 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009410
9411 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009412 std::shared_ptr<FakePointerController> fakePointerController =
9413 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009414 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009415
9416 // Setup policy for associated displays and show touches.
9417 const uint8_t hdmi1 = 0;
9418 const uint8_t hdmi2 = 1;
9419 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9420 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9421 mFakePolicy->setShowTouches(true);
9422
9423 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009424 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009425 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009426
9427 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009428 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9429 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9430 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009431
9432 // Two fingers down at default display.
9433 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9434 processPosition(mapper, x1, y1);
9435 processId(mapper, 1);
9436 processSlot(mapper, 1);
9437 processPosition(mapper, x2, y2);
9438 processId(mapper, 2);
9439 processSync(mapper);
9440
9441 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9442 fakePointerController->getSpots().find(DISPLAY_ID);
9443 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9444 ASSERT_EQ(size_t(2), iter->second.size());
9445
9446 // Two fingers down at second display.
9447 processPosition(mapper2, x1, y1);
9448 processId(mapper2, 1);
9449 processSlot(mapper2, 1);
9450 processPosition(mapper2, x2, y2);
9451 processId(mapper2, 2);
9452 processSync(mapper2);
9453
9454 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9455 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9456 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009457
9458 // Disable the show touches configuration and ensure the spots are cleared.
9459 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009460 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9461 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009462
9463 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009464}
9465
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009466TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009467 prepareAxes(POSITION);
9468 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009469 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009470 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009471
9472 NotifyMotionArgs motionArgs;
9473 // Unrotated video frame
9474 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9475 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009476 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009477 processPosition(mapper, 100, 200);
9478 processSync(mapper);
9479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9480 ASSERT_EQ(frames, motionArgs.videoFrames);
9481
9482 // Subsequent touch events should not have any videoframes
9483 // This is implemented separately in FakeEventHub,
9484 // but that should match the behaviour of TouchVideoDevice.
9485 processPosition(mapper, 200, 200);
9486 processSync(mapper);
9487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9488 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9489}
9490
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009491TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009492 prepareAxes(POSITION);
9493 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009494 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009495 // Unrotated video frame
9496 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9497 NotifyMotionArgs motionArgs;
9498
9499 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009500 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009501 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9502 clearViewports();
9503 prepareDisplay(orientation);
9504 std::vector<TouchVideoFrame> frames{frame};
9505 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9506 processPosition(mapper, 100, 200);
9507 processSync(mapper);
9508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9509 ASSERT_EQ(frames, motionArgs.videoFrames);
9510 }
9511}
9512
9513TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9514 prepareAxes(POSITION);
9515 addConfigurationProperty("touch.deviceType", "touchScreen");
9516 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9517 // orientation-aware are affected by display rotation.
9518 addConfigurationProperty("touch.orientationAware", "0");
9519 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9520 // Unrotated video frame
9521 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9522 NotifyMotionArgs motionArgs;
9523
9524 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009525 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009526 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9527 clearViewports();
9528 prepareDisplay(orientation);
9529 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009530 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009531 processPosition(mapper, 100, 200);
9532 processSync(mapper);
9533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009534 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9535 // compared to the display. This is so that when the window transform (which contains the
9536 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9537 // window's coordinate space.
9538 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009539 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009540
9541 // Release finger.
9542 processSync(mapper);
9543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009544 }
9545}
9546
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009547TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009548 prepareAxes(POSITION);
9549 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009550 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009551 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9552 // so mix these.
9553 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9554 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9555 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9556 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9557 NotifyMotionArgs motionArgs;
9558
Michael Wrighta9cf4192022-12-01 23:46:39 +00009559 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009560 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009561 processPosition(mapper, 100, 200);
9562 processSync(mapper);
9563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009564 ASSERT_EQ(frames, motionArgs.videoFrames);
9565}
9566
9567TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9568 prepareAxes(POSITION);
9569 addConfigurationProperty("touch.deviceType", "touchScreen");
9570 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9571 // orientation-aware are affected by display rotation.
9572 addConfigurationProperty("touch.orientationAware", "0");
9573 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9574 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9575 // so mix these.
9576 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9577 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9578 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9579 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9580 NotifyMotionArgs motionArgs;
9581
Michael Wrighta9cf4192022-12-01 23:46:39 +00009582 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009583 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9584 processPosition(mapper, 100, 200);
9585 processSync(mapper);
9586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9587 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9588 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9589 // compared to the display. This is so that when the window transform (which contains the
9590 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9591 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009592 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009593 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009594 ASSERT_EQ(frames, motionArgs.videoFrames);
9595}
9596
Arthur Hung9da14732019-09-02 16:16:58 +08009597/**
9598 * If we had defined port associations, but the viewport is not ready, the touch device would be
9599 * expected to be disabled, and it should be enabled after the viewport has found.
9600 */
9601TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009602 constexpr uint8_t hdmi2 = 1;
9603 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009604 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009605
9606 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9607
9608 addConfigurationProperty("touch.deviceType", "touchScreen");
9609 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009610 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009611
9612 ASSERT_EQ(mDevice->isEnabled(), false);
9613
9614 // Add display on hdmi2, the device should be enabled and can receive touch event.
9615 prepareSecondaryDisplay(type, hdmi2);
9616 ASSERT_EQ(mDevice->isEnabled(), true);
9617
9618 // Send a touch event.
9619 processPosition(mapper, 100, 100);
9620 processSync(mapper);
9621
9622 NotifyMotionArgs args;
9623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9624 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9625}
9626
Arthur Hung421eb1c2020-01-16 00:09:42 +08009627TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009628 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009629 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009630 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009631 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009632
9633 NotifyMotionArgs motionArgs;
9634
9635 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9636 // finger down
9637 processId(mapper, 1);
9638 processPosition(mapper, x1, y1);
9639 processSync(mapper);
9640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9641 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009642 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009643
9644 // finger move
9645 processId(mapper, 1);
9646 processPosition(mapper, x2, y2);
9647 processSync(mapper);
9648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9649 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009650 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009651
9652 // finger up.
9653 processId(mapper, -1);
9654 processSync(mapper);
9655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9656 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009657 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009658
9659 // new finger down
9660 processId(mapper, 1);
9661 processPosition(mapper, x3, y3);
9662 processSync(mapper);
9663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9664 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009665 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009666}
9667
9668/**
arthurhungcc7f9802020-04-30 17:55:40 +08009669 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9670 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009671 */
arthurhungcc7f9802020-04-30 17:55:40 +08009672TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009673 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009674 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009675 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009676 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009677
9678 NotifyMotionArgs motionArgs;
9679
9680 // default tool type is finger
9681 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009682 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009683 processPosition(mapper, x1, y1);
9684 processSync(mapper);
9685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9686 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009687 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009688
9689 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9690 processToolType(mapper, MT_TOOL_PALM);
9691 processSync(mapper);
9692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9693 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9694
9695 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009696 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009697 processPosition(mapper, x2, y2);
9698 processSync(mapper);
9699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9700
9701 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009702 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009703 processSync(mapper);
9704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9705
9706 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009707 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009708 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009709 processPosition(mapper, x3, y3);
9710 processSync(mapper);
9711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9712 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009713 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009714}
9715
arthurhungbf89a482020-04-17 17:37:55 +08009716/**
arthurhungcc7f9802020-04-30 17:55:40 +08009717 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9718 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009719 */
arthurhungcc7f9802020-04-30 17:55:40 +08009720TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009721 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009722 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009723 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9724 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9725
9726 NotifyMotionArgs motionArgs;
9727
9728 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009729 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9730 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009731 processPosition(mapper, x1, y1);
9732 processSync(mapper);
9733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9734 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009735 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009736
9737 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009738 processSlot(mapper, SECOND_SLOT);
9739 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009740 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009741 processSync(mapper);
9742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009743 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009744 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009745
9746 // If the tool type of the first finger changes to MT_TOOL_PALM,
9747 // we expect to receive ACTION_POINTER_UP with cancel flag.
9748 processSlot(mapper, FIRST_SLOT);
9749 processId(mapper, FIRST_TRACKING_ID);
9750 processToolType(mapper, MT_TOOL_PALM);
9751 processSync(mapper);
9752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009753 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009754 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9755
9756 // The following MOVE events of second finger should be processed.
9757 processSlot(mapper, SECOND_SLOT);
9758 processId(mapper, SECOND_TRACKING_ID);
9759 processPosition(mapper, x2 + 1, y2 + 1);
9760 processSync(mapper);
9761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9762 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9763 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9764
9765 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9766 // it. Second finger receive move.
9767 processSlot(mapper, FIRST_SLOT);
9768 processId(mapper, INVALID_TRACKING_ID);
9769 processSync(mapper);
9770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9771 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9772 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9773
9774 // Second finger keeps moving.
9775 processSlot(mapper, SECOND_SLOT);
9776 processId(mapper, SECOND_TRACKING_ID);
9777 processPosition(mapper, x2 + 2, y2 + 2);
9778 processSync(mapper);
9779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9780 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9781 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9782
9783 // Second finger up.
9784 processId(mapper, INVALID_TRACKING_ID);
9785 processSync(mapper);
9786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9787 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9788 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9789}
9790
9791/**
9792 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9793 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9794 */
9795TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9796 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009797 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009798 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9799 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9800
9801 NotifyMotionArgs motionArgs;
9802
9803 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9804 // First finger down.
9805 processId(mapper, FIRST_TRACKING_ID);
9806 processPosition(mapper, x1, y1);
9807 processSync(mapper);
9808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9809 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009810 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009811
9812 // Second finger down.
9813 processSlot(mapper, SECOND_SLOT);
9814 processId(mapper, SECOND_TRACKING_ID);
9815 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009816 processSync(mapper);
9817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009818 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009819 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009820
arthurhungcc7f9802020-04-30 17:55:40 +08009821 // If the tool type of the first finger changes to MT_TOOL_PALM,
9822 // we expect to receive ACTION_POINTER_UP with cancel flag.
9823 processSlot(mapper, FIRST_SLOT);
9824 processId(mapper, FIRST_TRACKING_ID);
9825 processToolType(mapper, MT_TOOL_PALM);
9826 processSync(mapper);
9827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009828 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009829 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9830
9831 // Second finger keeps moving.
9832 processSlot(mapper, SECOND_SLOT);
9833 processId(mapper, SECOND_TRACKING_ID);
9834 processPosition(mapper, x2 + 1, y2 + 1);
9835 processSync(mapper);
9836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9837 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9838
9839 // second finger becomes palm, receive cancel due to only 1 finger is active.
9840 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009841 processToolType(mapper, MT_TOOL_PALM);
9842 processSync(mapper);
9843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9844 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9845
arthurhungcc7f9802020-04-30 17:55:40 +08009846 // third finger down.
9847 processSlot(mapper, THIRD_SLOT);
9848 processId(mapper, THIRD_TRACKING_ID);
9849 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009850 processPosition(mapper, x3, y3);
9851 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9853 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009854 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009855 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9856
9857 // third finger move
9858 processId(mapper, THIRD_TRACKING_ID);
9859 processPosition(mapper, x3 + 1, y3 + 1);
9860 processSync(mapper);
9861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9863
9864 // first finger up, third finger receive move.
9865 processSlot(mapper, FIRST_SLOT);
9866 processId(mapper, INVALID_TRACKING_ID);
9867 processSync(mapper);
9868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9869 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9870 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9871
9872 // second finger up, third finger receive move.
9873 processSlot(mapper, SECOND_SLOT);
9874 processId(mapper, INVALID_TRACKING_ID);
9875 processSync(mapper);
9876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9877 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9878 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9879
9880 // third finger up.
9881 processSlot(mapper, THIRD_SLOT);
9882 processId(mapper, INVALID_TRACKING_ID);
9883 processSync(mapper);
9884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9885 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9886 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9887}
9888
9889/**
9890 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9891 * and the active finger could still be allowed to receive the events
9892 */
9893TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9894 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009895 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009896 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9897 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9898
9899 NotifyMotionArgs motionArgs;
9900
9901 // default tool type is finger
9902 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9903 processId(mapper, FIRST_TRACKING_ID);
9904 processPosition(mapper, x1, y1);
9905 processSync(mapper);
9906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9907 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009908 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009909
9910 // Second finger down.
9911 processSlot(mapper, SECOND_SLOT);
9912 processId(mapper, SECOND_TRACKING_ID);
9913 processPosition(mapper, x2, y2);
9914 processSync(mapper);
9915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009916 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009917 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009918
9919 // If the tool type of the second finger changes to MT_TOOL_PALM,
9920 // we expect to receive ACTION_POINTER_UP with cancel flag.
9921 processId(mapper, SECOND_TRACKING_ID);
9922 processToolType(mapper, MT_TOOL_PALM);
9923 processSync(mapper);
9924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009925 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009926 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9927
9928 // The following MOVE event should be processed.
9929 processSlot(mapper, FIRST_SLOT);
9930 processId(mapper, FIRST_TRACKING_ID);
9931 processPosition(mapper, x1 + 1, y1 + 1);
9932 processSync(mapper);
9933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9934 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9935 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9936
9937 // second finger up.
9938 processSlot(mapper, SECOND_SLOT);
9939 processId(mapper, INVALID_TRACKING_ID);
9940 processSync(mapper);
9941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9942 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9943
9944 // first finger keep moving
9945 processSlot(mapper, FIRST_SLOT);
9946 processId(mapper, FIRST_TRACKING_ID);
9947 processPosition(mapper, x1 + 2, y1 + 2);
9948 processSync(mapper);
9949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9951
9952 // first finger up.
9953 processId(mapper, INVALID_TRACKING_ID);
9954 processSync(mapper);
9955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9956 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9957 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009958}
9959
Arthur Hung9ad18942021-06-19 02:04:46 +00009960/**
9961 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9962 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9963 * cause slot be valid again.
9964 */
9965TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9966 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009967 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009968 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9969 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9970
9971 NotifyMotionArgs motionArgs;
9972
9973 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9974 // First finger down.
9975 processId(mapper, FIRST_TRACKING_ID);
9976 processPosition(mapper, x1, y1);
9977 processPressure(mapper, RAW_PRESSURE_MAX);
9978 processSync(mapper);
9979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9980 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9981 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9982
9983 // First finger move.
9984 processId(mapper, FIRST_TRACKING_ID);
9985 processPosition(mapper, x1 + 1, y1 + 1);
9986 processPressure(mapper, RAW_PRESSURE_MAX);
9987 processSync(mapper);
9988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9989 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9990 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9991
9992 // Second finger down.
9993 processSlot(mapper, SECOND_SLOT);
9994 processId(mapper, SECOND_TRACKING_ID);
9995 processPosition(mapper, x2, y2);
9996 processPressure(mapper, RAW_PRESSURE_MAX);
9997 processSync(mapper);
9998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009999 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010000 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10001
10002 // second finger up with some unexpected data.
10003 processSlot(mapper, SECOND_SLOT);
10004 processId(mapper, INVALID_TRACKING_ID);
10005 processPosition(mapper, x2, y2);
10006 processSync(mapper);
10007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010008 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010009 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10010
10011 // first finger up with some unexpected data.
10012 processSlot(mapper, FIRST_SLOT);
10013 processId(mapper, INVALID_TRACKING_ID);
10014 processPosition(mapper, x2, y2);
10015 processPressure(mapper, RAW_PRESSURE_MAX);
10016 processSync(mapper);
10017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10018 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10019 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10020}
10021
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010022TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10023 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010024 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010025 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10026 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10027
10028 // First finger down.
10029 processId(mapper, FIRST_TRACKING_ID);
10030 processPosition(mapper, 100, 200);
10031 processPressure(mapper, RAW_PRESSURE_MAX);
10032 processSync(mapper);
10033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10034 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10035
10036 // Second finger down.
10037 processSlot(mapper, SECOND_SLOT);
10038 processId(mapper, SECOND_TRACKING_ID);
10039 processPosition(mapper, 300, 400);
10040 processPressure(mapper, RAW_PRESSURE_MAX);
10041 processSync(mapper);
10042 ASSERT_NO_FATAL_FAILURE(
10043 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10044
10045 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010046 // preserved. Resetting should cancel the ongoing gesture.
10047 resetMapper(mapper, ARBITRARY_TIME);
10048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10049 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010050
10051 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10052 // the existing touch state to generate a down event.
10053 processPosition(mapper, 301, 302);
10054 processSync(mapper);
10055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10056 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10058 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10059
10060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10061}
10062
10063TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10064 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010065 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010066 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10067 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10068
10069 // First finger touches down and releases.
10070 processId(mapper, FIRST_TRACKING_ID);
10071 processPosition(mapper, 100, 200);
10072 processPressure(mapper, RAW_PRESSURE_MAX);
10073 processSync(mapper);
10074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10075 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10076 processId(mapper, INVALID_TRACKING_ID);
10077 processSync(mapper);
10078 ASSERT_NO_FATAL_FAILURE(
10079 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10080
10081 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10082 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010083 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10085
10086 // Send an empty sync frame. Since there are no pointers, no events are generated.
10087 processSync(mapper);
10088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10089}
10090
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010091TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010092 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010093 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010094 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
10095 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010097
10098 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10099 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10100 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10101 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10102 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10103
10104 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010105 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010106 processId(mapper, FIRST_TRACKING_ID);
10107 processToolType(mapper, MT_TOOL_PEN);
10108 processPosition(mapper, 100, 200);
10109 processPressure(mapper, RAW_PRESSURE_MAX);
10110 processSync(mapper);
10111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10112 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10113 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010114 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010115
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010116 // Now that we know the device supports styluses, ensure that the device is re-configured with
10117 // the stylus source.
10118 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10119 {
10120 const auto& devices = mReader->getInputDevices();
10121 auto deviceInfo =
10122 std::find_if(devices.begin(), devices.end(),
10123 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10124 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10125 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10126 }
10127
10128 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10130
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010131 processId(mapper, INVALID_TRACKING_ID);
10132 processSync(mapper);
10133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10134 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10135 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010136 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010137}
10138
Seunghwan Choi356026c2023-02-01 14:37:25 +090010139TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10140 addConfigurationProperty("touch.deviceType", "touchScreen");
10141 prepareDisplay(ui::ROTATION_0);
10142 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10143 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10144 // indicate stylus presence dynamically.
10145 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10146 std::shared_ptr<FakePointerController> fakePointerController =
10147 std::make_shared<FakePointerController>();
10148 mFakePolicy->setPointerController(fakePointerController);
10149 mFakePolicy->setStylusPointerIconEnabled(true);
10150 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10151
10152 processId(mapper, FIRST_TRACKING_ID);
10153 processPressure(mapper, RAW_PRESSURE_MIN);
10154 processPosition(mapper, 100, 200);
10155 processToolType(mapper, MT_TOOL_PEN);
10156 processSync(mapper);
10157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10158 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010159 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010160 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10161 ASSERT_TRUE(fakePointerController->isPointerShown());
10162 ASSERT_NO_FATAL_FAILURE(
10163 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10164}
10165
10166TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10167 addConfigurationProperty("touch.deviceType", "touchScreen");
10168 prepareDisplay(ui::ROTATION_0);
10169 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10170 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10171 // indicate stylus presence dynamically.
10172 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10173 std::shared_ptr<FakePointerController> fakePointerController =
10174 std::make_shared<FakePointerController>();
10175 mFakePolicy->setPointerController(fakePointerController);
10176 mFakePolicy->setStylusPointerIconEnabled(false);
10177 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10178
10179 processId(mapper, FIRST_TRACKING_ID);
10180 processPressure(mapper, RAW_PRESSURE_MIN);
10181 processPosition(mapper, 100, 200);
10182 processToolType(mapper, MT_TOOL_PEN);
10183 processSync(mapper);
10184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10185 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010186 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010187 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10188 ASSERT_FALSE(fakePointerController->isPointerShown());
10189}
10190
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010191// --- MultiTouchInputMapperTest_ExternalDevice ---
10192
10193class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10194protected:
Chris Yea52ade12020-08-27 16:49:20 -070010195 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010196};
10197
10198/**
10199 * Expect fallback to internal viewport if device is external and external viewport is not present.
10200 */
10201TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10202 prepareAxes(POSITION);
10203 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010204 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010205 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10206
10207 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10208
10209 NotifyMotionArgs motionArgs;
10210
10211 // Expect the event to be sent to the internal viewport,
10212 // because an external viewport is not present.
10213 processPosition(mapper, 100, 100);
10214 processSync(mapper);
10215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10216 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10217
10218 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010219 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010220 processPosition(mapper, 100, 100);
10221 processSync(mapper);
10222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10223 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10224}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010225
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010226TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10227 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10228 std::shared_ptr<FakePointerController> fakePointerController =
10229 std::make_shared<FakePointerController>();
10230 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10231 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010232
10233 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010234 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010235 prepareAxes(POSITION | ID | SLOT);
10236 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10237 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10238 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010239 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010240 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10241
10242 // captured touchpad should be a touchpad source
10243 NotifyDeviceResetArgs resetArgs;
10244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10245 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10246
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010247 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010248
10249 const InputDeviceInfo::MotionRange* relRangeX =
10250 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10251 ASSERT_NE(relRangeX, nullptr);
10252 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10253 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10254 const InputDeviceInfo::MotionRange* relRangeY =
10255 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10256 ASSERT_NE(relRangeY, nullptr);
10257 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10258 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10259
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010260 // run captured pointer tests - note that this is unscaled, so input listener events should be
10261 // identical to what the hardware sends (accounting for any
10262 // calibration).
10263 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010264 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010265 processId(mapper, 1);
10266 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10267 processKey(mapper, BTN_TOUCH, 1);
10268 processSync(mapper);
10269
10270 // expect coord[0] to contain initial location of touch 0
10271 NotifyMotionArgs args;
10272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10273 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10274 ASSERT_EQ(1U, args.pointerCount);
10275 ASSERT_EQ(0, args.pointerProperties[0].id);
10276 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10277 ASSERT_NO_FATAL_FAILURE(
10278 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10279
10280 // FINGER 1 DOWN
10281 processSlot(mapper, 1);
10282 processId(mapper, 2);
10283 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10284 processSync(mapper);
10285
10286 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010288 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010289 ASSERT_EQ(2U, args.pointerCount);
10290 ASSERT_EQ(0, args.pointerProperties[0].id);
10291 ASSERT_EQ(1, args.pointerProperties[1].id);
10292 ASSERT_NO_FATAL_FAILURE(
10293 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10294 ASSERT_NO_FATAL_FAILURE(
10295 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10296
10297 // FINGER 1 MOVE
10298 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10299 processSync(mapper);
10300
10301 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10302 // from move
10303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10305 ASSERT_NO_FATAL_FAILURE(
10306 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10307 ASSERT_NO_FATAL_FAILURE(
10308 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10309
10310 // FINGER 0 MOVE
10311 processSlot(mapper, 0);
10312 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10313 processSync(mapper);
10314
10315 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10318 ASSERT_NO_FATAL_FAILURE(
10319 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10320 ASSERT_NO_FATAL_FAILURE(
10321 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10322
10323 // BUTTON DOWN
10324 processKey(mapper, BTN_LEFT, 1);
10325 processSync(mapper);
10326
10327 // touchinputmapper design sends a move before button press
10328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10331 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10332
10333 // BUTTON UP
10334 processKey(mapper, BTN_LEFT, 0);
10335 processSync(mapper);
10336
10337 // touchinputmapper design sends a move after button release
10338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10339 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10342
10343 // FINGER 0 UP
10344 processId(mapper, -1);
10345 processSync(mapper);
10346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10347 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10348
10349 // FINGER 1 MOVE
10350 processSlot(mapper, 1);
10351 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10352 processSync(mapper);
10353
10354 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10356 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10357 ASSERT_EQ(1U, args.pointerCount);
10358 ASSERT_EQ(1, args.pointerProperties[0].id);
10359 ASSERT_NO_FATAL_FAILURE(
10360 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10361
10362 // FINGER 1 UP
10363 processId(mapper, -1);
10364 processKey(mapper, BTN_TOUCH, 0);
10365 processSync(mapper);
10366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10367 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10368
Josep del Río2d8c79a2023-01-23 19:33:50 +000010369 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010370 mFakePolicy->setPointerCapture(false);
10371 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010373 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010374}
10375
10376TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10377 std::shared_ptr<FakePointerController> fakePointerController =
10378 std::make_shared<FakePointerController>();
10379 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10380 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010381
10382 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010383 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010384 prepareAxes(POSITION | ID | SLOT);
10385 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10386 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010387 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010388 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10389 // run uncaptured pointer tests - pushes out generic events
10390 // FINGER 0 DOWN
10391 processId(mapper, 3);
10392 processPosition(mapper, 100, 100);
10393 processKey(mapper, BTN_TOUCH, 1);
10394 processSync(mapper);
10395
10396 // start at (100,100), cursor should be at (0,0) * scale
10397 NotifyMotionArgs args;
10398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10399 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10400 ASSERT_NO_FATAL_FAILURE(
10401 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10402
10403 // FINGER 0 MOVE
10404 processPosition(mapper, 200, 200);
10405 processSync(mapper);
10406
10407 // compute scaling to help with touch position checking
10408 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10409 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10410 float scale =
10411 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10412
10413 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10415 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10417 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010418
10419 // BUTTON DOWN
10420 processKey(mapper, BTN_LEFT, 1);
10421 processSync(mapper);
10422
10423 // touchinputmapper design sends a move before button press
10424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10425 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10427 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10428
10429 // BUTTON UP
10430 processKey(mapper, BTN_LEFT, 0);
10431 processSync(mapper);
10432
10433 // touchinputmapper design sends a move after button release
10434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10435 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10437 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010438}
10439
10440TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10441 std::shared_ptr<FakePointerController> fakePointerController =
10442 std::make_shared<FakePointerController>();
10443
Michael Wrighta9cf4192022-12-01 23:46:39 +000010444 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010445 prepareAxes(POSITION | ID | SLOT);
10446 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010447 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010448 mFakePolicy->setPointerCapture(false);
10449 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10450
Josep del Río2d8c79a2023-01-23 19:33:50 +000010451 // uncaptured touchpad should be a pointer device
10452 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010453
Josep del Río2d8c79a2023-01-23 19:33:50 +000010454 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010455 mFakePolicy->setPointerCapture(true);
10456 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10457 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10458}
10459
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010460// --- BluetoothMultiTouchInputMapperTest ---
10461
10462class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10463protected:
10464 void SetUp() override {
10465 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10466 }
10467};
10468
10469TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10470 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010471 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010472 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10473 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10474
10475 nsecs_t kernelEventTime = ARBITRARY_TIME;
10476 nsecs_t expectedEventTime = ARBITRARY_TIME;
10477 // Touch down.
10478 processId(mapper, FIRST_TRACKING_ID);
10479 processPosition(mapper, 100, 200);
10480 processPressure(mapper, RAW_PRESSURE_MAX);
10481 processSync(mapper, ARBITRARY_TIME);
10482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10483 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10484
10485 // Process several events that come in quick succession, according to their timestamps.
10486 for (int i = 0; i < 3; i++) {
10487 constexpr static nsecs_t delta = ms2ns(1);
10488 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10489 kernelEventTime += delta;
10490 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10491
10492 processPosition(mapper, 101 + i, 201 + i);
10493 processSync(mapper, kernelEventTime);
10494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10495 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10496 WithEventTime(expectedEventTime))));
10497 }
10498
10499 // Release the touch.
10500 processId(mapper, INVALID_TRACKING_ID);
10501 processPressure(mapper, RAW_PRESSURE_MIN);
10502 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10504 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10505 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10506}
10507
10508// --- MultiTouchPointerModeTest ---
10509
HQ Liue6983c72022-04-19 22:14:56 +000010510class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10511protected:
10512 float mPointerMovementScale;
10513 float mPointerXZoomScale;
10514 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10515 addConfigurationProperty("touch.deviceType", "pointer");
10516 std::shared_ptr<FakePointerController> fakePointerController =
10517 std::make_shared<FakePointerController>();
10518 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10519 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010520 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010521
10522 prepareAxes(POSITION);
10523 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10524 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10525 // needs to be disabled, and the pointer gesture needs to be enabled.
10526 mFakePolicy->setPointerCapture(false);
10527 mFakePolicy->setPointerGestureEnabled(true);
10528 mFakePolicy->setPointerController(fakePointerController);
10529
10530 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10531 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10532 mPointerMovementScale =
10533 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10534 mPointerXZoomScale =
10535 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10536 }
10537
10538 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10539 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10540 /*flat*/ 0,
10541 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10542 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10543 /*flat*/ 0,
10544 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10545 }
10546};
10547
10548/**
10549 * Two fingers down on a pointer mode touch pad. The width
10550 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10551 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10552 * be greater than the both value to be freeform gesture, so that after two
10553 * fingers start to move downwards, the gesture should be swipe.
10554 */
10555TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10556 // The min freeform gesture width is 25units/mm x 30mm = 750
10557 // which is greater than fraction of the diagnal length of the touchpad (349).
10558 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010559 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010560 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10561 NotifyMotionArgs motionArgs;
10562
10563 // Two fingers down at once.
10564 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10565 // Pointer's initial position is used the [0,0] coordinate.
10566 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10567
10568 processId(mapper, FIRST_TRACKING_ID);
10569 processPosition(mapper, x1, y1);
10570 processMTSync(mapper);
10571 processId(mapper, SECOND_TRACKING_ID);
10572 processPosition(mapper, x2, y2);
10573 processMTSync(mapper);
10574 processSync(mapper);
10575
10576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10577 ASSERT_EQ(1U, motionArgs.pointerCount);
10578 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010579 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010580 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010581 ASSERT_NO_FATAL_FAILURE(
10582 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10583
10584 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10585 // that there should be 1 pointer.
10586 int32_t movingDistance = 200;
10587 y1 += movingDistance;
10588 y2 += movingDistance;
10589
10590 processId(mapper, FIRST_TRACKING_ID);
10591 processPosition(mapper, x1, y1);
10592 processMTSync(mapper);
10593 processId(mapper, SECOND_TRACKING_ID);
10594 processPosition(mapper, x2, y2);
10595 processMTSync(mapper);
10596 processSync(mapper);
10597
10598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10599 ASSERT_EQ(1U, motionArgs.pointerCount);
10600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010601 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010602 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10604 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10605 0, 0, 0, 0));
10606}
10607
10608/**
10609 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10610 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10611 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10612 * value to be freeform gesture, so that after two fingers start to move downwards,
10613 * the gesture should be swipe.
10614 */
10615TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10616 // The min freeform gesture width is 5units/mm x 30mm = 150
10617 // which is greater than fraction of the diagnal length of the touchpad (349).
10618 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010619 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
HQ Liue6983c72022-04-19 22:14:56 +000010620 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10621 NotifyMotionArgs motionArgs;
10622
10623 // Two fingers down at once.
10624 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10625 // Pointer's initial position is used the [0,0] coordinate.
10626 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10627
10628 processId(mapper, FIRST_TRACKING_ID);
10629 processPosition(mapper, x1, y1);
10630 processMTSync(mapper);
10631 processId(mapper, SECOND_TRACKING_ID);
10632 processPosition(mapper, x2, y2);
10633 processMTSync(mapper);
10634 processSync(mapper);
10635
10636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10637 ASSERT_EQ(1U, motionArgs.pointerCount);
10638 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010639 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010640 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010641 ASSERT_NO_FATAL_FAILURE(
10642 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10643
10644 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10645 // and there should be 1 pointer.
10646 int32_t movingDistance = 200;
10647 y1 += movingDistance;
10648 y2 += movingDistance;
10649
10650 processId(mapper, FIRST_TRACKING_ID);
10651 processPosition(mapper, x1, y1);
10652 processMTSync(mapper);
10653 processId(mapper, SECOND_TRACKING_ID);
10654 processPosition(mapper, x2, y2);
10655 processMTSync(mapper);
10656 processSync(mapper);
10657
10658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10659 ASSERT_EQ(1U, motionArgs.pointerCount);
10660 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010661 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010662 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010663 // New coordinate is the scaled relative coordinate from the initial coordinate.
10664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10665 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10666 0, 0, 0, 0));
10667}
10668
10669/**
10670 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10671 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10672 * freeform gestures after two fingers start to move downwards.
10673 */
10674TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010675 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010676 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10677
10678 NotifyMotionArgs motionArgs;
10679
10680 // Two fingers down at once. Wider than the max swipe width.
10681 // The gesture is expected to be PRESS, then transformed to FREEFORM
10682 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10683
10684 processId(mapper, FIRST_TRACKING_ID);
10685 processPosition(mapper, x1, y1);
10686 processMTSync(mapper);
10687 processId(mapper, SECOND_TRACKING_ID);
10688 processPosition(mapper, x2, y2);
10689 processMTSync(mapper);
10690 processSync(mapper);
10691
10692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10693 ASSERT_EQ(1U, motionArgs.pointerCount);
10694 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010695 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010696 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010697 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10698 ASSERT_NO_FATAL_FAILURE(
10699 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10700
10701 int32_t movingDistance = 200;
10702
10703 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10704 // then two down events for two pointers.
10705 y1 += movingDistance;
10706 y2 += movingDistance;
10707
10708 processId(mapper, FIRST_TRACKING_ID);
10709 processPosition(mapper, x1, y1);
10710 processMTSync(mapper);
10711 processId(mapper, SECOND_TRACKING_ID);
10712 processPosition(mapper, x2, y2);
10713 processMTSync(mapper);
10714 processSync(mapper);
10715
10716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10717 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10718 ASSERT_EQ(1U, motionArgs.pointerCount);
10719 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010721 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
HQ Liue6983c72022-04-19 22:14:56 +000010722 ASSERT_EQ(1U, motionArgs.pointerCount);
10723 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010725 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010726 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010727 ASSERT_EQ(2U, motionArgs.pointerCount);
10728 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010729 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010730 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010731 // Two pointers' scaled relative coordinates from their initial centroid.
10732 // Initial y coordinates are 0 as y1 and y2 have the same value.
10733 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10734 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10735 // When pointers move, the new coordinates equal to the initial coordinates plus
10736 // scaled moving distance.
10737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10738 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10739 0, 0, 0, 0));
10740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10741 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10742 0, 0, 0, 0));
10743
10744 // Move two fingers down again, expect one MOVE motion event.
10745 y1 += movingDistance;
10746 y2 += movingDistance;
10747
10748 processId(mapper, FIRST_TRACKING_ID);
10749 processPosition(mapper, x1, y1);
10750 processMTSync(mapper);
10751 processId(mapper, SECOND_TRACKING_ID);
10752 processPosition(mapper, x2, y2);
10753 processMTSync(mapper);
10754 processSync(mapper);
10755
10756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10757 ASSERT_EQ(2U, motionArgs.pointerCount);
10758 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010759 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010760 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10762 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10763 0, 0, 0, 0, 0));
10764 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10765 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10766 0, 0, 0, 0, 0));
10767}
10768
Harry Cutts39b7ca22022-10-05 15:55:48 +000010769TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010770 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Harry Cutts39b7ca22022-10-05 15:55:48 +000010771 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10772 NotifyMotionArgs motionArgs;
10773
10774 // Place two fingers down.
10775 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10776
10777 processId(mapper, FIRST_TRACKING_ID);
10778 processPosition(mapper, x1, y1);
10779 processMTSync(mapper);
10780 processId(mapper, SECOND_TRACKING_ID);
10781 processPosition(mapper, x2, y2);
10782 processMTSync(mapper);
10783 processSync(mapper);
10784
10785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10786 ASSERT_EQ(1U, motionArgs.pointerCount);
10787 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10788 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10789 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10790 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10791
10792 // Move the two fingers down and to the left.
10793 int32_t movingDistance = 200;
10794 x1 -= movingDistance;
10795 y1 += movingDistance;
10796 x2 -= movingDistance;
10797 y2 += movingDistance;
10798
10799 processId(mapper, FIRST_TRACKING_ID);
10800 processPosition(mapper, x1, y1);
10801 processMTSync(mapper);
10802 processId(mapper, SECOND_TRACKING_ID);
10803 processPosition(mapper, x2, y2);
10804 processMTSync(mapper);
10805 processSync(mapper);
10806
10807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10808 ASSERT_EQ(1U, motionArgs.pointerCount);
10809 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10810 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10811 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10812 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10813}
10814
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010815TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010816 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010817 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10818 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10820
10821 // Start a stylus gesture.
10822 processKey(mapper, BTN_TOOL_PEN, 1);
10823 processId(mapper, FIRST_TRACKING_ID);
10824 processPosition(mapper, 100, 200);
10825 processSync(mapper);
10826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10827 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10828 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010829 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010830 // TODO(b/257078296): Pointer mode generates extra event.
10831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10832 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10833 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010834 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10836
10837 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10838 // gesture should be disabled.
10839 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10840 viewport->isActive = false;
10841 mFakePolicy->updateViewport(*viewport);
10842 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10844 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10845 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010846 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010847 // TODO(b/257078296): Pointer mode generates extra event.
10848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10849 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10850 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010851 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10853}
10854
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010855// --- JoystickInputMapperTest ---
10856
10857class JoystickInputMapperTest : public InputMapperTest {
10858protected:
10859 static const int32_t RAW_X_MIN;
10860 static const int32_t RAW_X_MAX;
10861 static const int32_t RAW_Y_MIN;
10862 static const int32_t RAW_Y_MAX;
10863
10864 void SetUp() override {
10865 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10866 }
10867 void prepareAxes() {
10868 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10869 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10870 }
10871
10872 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10873 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10874 }
10875
10876 void processSync(JoystickInputMapper& mapper) {
10877 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10878 }
10879
Michael Wrighta9cf4192022-12-01 23:46:39 +000010880 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010881 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10882 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10883 NO_PORT, ViewportType::VIRTUAL);
10884 }
10885};
10886
10887const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10888const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10889const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10890const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10891
10892TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10893 prepareAxes();
10894 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10895
10896 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10897
Michael Wrighta9cf4192022-12-01 23:46:39 +000010898 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010899
10900 // Send an axis event
10901 processAxis(mapper, ABS_X, 100);
10902 processSync(mapper);
10903
10904 NotifyMotionArgs args;
10905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10906 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10907
10908 // Send another axis event
10909 processAxis(mapper, ABS_Y, 100);
10910 processSync(mapper);
10911
10912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10913 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10914}
10915
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010916// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010917
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010918class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010919protected:
10920 static const char* DEVICE_NAME;
10921 static const char* DEVICE_LOCATION;
10922 static const int32_t DEVICE_ID;
10923 static const int32_t DEVICE_GENERATION;
10924 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010925 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010926 static const int32_t EVENTHUB_ID;
10927
10928 std::shared_ptr<FakeEventHub> mFakeEventHub;
10929 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010930 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010931 std::unique_ptr<InstrumentedInputReader> mReader;
10932 std::shared_ptr<InputDevice> mDevice;
10933
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010934 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010935 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010936 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010937 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010938 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010939 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010940 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10941 }
10942
10943 void SetUp() override { SetUp(DEVICE_CLASSES); }
10944
10945 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010946 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010947 mFakePolicy.clear();
10948 }
10949
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010950 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010951 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10952 mReader->requestRefreshConfiguration(changes);
10953 mReader->loopOnce();
10954 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010955 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010956 }
10957
10958 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10959 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010960 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010961 InputDeviceIdentifier identifier;
10962 identifier.name = name;
10963 identifier.location = location;
10964 std::shared_ptr<InputDevice> device =
10965 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10966 identifier);
10967 mReader->pushNextDevice(device);
10968 mFakeEventHub->addDevice(eventHubId, name, classes);
10969 mReader->loopOnce();
10970 return device;
10971 }
10972
10973 template <class T, typename... Args>
10974 T& addControllerAndConfigure(Args... args) {
10975 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10976
10977 return controller;
10978 }
10979};
10980
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010981const char* PeripheralControllerTest::DEVICE_NAME = "device";
10982const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10983const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10984const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10985const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010986const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10987 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010988const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010989
10990// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010991class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010992protected:
10993 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010994 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010995 }
10996};
10997
10998TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010999 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011000
Harry Cuttsa5b71292022-11-28 12:56:17 +000011001 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11002 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11003 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011004}
11005
11006TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011007 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011008
Harry Cuttsa5b71292022-11-28 12:56:17 +000011009 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11010 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11011 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011012}
11013
11014// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011015class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011016protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011017 void SetUp() override {
11018 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11019 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011020};
11021
Chris Ye85758332021-05-16 23:05:17 -070011022TEST_F(LightControllerTest, MonoLight) {
11023 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011024 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011025 .maxBrightness = 255,
11026 .flags = InputLightClass::BRIGHTNESS,
11027 .path = ""};
11028 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011029
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011030 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011031 InputDeviceInfo info;
11032 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011033 std::vector<InputDeviceLightInfo> lights = info.getLights();
11034 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011035 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11036 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11037
11038 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11039 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11040}
11041
11042TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11043 RawLightInfo infoMono = {.id = 1,
11044 .name = "mono_keyboard_backlight",
11045 .maxBrightness = 255,
11046 .flags = InputLightClass::BRIGHTNESS |
11047 InputLightClass::KEYBOARD_BACKLIGHT,
11048 .path = ""};
11049 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11050
11051 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11052 InputDeviceInfo info;
11053 controller.populateDeviceInfo(&info);
11054 std::vector<InputDeviceLightInfo> lights = info.getLights();
11055 ASSERT_EQ(1U, lights.size());
11056 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11057 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011058
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011059 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11060 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011061}
11062
11063TEST_F(LightControllerTest, RGBLight) {
11064 RawLightInfo infoRed = {.id = 1,
11065 .name = "red",
11066 .maxBrightness = 255,
11067 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11068 .path = ""};
11069 RawLightInfo infoGreen = {.id = 2,
11070 .name = "green",
11071 .maxBrightness = 255,
11072 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11073 .path = ""};
11074 RawLightInfo infoBlue = {.id = 3,
11075 .name = "blue",
11076 .maxBrightness = 255,
11077 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11078 .path = ""};
11079 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11080 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11081 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11082
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011083 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011084 InputDeviceInfo info;
11085 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011086 std::vector<InputDeviceLightInfo> lights = info.getLights();
11087 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011088 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11089 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11090 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11091
11092 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11093 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11094}
11095
11096TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11097 RawLightInfo infoRed = {.id = 1,
11098 .name = "red_keyboard_backlight",
11099 .maxBrightness = 255,
11100 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11101 InputLightClass::KEYBOARD_BACKLIGHT,
11102 .path = ""};
11103 RawLightInfo infoGreen = {.id = 2,
11104 .name = "green_keyboard_backlight",
11105 .maxBrightness = 255,
11106 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11107 InputLightClass::KEYBOARD_BACKLIGHT,
11108 .path = ""};
11109 RawLightInfo infoBlue = {.id = 3,
11110 .name = "blue_keyboard_backlight",
11111 .maxBrightness = 255,
11112 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11113 InputLightClass::KEYBOARD_BACKLIGHT,
11114 .path = ""};
11115 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11116 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11117 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11118
11119 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11120 InputDeviceInfo info;
11121 controller.populateDeviceInfo(&info);
11122 std::vector<InputDeviceLightInfo> lights = info.getLights();
11123 ASSERT_EQ(1U, lights.size());
11124 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11125 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11126 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11127
11128 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11129 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11130}
11131
11132TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11133 RawLightInfo infoRed = {.id = 1,
11134 .name = "red",
11135 .maxBrightness = 255,
11136 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11137 .path = ""};
11138 RawLightInfo infoGreen = {.id = 2,
11139 .name = "green",
11140 .maxBrightness = 255,
11141 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11142 .path = ""};
11143 RawLightInfo infoBlue = {.id = 3,
11144 .name = "blue",
11145 .maxBrightness = 255,
11146 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11147 .path = ""};
11148 RawLightInfo infoGlobal = {.id = 3,
11149 .name = "global_keyboard_backlight",
11150 .maxBrightness = 255,
11151 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11152 InputLightClass::KEYBOARD_BACKLIGHT,
11153 .path = ""};
11154 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11155 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11156 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11157 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11158
11159 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11160 InputDeviceInfo info;
11161 controller.populateDeviceInfo(&info);
11162 std::vector<InputDeviceLightInfo> lights = info.getLights();
11163 ASSERT_EQ(1U, lights.size());
11164 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11165 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11166 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011167
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011168 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11169 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011170}
11171
11172TEST_F(LightControllerTest, MultiColorRGBLight) {
11173 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011174 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011175 .maxBrightness = 255,
11176 .flags = InputLightClass::BRIGHTNESS |
11177 InputLightClass::MULTI_INTENSITY |
11178 InputLightClass::MULTI_INDEX,
11179 .path = ""};
11180
11181 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11182
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011183 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011184 InputDeviceInfo info;
11185 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011186 std::vector<InputDeviceLightInfo> lights = info.getLights();
11187 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011188 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11189 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11190 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11191
11192 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11193 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11194}
11195
11196TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11197 RawLightInfo infoColor = {.id = 1,
11198 .name = "multi_color_keyboard_backlight",
11199 .maxBrightness = 255,
11200 .flags = InputLightClass::BRIGHTNESS |
11201 InputLightClass::MULTI_INTENSITY |
11202 InputLightClass::MULTI_INDEX |
11203 InputLightClass::KEYBOARD_BACKLIGHT,
11204 .path = ""};
11205
11206 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11207
11208 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11209 InputDeviceInfo info;
11210 controller.populateDeviceInfo(&info);
11211 std::vector<InputDeviceLightInfo> lights = info.getLights();
11212 ASSERT_EQ(1U, lights.size());
11213 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11214 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11215 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011216
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011217 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11218 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011219}
11220
11221TEST_F(LightControllerTest, PlayerIdLight) {
11222 RawLightInfo info1 = {.id = 1,
11223 .name = "player1",
11224 .maxBrightness = 255,
11225 .flags = InputLightClass::BRIGHTNESS,
11226 .path = ""};
11227 RawLightInfo info2 = {.id = 2,
11228 .name = "player2",
11229 .maxBrightness = 255,
11230 .flags = InputLightClass::BRIGHTNESS,
11231 .path = ""};
11232 RawLightInfo info3 = {.id = 3,
11233 .name = "player3",
11234 .maxBrightness = 255,
11235 .flags = InputLightClass::BRIGHTNESS,
11236 .path = ""};
11237 RawLightInfo info4 = {.id = 4,
11238 .name = "player4",
11239 .maxBrightness = 255,
11240 .flags = InputLightClass::BRIGHTNESS,
11241 .path = ""};
11242 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11243 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11244 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11245 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11246
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011247 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011248 InputDeviceInfo info;
11249 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011250 std::vector<InputDeviceLightInfo> lights = info.getLights();
11251 ASSERT_EQ(1U, lights.size());
11252 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011253 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11254 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011255
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011256 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11257 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11258 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011259}
11260
Michael Wrightd02c5b62014-02-10 15:10:22 -080011261} // namespace android