blob: 72fe2af03311a39f728bcd175ee1bc5294cc7abe [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;
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700164 std::list<NotifyArgs> mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700166 std::mutex mLock;
167 std::condition_variable mStateChangedCondition;
168 bool mConfigureWasCalled GUARDED_BY(mLock);
169 bool mResetWasCalled GUARDED_BY(mLock);
170 bool mProcessWasCalled GUARDED_BY(mLock);
171 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172
Arthur Hungc23540e2018-11-29 20:42:11 +0800173 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800174public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000175 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
176 uint32_t sources)
177 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800178 mSources(sources),
179 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800181 mConfigureWasCalled(false),
182 mResetWasCalled(false),
183 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184
Chris Yea52ade12020-08-27 16:49:20 -0700185 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186
187 void setKeyboardType(int32_t keyboardType) {
188 mKeyboardType = keyboardType;
189 }
190
191 void setMetaState(int32_t metaState) {
192 mMetaState = metaState;
193 }
194
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700195 // Sets the return value for the `process` call.
196 void setProcessResult(std::list<NotifyArgs> notifyArgs) {
197 mProcessResult.clear();
198 for (auto notifyArg : notifyArgs) {
199 mProcessResult.push_back(notifyArg);
200 }
201 }
202
Michael Wrightd02c5b62014-02-10 15:10:22 -0800203 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700204 std::unique_lock<std::mutex> lock(mLock);
205 base::ScopedLockAssertion assumeLocked(mLock);
206 const bool configureCalled =
207 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
208 return mConfigureWasCalled;
209 });
210 if (!configureCalled) {
211 FAIL() << "Expected configure() to have been called.";
212 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800213 mConfigureWasCalled = false;
214 }
215
216 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700217 std::unique_lock<std::mutex> lock(mLock);
218 base::ScopedLockAssertion assumeLocked(mLock);
219 const bool resetCalled =
220 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
221 return mResetWasCalled;
222 });
223 if (!resetCalled) {
224 FAIL() << "Expected reset() to have been called.";
225 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800226 mResetWasCalled = false;
227 }
228
Yi Kong9b14ac62018-07-17 13:48:38 -0700229 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700230 std::unique_lock<std::mutex> lock(mLock);
231 base::ScopedLockAssertion assumeLocked(mLock);
232 const bool processCalled =
233 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
234 return mProcessWasCalled;
235 });
236 if (!processCalled) {
237 FAIL() << "Expected process() to have been called.";
238 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800239 if (outLastEvent) {
240 *outLastEvent = mLastEvent;
241 }
242 mProcessWasCalled = false;
243 }
244
245 void setKeyCodeState(int32_t keyCode, int32_t state) {
246 mKeyCodeStates.replaceValueFor(keyCode, state);
247 }
248
249 void setScanCodeState(int32_t scanCode, int32_t state) {
250 mScanCodeStates.replaceValueFor(scanCode, state);
251 }
252
253 void setSwitchState(int32_t switchCode, int32_t state) {
254 mSwitchStates.replaceValueFor(switchCode, state);
255 }
256
257 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800258 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 }
260
Philip Junker4af3b3d2021-12-14 10:36:55 +0100261 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
262 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
263 }
264
Michael Wrightd02c5b62014-02-10 15:10:22 -0800265private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100266 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800267
Harry Cuttsd02ea102023-03-17 18:21:30 +0000268 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269 InputMapper::populateDeviceInfo(deviceInfo);
270
271 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000272 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800273 }
274 }
275
Arpit Singhed6c3de2023-04-05 19:24:37 +0000276 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000277 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700278 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800280
281 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800282 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000283 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000284 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800285 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700286
287 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700288 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800289 }
290
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700291 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700292 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800293 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700294 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700295 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296 }
297
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700298 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700299 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800300 mLastEvent = *rawEvent;
301 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700302 mStateChangedCondition.notify_all();
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700303 return mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800304 }
305
Chris Yea52ade12020-08-27 16:49:20 -0700306 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800307 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
308 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
309 }
310
Philip Junker4af3b3d2021-12-14 10:36:55 +0100311 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
312 auto it = mKeyCodeMapping.find(locationKeyCode);
313 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
314 }
315
Chris Yea52ade12020-08-27 16:49:20 -0700316 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800317 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
318 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
319 }
320
Chris Yea52ade12020-08-27 16:49:20 -0700321 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800322 ssize_t index = mSwitchStates.indexOfKey(switchCode);
323 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
324 }
325
Chris Yea52ade12020-08-27 16:49:20 -0700326 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700327 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700328 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700329 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800330 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
331 if (keyCodes[i] == mSupportedKeyCodes[j]) {
332 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800333 }
334 }
335 }
Chris Yea52ade12020-08-27 16:49:20 -0700336 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337 return result;
338 }
339
340 virtual int32_t getMetaState() {
341 return mMetaState;
342 }
343
344 virtual void fadePointer() {
345 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800346
347 virtual std::optional<int32_t> getAssociatedDisplay() {
348 if (mViewport) {
349 return std::make_optional(mViewport->displayId);
350 }
351 return std::nullopt;
352 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353};
354
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700355// --- InputReaderPolicyTest ---
356class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700357protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700358 sp<FakeInputReaderPolicy> mFakePolicy;
359
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700360 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700361 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700362};
363
364/**
365 * Check that empty set of viewports is an acceptable configuration.
366 * Also try to get internal viewport two different ways - by type and by uniqueId.
367 *
368 * There will be confusion if two viewports with empty uniqueId and identical type are present.
369 * Such configuration is not currently allowed.
370 */
371TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700372 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700373
374 // We didn't add any viewports yet, so there shouldn't be any.
375 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100376 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700377 ASSERT_FALSE(internalViewport);
378
379 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000380 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000381 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700382
383 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700384 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700385 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100386 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700387
388 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100389 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700390 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700391 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700392
393 mFakePolicy->clearViewports();
394 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700395 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700396 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100397 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700398 ASSERT_FALSE(internalViewport);
399}
400
401TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
402 const std::string internalUniqueId = "local:0";
403 const std::string externalUniqueId = "local:1";
404 const std::string virtualUniqueId1 = "virtual:2";
405 const std::string virtualUniqueId2 = "virtual:3";
406 constexpr int32_t virtualDisplayId1 = 2;
407 constexpr int32_t virtualDisplayId2 = 3;
408
409 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000410 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000411 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000412 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700413 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000414 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000415 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000416 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700417 // Add an virtual viewport
418 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000419 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000420 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700421 // Add another virtual viewport
422 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000423 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000424 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700425
426 // Check matching by type for internal
427 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100428 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700429 ASSERT_TRUE(internalViewport);
430 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
431
432 // Check matching by type for external
433 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100434 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700435 ASSERT_TRUE(externalViewport);
436 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
437
438 // Check matching by uniqueId for virtual viewport #1
439 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700440 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700441 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100442 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700443 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
444 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
445
446 // Check matching by uniqueId for virtual viewport #2
447 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700448 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700449 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100450 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700451 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
452 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
453}
454
455
456/**
457 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
458 * that lookup works by checking display id.
459 * Check that 2 viewports of each kind is possible, for all existing viewport types.
460 */
461TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
462 const std::string uniqueId1 = "uniqueId1";
463 const std::string uniqueId2 = "uniqueId2";
464 constexpr int32_t displayId1 = 2;
465 constexpr int32_t displayId2 = 3;
466
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100467 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
468 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700469 for (const ViewportType& type : types) {
470 mFakePolicy->clearViewports();
471 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000472 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000473 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700474 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000475 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000476 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700477
478 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700479 std::optional<DisplayViewport> viewport1 =
480 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700481 ASSERT_TRUE(viewport1);
482 ASSERT_EQ(displayId1, viewport1->displayId);
483 ASSERT_EQ(type, viewport1->type);
484
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700485 std::optional<DisplayViewport> viewport2 =
486 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700487 ASSERT_TRUE(viewport2);
488 ASSERT_EQ(displayId2, viewport2->displayId);
489 ASSERT_EQ(type, viewport2->type);
490
491 // When there are multiple viewports of the same kind, and uniqueId is not specified
492 // in the call to getDisplayViewport, then that situation is not supported.
493 // The viewports can be stored in any order, so we cannot rely on the order, since that
494 // is just implementation detail.
495 // However, we can check that it still returns *a* viewport, we just cannot assert
496 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700497 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700498 ASSERT_TRUE(someViewport);
499 }
500}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800501
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700502/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000503 * When we have multiple internal displays make sure we always return the default display when
504 * querying by type.
505 */
506TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
507 const std::string uniqueId1 = "uniqueId1";
508 const std::string uniqueId2 = "uniqueId2";
509 constexpr int32_t nonDefaultDisplayId = 2;
510 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
511 "Test display ID should not be ADISPLAY_ID_DEFAULT");
512
513 // Add the default display first and ensure it gets returned.
514 mFakePolicy->clearViewports();
515 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000516 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000517 ViewportType::INTERNAL);
518 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000519 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000520 ViewportType::INTERNAL);
521
522 std::optional<DisplayViewport> viewport =
523 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
524 ASSERT_TRUE(viewport);
525 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
526 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
527
528 // Add the default display second to make sure order doesn't matter.
529 mFakePolicy->clearViewports();
530 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000531 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000532 ViewportType::INTERNAL);
533 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000534 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000535 ViewportType::INTERNAL);
536
537 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
538 ASSERT_TRUE(viewport);
539 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
540 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
541}
542
543/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700544 * Check getDisplayViewportByPort
545 */
546TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100547 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700548 const std::string uniqueId1 = "uniqueId1";
549 const std::string uniqueId2 = "uniqueId2";
550 constexpr int32_t displayId1 = 1;
551 constexpr int32_t displayId2 = 2;
552 const uint8_t hdmi1 = 0;
553 const uint8_t hdmi2 = 1;
554 const uint8_t hdmi3 = 2;
555
556 mFakePolicy->clearViewports();
557 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000558 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000559 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700560 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000561 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000562 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700563
564 // Check that correct display viewport was returned by comparing the display ports.
565 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
566 ASSERT_TRUE(hdmi1Viewport);
567 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
568 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
569
570 // Check that we can still get the same viewport using the uniqueId
571 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
572 ASSERT_TRUE(hdmi1Viewport);
573 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
574 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
575 ASSERT_EQ(type, hdmi1Viewport->type);
576
577 // Check that we cannot find a port with "HDMI2", because we never added one
578 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
579 ASSERT_FALSE(hdmi2Viewport);
580}
581
Michael Wrightd02c5b62014-02-10 15:10:22 -0800582// --- InputReaderTest ---
583
584class InputReaderTest : public testing::Test {
585protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700586 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800587 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700588 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000589 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800590
Chris Yea52ade12020-08-27 16:49:20 -0700591 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700592 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700593 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700594 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800595
Prabir Pradhan28efc192019-11-05 01:10:04 +0000596 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700597 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800598 }
599
Chris Yea52ade12020-08-27 16:49:20 -0700600 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700601 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800603 }
604
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700605 void addDevice(int32_t eventHubId, const std::string& name,
606 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800607 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800608
609 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800610 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800611 }
612 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000613 mReader->loopOnce();
614 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700615 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700617 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 }
619
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800620 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700621 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000622 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700623 }
624
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800625 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700626 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000627 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700628 }
629
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800630 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700631 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700632 ftl::Flags<InputDeviceClass> classes,
633 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800634 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800635 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000636 FakeInputMapper& mapper =
637 device->addMapper<FakeInputMapper>(eventHubId,
638 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800639 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800640 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800641 return mapper;
642 }
643};
644
Chris Ye98d3f532020-10-01 21:48:59 -0700645TEST_F(InputReaderTest, PolicyGetInputDevices) {
646 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700647 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700648 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800649
650 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700651 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800652 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800653 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100654 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
656 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000657 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658}
659
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000660TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
661 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
662 mFakeEventHub->setSysfsRootPath(1, "xyz");
663
664 // Should also have received a notification describing the new input device.
665 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
666 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
667 ASSERT_EQ(0U, inputDevice.getLights().size());
668
669 RawLightInfo infoMonolight = {.id = 123,
670 .name = "mono_keyboard_backlight",
671 .maxBrightness = 255,
672 .flags = InputLightClass::BRIGHTNESS,
673 .path = ""};
674 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
675 mReader->sysfsNodeChanged("xyz");
676 mReader->loopOnce();
677
678 // Should also have received a notification describing the new recreated input device.
679 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
680 inputDevice = mFakePolicy->getInputDevices()[0];
681 ASSERT_EQ(1U, inputDevice.getLights().size());
682}
683
Chris Yee7310032020-09-22 15:36:28 -0700684TEST_F(InputReaderTest, GetMergedInputDevices) {
685 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
686 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
687 // Add two subdevices to device
688 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
689 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000690 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
691 AINPUT_SOURCE_KEYBOARD);
692 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
693 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700694
695 // Push same device instance for next device to be added, so they'll have same identifier.
696 mReader->pushNextDevice(device);
697 mReader->pushNextDevice(device);
698 ASSERT_NO_FATAL_FAILURE(
699 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
700 ASSERT_NO_FATAL_FAILURE(
701 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
702
703 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000704 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700705}
706
Chris Yee14523a2020-12-19 13:46:00 -0800707TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
708 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
709 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
710 // Add two subdevices to device
711 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
712 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000713 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
714 AINPUT_SOURCE_KEYBOARD);
715 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
716 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800717
718 // Push same device instance for next device to be added, so they'll have same identifier.
719 mReader->pushNextDevice(device);
720 mReader->pushNextDevice(device);
721 // Sensor device is initially disabled
722 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
723 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
724 nullptr));
725 // Device is disabled because the only sub device is a sensor device and disabled initially.
726 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
727 ASSERT_FALSE(device->isEnabled());
728 ASSERT_NO_FATAL_FAILURE(
729 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
730 // The merged device is enabled if any sub device is enabled
731 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
732 ASSERT_TRUE(device->isEnabled());
733}
734
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700735TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800736 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700737 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800738 constexpr int32_t eventHubId = 1;
739 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700740 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000741 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
742 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800743 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800744 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700745
Yi Kong9b14ac62018-07-17 13:48:38 -0700746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700747
748 NotifyDeviceResetArgs resetArgs;
749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700750 ASSERT_EQ(deviceId, resetArgs.deviceId);
751
752 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800753 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000754 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700755
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700757 ASSERT_EQ(deviceId, resetArgs.deviceId);
758 ASSERT_EQ(device->isEnabled(), false);
759
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800760 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000761 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700764 ASSERT_EQ(device->isEnabled(), false);
765
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800766 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000767 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700769 ASSERT_EQ(deviceId, resetArgs.deviceId);
770 ASSERT_EQ(device->isEnabled(), true);
771}
772
Michael Wrightd02c5b62014-02-10 15:10:22 -0800773TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800774 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700775 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800776 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800777 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800778 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800779 AINPUT_SOURCE_KEYBOARD, nullptr);
780 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800781
782 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
783 AINPUT_SOURCE_ANY, AKEYCODE_A))
784 << "Should return unknown when the device id is >= 0 but unknown.";
785
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800786 ASSERT_EQ(AKEY_STATE_UNKNOWN,
787 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
788 << "Should return unknown when the device id is valid but the sources are not "
789 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800790
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800791 ASSERT_EQ(AKEY_STATE_DOWN,
792 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
793 AKEYCODE_A))
794 << "Should return value provided by mapper when device id is valid and the device "
795 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800796
797 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
798 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
799 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
800
801 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
802 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
803 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
804}
805
Philip Junker4af3b3d2021-12-14 10:36:55 +0100806TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
807 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
808 constexpr int32_t eventHubId = 1;
809 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
810 InputDeviceClass::KEYBOARD,
811 AINPUT_SOURCE_KEYBOARD, nullptr);
812 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
813
814 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
815 << "Should return unknown when the device with the specified id is not found.";
816
817 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
818 << "Should return correct mapping when device id is valid and mapping exists.";
819
820 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
821 << "Should return the location key code when device id is valid and there's no "
822 "mapping.";
823}
824
825TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
826 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
827 constexpr int32_t eventHubId = 1;
828 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
829 InputDeviceClass::JOYSTICK,
830 AINPUT_SOURCE_GAMEPAD, nullptr);
831 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
832
833 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
834 << "Should return unknown when the device id is valid but there is no keyboard mapper";
835}
836
Michael Wrightd02c5b62014-02-10 15:10:22 -0800837TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800838 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700839 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800840 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800841 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800842 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800843 AINPUT_SOURCE_KEYBOARD, nullptr);
844 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845
846 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
847 AINPUT_SOURCE_ANY, KEY_A))
848 << "Should return unknown when the device id is >= 0 but unknown.";
849
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800850 ASSERT_EQ(AKEY_STATE_UNKNOWN,
851 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
852 << "Should return unknown when the device id is valid but the sources are not "
853 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800854
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800855 ASSERT_EQ(AKEY_STATE_DOWN,
856 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
857 KEY_A))
858 << "Should return value provided by mapper when device id is valid and the device "
859 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860
861 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
862 AINPUT_SOURCE_TRACKBALL, KEY_A))
863 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
864
865 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
866 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
867 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
868}
869
870TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800871 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700872 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800873 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800874 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800875 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800876 AINPUT_SOURCE_KEYBOARD, nullptr);
877 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800878
879 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
880 AINPUT_SOURCE_ANY, SW_LID))
881 << "Should return unknown when the device id is >= 0 but unknown.";
882
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800883 ASSERT_EQ(AKEY_STATE_UNKNOWN,
884 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
885 << "Should return unknown when the device id is valid but the sources are not "
886 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800887
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800888 ASSERT_EQ(AKEY_STATE_DOWN,
889 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
890 SW_LID))
891 << "Should return value provided by mapper when device id is valid and the device "
892 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800893
894 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
895 AINPUT_SOURCE_TRACKBALL, SW_LID))
896 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
897
898 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
899 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
900 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
901}
902
903TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800904 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700905 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800906 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800907 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800908 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800909 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100910
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800911 mapper.addSupportedKeyCode(AKEYCODE_A);
912 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800913
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700914 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800915 uint8_t flags[4] = { 0, 0, 0, 1 };
916
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700917 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800918 << "Should return false when device id is >= 0 but unknown.";
919 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
920
921 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700922 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800923 << "Should return false when device id is valid but the sources are not supported by "
924 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800925 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
926
927 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700928 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800929 keyCodes, flags))
930 << "Should return value provided by mapper when device id is valid and the device "
931 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800932 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
933
934 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700935 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
936 << "Should return false when the device id is < 0 but the sources are not supported by "
937 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800938 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
939
940 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700941 ASSERT_TRUE(
942 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
943 << "Should return value provided by mapper when device id is < 0 and one of the "
944 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
946}
947
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000948TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800949 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700950 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800951
952 NotifyConfigurationChangedArgs args;
953
954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
955 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
956}
957
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000958TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800959 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700960 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000961 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800962 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000963 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800964 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800965 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800966 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800967
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000968 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000969 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800970 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
971
972 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800973 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000974 ASSERT_EQ(when, event.when);
975 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800976 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800977 ASSERT_EQ(EV_KEY, event.type);
978 ASSERT_EQ(KEY_A, event.code);
979 ASSERT_EQ(1, event.value);
980}
981
Garfield Tan1c7bc862020-01-28 13:24:04 -0800982TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800983 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700984 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800985 constexpr int32_t eventHubId = 1;
986 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800987 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000988 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
989 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800990 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800991 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800992
993 NotifyDeviceResetArgs resetArgs;
994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800995 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800996
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800997 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000998 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001000 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001001 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001002
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001003 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001004 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001006 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001007 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001008
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001009 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001010 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001012 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001013 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001014}
1015
Garfield Tan1c7bc862020-01-28 13:24:04 -08001016TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1017 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001018 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001019 constexpr int32_t eventHubId = 1;
1020 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1021 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001022 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1023 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001024 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001025 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1026
1027 NotifyDeviceResetArgs resetArgs;
1028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1029 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1030}
1031
Arthur Hungc23540e2018-11-29 20:42:11 +08001032TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001033 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001034 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001035 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001036 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001037 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1038 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001039 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1040 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001041 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001042
1043 const uint8_t hdmi1 = 1;
1044
1045 // Associated touch screen with second display.
1046 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1047
1048 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001049 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001050 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001051 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001052 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001053 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001054 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001055 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001056 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001057
1058 // Add the device, and make sure all of the callbacks are triggered.
1059 // The device is added after the input port associations are processed since
1060 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001061 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001064 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001065
Arthur Hung2c9a3342019-07-23 14:18:59 +08001066 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001067 ASSERT_EQ(deviceId, device->getId());
1068 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1069 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001070
1071 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001072 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001073 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001074 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001075}
1076
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001077TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1078 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001079 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001080 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1081 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1082 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001083 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1084 AINPUT_SOURCE_KEYBOARD);
1085 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1086 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001087 mReader->pushNextDevice(device);
1088 mReader->pushNextDevice(device);
1089 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1090 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1091
1092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1093
1094 NotifyDeviceResetArgs resetArgs;
1095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1096 ASSERT_EQ(deviceId, resetArgs.deviceId);
1097 ASSERT_TRUE(device->isEnabled());
1098 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1099 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1100
1101 disableDevice(deviceId);
1102 mReader->loopOnce();
1103
1104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1105 ASSERT_EQ(deviceId, resetArgs.deviceId);
1106 ASSERT_FALSE(device->isEnabled());
1107 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1108 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1109
1110 enableDevice(deviceId);
1111 mReader->loopOnce();
1112
1113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1114 ASSERT_EQ(deviceId, resetArgs.deviceId);
1115 ASSERT_TRUE(device->isEnabled());
1116 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1117 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1118}
1119
1120TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1121 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001122 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001123 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1124 // Add two subdevices to device
1125 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1126 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001127 device->addMapper<FakeInputMapper>(eventHubIds[0],
1128 mFakePolicy->getReaderConfiguration(),
1129 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001130 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001131 device->addMapper<FakeInputMapper>(eventHubIds[1],
1132 mFakePolicy->getReaderConfiguration(),
1133 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001134 mReader->pushNextDevice(device);
1135 mReader->pushNextDevice(device);
1136 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1137 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1138
1139 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1140 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1141
1142 ASSERT_EQ(AKEY_STATE_DOWN,
1143 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1144 ASSERT_EQ(AKEY_STATE_DOWN,
1145 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1146 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1147 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1148}
1149
Prabir Pradhan7e186182020-11-10 13:56:45 -08001150TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1151 NotifyPointerCaptureChangedArgs args;
1152
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001153 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001154 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001155 mReader->loopOnce();
1156 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001157 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1158 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001159
1160 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001161 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001162 mReader->loopOnce();
1163 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001164 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001165
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001166 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001167 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001168 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001169 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001170 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001171}
1172
Chris Ye87143712020-11-10 05:05:58 +00001173class FakeVibratorInputMapper : public FakeInputMapper {
1174public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001175 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1176 const InputReaderConfiguration& readerConfig, uint32_t sources)
1177 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001178
1179 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1180};
1181
1182TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1183 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001184 ftl::Flags<InputDeviceClass> deviceClass =
1185 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001186 constexpr int32_t eventHubId = 1;
1187 const char* DEVICE_LOCATION = "BLUETOOTH";
1188 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1189 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001190 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1191 mFakePolicy->getReaderConfiguration(),
1192 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001193 mReader->pushNextDevice(device);
1194
1195 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1196 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1197
1198 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1199 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1200}
1201
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001202// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001203
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001204class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001205public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001206 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001207
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001208 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001209
Andy Chenf9f1a022022-08-29 20:07:10 -04001210 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1211
Chris Yee2b1e5c2021-03-10 22:45:12 -08001212 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1213
1214 void dump(std::string& dump) override {}
1215
1216 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1217 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001218 }
1219
Chris Yee2b1e5c2021-03-10 22:45:12 -08001220 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1221 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001222 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001223
1224 bool setLightColor(int32_t lightId, int32_t color) override {
1225 getDeviceContext().setLightBrightness(lightId, color >> 24);
1226 return true;
1227 }
1228
1229 std::optional<int32_t> getLightColor(int32_t lightId) override {
1230 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1231 if (!result.has_value()) {
1232 return std::nullopt;
1233 }
1234 return result.value() << 24;
1235 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001236
1237 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1238
1239 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1240
1241private:
1242 InputDeviceContext& mDeviceContext;
1243 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1244 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001245 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001246};
1247
Chris Yee2b1e5c2021-03-10 22:45:12 -08001248TEST_F(InputReaderTest, BatteryGetCapacity) {
1249 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001250 ftl::Flags<InputDeviceClass> deviceClass =
1251 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001252 constexpr int32_t eventHubId = 1;
1253 const char* DEVICE_LOCATION = "BLUETOOTH";
1254 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001255 FakePeripheralController& controller =
1256 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001257 mReader->pushNextDevice(device);
1258
1259 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1260
Harry Cuttsa5b71292022-11-28 12:56:17 +00001261 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1262 FakeEventHub::BATTERY_CAPACITY);
1263 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001264}
1265
1266TEST_F(InputReaderTest, BatteryGetStatus) {
1267 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001268 ftl::Flags<InputDeviceClass> deviceClass =
1269 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001270 constexpr int32_t eventHubId = 1;
1271 const char* DEVICE_LOCATION = "BLUETOOTH";
1272 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001273 FakePeripheralController& controller =
1274 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001275 mReader->pushNextDevice(device);
1276
1277 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1278
Harry Cuttsa5b71292022-11-28 12:56:17 +00001279 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1280 FakeEventHub::BATTERY_STATUS);
1281 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001282}
1283
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001284TEST_F(InputReaderTest, BatteryGetDevicePath) {
1285 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1286 ftl::Flags<InputDeviceClass> deviceClass =
1287 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1288 constexpr int32_t eventHubId = 1;
1289 const char* DEVICE_LOCATION = "BLUETOOTH";
1290 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1291 device->addController<FakePeripheralController>(eventHubId);
1292 mReader->pushNextDevice(device);
1293
1294 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1295
Harry Cuttsa5b71292022-11-28 12:56:17 +00001296 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001297}
1298
Chris Ye3fdbfef2021-01-06 18:45:18 -08001299TEST_F(InputReaderTest, LightGetColor) {
1300 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001301 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001302 constexpr int32_t eventHubId = 1;
1303 const char* DEVICE_LOCATION = "BLUETOOTH";
1304 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001305 FakePeripheralController& controller =
1306 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001307 mReader->pushNextDevice(device);
1308 RawLightInfo info = {.id = 1,
1309 .name = "Mono",
1310 .maxBrightness = 255,
1311 .flags = InputLightClass::BRIGHTNESS,
1312 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001313 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1314 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001315
1316 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001317
Harry Cutts33476232023-01-30 19:57:29 +00001318 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1319 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1320 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1321 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001322}
1323
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001324// --- InputReaderIntegrationTest ---
1325
1326// These tests create and interact with the InputReader only through its interface.
1327// The InputReader is started during SetUp(), which starts its processing in its own
1328// thread. The tests use linux uinput to emulate input devices.
1329// NOTE: Interacting with the physical device while these tests are running may cause
1330// the tests to fail.
1331class InputReaderIntegrationTest : public testing::Test {
1332protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001333 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001334 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001335 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001336
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001337 std::shared_ptr<FakePointerController> mFakePointerController;
1338
Chris Yea52ade12020-08-27 16:49:20 -07001339 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001340#if !defined(__ANDROID__)
1341 GTEST_SKIP();
1342#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001343 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001344 mFakePointerController = std::make_shared<FakePointerController>();
1345 mFakePolicy->setPointerController(mFakePointerController);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001346
Arpit Singh440bf652023-08-09 09:23:43 +00001347 setupInputReader();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001348 }
1349
Chris Yea52ade12020-08-27 16:49:20 -07001350 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001351#if !defined(__ANDROID__)
1352 return;
1353#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001354 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001355 mReader.reset();
1356 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001357 mFakePolicy.clear();
1358 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001359
1360 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1361 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1362 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1363 [&name](const InputDeviceInfo& info) {
1364 return info.getIdentifier().name == name;
1365 });
1366 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1367 }
Arpit Singh440bf652023-08-09 09:23:43 +00001368
1369 void setupInputReader() {
1370 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1371 /*eventDidNotHappenTimeout=*/30ms);
1372
1373 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1374 *mTestListener);
1375 ASSERT_EQ(mReader->start(), OK);
1376
1377 // Since this test is run on a real device, all the input devices connected
1378 // to the test device will show up in mReader. We wait for those input devices to
1379 // show up before beginning the tests.
1380 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1381 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
1382 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1383 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001384};
1385
1386TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1387 // An invalid input device that is only used for this test.
1388 class InvalidUinputDevice : public UinputDevice {
1389 public:
Harry Cutts33476232023-01-30 19:57:29 +00001390 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001391
1392 private:
1393 void configureDevice(int fd, uinput_user_dev* device) override {}
1394 };
1395
1396 const size_t numDevices = mFakePolicy->getInputDevices().size();
1397
1398 // UinputDevice does not set any event or key bits, so InputReader should not
1399 // consider it as a valid device.
1400 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1401 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1402 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1403 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1404
1405 invalidDevice.reset();
1406 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1407 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1408 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1409}
1410
1411TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1412 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1413
1414 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1415 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1416 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1417 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1418
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001419 const auto device = findDeviceByName(keyboard->getName());
1420 ASSERT_TRUE(device.has_value());
1421 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1422 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1423 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001424
1425 keyboard.reset();
1426 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1427 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1428 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1429}
1430
1431TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1432 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1433 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1434
1435 NotifyConfigurationChangedArgs configChangedArgs;
1436 ASSERT_NO_FATAL_FAILURE(
1437 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001438 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001439 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1440
1441 NotifyKeyArgs keyArgs;
1442 keyboard->pressAndReleaseHomeKey();
1443 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1444 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001445 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001446 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001447 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001448 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001449 prevTimestamp = keyArgs.eventTime;
1450
1451 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1452 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001453 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001454 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001455 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001456}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001457
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001458TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1459 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1460 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1461
1462 const auto device = findDeviceByName(stylus->getName());
1463 ASSERT_TRUE(device.has_value());
1464
Prabir Pradhana3621852022-10-14 18:57:23 +00001465 // An external stylus with buttons should also be recognized as a keyboard.
1466 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001467 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1468 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1469
1470 const auto DOWN =
1471 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1472 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1473
1474 stylus->pressAndReleaseKey(BTN_STYLUS);
1475 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1476 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1477 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1478 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1479
1480 stylus->pressAndReleaseKey(BTN_STYLUS2);
1481 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1482 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1483 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1484 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1485
1486 stylus->pressAndReleaseKey(BTN_STYLUS3);
1487 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1488 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1489 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1490 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1491}
1492
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001493/**
1494 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1495 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1496 * are passed to the listener.
1497 */
1498static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1499TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1500 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1501 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1502 NotifyKeyArgs keyArgs;
1503
1504 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1505 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1506 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1507 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1508
1509 controller->pressAndReleaseKey(BTN_GEAR_UP);
1510 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1511 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1512 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1513}
1514
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001515// --- TouchIntegrationTest ---
1516
Arpit Singh440bf652023-08-09 09:23:43 +00001517class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
Arthur Hungaab25622020-01-16 11:22:11 +08001518protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001519 const std::string UNIQUE_ID = "local:0";
1520
Chris Yea52ade12020-08-27 16:49:20 -07001521 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001522#if !defined(__ANDROID__)
1523 GTEST_SKIP();
1524#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001525 InputReaderIntegrationTest::SetUp();
1526 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001527 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1528 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001529
1530 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1531 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1532 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001533 const auto info = findDeviceByName(mDevice->getName());
1534 ASSERT_TRUE(info);
1535 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001536 }
1537
1538 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001539 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001540 std::optional<uint8_t> physicalPort,
1541 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001542 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001543 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001544 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001545 }
1546
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001547 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1548 NotifyMotionArgs args;
1549 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1550 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001551 ASSERT_EQ(points.size(), args.getPointerCount());
1552 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001553 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1554 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1555 }
1556 }
1557
Arthur Hungaab25622020-01-16 11:22:11 +08001558 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001559 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001560};
1561
Arpit Singh440bf652023-08-09 09:23:43 +00001562enum class TouchIntegrationTestDisplays { DISPLAY_INTERNAL, DISPLAY_INPUT_PORT, DISPLAY_UNIQUE_ID };
1563
1564class TouchIntegrationTest : public BaseTouchIntegrationTest,
1565 public testing::WithParamInterface<TouchIntegrationTestDisplays> {
1566protected:
1567 static constexpr std::optional<uint8_t> DISPLAY_PORT = 0;
1568 const std::string INPUT_PORT = "uinput_touch/input0";
1569
1570 void SetUp() override {
1571#if !defined(__ANDROID__)
1572 GTEST_SKIP();
1573#endif
1574 if (GetParam() == TouchIntegrationTestDisplays::DISPLAY_INTERNAL) {
1575 BaseTouchIntegrationTest::SetUp();
1576 return;
1577 }
1578
1579 // setup policy with a input-port or UniqueId association to the display
1580 bool isInputPortAssociation =
1581 GetParam() == TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT;
1582
1583 mFakePolicy = sp<FakeInputReaderPolicy>::make();
1584 if (isInputPortAssociation) {
1585 mFakePolicy->addInputPortAssociation(INPUT_PORT, DISPLAY_PORT.value());
1586 } else {
1587 mFakePolicy->addInputUniqueIdAssociation(INPUT_PORT, UNIQUE_ID);
1588 }
1589 mFakePointerController = std::make_shared<FakePointerController>();
1590 mFakePolicy->setPointerController(mFakePointerController);
1591
1592 InputReaderIntegrationTest::setupInputReader();
1593
1594 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
1595 INPUT_PORT);
1596 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1597
1598 // Add a display linked to a physical port or UniqueId.
1599 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1600 UNIQUE_ID, isInputPortAssociation ? DISPLAY_PORT : NO_PORT,
1601 ViewportType::INTERNAL);
1602 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1603 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1604 const auto info = findDeviceByName(mDevice->getName());
1605 ASSERT_TRUE(info);
1606 mDeviceInfo = *info;
1607 }
1608};
1609
1610TEST_P(TouchIntegrationTest, MultiTouchDeviceSource) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001611 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1612 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1613 // presses).
1614 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1615 mDeviceInfo.getSources());
1616}
1617
Arpit Singh440bf652023-08-09 09:23:43 +00001618TEST_P(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001619 NotifyMotionArgs args;
1620 const Point centerPoint = mDevice->getCenterPoint();
1621
1622 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001623 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001624 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001625 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001626 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1627 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1628
1629 // ACTION_MOVE
1630 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001631 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001632 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1633 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1634
1635 // ACTION_UP
1636 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001637 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001638 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1639 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1640}
1641
Arpit Singh440bf652023-08-09 09:23:43 +00001642TEST_P(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001643 NotifyMotionArgs args;
1644 const Point centerPoint = mDevice->getCenterPoint();
1645
1646 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001647 mDevice->sendSlot(FIRST_SLOT);
1648 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001649 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001650 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001651 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1652 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1653
1654 // ACTION_POINTER_DOWN (Second slot)
1655 const Point secondPoint = centerPoint + Point(100, 100);
1656 mDevice->sendSlot(SECOND_SLOT);
1657 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001658 mDevice->sendDown(secondPoint);
1659 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001660 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001661 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001662
1663 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001664 mDevice->sendMove(secondPoint + Point(1, 1));
1665 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001666 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1667 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1668
1669 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001670 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001671 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001672 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001673 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001674
1675 // ACTION_UP
1676 mDevice->sendSlot(FIRST_SLOT);
1677 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001678 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001679 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1680 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1681}
1682
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001683/**
1684 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1685 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1686 * data?
1687 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1688 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1689 * for Pointer 0 only is generated after.
1690 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1691 * events, we will not miss any information.
1692 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1693 * event generated afterwards that contains the newest movement of pointer 0.
1694 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1695 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1696 * losing information about non-palm pointers.
1697 */
Arpit Singh440bf652023-08-09 09:23:43 +00001698TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001699 NotifyMotionArgs args;
1700 const Point centerPoint = mDevice->getCenterPoint();
1701
1702 // ACTION_DOWN
1703 mDevice->sendSlot(FIRST_SLOT);
1704 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1705 mDevice->sendDown(centerPoint);
1706 mDevice->sendSync();
1707 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1708
1709 // ACTION_POINTER_DOWN (Second slot)
1710 const Point secondPoint = centerPoint + Point(100, 100);
1711 mDevice->sendSlot(SECOND_SLOT);
1712 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1713 mDevice->sendDown(secondPoint);
1714 mDevice->sendSync();
1715 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1716
1717 // ACTION_MOVE (First slot)
1718 mDevice->sendSlot(FIRST_SLOT);
1719 mDevice->sendMove(centerPoint + Point(5, 5));
1720 // ACTION_POINTER_UP (Second slot)
1721 mDevice->sendSlot(SECOND_SLOT);
1722 mDevice->sendPointerUp();
1723 // Send a single sync for the above 2 pointer updates
1724 mDevice->sendSync();
1725
1726 // First, we should get POINTER_UP for the second pointer
1727 assertReceivedMotion(ACTION_POINTER_1_UP,
1728 {/*first pointer */ centerPoint + Point(5, 5),
1729 /*second pointer*/ secondPoint});
1730
1731 // Next, the MOVE event for the first pointer
1732 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1733}
1734
1735/**
1736 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1737 * move, and then it will go up, all in the same frame.
1738 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1739 * gets sent to the listener.
1740 */
Arpit Singh440bf652023-08-09 09:23:43 +00001741TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001742 NotifyMotionArgs args;
1743 const Point centerPoint = mDevice->getCenterPoint();
1744
1745 // ACTION_DOWN
1746 mDevice->sendSlot(FIRST_SLOT);
1747 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1748 mDevice->sendDown(centerPoint);
1749 mDevice->sendSync();
1750 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1751
1752 // ACTION_POINTER_DOWN (Second slot)
1753 const Point secondPoint = centerPoint + Point(100, 100);
1754 mDevice->sendSlot(SECOND_SLOT);
1755 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1756 mDevice->sendDown(secondPoint);
1757 mDevice->sendSync();
1758 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1759
1760 // ACTION_MOVE (First slot)
1761 mDevice->sendSlot(FIRST_SLOT);
1762 mDevice->sendMove(centerPoint + Point(5, 5));
1763 // ACTION_POINTER_UP (Second slot)
1764 mDevice->sendSlot(SECOND_SLOT);
1765 mDevice->sendMove(secondPoint + Point(6, 6));
1766 mDevice->sendPointerUp();
1767 // Send a single sync for the above 2 pointer updates
1768 mDevice->sendSync();
1769
1770 // First, we should get POINTER_UP for the second pointer
1771 // The movement of the second pointer during the liftoff frame is ignored.
1772 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1773 assertReceivedMotion(ACTION_POINTER_1_UP,
1774 {/*first pointer */ centerPoint + Point(5, 5),
1775 /*second pointer*/ secondPoint});
1776
1777 // Next, the MOVE event for the first pointer
1778 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1779}
1780
Arpit Singh440bf652023-08-09 09:23:43 +00001781TEST_P(TouchIntegrationTest, InputEvent_ProcessPalm) {
Arthur Hungaab25622020-01-16 11:22:11 +08001782 NotifyMotionArgs args;
1783 const Point centerPoint = mDevice->getCenterPoint();
1784
1785 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001786 mDevice->sendSlot(FIRST_SLOT);
1787 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001788 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001789 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001790 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1791 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1792
arthurhungcc7f9802020-04-30 17:55:40 +08001793 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001794 const Point secondPoint = centerPoint + Point(100, 100);
1795 mDevice->sendSlot(SECOND_SLOT);
1796 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1797 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001798 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001799 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001800 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001801
arthurhungcc7f9802020-04-30 17:55:40 +08001802 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001803 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001804 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001805 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1806 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1807
arthurhungcc7f9802020-04-30 17:55:40 +08001808 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1809 // a palm event.
1810 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001811 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001812 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001813 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001814 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001815 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001816
arthurhungcc7f9802020-04-30 17:55:40 +08001817 // Send up to second slot, expect first slot send moving.
1818 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001819 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001820 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1821 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001822
arthurhungcc7f9802020-04-30 17:55:40 +08001823 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001824 mDevice->sendSlot(FIRST_SLOT);
1825 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001826 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001827
arthurhungcc7f9802020-04-30 17:55:40 +08001828 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1829 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001830}
1831
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +00001832/**
1833 * Some drivers historically have reported axis values outside of the range specified in the
1834 * evdev axis info. Ensure we don't crash when this happens. For example, a driver may report a
1835 * pressure value greater than the reported maximum, since it unclear what specific meaning the
1836 * maximum value for pressure has (beyond the maximum value that can be produced by a sensor),
1837 * and no units for pressure (resolution) is specified by the evdev documentation.
1838 */
1839TEST_P(TouchIntegrationTest, AcceptsAxisValuesOutsideReportedRange) {
1840 const Point centerPoint = mDevice->getCenterPoint();
1841
1842 // Down with pressure outside the reported range
1843 mDevice->sendSlot(FIRST_SLOT);
1844 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1845 mDevice->sendDown(centerPoint);
1846 mDevice->sendPressure(UinputTouchScreen::RAW_PRESSURE_MAX + 2);
1847 mDevice->sendSync();
1848 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1849 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1850
1851 // Move to a point outside the reported range
1852 mDevice->sendMove(Point(DISPLAY_WIDTH, DISPLAY_HEIGHT) + Point(1, 1));
1853 mDevice->sendSync();
1854 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1855 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1856
1857 // Up
1858 mDevice->sendUp();
1859 mDevice->sendSync();
1860 ASSERT_NO_FATAL_FAILURE(
1861 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1862}
1863
Arpit Singh440bf652023-08-09 09:23:43 +00001864TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00001865 const Point centerPoint = mDevice->getCenterPoint();
1866
1867 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1868 mDevice->sendSlot(FIRST_SLOT);
1869 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1870 mDevice->sendToolType(MT_TOOL_PEN);
1871 mDevice->sendDown(centerPoint);
1872 mDevice->sendSync();
1873 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1874 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001875 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001876
1877 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1878
1879 // Release the stylus touch.
1880 mDevice->sendUp();
1881 mDevice->sendSync();
1882 ASSERT_NO_FATAL_FAILURE(
1883 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1884
1885 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1886
1887 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1888 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1889 mDevice->sendToolType(MT_TOOL_FINGER);
1890 mDevice->sendDown(centerPoint);
1891 mDevice->sendSync();
1892 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1893 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001894 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001895
1896 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1897
1898 mDevice->sendUp();
1899 mDevice->sendSync();
1900 ASSERT_NO_FATAL_FAILURE(
1901 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1902
1903 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1904 // The policy should be notified of the stylus presence.
1905 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1906 mDevice->sendToolType(MT_TOOL_PEN);
1907 mDevice->sendMove(centerPoint);
1908 mDevice->sendSync();
1909 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1910 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001911 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001912
1913 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1914}
1915
Arpit Singh440bf652023-08-09 09:23:43 +00001916TEST_P(TouchIntegrationTest, ExternalStylusConnectedDuringTouchGesture) {
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001917 const Point centerPoint = mDevice->getCenterPoint();
1918
1919 // Down
1920 mDevice->sendSlot(FIRST_SLOT);
1921 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1922 mDevice->sendDown(centerPoint);
1923 mDevice->sendSync();
1924 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1925 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1926
1927 // Move
1928 mDevice->sendMove(centerPoint + Point(1, 1));
1929 mDevice->sendSync();
1930 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1931 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1932
1933 // Connecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
1934 auto externalStylus = createUinputDevice<UinputExternalStylus>();
1935 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1936 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1937 const auto stylusInfo = findDeviceByName(externalStylus->getName());
1938 ASSERT_TRUE(stylusInfo);
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001939
1940 // Move
1941 mDevice->sendMove(centerPoint + Point(2, 2));
1942 mDevice->sendSync();
1943 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1944 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1945
1946 // Disconnecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
1947 externalStylus.reset();
1948 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1949 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1950 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
1951
1952 // Up
1953 mDevice->sendUp();
1954 mDevice->sendSync();
1955 ASSERT_NO_FATAL_FAILURE(
1956 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1957
1958 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
1959}
1960
Arpit Singh440bf652023-08-09 09:23:43 +00001961INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
1962 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
1963 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
1964 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
1965
Prabir Pradhan124ea442022-10-28 20:27:44 +00001966// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001967
Prabir Pradhan124ea442022-10-28 20:27:44 +00001968// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1969// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1970// stylus.
1971template <typename UinputStylusDevice>
Arpit Singh440bf652023-08-09 09:23:43 +00001972class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001973protected:
1974 void SetUp() override {
1975#if !defined(__ANDROID__)
1976 GTEST_SKIP();
1977#endif
Arpit Singh440bf652023-08-09 09:23:43 +00001978 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00001979 mTouchscreen = mDevice.get();
1980 mTouchscreenInfo = mDeviceInfo;
1981
1982 setUpStylusDevice();
1983 }
1984
1985 UinputStylusDevice* mStylus{nullptr};
1986 InputDeviceInfo mStylusInfo{};
1987
1988 UinputTouchScreen* mTouchscreen{nullptr};
1989 InputDeviceInfo mTouchscreenInfo{};
1990
1991private:
1992 // When we are attempting to test stylus button events that are sent from the touchscreen,
1993 // use the same Uinput device for the touchscreen and the stylus.
1994 template <typename T = UinputStylusDevice>
1995 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1996 mStylus = mDevice.get();
1997 mStylusInfo = mDeviceInfo;
1998 }
1999
2000 // When we are attempting to stylus buttons from an external stylus being merged with touches
2001 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2002 template <typename T = UinputStylusDevice>
2003 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2004 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2005 mStylus = mStylusDeviceLifecycleTracker.get();
2006 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2007 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2008 const auto info = findDeviceByName(mStylus->getName());
2009 ASSERT_TRUE(info);
2010 mStylusInfo = *info;
2011 }
2012
2013 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2014
2015 // Hide the base class's device to expose it with a different name for readability.
Arpit Singh440bf652023-08-09 09:23:43 +00002016 using BaseTouchIntegrationTest::mDevice;
2017 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00002018};
2019
2020using StylusButtonIntegrationTestTypes =
2021 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2022TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2023
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002024TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002025 const auto stylusId = TestFixture::mStylusInfo.getId();
2026
2027 TestFixture::mStylus->pressKey(BTN_STYLUS);
2028 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2029 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2030 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2031
2032 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2033 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002034 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002035 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002036}
2037
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002038TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002039 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2040 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2041 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002042
2043 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002044 TestFixture::mStylus->pressKey(BTN_STYLUS);
2045 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002046 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002047 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002048
2049 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002050 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2051 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2052 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2053 TestFixture::mTouchscreen->sendDown(centerPoint);
2054 TestFixture::mTouchscreen->sendSync();
2055 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002056 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002057 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002058 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2059 WithDeviceId(touchscreenId))));
2060 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002061 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002062 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002063 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2064 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002065
Prabir Pradhan124ea442022-10-28 20:27:44 +00002066 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2067 TestFixture::mTouchscreen->sendSync();
2068 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002069 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002070 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002071 WithDeviceId(touchscreenId))));
2072 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002073 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002074 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002075 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002076
2077 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002078 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2079 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002080 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002081 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002082}
2083
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002084TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002085 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2086 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2087 const auto stylusId = TestFixture::mStylusInfo.getId();
2088 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002089 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002090
2091 // Press the stylus button.
2092 TestFixture::mStylus->pressKey(BTN_STYLUS);
2093 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2094 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2095 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2096
2097 // Start hovering with the stylus.
2098 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2099 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2100 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2101 TestFixture::mTouchscreen->sendMove(centerPoint);
2102 TestFixture::mTouchscreen->sendSync();
2103 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2104 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2105 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2106 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2107 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2108 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2109 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2110 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2111 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2112
2113 // Touch down with the stylus.
2114 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2115 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2116 TestFixture::mTouchscreen->sendDown(centerPoint);
2117 TestFixture::mTouchscreen->sendSync();
2118 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2119 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2120 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2121
2122 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2123 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2124 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2125
2126 // Stop touching with the stylus, and start hovering.
2127 TestFixture::mTouchscreen->sendUp();
2128 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2129 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2130 TestFixture::mTouchscreen->sendMove(centerPoint);
2131 TestFixture::mTouchscreen->sendSync();
2132 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2133 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2134 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2135 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2136 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2137 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2138 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2139 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2140 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2141
2142 // Stop hovering.
2143 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2144 TestFixture::mTouchscreen->sendSync();
2145 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2146 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2147 WithButtonState(0))));
2148 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2149 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2150 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2151 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2152
2153 // Release the stylus button.
2154 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2155 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2156 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2157 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2158}
2159
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002160TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002161 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2162 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2163 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002164
2165 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002166 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2167 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2168 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2169 TestFixture::mTouchscreen->sendDown(centerPoint);
2170 TestFixture::mTouchscreen->sendSync();
2171 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002172 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002173 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002174 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002175
2176 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002177 TestFixture::mStylus->pressKey(BTN_STYLUS);
2178 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002179 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002180 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2181 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002182 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002183 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002184 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2185 WithDeviceId(touchscreenId))));
2186 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002187 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002188 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002189 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2190 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002191
Prabir Pradhan124ea442022-10-28 20:27:44 +00002192 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2193 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002194 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002195 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2196 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002197 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002198 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002199 WithDeviceId(touchscreenId))));
2200 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002201 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002202 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002203 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002204
2205 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002206 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2207 TestFixture::mTouchscreen->sendSync();
2208 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002209 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002210 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002211 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002212}
2213
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002214TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002215 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2216 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002217 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002218
2219 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2220 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2221 const auto stylusId = TestFixture::mStylusInfo.getId();
2222
2223 // Start a stylus gesture. By the time this event is processed, the configuration change that
2224 // was requested is guaranteed to be completed.
2225 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2226 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2227 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2228 TestFixture::mTouchscreen->sendDown(centerPoint);
2229 TestFixture::mTouchscreen->sendSync();
2230 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2231 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002232 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002233 WithDeviceId(touchscreenId))));
2234
2235 // Press and release a stylus button. Each change only generates a MOVE motion event.
2236 // Key events are unaffected.
2237 TestFixture::mStylus->pressKey(BTN_STYLUS);
2238 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2239 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2240 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2241 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2242 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002243 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002244 WithDeviceId(touchscreenId))));
2245
2246 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2247 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2248 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2249 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2250 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2251 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002252 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002253 WithDeviceId(touchscreenId))));
2254
2255 // Finish the stylus gesture.
2256 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2257 TestFixture::mTouchscreen->sendSync();
2258 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2259 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002260 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002261 WithDeviceId(touchscreenId))));
2262}
2263
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002264// --- ExternalStylusIntegrationTest ---
2265
2266// Verify the behavior of an external stylus. An external stylus can report pressure or button
2267// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2268// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singh440bf652023-08-09 09:23:43 +00002269using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002270
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002271TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002272 const Point centerPoint = mDevice->getCenterPoint();
2273
2274 // Create an external stylus capable of reporting pressure data that
2275 // should be fused with a touch pointer.
2276 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2277 createUinputDevice<UinputExternalStylusWithPressure>();
2278 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2279 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2280 const auto stylusInfo = findDeviceByName(stylus->getName());
2281 ASSERT_TRUE(stylusInfo);
2282
2283 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2284
2285 const auto touchscreenId = mDeviceInfo.getId();
2286
2287 // Set a pressure value on the stylus. It doesn't generate any events.
2288 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2289 stylus->setPressure(100);
2290 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2291
2292 // Start a finger gesture, and ensure it shows up as stylus gesture
2293 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002294 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002295 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002296 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002297 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002298 mDevice->sendSync();
2299 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2300 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002301 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002302 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002303
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002304 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2305 // event with the updated pressure.
2306 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002307 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2308 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002309 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002310 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002311
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002312 // The external stylus did not generate any events.
2313 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2314 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2315}
2316
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002317TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002318 const Point centerPoint = mDevice->getCenterPoint();
2319
2320 // Create an external stylus capable of reporting pressure data that
2321 // should be fused with a touch pointer.
2322 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2323 createUinputDevice<UinputExternalStylusWithPressure>();
2324 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2325 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2326 const auto stylusInfo = findDeviceByName(stylus->getName());
2327 ASSERT_TRUE(stylusInfo);
2328
2329 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2330
2331 const auto touchscreenId = mDeviceInfo.getId();
2332
2333 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2334 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002335 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2336 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002337 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002338 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002339
2340 // Start a finger gesture. The touch device will withhold generating any touches for
2341 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2342 mDevice->sendSlot(FIRST_SLOT);
2343 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2344 mDevice->sendToolType(MT_TOOL_FINGER);
2345 mDevice->sendDown(centerPoint);
2346 auto waitUntil = std::chrono::system_clock::now() +
2347 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002348 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002349 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002350
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002351 // Since the external stylus did not report a pressure value within the timeout,
2352 // it shows up as a finger pointer.
2353 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2354 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002355 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002356 WithPressure(1.f))));
2357
2358 // Change the pressure on the external stylus. Since the pressure was not present at the start
2359 // of the gesture, it is ignored for now.
2360 stylus->setPressure(200);
2361 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2362
2363 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002364 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2365 mDevice->sendSync();
2366 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2367 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002368 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002369
2370 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2371 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2372 mDevice->sendToolType(MT_TOOL_FINGER);
2373 mDevice->sendDown(centerPoint);
2374 mDevice->sendSync();
2375 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2376 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002377 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002378 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2379
2380 // The external stylus did not generate any events.
2381 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2382 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002383}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002384
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002385TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002386 const Point centerPoint = mDevice->getCenterPoint();
2387
2388 // Create an external stylus device that does not support pressure. It should not affect any
2389 // touch pointers.
2390 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2391 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2392 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2393 const auto stylusInfo = findDeviceByName(stylus->getName());
2394 ASSERT_TRUE(stylusInfo);
2395
2396 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2397
2398 const auto touchscreenId = mDeviceInfo.getId();
2399
2400 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2401 // pressure data from the external stylus.
2402 mDevice->sendSlot(FIRST_SLOT);
2403 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2404 mDevice->sendToolType(MT_TOOL_FINGER);
2405 mDevice->sendDown(centerPoint);
2406 auto waitUntil = std::chrono::system_clock::now() +
2407 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2408 mDevice->sendSync();
2409 ASSERT_NO_FATAL_FAILURE(
2410 mTestListener
2411 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2412 WithToolType(
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002413 ToolType::FINGER),
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002414 WithButtonState(0),
2415 WithDeviceId(touchscreenId),
2416 WithPressure(1.f)),
2417 waitUntil));
2418
2419 // The external stylus did not generate any events.
2420 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2421 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2422}
2423
Michael Wrightd02c5b62014-02-10 15:10:22 -08002424// --- InputDeviceTest ---
2425class InputDeviceTest : public testing::Test {
2426protected:
2427 static const char* DEVICE_NAME;
2428 static const char* DEVICE_LOCATION;
2429 static const int32_t DEVICE_ID;
2430 static const int32_t DEVICE_GENERATION;
2431 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002432 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002433 static const int32_t EVENTHUB_ID;
2434 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2435
2436 std::shared_ptr<FakeEventHub> mFakeEventHub;
2437 sp<FakeInputReaderPolicy> mFakePolicy;
2438 std::unique_ptr<TestInputListener> mFakeListener;
2439 std::unique_ptr<InstrumentedInputReader> mReader;
2440 std::shared_ptr<InputDevice> mDevice;
2441
2442 void SetUp() override {
2443 mFakeEventHub = std::make_unique<FakeEventHub>();
2444 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2445 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002446 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002447 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002448 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002449 identifier.name = DEVICE_NAME;
2450 identifier.location = DEVICE_LOCATION;
2451 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2452 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2453 identifier);
2454 mReader->pushNextDevice(mDevice);
2455 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002456 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002457 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002458
2459 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002460 mFakeListener.reset();
2461 mFakePolicy.clear();
2462 }
2463};
2464
2465const char* InputDeviceTest::DEVICE_NAME = "device";
2466const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2467const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2468const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002469const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002470const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2471 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002472const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002473const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2474
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002475TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002476 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002477 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2478 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002479}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002480
Michael Wrightd02c5b62014-02-10 15:10:22 -08002481TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2482 ASSERT_EQ(mDevice->isEnabled(), false);
2483}
2484
2485TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2486 // Configuration.
2487 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002488 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002489
2490 // Reset.
2491 unused += mDevice->reset(ARBITRARY_TIME);
2492
2493 NotifyDeviceResetArgs resetArgs;
2494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2495 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2496 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2497
2498 // Metadata.
2499 ASSERT_TRUE(mDevice->isIgnored());
2500 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2501
2502 InputDeviceInfo info = mDevice->getDeviceInfo();
2503 ASSERT_EQ(DEVICE_ID, info.getId());
2504 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2505 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2506 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2507
2508 // State queries.
2509 ASSERT_EQ(0, mDevice->getMetaState());
2510
2511 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2512 << "Ignored device should return unknown key code state.";
2513 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2514 << "Ignored device should return unknown scan code state.";
2515 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2516 << "Ignored device should return unknown switch state.";
2517
2518 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2519 uint8_t flags[2] = { 0, 1 };
2520 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2521 << "Ignored device should never mark any key codes.";
2522 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2523 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2524}
2525
2526TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2527 // Configuration.
2528 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2529
2530 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002531 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2532 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002533 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2534 mapper1.setMetaState(AMETA_ALT_ON);
2535 mapper1.addSupportedKeyCode(AKEYCODE_A);
2536 mapper1.addSupportedKeyCode(AKEYCODE_B);
2537 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2538 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2539 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2540 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2541 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2542
2543 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002544 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2545 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002546 mapper2.setMetaState(AMETA_SHIFT_ON);
2547
2548 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002549 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002550
Harry Cuttsf13161a2023-03-08 14:15:49 +00002551 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2552 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002553 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002554 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002555
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002556 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2557 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002558
2559 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002560 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002561 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2562 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002563
2564 NotifyDeviceResetArgs resetArgs;
2565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2566 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2567 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2568
2569 // Metadata.
2570 ASSERT_FALSE(mDevice->isIgnored());
2571 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2572
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002573 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002574 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002575 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002576 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2577 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2578
2579 // State queries.
2580 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2581 << "Should query mappers and combine meta states.";
2582
2583 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2584 << "Should return unknown key code state when source not supported.";
2585 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2586 << "Should return unknown scan code state when source not supported.";
2587 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2588 << "Should return unknown switch state when source not supported.";
2589
2590 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2591 << "Should query mapper when source is supported.";
2592 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2593 << "Should query mapper when source is supported.";
2594 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2595 << "Should query mapper when source is supported.";
2596
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002597 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002598 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002599 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002600 << "Should do nothing when source is unsupported.";
2601 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2602 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2603 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2604 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2605
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002606 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002607 << "Should query mapper when source is supported.";
2608 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2609 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2610 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2611 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2612
2613 // Event handling.
2614 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002615 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002616 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002617
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002618 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2619 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002620}
2621
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -07002622TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2623 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2624 FakeInputMapper& mapper =
2625 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2626 AINPUT_SOURCE_KEYBOARD);
2627 NotifyMotionArgs args1;
2628 NotifySwitchArgs args2;
2629 NotifyKeyArgs args3;
2630 mapper.setProcessResult({args1, args2, args3});
2631
2632 InputReaderConfiguration config;
2633 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2634
2635 RawEvent event;
2636 event.deviceId = EVENTHUB_ID;
2637 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2638
2639 for (auto& arg : notifyArgs) {
2640 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2641 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2642 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2643 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2644 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2645 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2646 }
2647 }
2648}
2649
2650TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2651 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2652 FakeInputMapper& mapper =
2653 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2654 AINPUT_SOURCE_KEYBOARD);
2655 NotifyMotionArgs args;
2656 mapper.setProcessResult({args});
2657
2658 InputReaderConfiguration config;
2659 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2660
2661 RawEvent event;
2662 event.deviceId = EVENTHUB_ID;
2663 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2664
2665 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2666 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2667}
2668
2669TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2670 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2671 FakeInputMapper& mapper =
2672 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2673 AINPUT_SOURCE_KEYBOARD);
2674 NotifyMotionArgs args;
2675 args.policyFlags = POLICY_FLAG_WAKE;
2676 mapper.setProcessResult({args});
2677
2678 InputReaderConfiguration config;
2679 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2680
2681 RawEvent event;
2682 event.deviceId = EVENTHUB_ID;
2683 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2684
2685 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2686 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2687}
2688
Arthur Hung2c9a3342019-07-23 14:18:59 +08002689// A single input device is associated with a specific display. Check that:
2690// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh3e56f7e2023-07-07 13:12:37 +00002691// 2. Device is disabled when setEnabled API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002692TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002693 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2694 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002695
2696 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002697 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002698 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2699 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002700
2701 // Device should be enabled by default.
2702 ASSERT_TRUE(mDevice->isEnabled());
2703
2704 // Prepare associated info.
2705 constexpr uint8_t hdmi = 1;
2706 const std::string UNIQUE_ID = "local:1";
2707
2708 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002709 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002710 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002711 // Device should be disabled because it is associated with a specific display via
2712 // input port <-> display port association, but the corresponding display is not found
2713 ASSERT_FALSE(mDevice->isEnabled());
2714
2715 // Prepare displays.
2716 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002717 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002718 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002719 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002720 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002721 ASSERT_TRUE(mDevice->isEnabled());
2722
2723 // Device should be disabled after set disable.
2724 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002725 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002726 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002727 ASSERT_FALSE(mDevice->isEnabled());
2728
2729 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002730 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002731 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002732 ASSERT_FALSE(mDevice->isEnabled());
2733}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002734
Christine Franks1ba71cc2021-04-07 14:37:42 -07002735TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2736 // Device should be enabled by default.
2737 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002738 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2739 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002740 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002741 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2742 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002743 ASSERT_TRUE(mDevice->isEnabled());
2744
2745 // Device should be disabled because it is associated with a specific display, but the
2746 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002747 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002748 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002749 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002750 ASSERT_FALSE(mDevice->isEnabled());
2751
2752 // Device should be enabled when a display is found.
2753 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002754 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002755 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002756 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002757 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002758 ASSERT_TRUE(mDevice->isEnabled());
2759
2760 // Device should be disabled after set disable.
2761 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002762 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002763 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002764 ASSERT_FALSE(mDevice->isEnabled());
2765
2766 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002767 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002768 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002769 ASSERT_FALSE(mDevice->isEnabled());
2770}
2771
Christine Franks2a2293c2022-01-18 11:51:16 -08002772TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2773 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002774 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2775 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002776 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002777 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2778 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002779
Christine Franks2a2293c2022-01-18 11:51:16 -08002780 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2781 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002782 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002783 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002784 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002785 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002786 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2787}
2788
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002789/**
2790 * This test reproduces a crash caused by a dangling reference that remains after device is added
2791 * and removed. The reference is accessed in InputDevice::dump(..);
2792 */
2793TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2794 constexpr int32_t TEST_EVENTHUB_ID = 10;
2795 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2796
Harry Cutts33476232023-01-30 19:57:29 +00002797 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh7f1765e2023-07-07 13:12:37 +00002798 device.addEventHubDevice(TEST_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002799 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2800 std::string dumpStr, eventHubDevStr;
2801 device.dump(dumpStr, eventHubDevStr);
2802}
2803
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002804TEST_F(InputDeviceTest, GetBluetoothAddress) {
2805 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2806 ASSERT_TRUE(address);
2807 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2808}
2809
Michael Wrightd02c5b62014-02-10 15:10:22 -08002810// --- SwitchInputMapperTest ---
2811
2812class SwitchInputMapperTest : public InputMapperTest {
2813protected:
2814};
2815
2816TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002817 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002818
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002819 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002820}
2821
2822TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002823 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002825 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002826 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002827
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002828 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002829 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002830}
2831
2832TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002833 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002834 std::list<NotifyArgs> out;
2835 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2836 ASSERT_TRUE(out.empty());
2837 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2838 ASSERT_TRUE(out.empty());
2839 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2840 ASSERT_TRUE(out.empty());
2841 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002842
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002843 ASSERT_EQ(1u, out.size());
2844 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002845 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002846 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2847 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002848 args.switchMask);
2849 ASSERT_EQ(uint32_t(0), args.policyFlags);
2850}
2851
Chris Ye87143712020-11-10 05:05:58 +00002852// --- VibratorInputMapperTest ---
2853class VibratorInputMapperTest : public InputMapperTest {
2854protected:
2855 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2856};
2857
2858TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002859 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002860
2861 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2862}
2863
2864TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002865 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002866
2867 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2868}
2869
2870TEST_F(VibratorInputMapperTest, Vibrate) {
2871 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002872 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00002873 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002874
2875 VibrationElement pattern(2);
2876 VibrationSequence sequence(2);
2877 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002878 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2879 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002880 sequence.addElement(pattern);
2881 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002882 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2883 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002884 sequence.addElement(pattern);
2885
2886 std::vector<int64_t> timings = {0, 1};
2887 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2888
2889 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002890 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002891 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002892 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002893 // Verify vibrator state listener was notified.
2894 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002895 ASSERT_EQ(1u, out.size());
2896 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2897 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2898 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002899 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002900 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002901 ASSERT_FALSE(mapper.isVibrating());
2902 // Verify vibrator state listener was notified.
2903 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002904 ASSERT_EQ(1u, out.size());
2905 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2906 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2907 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002908}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002909
Chris Yef59a2f42020-10-16 12:55:26 -07002910// --- SensorInputMapperTest ---
2911
2912class SensorInputMapperTest : public InputMapperTest {
2913protected:
2914 static const int32_t ACCEL_RAW_MIN;
2915 static const int32_t ACCEL_RAW_MAX;
2916 static const int32_t ACCEL_RAW_FUZZ;
2917 static const int32_t ACCEL_RAW_FLAT;
2918 static const int32_t ACCEL_RAW_RESOLUTION;
2919
2920 static const int32_t GYRO_RAW_MIN;
2921 static const int32_t GYRO_RAW_MAX;
2922 static const int32_t GYRO_RAW_FUZZ;
2923 static const int32_t GYRO_RAW_FLAT;
2924 static const int32_t GYRO_RAW_RESOLUTION;
2925
2926 static const float GRAVITY_MS2_UNIT;
2927 static const float DEGREE_RADIAN_UNIT;
2928
2929 void prepareAccelAxes();
2930 void prepareGyroAxes();
2931 void setAccelProperties();
2932 void setGyroProperties();
2933 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2934};
2935
2936const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2937const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2938const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2939const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2940const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2941
2942const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2943const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2944const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2945const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2946const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2947
2948const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2949const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2950
2951void SensorInputMapperTest::prepareAccelAxes() {
2952 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2953 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2954 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2955 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2956 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2957 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2958}
2959
2960void SensorInputMapperTest::prepareGyroAxes() {
2961 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2962 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2963 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2964 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2965 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2966 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2967}
2968
2969void SensorInputMapperTest::setAccelProperties() {
2970 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2971 /* sensorDataIndex */ 0);
2972 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2973 /* sensorDataIndex */ 1);
2974 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2975 /* sensorDataIndex */ 2);
2976 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2977 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2978 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2979 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2980 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2981}
2982
2983void SensorInputMapperTest::setGyroProperties() {
2984 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2985 /* sensorDataIndex */ 0);
2986 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2987 /* sensorDataIndex */ 1);
2988 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2989 /* sensorDataIndex */ 2);
2990 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2991 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2992 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2993 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2994 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2995}
2996
2997TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00002998 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002999
3000 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3001}
3002
3003TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3004 setAccelProperties();
3005 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003006 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003007
3008 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3009 std::chrono::microseconds(10000),
3010 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003011 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3013 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3015 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003017
3018 NotifySensorArgs args;
3019 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3020 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3021 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3022
3023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3024 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3025 ASSERT_EQ(args.deviceId, DEVICE_ID);
3026 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3027 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3028 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3029 ASSERT_EQ(args.values, values);
3030 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3031}
3032
3033TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3034 setGyroProperties();
3035 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003036 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003037
3038 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3039 std::chrono::microseconds(10000),
3040 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003041 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003042 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3046 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003047
3048 NotifySensorArgs args;
3049 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3050 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3051 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3052
3053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3054 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3055 ASSERT_EQ(args.deviceId, DEVICE_ID);
3056 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3057 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3058 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3059 ASSERT_EQ(args.values, values);
3060 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3061}
3062
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063// --- KeyboardInputMapperTest ---
3064
3065class KeyboardInputMapperTest : public InputMapperTest {
3066protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003067 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003068 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003069 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003070
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003071 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003072 int32_t originalKeyCode, int32_t rotatedKeyCode,
3073 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074};
3075
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003076/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3077 * orientation.
3078 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003079void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003080 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3081 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003082}
3083
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003084void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003085 int32_t originalScanCode, int32_t originalKeyCode,
3086 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003087 NotifyKeyArgs args;
3088
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003089 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3091 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3092 ASSERT_EQ(originalScanCode, args.scanCode);
3093 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003094 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003095
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3098 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3099 ASSERT_EQ(originalScanCode, args.scanCode);
3100 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003101 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102}
3103
Michael Wrightd02c5b62014-02-10 15:10:22 -08003104TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003105 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003106 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003107 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003108
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003109 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110}
3111
3112TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3113 const int32_t USAGE_A = 0x070004;
3114 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003115 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3116 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003117 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3118 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3119 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003120
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003121 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003122 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003123 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003124 // Initial metastate is AMETA_NONE.
3125 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003126
3127 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003128 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003129 NotifyKeyArgs args;
3130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3131 ASSERT_EQ(DEVICE_ID, args.deviceId);
3132 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3133 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3134 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3135 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3136 ASSERT_EQ(KEY_HOME, args.scanCode);
3137 ASSERT_EQ(AMETA_NONE, args.metaState);
3138 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3139 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3140 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3141
3142 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003143 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3145 ASSERT_EQ(DEVICE_ID, args.deviceId);
3146 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3147 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3148 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3149 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3150 ASSERT_EQ(KEY_HOME, args.scanCode);
3151 ASSERT_EQ(AMETA_NONE, args.metaState);
3152 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3153 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3154 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3155
3156 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3160 ASSERT_EQ(DEVICE_ID, args.deviceId);
3161 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3162 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3163 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3164 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3165 ASSERT_EQ(0, args.scanCode);
3166 ASSERT_EQ(AMETA_NONE, args.metaState);
3167 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3168 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3169 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3170
3171 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3173 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3175 ASSERT_EQ(DEVICE_ID, args.deviceId);
3176 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3177 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3178 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3179 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3180 ASSERT_EQ(0, args.scanCode);
3181 ASSERT_EQ(AMETA_NONE, args.metaState);
3182 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3183 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3184 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3185
3186 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3190 ASSERT_EQ(DEVICE_ID, args.deviceId);
3191 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3192 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3193 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3194 ASSERT_EQ(0, args.keyCode);
3195 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3196 ASSERT_EQ(AMETA_NONE, args.metaState);
3197 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3198 ASSERT_EQ(0U, args.policyFlags);
3199 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3200
3201 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3203 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3205 ASSERT_EQ(DEVICE_ID, args.deviceId);
3206 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3207 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3208 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3209 ASSERT_EQ(0, args.keyCode);
3210 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3211 ASSERT_EQ(AMETA_NONE, args.metaState);
3212 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3213 ASSERT_EQ(0U, args.policyFlags);
3214 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3215}
3216
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003217TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3218 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3219 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3220 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3221
3222 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003223 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003224 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3225
3226 // Key down by scan code.
3227 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3228 NotifyKeyArgs args;
3229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3230 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3231
3232 // Key up by scan code.
3233 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3235 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3236}
3237
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003238/**
3239 * Ensure that the readTime is set to the time when the EV_KEY is received.
3240 */
3241TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3242 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3243
3244 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003245 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003246 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3247 NotifyKeyArgs args;
3248
3249 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003250 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3252 ASSERT_EQ(12, args.readTime);
3253
3254 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003255 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3257 ASSERT_EQ(15, args.readTime);
3258}
3259
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003261 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3262 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003263 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3264 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3265 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003267 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003268 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003269 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003270
Arthur Hung95f68612022-04-07 14:08:22 +08003271 // Initial metastate is AMETA_NONE.
3272 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273
3274 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276 NotifyKeyArgs args;
3277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3278 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003279 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003280 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003281
3282 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003283 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3285 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003286 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003287
3288 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003289 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3291 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003292 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293
3294 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003295 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3297 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003298 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003299 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300}
3301
3302TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003303 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3304 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3305 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3306 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003307
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003308 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003309 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003310 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003311
Michael Wrighta9cf4192022-12-01 23:46:39 +00003312 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003313 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3314 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3315 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3316 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3317 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3318 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3319 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3320 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3321}
3322
3323TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003324 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3325 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3326 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3327 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328
Michael Wrightd02c5b62014-02-10 15:10:22 -08003329 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003330 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003331 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003332 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333
Michael Wrighta9cf4192022-12-01 23:46:39 +00003334 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003335 ASSERT_NO_FATAL_FAILURE(
3336 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3337 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3338 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3339 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3340 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3341 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3342 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003343
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003344 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003345 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003346 ASSERT_NO_FATAL_FAILURE(
3347 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3348 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3349 AKEYCODE_DPAD_UP, DISPLAY_ID));
3350 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3351 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3352 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3353 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003354
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003355 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003356 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003357 ASSERT_NO_FATAL_FAILURE(
3358 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3359 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3360 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3361 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3362 AKEYCODE_DPAD_UP, DISPLAY_ID));
3363 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3364 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003365
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003366 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003367 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003368 ASSERT_NO_FATAL_FAILURE(
3369 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3370 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3371 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3372 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3373 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3374 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3375 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003376
3377 // Special case: if orientation changes while key is down, we still emit the same keycode
3378 // in the key up as we did in the key down.
3379 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003380 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003381 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003382 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3384 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3385 ASSERT_EQ(KEY_UP, args.scanCode);
3386 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3387
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003388 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003389 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3392 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3393 ASSERT_EQ(KEY_UP, args.scanCode);
3394 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3395}
3396
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003397TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3398 // If the keyboard is not orientation aware,
3399 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003400 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003401
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003402 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003403 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003404 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003405 NotifyKeyArgs args;
3406
3407 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3412 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3413
Michael Wrighta9cf4192022-12-01 23:46:39 +00003414 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003415 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003417 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3419 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3420}
3421
3422TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3423 // If the keyboard is orientation aware,
3424 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003425 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003426
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003427 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003428 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003429 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003430 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003431 NotifyKeyArgs args;
3432
3433 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3434 // ^--- already checked by the previous test
3435
Michael Wrighta9cf4192022-12-01 23:46:39 +00003436 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003437 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003438 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3442 ASSERT_EQ(DISPLAY_ID, args.displayId);
3443
3444 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003445 clearViewports();
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 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3452 ASSERT_EQ(newDisplayId, args.displayId);
3453}
3454
Michael Wrightd02c5b62014-02-10 15:10:22 -08003455TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003456 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003457 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003458 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003459
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003460 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003461 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003463 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003464 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003465}
3466
Philip Junker4af3b3d2021-12-14 10:36:55 +01003467TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3468 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003469 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003470 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3471
3472 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3473 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3474 << "If a mapping is available, the result is equal to the mapping";
3475
3476 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3477 << "If no mapping is available, the result is the key location";
3478}
3479
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003481 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003482 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003483 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003484
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003485 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003486 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003487
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003488 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003489 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003490}
3491
3492TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003493 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003494 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003495 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003496
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003497 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498
Michael Wrightd02c5b62014-02-10 15:10:22 -08003499 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003500 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003501 ASSERT_TRUE(flags[0]);
3502 ASSERT_FALSE(flags[1]);
3503}
3504
3505TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003506 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3507 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3508 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3509 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3510 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3511 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003512
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003513 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003514 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003515 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003516 // Initial metastate is AMETA_NONE.
3517 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003518
3519 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003520 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3521 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3522 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003523
3524 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003525 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003527 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3528 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3529 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003530 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531
3532 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003533 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3534 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003535 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3536 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3537 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003538 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003539
3540 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003543 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3544 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3545 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003546 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003547
3548 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3550 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003551 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3552 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3553 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003554 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003555
3556 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3558 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003559 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3560 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3561 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003562 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003563
3564 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003565 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003567 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3568 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3569 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003570 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003571}
3572
Chris Yea52ade12020-08-27 16:49:20 -07003573TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3574 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3575 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3576 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3577 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3578
3579 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003580 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003581 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3582
Chris Yea52ade12020-08-27 16:49:20 -07003583 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003584 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003585 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3586 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3587 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3588 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3589
3590 NotifyKeyArgs args;
3591 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3594 ASSERT_EQ(AMETA_NONE, args.metaState);
3595 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3596 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3597 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3598
3599 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003600 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3602 ASSERT_EQ(AMETA_NONE, args.metaState);
3603 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3604 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3605 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3606}
3607
Arthur Hung2c9a3342019-07-23 14:18:59 +08003608TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3609 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003610 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3611 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3612 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3613 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003614
3615 // keyboard 2.
3616 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003617 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003618 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003619 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003620 std::shared_ptr<InputDevice> device2 =
3621 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003622 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003623
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003624 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3625 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3626 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3627 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003628
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003629 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003630 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003631 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003632
Arpit Singh67ca6842023-04-26 14:43:16 +00003633 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003634 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003635 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3636 mFakePolicy
3637 ->getReaderConfiguration(),
3638 AINPUT_SOURCE_KEYBOARD,
3639 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003640 std::list<NotifyArgs> unused =
3641 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003642 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003643 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003644
3645 // Prepared displays and associated info.
3646 constexpr uint8_t hdmi1 = 0;
3647 constexpr uint8_t hdmi2 = 1;
3648 const std::string SECONDARY_UNIQUE_ID = "local:1";
3649
3650 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3651 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3652
3653 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003654 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003655 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003656 ASSERT_FALSE(device2->isEnabled());
3657
3658 // Prepare second display.
3659 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003660 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003661 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003662 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003663 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003664 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003665 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003666 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003667
3668 // Device should be enabled after the associated display is found.
3669 ASSERT_TRUE(mDevice->isEnabled());
3670 ASSERT_TRUE(device2->isEnabled());
3671
3672 // Test pad key events
3673 ASSERT_NO_FATAL_FAILURE(
3674 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3675 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3676 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3677 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3678 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3679 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3680 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3681
3682 ASSERT_NO_FATAL_FAILURE(
3683 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3684 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3685 AKEYCODE_DPAD_RIGHT, newDisplayId));
3686 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3687 AKEYCODE_DPAD_DOWN, newDisplayId));
3688 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3689 AKEYCODE_DPAD_LEFT, newDisplayId));
3690}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003691
arthurhungc903df12020-08-11 15:08:42 +08003692TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3693 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3694 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3695 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3696 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3697 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3698 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3699
3700 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003701 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003702 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003703 // Initial metastate is AMETA_NONE.
3704 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003705
3706 // Initialization should have turned all of the lights off.
3707 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3708 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3709 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3710
3711 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003714 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3715 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3716
3717 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003718 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3719 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003720 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3721 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3722
3723 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003724 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3725 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003726 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3727 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3728
3729 mFakeEventHub->removeDevice(EVENTHUB_ID);
3730 mReader->loopOnce();
3731
3732 // keyboard 2 should default toggle keys.
3733 const std::string USB2 = "USB2";
3734 const std::string DEVICE_NAME2 = "KEYBOARD2";
3735 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3736 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3737 std::shared_ptr<InputDevice> device2 =
3738 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003739 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003740 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3741 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3742 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3743 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3744 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3745 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3746
Arpit Singh67ca6842023-04-26 14:43:16 +00003747 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003748 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003749 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3750 mFakePolicy
3751 ->getReaderConfiguration(),
3752 AINPUT_SOURCE_KEYBOARD,
3753 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003754 std::list<NotifyArgs> unused =
3755 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003756 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003757 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003758
3759 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3760 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3761 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003762 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3763 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003764}
3765
Arthur Hungcb40a002021-08-03 14:31:01 +00003766TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3767 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3768 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3769 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3770
3771 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh67ca6842023-04-26 14:43:16 +00003772 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003773 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3774 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003775 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003776 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003777 // Initial metastate is AMETA_NONE.
3778 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003779
3780 mReader->toggleCapsLockState(DEVICE_ID);
3781 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3782}
3783
Arthur Hungfb3cc112022-04-13 07:39:50 +00003784TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3785 // keyboard 1.
3786 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3787 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3788 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3789 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3790 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3791 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3792
3793 KeyboardInputMapper& mapper1 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003794 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003795 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3796
3797 // keyboard 2.
3798 const std::string USB2 = "USB2";
3799 const std::string DEVICE_NAME2 = "KEYBOARD2";
3800 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3801 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3802 std::shared_ptr<InputDevice> device2 =
3803 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3804 ftl::Flags<InputDeviceClass>(0));
3805 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3806 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3807 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3808 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3809 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3810 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3811
Arpit Singh67ca6842023-04-26 14:43:16 +00003812 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003813 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003814 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3815 mFakePolicy
3816 ->getReaderConfiguration(),
3817 AINPUT_SOURCE_KEYBOARD,
3818 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003819 std::list<NotifyArgs> unused =
3820 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003821 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003822 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003823
Arthur Hung95f68612022-04-07 14:08:22 +08003824 // Initial metastate is AMETA_NONE.
3825 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3826 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3827
3828 // Toggle num lock on and off.
3829 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3830 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003831 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3832 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3833 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3834
3835 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3836 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3837 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3838 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3839 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3840
3841 // Toggle caps lock on and off.
3842 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3843 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3844 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3845 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3846 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3847
3848 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3849 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3850 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3851 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3852 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3853
3854 // Toggle scroll lock on and off.
3855 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3856 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3857 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3858 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3859 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3860
3861 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3862 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3863 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3864 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3865 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3866}
3867
Arthur Hung2141d542022-08-23 07:45:21 +00003868TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3869 const int32_t USAGE_A = 0x070004;
3870 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3871 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3872
3873 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003874 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00003875 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3876 // Key down by scan code.
3877 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3878 NotifyKeyArgs args;
3879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3880 ASSERT_EQ(DEVICE_ID, args.deviceId);
3881 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3882 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3883 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3884 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3885 ASSERT_EQ(KEY_HOME, args.scanCode);
3886 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3887
3888 // Disable device, it should synthesize cancellation events for down events.
3889 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003890 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00003891
3892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3893 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3894 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3895 ASSERT_EQ(KEY_HOME, args.scanCode);
3896 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3897}
3898
Zixuan Qufecb6062022-11-12 04:44:31 +00003899TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh67ca6842023-04-26 14:43:16 +00003900 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3901 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00003902 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003903 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3904 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00003905
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003906 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00003907 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3908
3909 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003910 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00003911
3912 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3913 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3914 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3915 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3916 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003917 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
3918
3919 // Call change layout association with the same values: Generation shouldn't change
3920 generation = mReader->getContext()->getGeneration();
3921 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3922 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3923 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
3924 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00003925}
3926
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003927TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3928 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3929 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3930
3931 // Configuration
Arpit Singh67ca6842023-04-26 14:43:16 +00003932 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003933 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3934 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003935 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003936
3937 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3938 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3939}
3940
Justin Chung71ddb432023-03-27 04:29:07 +00003941TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
3942 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
3943 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003944 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Justin Chung71ddb432023-03-27 04:29:07 +00003945 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3946 NotifyKeyArgs args;
3947
3948 // Key down
3949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
3950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3951 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
3952}
3953
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003954// --- KeyboardInputMapperTest_ExternalDevice ---
3955
3956class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3957protected:
Chris Yea52ade12020-08-27 16:49:20 -07003958 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003959};
3960
3961TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003962 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3963 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003964
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003965 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3966 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3967 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3968 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003969
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003970 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003971 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003972 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003973
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003974 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003975 NotifyKeyArgs args;
3976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3977 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3978
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003979 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3981 ASSERT_EQ(uint32_t(0), args.policyFlags);
3982
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003983 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003985 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003986
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003987 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3989 ASSERT_EQ(uint32_t(0), args.policyFlags);
3990
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3993 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3994
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003995 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3997 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3998}
3999
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004000TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004001 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004002
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004003 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4004 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4005 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004006
Powei Fengd041c5d2019-05-03 17:11:33 -07004007 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004008 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004009 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004010 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004011
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004013 NotifyKeyArgs args;
4014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4015 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4016
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004017 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4019 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4020
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4023 ASSERT_EQ(uint32_t(0), args.policyFlags);
4024
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004025 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4027 ASSERT_EQ(uint32_t(0), args.policyFlags);
4028
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4031 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4032
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004033 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4035 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4036}
4037
Michael Wrightd02c5b62014-02-10 15:10:22 -08004038// --- CursorInputMapperTest ---
4039
4040class CursorInputMapperTest : public InputMapperTest {
4041protected:
4042 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4043
Michael Wright17db18e2020-06-26 20:51:44 +01004044 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004045
Chris Yea52ade12020-08-27 16:49:20 -07004046 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004047 InputMapperTest::SetUp();
4048
Michael Wright17db18e2020-06-26 20:51:44 +01004049 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004050 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004051 }
4052
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004053 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4054 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004055
Michael Wrighta9cf4192022-12-01 23:46:39 +00004056 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004057 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4058 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4059 }
4060
4061 void prepareSecondaryDisplay() {
4062 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004063 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004064 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004065 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004066
4067 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4068 float pressure) {
4069 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4070 0.0f, 0.0f, 0.0f, EPSILON));
4071 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072};
4073
4074const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4075
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004076void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4077 int32_t originalY, int32_t rotatedX,
4078 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004079 NotifyMotionArgs args;
4080
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004081 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4085 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004086 ASSERT_NO_FATAL_FAILURE(
4087 assertCursorPointerCoords(args.pointerCoords[0],
4088 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4089 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004090}
4091
4092TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004093 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004094 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004095
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004096 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004097}
4098
4099TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004100 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004101 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004102
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004103 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004104}
4105
4106TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004108 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109
4110 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004111 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112
4113 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004114 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4115 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004116 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4117 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4118
4119 // When the bounds are set, then there should be a valid motion range.
4120 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4121
4122 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004123 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004124
4125 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4126 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4127 1, 800 - 1, 0.0f, 0.0f));
4128 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4129 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4130 2, 480 - 1, 0.0f, 0.0f));
4131 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4132 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4133 0.0f, 1.0f, 0.0f, 0.0f));
4134}
4135
4136TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004137 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004138 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004139
4140 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004141 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142
4143 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4144 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4145 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4146 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4147 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4148 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4149 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4150 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4151 0.0f, 1.0f, 0.0f, 0.0f));
4152}
4153
4154TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004156 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157
arthurhungdcef2dc2020-08-11 14:47:50 +08004158 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004159
4160 NotifyMotionArgs args;
4161
4162 // Button press.
4163 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4167 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4168 ASSERT_EQ(DEVICE_ID, args.deviceId);
4169 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4170 ASSERT_EQ(uint32_t(0), args.policyFlags);
4171 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4172 ASSERT_EQ(0, args.flags);
4173 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4174 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4175 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004176 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004178 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004179 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4181 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4182 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4183
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4185 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4186 ASSERT_EQ(DEVICE_ID, args.deviceId);
4187 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4188 ASSERT_EQ(uint32_t(0), args.policyFlags);
4189 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4190 ASSERT_EQ(0, args.flags);
4191 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4192 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4193 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004194 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004195 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004196 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004197 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004198 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4199 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4200 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4201
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004203 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4204 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4206 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4207 ASSERT_EQ(DEVICE_ID, args.deviceId);
4208 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4209 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004210 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4211 ASSERT_EQ(0, args.flags);
4212 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4213 ASSERT_EQ(0, args.buttonState);
4214 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004215 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004216 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004217 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004218 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004219 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4220 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4221 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4222
4223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4224 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4225 ASSERT_EQ(DEVICE_ID, args.deviceId);
4226 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4227 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4229 ASSERT_EQ(0, args.flags);
4230 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4231 ASSERT_EQ(0, args.buttonState);
4232 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004233 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004234 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004235 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004236 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4238 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4239 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4240}
4241
4242TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004244 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245
4246 NotifyMotionArgs args;
4247
4248 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004249 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4250 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004253 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4254 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4255 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004256
4257 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004258 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004262 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4263 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264}
4265
4266TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004268 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269
4270 NotifyMotionArgs args;
4271
4272 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4276 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004277 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4280 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004281 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004282
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4285 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004287 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004288 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004289
4290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004291 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004292 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004293}
4294
4295TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004297 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004298
4299 NotifyMotionArgs args;
4300
4301 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4304 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4307 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004308 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4309 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4310 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4313 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004314 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4315 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4316 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004317
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004319 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4320 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4321 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4323 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004324 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4325 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4326 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004327
4328 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004329 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4330 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004332 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004333 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004334
4335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004337 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338}
4339
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004340TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004341 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004342 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004343 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4344 // need to be rotated.
4345 addConfigurationProperty("cursor.orientationAware", "1");
Arpit Singhe036ad72023-04-27 12:48:15 +00004346 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004347
Michael Wrighta9cf4192022-12-01 23:46:39 +00004348 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4350 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4351 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4352 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4353 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4354 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4355 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4356 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4357}
4358
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004359TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004360 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004361 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004362 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4363 // orientation-aware are affected by display rotation.
Arpit Singhe036ad72023-04-27 12:48:15 +00004364 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004366 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004367 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004368 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4369 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4370 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4371 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4372 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4373 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4374 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4375 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4376
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004377 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004378 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004379 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4380 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4381 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4382 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4383 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4384 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4385 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4386 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004387
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004388 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004389 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4391 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4392 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4393 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4394 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4395 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4396 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4397 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4398
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004399 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004400 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004401 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4402 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4403 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4404 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4405 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4406 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4407 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4408 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409}
4410
4411TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004412 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004413 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414
4415 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4416 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004417
4418 NotifyMotionArgs motionArgs;
4419 NotifyKeyArgs keyArgs;
4420
4421 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004422 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4423 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4425 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4426 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004427 ASSERT_NO_FATAL_FAILURE(
4428 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4431 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4432 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004433 ASSERT_NO_FATAL_FAILURE(
4434 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004435
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004436 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4437 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004439 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004441 ASSERT_NO_FATAL_FAILURE(
4442 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004443
4444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004445 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004447 ASSERT_NO_FATAL_FAILURE(
4448 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004449
4450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004451 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004452 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004453 ASSERT_NO_FATAL_FAILURE(
4454 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004455
4456 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4461 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4462 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004463 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004464 ASSERT_NO_FATAL_FAILURE(
4465 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4468 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4469 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004470 ASSERT_NO_FATAL_FAILURE(
4471 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004472
4473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4474 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4475 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004476 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004477 ASSERT_NO_FATAL_FAILURE(
4478 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004479
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004483 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004484 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004485 ASSERT_NO_FATAL_FAILURE(
4486 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004487
4488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004490 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004491 ASSERT_NO_FATAL_FAILURE(
4492 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004494 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4498 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004499 ASSERT_NO_FATAL_FAILURE(
4500 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004503
4504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004505 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004507 ASSERT_NO_FATAL_FAILURE(
4508 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004509
Michael Wrightd02c5b62014-02-10 15:10:22 -08004510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4511 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004513 ASSERT_NO_FATAL_FAILURE(
4514 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515
4516 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4520 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4521 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004522
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004524 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004525 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004526 ASSERT_NO_FATAL_FAILURE(
4527 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004528
4529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4530 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4531 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004532 ASSERT_NO_FATAL_FAILURE(
4533 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004538 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004539 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004540 ASSERT_NO_FATAL_FAILURE(
4541 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004542
4543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004545 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004546
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004547 ASSERT_NO_FATAL_FAILURE(
4548 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4550 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4551 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4552
4553 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004554 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4555 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4557 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4558 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004559
Michael Wrightd02c5b62014-02-10 15:10:22 -08004560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004561 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004562 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004563 ASSERT_NO_FATAL_FAILURE(
4564 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004565
4566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4567 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4568 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004569 ASSERT_NO_FATAL_FAILURE(
4570 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004571
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004572 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4573 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004575 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004576 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004577 ASSERT_NO_FATAL_FAILURE(
4578 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004579
4580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4581 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4582 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004583 ASSERT_NO_FATAL_FAILURE(
4584 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004585
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4587 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4588 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4589
4590 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004591 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4594 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4595 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004596
Michael Wrightd02c5b62014-02-10 15:10:22 -08004597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004598 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004599 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004600 ASSERT_NO_FATAL_FAILURE(
4601 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004602
4603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4604 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4605 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004606 ASSERT_NO_FATAL_FAILURE(
4607 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004608
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004609 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4610 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004612 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004614 ASSERT_NO_FATAL_FAILURE(
4615 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004616
4617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4618 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4619 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004620 ASSERT_NO_FATAL_FAILURE(
4621 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004622
Michael Wrightd02c5b62014-02-10 15:10:22 -08004623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4624 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4625 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4626
4627 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004628 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4629 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4631 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4632 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004633
Michael Wrightd02c5b62014-02-10 15:10:22 -08004634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004635 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004636 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004637 ASSERT_NO_FATAL_FAILURE(
4638 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004639
4640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4641 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4642 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004643 ASSERT_NO_FATAL_FAILURE(
4644 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004645
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004646 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004649 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004650 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004651 ASSERT_NO_FATAL_FAILURE(
4652 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004653
4654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4655 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4656 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004657 ASSERT_NO_FATAL_FAILURE(
4658 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004659
Michael Wrightd02c5b62014-02-10 15:10:22 -08004660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4661 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4662 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4663}
4664
4665TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004667 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668
4669 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4670 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004671
4672 NotifyMotionArgs args;
4673
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004674 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4675 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4676 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004678 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4679 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4681 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 +00004682 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004683}
4684
4685TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004686 addConfigurationProperty("cursor.mode", "pointer");
4687 mFakePolicy->setPointerCapture(true);
Arpit Singhe036ad72023-04-27 12:48:15 +00004688 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004689
4690 NotifyDeviceResetArgs resetArgs;
4691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4692 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4693 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4694
4695 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4696 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004697
4698 NotifyMotionArgs args;
4699
4700 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004701 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4702 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4703 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4705 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4706 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4707 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4708 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 +00004709 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004710
4711 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4715 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4716 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4718 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4720 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4721 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4723 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4724
4725 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004726 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4727 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4729 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4730 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4732 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4734 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4735 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4737 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4738
4739 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4741 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4742 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4744 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4745 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4747 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 +00004748 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004749
4750 // Disable pointer capture and check that the device generation got bumped
4751 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004752 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004753 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004754 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004755 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004756
4757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004758 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4759
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004760 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4761 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4762 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4764 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004765 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4767 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 +00004768 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769}
4770
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004771/**
4772 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4773 * pointer acceleration or speed processing should not be applied.
4774 */
4775TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4776 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004777 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4778 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004779 mFakePolicy->setVelocityControlParams(testParams);
Arpit Singhe036ad72023-04-27 12:48:15 +00004780 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004781
4782 NotifyDeviceResetArgs resetArgs;
4783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4784 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4785 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4786
4787 NotifyMotionArgs args;
4788
4789 // Move and verify scale is applied.
4790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4794 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4795 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4796 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4797 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4798 ASSERT_GT(relX, 10);
4799 ASSERT_GT(relY, 20);
4800
4801 // Enable Pointer Capture
4802 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004803 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004804 NotifyPointerCaptureChangedArgs captureArgs;
4805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4806 ASSERT_TRUE(captureArgs.request.enable);
4807
4808 // Move and verify scale is not applied.
4809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4810 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4811 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4813 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4814 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4815 ASSERT_EQ(10, args.pointerCoords[0].getX());
4816 ASSERT_EQ(20, args.pointerCoords[0].getY());
4817}
4818
Prabir Pradhan208360b2022-06-24 18:37:04 +00004819TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4820 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004821 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan208360b2022-06-24 18:37:04 +00004822
4823 NotifyDeviceResetArgs resetArgs;
4824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4825 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4826 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4827
4828 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004829 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004830
4831 NotifyMotionArgs args;
4832
4833 // Verify that the coordinates are rotated.
4834 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4835 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4836 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4838 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4839 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4840 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4841 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4842
4843 // Enable Pointer Capture.
4844 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004845 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004846 NotifyPointerCaptureChangedArgs captureArgs;
4847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4848 ASSERT_TRUE(captureArgs.request.enable);
4849
4850 // Move and verify rotation is not applied.
4851 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4852 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4853 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4855 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4856 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4857 ASSERT_EQ(10, args.pointerCoords[0].getX());
4858 ASSERT_EQ(20, args.pointerCoords[0].getY());
4859}
4860
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004861TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004862 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004863
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004864 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004865 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004866
4867 // Set up the secondary display as the display on which the pointer should be shown.
4868 // The InputDevice is not associated with any display.
4869 prepareSecondaryDisplay();
4870 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004871 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004872
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004873 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004874 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004875
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004876 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004877 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4878 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4879 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004881 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4882 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4883 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004884 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004885}
4886
4887TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004888 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004889
4890 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004891 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004892
4893 // Set up the secondary display as the display on which the pointer should be shown,
4894 // and associate the InputDevice with the secondary display.
4895 prepareSecondaryDisplay();
4896 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4897 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004898 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004899
4900 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4901 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004902
4903 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4904 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4905 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004907 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4908 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4909 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004910 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004911}
4912
4913TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004914 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004915
4916 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004917 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004918 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4919
4920 // Associate the InputDevice with the secondary display.
4921 prepareSecondaryDisplay();
4922 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004923 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004924
4925 // The mapper should not generate any events because it is associated with a display that is
4926 // different from the pointer display.
4927 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4928 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4929 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004931}
4932
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004933// --- BluetoothCursorInputMapperTest ---
4934
4935class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4936protected:
4937 void SetUp() override {
4938 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4939
4940 mFakePointerController = std::make_shared<FakePointerController>();
4941 mFakePolicy->setPointerController(mFakePointerController);
4942 }
4943};
4944
4945TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4946 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004947 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004948
4949 nsecs_t kernelEventTime = ARBITRARY_TIME;
4950 nsecs_t expectedEventTime = ARBITRARY_TIME;
4951 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4952 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4954 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4955 WithEventTime(expectedEventTime))));
4956
4957 // Process several events that come in quick succession, according to their timestamps.
4958 for (int i = 0; i < 3; i++) {
4959 constexpr static nsecs_t delta = ms2ns(1);
4960 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4961 kernelEventTime += delta;
4962 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4963
4964 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4965 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4967 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4968 WithEventTime(expectedEventTime))));
4969 }
4970}
4971
4972TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4973 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004974 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004975
4976 nsecs_t expectedEventTime = ARBITRARY_TIME;
4977 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4978 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4980 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4981 WithEventTime(expectedEventTime))));
4982
4983 // Process several events with the same timestamp from the kernel.
4984 // Ensure that we do not generate events too far into the future.
4985 constexpr static int32_t numEvents =
4986 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4987 for (int i = 0; i < numEvents; i++) {
4988 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4989
4990 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4993 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4994 WithEventTime(expectedEventTime))));
4995 }
4996
4997 // By processing more events with the same timestamp, we should not generate events with a
4998 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4999 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
5000 for (int i = 0; i < 3; i++) {
5001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5002 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5004 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5005 WithEventTime(cappedEventTime))));
5006 }
5007}
5008
5009TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
5010 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00005011 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005012
5013 nsecs_t kernelEventTime = ARBITRARY_TIME;
5014 nsecs_t expectedEventTime = ARBITRARY_TIME;
5015 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5016 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5018 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5019 WithEventTime(expectedEventTime))));
5020
5021 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
5022 // smoothening is not needed, its timestamp is not affected.
5023 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
5024 expectedEventTime = kernelEventTime;
5025
5026 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5027 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5029 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5030 WithEventTime(expectedEventTime))));
5031}
5032
Michael Wrightd02c5b62014-02-10 15:10:22 -08005033// --- TouchInputMapperTest ---
5034
5035class TouchInputMapperTest : public InputMapperTest {
5036protected:
5037 static const int32_t RAW_X_MIN;
5038 static const int32_t RAW_X_MAX;
5039 static const int32_t RAW_Y_MIN;
5040 static const int32_t RAW_Y_MAX;
5041 static const int32_t RAW_TOUCH_MIN;
5042 static const int32_t RAW_TOUCH_MAX;
5043 static const int32_t RAW_TOOL_MIN;
5044 static const int32_t RAW_TOOL_MAX;
5045 static const int32_t RAW_PRESSURE_MIN;
5046 static const int32_t RAW_PRESSURE_MAX;
5047 static const int32_t RAW_ORIENTATION_MIN;
5048 static const int32_t RAW_ORIENTATION_MAX;
5049 static const int32_t RAW_DISTANCE_MIN;
5050 static const int32_t RAW_DISTANCE_MAX;
5051 static const int32_t RAW_TILT_MIN;
5052 static const int32_t RAW_TILT_MAX;
5053 static const int32_t RAW_ID_MIN;
5054 static const int32_t RAW_ID_MAX;
5055 static const int32_t RAW_SLOT_MIN;
5056 static const int32_t RAW_SLOT_MAX;
5057 static const float X_PRECISION;
5058 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005059 static const float X_PRECISION_VIRTUAL;
5060 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061
5062 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005063 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005064
5065 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5066
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005067 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005068 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005069
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070 enum Axes {
5071 POSITION = 1 << 0,
5072 TOUCH = 1 << 1,
5073 TOOL = 1 << 2,
5074 PRESSURE = 1 << 3,
5075 ORIENTATION = 1 << 4,
5076 MINOR = 1 << 5,
5077 ID = 1 << 6,
5078 DISTANCE = 1 << 7,
5079 TILT = 1 << 8,
5080 SLOT = 1 << 9,
5081 TOOL_TYPE = 1 << 10,
5082 };
5083
Michael Wrighta9cf4192022-12-01 23:46:39 +00005084 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005085 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00005086 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005088 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005089 int32_t toRawX(float displayX);
5090 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005091 int32_t toRotatedRawX(float displayX);
5092 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005093 float toCookedX(float rawX, float rawY);
5094 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005095 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005096 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005097 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005098 float toDisplayY(int32_t rawY, int32_t displayHeight);
5099
Michael Wrightd02c5b62014-02-10 15:10:22 -08005100};
5101
5102const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5103const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5104const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5105const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5106const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5107const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5108const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5109const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005110const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5111const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005112const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5113const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5114const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5115const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5116const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5117const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5118const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5119const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5120const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5121const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5122const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5123const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005124const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5125 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5126const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5127 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005128const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5129 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130
5131const float TouchInputMapperTest::GEOMETRIC_SCALE =
5132 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5133 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5134
5135const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5136 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5137 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5138};
5139
Michael Wrighta9cf4192022-12-01 23:46:39 +00005140void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005141 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5142 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005143}
5144
5145void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5146 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00005147 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005148}
5149
Michael Wrighta9cf4192022-12-01 23:46:39 +00005150void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005151 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5152 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5153 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005154}
5155
Michael Wrightd02c5b62014-02-10 15:10:22 -08005156void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005157 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5158 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5159 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5160 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005161}
5162
Jason Gerecke489fda82012-09-07 17:19:40 -07005163void TouchInputMapperTest::prepareLocationCalibration() {
5164 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5165}
5166
Michael Wrightd02c5b62014-02-10 15:10:22 -08005167int32_t TouchInputMapperTest::toRawX(float displayX) {
5168 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5169}
5170
5171int32_t TouchInputMapperTest::toRawY(float displayY) {
5172 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5173}
5174
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005175int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5176 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5177}
5178
5179int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5180 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5181}
5182
Jason Gerecke489fda82012-09-07 17:19:40 -07005183float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5184 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5185 return rawX;
5186}
5187
5188float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5189 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5190 return rawY;
5191}
5192
Michael Wrightd02c5b62014-02-10 15:10:22 -08005193float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005194 return toDisplayX(rawX, DISPLAY_WIDTH);
5195}
5196
5197float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5198 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005199}
5200
5201float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005202 return toDisplayY(rawY, DISPLAY_HEIGHT);
5203}
5204
5205float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5206 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005207}
5208
5209
5210// --- SingleTouchInputMapperTest ---
5211
5212class SingleTouchInputMapperTest : public TouchInputMapperTest {
5213protected:
5214 void prepareButtons();
5215 void prepareAxes(int axes);
5216
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005217 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5218 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5219 void processUp(SingleTouchInputMapper& mappery);
5220 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5221 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5222 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5223 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5224 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5225 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005226};
5227
5228void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005229 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005230}
5231
5232void SingleTouchInputMapperTest::prepareAxes(int axes) {
5233 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005234 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5235 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005236 }
5237 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005238 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5239 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005240 }
5241 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005242 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5243 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005244 }
5245 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005246 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5247 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005248 }
5249 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005250 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5251 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005252 }
5253}
5254
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005255void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005256 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5257 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5258 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005259}
5260
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005261void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005262 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005264}
5265
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005266void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005268}
5269
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005270void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005272}
5273
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005274void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5275 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005277}
5278
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005279void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005281}
5282
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005283void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5284 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005285 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5286 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005287}
5288
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005289void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5290 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005291 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005292}
5293
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005294void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005296}
5297
Michael Wrightd02c5b62014-02-10 15:10:22 -08005298TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005299 prepareButtons();
5300 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005301 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005302
Josep del Río2d8c79a2023-01-23 19:33:50 +00005303 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005304}
5305
Michael Wrightd02c5b62014-02-10 15:10:22 -08005306TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005307 prepareButtons();
5308 prepareAxes(POSITION);
5309 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00005310 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005311
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005312 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005313}
5314
5315TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
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();
Arpit Singha8c236b2023-04-25 13:56:05 +00005321 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322
5323 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005324 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005325
5326 // Virtual key is down.
5327 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5328 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5329 processDown(mapper, x, y);
5330 processSync(mapper);
5331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5332
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005333 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005334
5335 // Virtual key is up.
5336 processUp(mapper);
5337 processSync(mapper);
5338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5339
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005340 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005341}
5342
5343TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005344 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005345 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005346 prepareButtons();
5347 prepareAxes(POSITION);
5348 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005349 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005350
5351 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005352 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005353
5354 // Virtual key is down.
5355 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5356 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5357 processDown(mapper, x, y);
5358 processSync(mapper);
5359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5360
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005361 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005362
5363 // Virtual key is up.
5364 processUp(mapper);
5365 processSync(mapper);
5366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5367
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005368 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369}
5370
5371TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005372 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005373 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374 prepareButtons();
5375 prepareAxes(POSITION);
5376 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005377 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005378
Michael Wrightd02c5b62014-02-10 15:10:22 -08005379 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005380 ASSERT_TRUE(
5381 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382 ASSERT_TRUE(flags[0]);
5383 ASSERT_FALSE(flags[1]);
5384}
5385
5386TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005387 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005388 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005389 prepareButtons();
5390 prepareAxes(POSITION);
5391 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005392 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005393
arthurhungdcef2dc2020-08-11 14:47:50 +08005394 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005395
5396 NotifyKeyArgs args;
5397
5398 // Press virtual key.
5399 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5400 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5401 processDown(mapper, x, y);
5402 processSync(mapper);
5403
5404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5405 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5406 ASSERT_EQ(DEVICE_ID, args.deviceId);
5407 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5408 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5409 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5410 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5411 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5412 ASSERT_EQ(KEY_HOME, args.scanCode);
5413 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5414 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5415
5416 // Release virtual key.
5417 processUp(mapper);
5418 processSync(mapper);
5419
5420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5421 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5422 ASSERT_EQ(DEVICE_ID, args.deviceId);
5423 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5424 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5425 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5426 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5427 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5428 ASSERT_EQ(KEY_HOME, args.scanCode);
5429 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5430 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5431
5432 // Should not have sent any motions.
5433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5434}
5435
5436TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005437 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005438 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005439 prepareButtons();
5440 prepareAxes(POSITION);
5441 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005442 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005443
arthurhungdcef2dc2020-08-11 14:47:50 +08005444 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005445
5446 NotifyKeyArgs keyArgs;
5447
5448 // Press virtual key.
5449 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5450 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5451 processDown(mapper, x, y);
5452 processSync(mapper);
5453
5454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5455 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5456 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5457 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5458 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5459 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5460 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5461 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5462 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5463 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5464 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5465
5466 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5467 // into the display area.
5468 y -= 100;
5469 processMove(mapper, x, y);
5470 processSync(mapper);
5471
5472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5473 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5474 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5475 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5476 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5477 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5478 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5479 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5480 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5481 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5482 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5483 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5484
5485 NotifyMotionArgs motionArgs;
5486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5487 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5488 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5489 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5490 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5491 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5492 ASSERT_EQ(0, motionArgs.flags);
5493 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5494 ASSERT_EQ(0, motionArgs.buttonState);
5495 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005496 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005497 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005498 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5500 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5501 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5502 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5503 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5504
5505 // Keep moving out of bounds. Should generate a pointer move.
5506 y -= 50;
5507 processMove(mapper, x, y);
5508 processSync(mapper);
5509
5510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5511 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5512 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5513 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5514 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5515 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5516 ASSERT_EQ(0, motionArgs.flags);
5517 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5518 ASSERT_EQ(0, motionArgs.buttonState);
5519 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005520 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005521 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005522 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5524 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5525 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5526 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5527 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5528
5529 // Release out of bounds. Should generate a pointer up.
5530 processUp(mapper);
5531 processSync(mapper);
5532
5533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5534 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5535 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5536 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5537 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5538 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5539 ASSERT_EQ(0, motionArgs.flags);
5540 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5541 ASSERT_EQ(0, motionArgs.buttonState);
5542 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005543 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005544 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005545 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5547 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5548 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5549 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5550 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5551
5552 // Should not have sent any more keys or motions.
5553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5555}
5556
5557TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005558 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005559 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005560 prepareButtons();
5561 prepareAxes(POSITION);
5562 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005563 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005564
arthurhungdcef2dc2020-08-11 14:47:50 +08005565 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005566
5567 NotifyMotionArgs motionArgs;
5568
5569 // Initially go down out of bounds.
5570 int32_t x = -10;
5571 int32_t y = -10;
5572 processDown(mapper, x, y);
5573 processSync(mapper);
5574
5575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5576
5577 // Move into the display area. Should generate a pointer down.
5578 x = 50;
5579 y = 75;
5580 processMove(mapper, x, y);
5581 processSync(mapper);
5582
5583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5584 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5585 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5586 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5587 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5588 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5589 ASSERT_EQ(0, motionArgs.flags);
5590 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5591 ASSERT_EQ(0, motionArgs.buttonState);
5592 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005593 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005594 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005595 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5597 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5598 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5599 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5600 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5601
5602 // Release. Should generate a pointer up.
5603 processUp(mapper);
5604 processSync(mapper);
5605
5606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5607 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5608 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5609 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5610 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5611 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5612 ASSERT_EQ(0, motionArgs.flags);
5613 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5614 ASSERT_EQ(0, motionArgs.buttonState);
5615 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005616 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005617 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005618 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5620 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5621 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5622 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5623 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5624
5625 // Should not have sent any more keys or motions.
5626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5628}
5629
Santos Cordonfa5cf462017-04-05 10:37:00 -07005630TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005631 addConfigurationProperty("touch.deviceType", "touchScreen");
5632 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5633
Michael Wrighta9cf4192022-12-01 23:46:39 +00005634 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005635 prepareButtons();
5636 prepareAxes(POSITION);
5637 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005638 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005639
arthurhungdcef2dc2020-08-11 14:47:50 +08005640 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005641
5642 NotifyMotionArgs motionArgs;
5643
5644 // Down.
5645 int32_t x = 100;
5646 int32_t y = 125;
5647 processDown(mapper, x, y);
5648 processSync(mapper);
5649
5650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5651 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5652 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5653 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5654 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5655 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5656 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5657 ASSERT_EQ(0, motionArgs.flags);
5658 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5659 ASSERT_EQ(0, motionArgs.buttonState);
5660 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005661 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005662 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005663 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5665 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5666 1, 0, 0, 0, 0, 0, 0, 0));
5667 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5668 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5669 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5670
5671 // Move.
5672 x += 50;
5673 y += 75;
5674 processMove(mapper, x, y);
5675 processSync(mapper);
5676
5677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5678 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5679 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5680 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5681 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5682 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5683 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5684 ASSERT_EQ(0, motionArgs.flags);
5685 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5686 ASSERT_EQ(0, motionArgs.buttonState);
5687 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005688 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005689 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005690 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5692 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5693 1, 0, 0, 0, 0, 0, 0, 0));
5694 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5695 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5696 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5697
5698 // Up.
5699 processUp(mapper);
5700 processSync(mapper);
5701
5702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5703 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5704 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5705 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5706 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5707 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5708 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5709 ASSERT_EQ(0, motionArgs.flags);
5710 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5711 ASSERT_EQ(0, motionArgs.buttonState);
5712 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005713 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005714 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005715 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005716 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5717 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5718 1, 0, 0, 0, 0, 0, 0, 0));
5719 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5720 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5721 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5722
5723 // Should not have sent any more keys or motions.
5724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5726}
5727
Michael Wrightd02c5b62014-02-10 15:10:22 -08005728TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005729 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005730 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005731 prepareButtons();
5732 prepareAxes(POSITION);
5733 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005734 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005735
arthurhungdcef2dc2020-08-11 14:47:50 +08005736 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005737
5738 NotifyMotionArgs motionArgs;
5739
5740 // Down.
5741 int32_t x = 100;
5742 int32_t y = 125;
5743 processDown(mapper, x, y);
5744 processSync(mapper);
5745
5746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5747 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5748 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5749 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5750 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5751 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5752 ASSERT_EQ(0, motionArgs.flags);
5753 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5754 ASSERT_EQ(0, motionArgs.buttonState);
5755 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005756 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005757 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005758 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5760 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5761 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5762 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5763 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5764
5765 // Move.
5766 x += 50;
5767 y += 75;
5768 processMove(mapper, x, y);
5769 processSync(mapper);
5770
5771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5772 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5773 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5774 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5775 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5776 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5777 ASSERT_EQ(0, motionArgs.flags);
5778 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5779 ASSERT_EQ(0, motionArgs.buttonState);
5780 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005781 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005782 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005783 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5785 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5786 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5787 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5788 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5789
5790 // Up.
5791 processUp(mapper);
5792 processSync(mapper);
5793
5794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5795 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5796 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5797 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5798 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5799 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5800 ASSERT_EQ(0, motionArgs.flags);
5801 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5802 ASSERT_EQ(0, motionArgs.buttonState);
5803 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005804 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005805 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005806 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5808 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5809 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5810 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5811 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5812
5813 // Should not have sent any more keys or motions.
5814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5816}
5817
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005818TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005819 addConfigurationProperty("touch.deviceType", "touchScreen");
5820 prepareButtons();
5821 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005822 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5823 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005824 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005825
5826 NotifyMotionArgs args;
5827
5828 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005829 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005830 processDown(mapper, toRawX(50), toRawY(75));
5831 processSync(mapper);
5832
5833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5834 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5835 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5836
5837 processUp(mapper);
5838 processSync(mapper);
5839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5840}
5841
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005842TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005843 addConfigurationProperty("touch.deviceType", "touchScreen");
5844 prepareButtons();
5845 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005846 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5847 // orientation-aware are affected by display rotation.
5848 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005849 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005850
5851 NotifyMotionArgs args;
5852
5853 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005854 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005855 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005856 processDown(mapper, toRawX(50), toRawY(75));
5857 processSync(mapper);
5858
5859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5860 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5861 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5862
5863 processUp(mapper);
5864 processSync(mapper);
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5866
5867 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005868 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005869 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005870 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005871 processSync(mapper);
5872
5873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5874 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5875 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5876
5877 processUp(mapper);
5878 processSync(mapper);
5879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5880
5881 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005882 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005883 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5885 processSync(mapper);
5886
5887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5888 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5889 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5890
5891 processUp(mapper);
5892 processSync(mapper);
5893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5894
5895 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005896 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005897 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005898 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005899 processSync(mapper);
5900
5901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5902 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5903 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5904
5905 processUp(mapper);
5906 processSync(mapper);
5907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5908}
5909
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005910TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5911 addConfigurationProperty("touch.deviceType", "touchScreen");
5912 prepareButtons();
5913 prepareAxes(POSITION);
5914 addConfigurationProperty("touch.orientationAware", "1");
5915 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5916 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005917 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005918 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005919 NotifyMotionArgs args;
5920
5921 // Orientation 0.
5922 processDown(mapper, toRawX(50), toRawY(75));
5923 processSync(mapper);
5924
5925 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5926 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5927 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5928
5929 processUp(mapper);
5930 processSync(mapper);
5931 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5932}
5933
5934TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5935 addConfigurationProperty("touch.deviceType", "touchScreen");
5936 prepareButtons();
5937 prepareAxes(POSITION);
5938 addConfigurationProperty("touch.orientationAware", "1");
5939 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5940 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005941 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005942 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005943 NotifyMotionArgs args;
5944
5945 // Orientation 90.
5946 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5947 processSync(mapper);
5948
5949 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5950 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5951 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5952
5953 processUp(mapper);
5954 processSync(mapper);
5955 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5956}
5957
5958TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5959 addConfigurationProperty("touch.deviceType", "touchScreen");
5960 prepareButtons();
5961 prepareAxes(POSITION);
5962 addConfigurationProperty("touch.orientationAware", "1");
5963 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5964 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005965 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005966 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005967 NotifyMotionArgs args;
5968
5969 // Orientation 180.
5970 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5971 processSync(mapper);
5972
5973 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5974 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5975 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5976
5977 processUp(mapper);
5978 processSync(mapper);
5979 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5980}
5981
5982TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5983 addConfigurationProperty("touch.deviceType", "touchScreen");
5984 prepareButtons();
5985 prepareAxes(POSITION);
5986 addConfigurationProperty("touch.orientationAware", "1");
5987 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5988 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005989 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005990 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005991 NotifyMotionArgs args;
5992
5993 // Orientation 270.
5994 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5995 processSync(mapper);
5996
5997 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5998 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5999 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6000
6001 processUp(mapper);
6002 processSync(mapper);
6003 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6004}
6005
6006TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6007 addConfigurationProperty("touch.deviceType", "touchScreen");
6008 prepareButtons();
6009 prepareAxes(POSITION);
6010 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6011 // orientation-aware are affected by display rotation.
6012 addConfigurationProperty("touch.orientationAware", "0");
6013 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00006014 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006015
6016 NotifyMotionArgs args;
6017
6018 // Orientation 90, Rotation 0.
6019 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006020 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006021 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6022 processSync(mapper);
6023
6024 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6025 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6026 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6027
6028 processUp(mapper);
6029 processSync(mapper);
6030 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6031
6032 // Orientation 90, Rotation 90.
6033 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006034 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006035 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006036 processSync(mapper);
6037
6038 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6039 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6040 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6041
6042 processUp(mapper);
6043 processSync(mapper);
6044 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6045
6046 // Orientation 90, Rotation 180.
6047 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006048 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006049 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6050 processSync(mapper);
6051
6052 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6053 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6054 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6055
6056 processUp(mapper);
6057 processSync(mapper);
6058 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6059
6060 // Orientation 90, Rotation 270.
6061 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006062 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006063 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 -07006064 processSync(mapper);
6065
6066 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6067 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6068 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6069
6070 processUp(mapper);
6071 processSync(mapper);
6072 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6073}
6074
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006075TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
6076 addConfigurationProperty("touch.deviceType", "touchScreen");
6077 prepareButtons();
6078 prepareAxes(POSITION);
6079 addConfigurationProperty("touch.orientationAware", "1");
6080 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006081 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006082
6083 // Set a physical frame in the display viewport.
6084 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6085 viewport->physicalLeft = 20;
6086 viewport->physicalTop = 600;
6087 viewport->physicalRight = 30;
6088 viewport->physicalBottom = 610;
6089 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006090 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006091
6092 // Start the touch.
6093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6094 processSync(mapper);
6095
6096 // Expect all input starting outside the physical frame to be ignored.
6097 const std::array<Point, 6> outsidePoints = {
6098 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6099 for (const auto& p : outsidePoints) {
6100 processMove(mapper, toRawX(p.x), toRawY(p.y));
6101 processSync(mapper);
6102 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6103 }
6104
6105 // Move the touch into the physical frame.
6106 processMove(mapper, toRawX(25), toRawY(605));
6107 processSync(mapper);
6108 NotifyMotionArgs args;
6109 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6110 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6111 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6112 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6113
6114 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
6115 for (const auto& p : outsidePoints) {
6116 processMove(mapper, toRawX(p.x), toRawY(p.y));
6117 processSync(mapper);
6118 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6119 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
6120 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6121 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6122 }
6123
6124 processUp(mapper);
6125 processSync(mapper);
6126 EXPECT_NO_FATAL_FAILURE(
6127 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
6128}
6129
Harry Cutts1db43992023-06-19 17:05:07 +00006130TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
6131 std::shared_ptr<FakePointerController> fakePointerController =
6132 std::make_shared<FakePointerController>();
6133 mFakePolicy->setPointerController(fakePointerController);
6134
6135 addConfigurationProperty("touch.deviceType", "pointer");
6136 prepareAxes(POSITION);
6137 prepareDisplay(ui::ROTATION_0);
6138 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
6139
6140 // Set a physical frame in the display viewport.
6141 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6142 viewport->physicalLeft = 20;
6143 viewport->physicalTop = 600;
6144 viewport->physicalRight = 30;
6145 viewport->physicalBottom = 610;
6146 mFakePolicy->updateViewport(*viewport);
6147 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
6148
6149 // Start the touch.
6150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6151 processSync(mapper);
6152
6153 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
6154 // produced.
6155 const std::array<Point, 6> outsidePoints = {
6156 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6157 for (const auto& p : outsidePoints) {
6158 processMove(mapper, toRawX(p.x), toRawY(p.y));
6159 processSync(mapper);
6160 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6161 }
6162}
6163
Michael Wrightd02c5b62014-02-10 15:10:22 -08006164TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006166 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006167 prepareButtons();
6168 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00006169 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006170
6171 // These calculations are based on the input device calibration documentation.
6172 int32_t rawX = 100;
6173 int32_t rawY = 200;
6174 int32_t rawPressure = 10;
6175 int32_t rawToolMajor = 12;
6176 int32_t rawDistance = 2;
6177 int32_t rawTiltX = 30;
6178 int32_t rawTiltY = 110;
6179
6180 float x = toDisplayX(rawX);
6181 float y = toDisplayY(rawY);
6182 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6183 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6184 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6185 float distance = float(rawDistance);
6186
6187 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6188 float tiltScale = M_PI / 180;
6189 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6190 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6191 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6192 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6193
6194 processDown(mapper, rawX, rawY);
6195 processPressure(mapper, rawPressure);
6196 processToolMajor(mapper, rawToolMajor);
6197 processDistance(mapper, rawDistance);
6198 processTilt(mapper, rawTiltX, rawTiltY);
6199 processSync(mapper);
6200
6201 NotifyMotionArgs args;
6202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6203 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6204 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6205 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6206}
6207
Jason Gerecke489fda82012-09-07 17:19:40 -07006208TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006209 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006210 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07006211 prepareLocationCalibration();
6212 prepareButtons();
6213 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006214 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006215
6216 int32_t rawX = 100;
6217 int32_t rawY = 200;
6218
6219 float x = toDisplayX(toCookedX(rawX, rawY));
6220 float y = toDisplayY(toCookedY(rawX, rawY));
6221
6222 processDown(mapper, rawX, rawY);
6223 processSync(mapper);
6224
6225 NotifyMotionArgs args;
6226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6228 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6229}
6230
Michael Wrightd02c5b62014-02-10 15:10:22 -08006231TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006232 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006233 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006234 prepareButtons();
6235 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006236 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006237
6238 NotifyMotionArgs motionArgs;
6239 NotifyKeyArgs keyArgs;
6240
6241 processDown(mapper, 100, 200);
6242 processSync(mapper);
6243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6244 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6245 ASSERT_EQ(0, motionArgs.buttonState);
6246
6247 // press BTN_LEFT, release BTN_LEFT
6248 processKey(mapper, BTN_LEFT, 1);
6249 processSync(mapper);
6250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6252 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6253
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6255 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6256 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6257
Michael Wrightd02c5b62014-02-10 15:10:22 -08006258 processKey(mapper, BTN_LEFT, 0);
6259 processSync(mapper);
6260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006261 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006262 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006263
6264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006265 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006266 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267
6268 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6269 processKey(mapper, BTN_RIGHT, 1);
6270 processKey(mapper, BTN_MIDDLE, 1);
6271 processSync(mapper);
6272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6274 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6275 motionArgs.buttonState);
6276
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6278 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6279 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6280
6281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6282 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6283 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6284 motionArgs.buttonState);
6285
Michael Wrightd02c5b62014-02-10 15:10:22 -08006286 processKey(mapper, BTN_RIGHT, 0);
6287 processSync(mapper);
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006289 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006291
6292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006294 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006295
6296 processKey(mapper, BTN_MIDDLE, 0);
6297 processSync(mapper);
6298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006299 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006300 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006301
6302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006304 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006305
6306 // press BTN_BACK, release BTN_BACK
6307 processKey(mapper, BTN_BACK, 1);
6308 processSync(mapper);
6309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6310 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6311 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006312
Michael Wrightd02c5b62014-02-10 15:10:22 -08006313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006315 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6316
6317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6318 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6319 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006320
6321 processKey(mapper, BTN_BACK, 0);
6322 processSync(mapper);
6323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006324 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006325 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006326
6327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006329 ASSERT_EQ(0, motionArgs.buttonState);
6330
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6332 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6333 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6334
6335 // press BTN_SIDE, release BTN_SIDE
6336 processKey(mapper, BTN_SIDE, 1);
6337 processSync(mapper);
6338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6339 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6340 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006341
Michael Wrightd02c5b62014-02-10 15:10:22 -08006342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006343 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006344 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6345
6346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6347 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6348 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006349
6350 processKey(mapper, BTN_SIDE, 0);
6351 processSync(mapper);
6352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006353 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006354 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006355
6356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006358 ASSERT_EQ(0, motionArgs.buttonState);
6359
Michael Wrightd02c5b62014-02-10 15:10:22 -08006360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6361 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6362 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6363
6364 // press BTN_FORWARD, release BTN_FORWARD
6365 processKey(mapper, BTN_FORWARD, 1);
6366 processSync(mapper);
6367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6368 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6369 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006370
Michael Wrightd02c5b62014-02-10 15:10:22 -08006371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006373 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6374
6375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6376 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6377 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006378
6379 processKey(mapper, BTN_FORWARD, 0);
6380 processSync(mapper);
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006382 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006383 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006384
6385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006387 ASSERT_EQ(0, motionArgs.buttonState);
6388
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6390 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6391 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6392
6393 // press BTN_EXTRA, release BTN_EXTRA
6394 processKey(mapper, BTN_EXTRA, 1);
6395 processSync(mapper);
6396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6397 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6398 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006399
Michael Wrightd02c5b62014-02-10 15:10:22 -08006400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006402 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6403
6404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6405 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6406 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006407
6408 processKey(mapper, BTN_EXTRA, 0);
6409 processSync(mapper);
6410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006411 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006412 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006413
6414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006415 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006416 ASSERT_EQ(0, motionArgs.buttonState);
6417
Michael Wrightd02c5b62014-02-10 15:10:22 -08006418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6419 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6420 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6421
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6423
Michael Wrightd02c5b62014-02-10 15:10:22 -08006424 // press BTN_STYLUS, release BTN_STYLUS
6425 processKey(mapper, BTN_STYLUS, 1);
6426 processSync(mapper);
6427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006429 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6430
6431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6432 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6433 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006434
6435 processKey(mapper, BTN_STYLUS, 0);
6436 processSync(mapper);
6437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006438 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006439 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006440
6441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006443 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006444
6445 // press BTN_STYLUS2, release BTN_STYLUS2
6446 processKey(mapper, BTN_STYLUS2, 1);
6447 processSync(mapper);
6448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6449 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006450 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6451
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6453 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6454 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006455
6456 processKey(mapper, BTN_STYLUS2, 0);
6457 processSync(mapper);
6458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006459 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006461
6462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006464 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006465
6466 // release touch
6467 processUp(mapper);
6468 processSync(mapper);
6469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6470 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6471 ASSERT_EQ(0, motionArgs.buttonState);
6472}
6473
6474TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006475 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006476 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006477 prepareButtons();
6478 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006479 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006480
6481 NotifyMotionArgs motionArgs;
6482
6483 // default tool type is finger
6484 processDown(mapper, 100, 200);
6485 processSync(mapper);
6486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6487 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006488 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006489
6490 // eraser
6491 processKey(mapper, BTN_TOOL_RUBBER, 1);
6492 processSync(mapper);
6493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6494 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006495 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006496
6497 // stylus
6498 processKey(mapper, BTN_TOOL_RUBBER, 0);
6499 processKey(mapper, BTN_TOOL_PEN, 1);
6500 processSync(mapper);
6501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006503 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006504
6505 // brush
6506 processKey(mapper, BTN_TOOL_PEN, 0);
6507 processKey(mapper, BTN_TOOL_BRUSH, 1);
6508 processSync(mapper);
6509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6510 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006511 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006512
6513 // pencil
6514 processKey(mapper, BTN_TOOL_BRUSH, 0);
6515 processKey(mapper, BTN_TOOL_PENCIL, 1);
6516 processSync(mapper);
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6518 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006519 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006520
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006521 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006522 processKey(mapper, BTN_TOOL_PENCIL, 0);
6523 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6524 processSync(mapper);
6525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6526 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006527 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006528
6529 // mouse
6530 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6531 processKey(mapper, BTN_TOOL_MOUSE, 1);
6532 processSync(mapper);
6533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006535 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006536
6537 // lens
6538 processKey(mapper, BTN_TOOL_MOUSE, 0);
6539 processKey(mapper, BTN_TOOL_LENS, 1);
6540 processSync(mapper);
6541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006543 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006544
6545 // double-tap
6546 processKey(mapper, BTN_TOOL_LENS, 0);
6547 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6548 processSync(mapper);
6549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6550 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006551 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006552
6553 // triple-tap
6554 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6555 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6556 processSync(mapper);
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006559 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006560
6561 // quad-tap
6562 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6563 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6564 processSync(mapper);
6565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6566 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006567 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006568
6569 // finger
6570 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6571 processKey(mapper, BTN_TOOL_FINGER, 1);
6572 processSync(mapper);
6573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006575 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006576
6577 // stylus trumps finger
6578 processKey(mapper, BTN_TOOL_PEN, 1);
6579 processSync(mapper);
6580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6581 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006582 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006583
6584 // eraser trumps stylus
6585 processKey(mapper, BTN_TOOL_RUBBER, 1);
6586 processSync(mapper);
6587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006589 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006590
6591 // mouse trumps eraser
6592 processKey(mapper, BTN_TOOL_MOUSE, 1);
6593 processSync(mapper);
6594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6595 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006596 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006597
6598 // back to default tool type
6599 processKey(mapper, BTN_TOOL_MOUSE, 0);
6600 processKey(mapper, BTN_TOOL_RUBBER, 0);
6601 processKey(mapper, BTN_TOOL_PEN, 0);
6602 processKey(mapper, BTN_TOOL_FINGER, 0);
6603 processSync(mapper);
6604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6605 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006606 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006607}
6608
6609TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006610 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006611 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006612 prepareButtons();
6613 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006614 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006615 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006616
6617 NotifyMotionArgs motionArgs;
6618
6619 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6620 processKey(mapper, BTN_TOOL_FINGER, 1);
6621 processMove(mapper, 100, 200);
6622 processSync(mapper);
6623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6624 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6626 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6627
6628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6629 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6631 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6632
6633 // move a little
6634 processMove(mapper, 150, 250);
6635 processSync(mapper);
6636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6637 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6639 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6640
6641 // down when BTN_TOUCH is pressed, pressure defaults to 1
6642 processKey(mapper, BTN_TOUCH, 1);
6643 processSync(mapper);
6644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6645 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6646 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6647 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6648
6649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6650 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6652 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6653
6654 // up when BTN_TOUCH is released, hover restored
6655 processKey(mapper, BTN_TOUCH, 0);
6656 processSync(mapper);
6657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6658 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6660 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6661
6662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6663 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6665 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6666
6667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6668 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6670 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6671
6672 // exit hover when pointer goes away
6673 processKey(mapper, BTN_TOOL_FINGER, 0);
6674 processSync(mapper);
6675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6676 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6678 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6679}
6680
6681TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006682 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006683 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006684 prepareButtons();
6685 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006686 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006687
6688 NotifyMotionArgs motionArgs;
6689
6690 // initially hovering because pressure is 0
6691 processDown(mapper, 100, 200);
6692 processPressure(mapper, 0);
6693 processSync(mapper);
6694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6695 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6697 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6698
6699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6700 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6702 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6703
6704 // move a little
6705 processMove(mapper, 150, 250);
6706 processSync(mapper);
6707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6708 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6710 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6711
6712 // down when pressure is non-zero
6713 processPressure(mapper, RAW_PRESSURE_MAX);
6714 processSync(mapper);
6715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6716 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6718 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6719
6720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6721 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6723 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6724
6725 // up when pressure becomes 0, hover restored
6726 processPressure(mapper, 0);
6727 processSync(mapper);
6728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6729 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6730 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6731 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6732
6733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6734 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6736 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6737
6738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6739 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6741 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6742
6743 // exit hover when pointer goes away
6744 processUp(mapper);
6745 processSync(mapper);
6746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6747 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6749 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6750}
6751
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006752TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6753 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006754 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006755 prepareButtons();
6756 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006757 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006758
6759 // Touch down.
6760 processDown(mapper, 100, 200);
6761 processPressure(mapper, 1);
6762 processSync(mapper);
6763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6764 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6765
6766 // Reset the mapper. This should cancel the ongoing gesture.
6767 resetMapper(mapper, ARBITRARY_TIME);
6768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6769 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6770
6771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6772}
6773
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006774TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6775 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006776 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006777 prepareButtons();
6778 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006779 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006780
6781 // Set the initial state for the touch pointer.
6782 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6783 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6784 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6785 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6786
6787 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006788 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6789 // does not generate any events.
6790 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006791
6792 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6793 // the recreated touch state to generate a down event.
6794 processSync(mapper);
6795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6796 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6797
6798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6799}
6800
lilinnan687e58f2022-07-19 16:00:50 +08006801TEST_F(SingleTouchInputMapperTest,
6802 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6803 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006804 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006805 prepareButtons();
6806 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006807 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006808 NotifyMotionArgs motionArgs;
6809
6810 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006811 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006812 processSync(mapper);
6813
6814 // We should receive a down event
6815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6816 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6817
6818 // Change display id
6819 clearViewports();
6820 prepareSecondaryDisplay(ViewportType::INTERNAL);
6821
6822 // We should receive a cancel event
6823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6824 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6825 // Then receive reset called
6826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6827}
6828
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006829TEST_F(SingleTouchInputMapperTest,
6830 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6831 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006832 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006833 prepareButtons();
6834 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006835 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6837 NotifyMotionArgs motionArgs;
6838
6839 // Start a new gesture.
6840 processDown(mapper, 100, 200);
6841 processSync(mapper);
6842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6843 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6844
6845 // Make the viewport inactive. This will put the device in disabled mode.
6846 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6847 viewport->isActive = false;
6848 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006849 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006850
6851 // We should receive a cancel event for the ongoing gesture.
6852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6853 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6854 // Then we should be notified that the device was reset.
6855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6856
6857 // No events are generated while the viewport is inactive.
6858 processMove(mapper, 101, 201);
6859 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006860 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006861 processSync(mapper);
6862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6863
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006864 // Start a new gesture while the viewport is still inactive.
6865 processDown(mapper, 300, 400);
6866 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6867 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6868 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6869 processSync(mapper);
6870
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006871 // Make the viewport active again. The device should resume processing events.
6872 viewport->isActive = true;
6873 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006874 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006875
6876 // The device is reset because it changes back to direct mode, without generating any events.
6877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6879
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006880 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006881 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6883 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006884
6885 // No more events.
6886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6888}
6889
Prabir Pradhan211ba622022-10-31 21:09:21 +00006890TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6891 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006892 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006893 prepareButtons();
6894 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006895 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6897
6898 // Press a stylus button.
6899 processKey(mapper, BTN_STYLUS, 1);
6900 processSync(mapper);
6901
6902 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6903 processDown(mapper, 100, 200);
6904 processSync(mapper);
6905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6906 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6907 WithCoords(toDisplayX(100), toDisplayY(200)),
6908 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6910 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6911 WithCoords(toDisplayX(100), toDisplayY(200)),
6912 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6913
6914 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6915 // the button has not actually been released, since there will be no pointers through which the
6916 // button state can be reported. The event is generated at the location of the pointer before
6917 // it went up.
6918 processUp(mapper);
6919 processSync(mapper);
6920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6921 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6922 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6924 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6925 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6926}
6927
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006928TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6929 addConfigurationProperty("touch.deviceType", "touchScreen");
6930 prepareDisplay(ui::ROTATION_0);
6931 prepareButtons();
6932 prepareAxes(POSITION);
6933
6934 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6935
Arpit Singha8c236b2023-04-25 13:56:05 +00006936 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6938
6939 // Press a stylus button.
6940 processKey(mapper, BTN_STYLUS, 1);
6941 processSync(mapper);
6942
6943 // Start a touch gesture and ensure that the stylus button is not reported.
6944 processDown(mapper, 100, 200);
6945 processSync(mapper);
6946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6947 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6948
6949 // Release and press the stylus button again.
6950 processKey(mapper, BTN_STYLUS, 0);
6951 processSync(mapper);
6952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6953 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6954 processKey(mapper, BTN_STYLUS, 1);
6955 processSync(mapper);
6956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6957 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6958
6959 // Release the touch gesture.
6960 processUp(mapper);
6961 processSync(mapper);
6962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6963 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6964
6965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6966}
6967
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006968TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6969 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6970 prepareDisplay(ui::ROTATION_0);
6971 prepareButtons();
6972 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006973 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6975
6976 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6977}
6978
Seunghwan Choi356026c2023-02-01 14:37:25 +09006979TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6980 std::shared_ptr<FakePointerController> fakePointerController =
6981 std::make_shared<FakePointerController>();
6982 addConfigurationProperty("touch.deviceType", "touchScreen");
6983 prepareDisplay(ui::ROTATION_0);
6984 prepareButtons();
6985 prepareAxes(POSITION);
6986 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6987 mFakePolicy->setPointerController(fakePointerController);
6988 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00006989 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006990
6991 processKey(mapper, BTN_TOOL_PEN, 1);
6992 processMove(mapper, 100, 200);
6993 processSync(mapper);
6994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6995 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006996 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006997 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6998 ASSERT_TRUE(fakePointerController->isPointerShown());
6999 ASSERT_NO_FATAL_FAILURE(
7000 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
7001}
7002
7003TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
7004 std::shared_ptr<FakePointerController> fakePointerController =
7005 std::make_shared<FakePointerController>();
7006 addConfigurationProperty("touch.deviceType", "touchScreen");
7007 prepareDisplay(ui::ROTATION_0);
7008 prepareButtons();
7009 prepareAxes(POSITION);
7010 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7011 mFakePolicy->setPointerController(fakePointerController);
7012 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00007013 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09007014
7015 processKey(mapper, BTN_TOOL_PEN, 1);
7016 processMove(mapper, 100, 200);
7017 processSync(mapper);
7018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7019 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007020 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09007021 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
7022 ASSERT_FALSE(fakePointerController->isPointerShown());
7023}
7024
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007025TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
7026 // Initialize the device without setting device source to touch navigation.
7027 addConfigurationProperty("touch.deviceType", "touchScreen");
7028 prepareDisplay(ui::ROTATION_0);
7029 prepareButtons();
7030 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007031 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007032
7033 // Ensure that the device is created as a touchscreen, not touch navigation.
7034 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7035
7036 // Add device type association after the device was created.
7037 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
7038
7039 // Send update to the mapper.
7040 std::list<NotifyArgs> unused2 =
7041 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007042 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007043
7044 // Check whether device type update was successful.
7045 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
7046}
7047
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007048TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
7049 // Initialize the device without setting device source to touch navigation.
7050 addConfigurationProperty("touch.deviceType", "touchScreen");
7051 prepareDisplay(ui::ROTATION_0);
7052 prepareButtons();
7053 prepareAxes(POSITION);
7054 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7055
7056 // Set a physical frame in the display viewport.
7057 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7058 viewport->physicalLeft = 0;
7059 viewport->physicalTop = 0;
7060 viewport->physicalRight = DISPLAY_WIDTH / 2;
7061 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
7062 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007063 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007064
Arpit Singha8c236b2023-04-25 13:56:05 +00007065 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007066
7067 // Hovering inside the physical frame produces events.
7068 processKey(mapper, BTN_TOOL_PEN, 1);
7069 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
7070 processSync(mapper);
7071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7072 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
7073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7074 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
7075
7076 // Leaving the physical frame ends the hovering gesture.
7077 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
7078 processSync(mapper);
7079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7080 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
7081
7082 // Moving outside the physical frame does not produce events.
7083 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
7084 processSync(mapper);
7085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7086
7087 // Re-entering the physical frame produces events.
7088 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
7089 processSync(mapper);
7090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7091 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
7092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7093 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
7094}
7095
Prabir Pradhan5632d622021-09-06 07:57:20 -07007096// --- TouchDisplayProjectionTest ---
7097
7098class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7099public:
7100 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7101 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7102 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00007103 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
7104 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
7105 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007106 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007107 auto rotatedWidth = naturalDisplayWidth;
7108 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007109 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00007110 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007111 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007112 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007113 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007114 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007115 inverseRotationFlags = ui::Transform::ROT_180;
7116 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007117 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007118 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007119 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007120 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007121 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007122 inverseRotationFlags = ui::Transform::ROT_0;
7123 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007124 }
7125
Prabir Pradhana9df3162022-12-05 23:57:27 +00007126 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007127 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7128
7129 std::optional<DisplayViewport> internalViewport =
7130 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7131 DisplayViewport& v = *internalViewport;
7132 v.displayId = DISPLAY_ID;
7133 v.orientation = orientation;
7134
7135 v.logicalLeft = 0;
7136 v.logicalTop = 0;
7137 v.logicalRight = 100;
7138 v.logicalBottom = 100;
7139
7140 v.physicalLeft = rotatedPhysicalDisplay.left;
7141 v.physicalTop = rotatedPhysicalDisplay.top;
7142 v.physicalRight = rotatedPhysicalDisplay.right;
7143 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7144
Prabir Pradhana9df3162022-12-05 23:57:27 +00007145 v.deviceWidth = rotatedWidth;
7146 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007147
7148 v.isActive = true;
7149 v.uniqueId = UNIQUE_ID;
7150 v.type = ViewportType::INTERNAL;
7151 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007152 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007153 }
7154
7155 void assertReceivedMove(const Point& point) {
7156 NotifyMotionArgs motionArgs;
7157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007159 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007160 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7161 1, 0, 0, 0, 0, 0, 0, 0));
7162 }
7163};
7164
7165TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7166 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007167 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007168
7169 prepareButtons();
7170 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007171 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007172
7173 NotifyMotionArgs motionArgs;
7174
7175 // Configure the DisplayViewport such that the logical display maps to a subsection of
7176 // the display panel called the physical display. Here, the physical display is bounded by the
7177 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7178 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7179 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7180 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7181
Michael Wrighta9cf4192022-12-01 23:46:39 +00007182 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007183 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7184
7185 // Touches outside the physical display should be ignored, and should not generate any
7186 // events. Ensure touches at the following points that lie outside of the physical display
7187 // area do not generate any events.
7188 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7189 processDown(mapper, toRawX(point.x), toRawY(point.y));
7190 processSync(mapper);
7191 processUp(mapper);
7192 processSync(mapper);
7193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7194 << "Unexpected event generated for touch outside physical display at point: "
7195 << point.x << ", " << point.y;
7196 }
7197 }
7198}
7199
7200TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7201 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007202 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007203
7204 prepareButtons();
7205 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007206 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007207
7208 NotifyMotionArgs motionArgs;
7209
7210 // Configure the DisplayViewport such that the logical display maps to a subsection of
7211 // the display panel called the physical display. Here, the physical display is bounded by the
7212 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7213 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7214
Michael Wrighta9cf4192022-12-01 23:46:39 +00007215 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007216 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7217
7218 // Touches that start outside the physical display should be ignored until it enters the
7219 // physical display bounds, at which point it should generate a down event. Start a touch at
7220 // the point (5, 100), which is outside the physical display bounds.
7221 static const Point kOutsidePoint{5, 100};
7222 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7223 processSync(mapper);
7224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7225
7226 // Move the touch into the physical display area. This should generate a pointer down.
7227 processMove(mapper, toRawX(11), toRawY(21));
7228 processSync(mapper);
7229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7230 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007231 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007232 ASSERT_NO_FATAL_FAILURE(
7233 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7234
7235 // Move the touch inside the physical display area. This should generate a pointer move.
7236 processMove(mapper, toRawX(69), toRawY(159));
7237 processSync(mapper);
7238 assertReceivedMove({69, 159});
7239
7240 // Move outside the physical display area. Since the pointer is already down, this should
7241 // now continue generating events.
7242 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7243 processSync(mapper);
7244 assertReceivedMove(kOutsidePoint);
7245
7246 // Release. This should generate a pointer up.
7247 processUp(mapper);
7248 processSync(mapper);
7249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7250 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7251 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7252 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7253
7254 // Ensure no more events were generated.
7255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7257 }
7258}
7259
Prabir Pradhana9df3162022-12-05 23:57:27 +00007260// --- TouchscreenPrecisionTests ---
7261
7262// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
7263// in various orientations and with different display rotations. We configure the touchscreen to
7264// have a higher resolution than that of the display by an integer scale factor in each axis so that
7265// we can enforce that coordinates match precisely as expected.
7266class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
7267 public ::testing::WithParamInterface<ui::Rotation> {
7268public:
7269 void SetUp() override {
7270 SingleTouchInputMapperTest::SetUp();
7271
7272 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7273 // four times the resolution of the display in the Y axis.
7274 prepareButtons();
7275 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007276 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7277 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007278 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007279 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7280 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007281 }
7282
7283 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7284 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7285 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7286 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7287
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007288 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7289 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7290
7291 static const int32_t PRECISION_RAW_X_FLAT = 16;
7292 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7293
7294 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7295 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7296
Prabir Pradhana9df3162022-12-05 23:57:27 +00007297 static const std::array<Point, 4> kRawCorners;
7298};
7299
7300const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7301 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7302 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7303 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7304 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7305}};
7306
7307// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7308// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7309// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7310TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7311 enum class Orientation {
7312 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7313 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7314 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7315 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7316 ftl_last = ORIENTATION_270,
7317 };
7318 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7319 Orientation::ORIENTATION_270;
7320 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7321 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7322 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7323 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7324 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7325 };
7326
7327 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7328
7329 // Configure the touchscreen as being installed in the one of the four different orientations
7330 // relative to the display.
7331 addConfigurationProperty("touch.deviceType", "touchScreen");
7332 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7333 prepareDisplay(ui::ROTATION_0);
7334
Arpit Singha8c236b2023-04-25 13:56:05 +00007335 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00007336
7337 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7338 // orientations of either 90 or 270) this means the display's natural resolution will be
7339 // flipped.
7340 const bool displayRotated =
7341 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7342 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7343 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7344 const Rect physicalFrame{0, 0, width, height};
7345 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7346
7347 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7348 const float expectedPrecisionX = displayRotated ? 4 : 2;
7349 const float expectedPrecisionY = displayRotated ? 2 : 4;
7350
7351 // Test all four corners.
7352 for (int i = 0; i < 4; i++) {
7353 const auto& raw = kRawCorners[i];
7354 processDown(mapper, raw.x, raw.y);
7355 processSync(mapper);
7356 const auto& expected = expectedPoints[i];
7357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7358 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7359 WithCoords(expected.x, expected.y),
7360 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7361 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7362 << "with touchscreen orientation "
7363 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7364 << expected.x << ", " << expected.y << ").";
7365 processUp(mapper);
7366 processSync(mapper);
7367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7368 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7369 WithCoords(expected.x, expected.y))));
7370 }
7371}
7372
Prabir Pradhan82687402022-12-06 01:32:53 +00007373TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7374 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7375 kMappedCorners = {
7376 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7377 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7378 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7379 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7380 };
7381
7382 const ui::Rotation displayRotation = GetParam();
7383
7384 addConfigurationProperty("touch.deviceType", "touchScreen");
7385 prepareDisplay(displayRotation);
7386
Arpit Singha8c236b2023-04-25 13:56:05 +00007387 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00007388
7389 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7390
7391 // Test all four corners.
7392 for (int i = 0; i < 4; i++) {
7393 const auto& expected = expectedPoints[i];
7394 const auto& raw = kRawCorners[i];
7395 processDown(mapper, raw.x, raw.y);
7396 processSync(mapper);
7397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7398 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7399 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7400 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7401 << "with display rotation " << ui::toCString(displayRotation)
7402 << ", expected point (" << expected.x << ", " << expected.y << ").";
7403 processUp(mapper);
7404 processSync(mapper);
7405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7406 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7407 WithCoords(expected.x, expected.y))));
7408 }
7409}
7410
Prabir Pradhan3e798762022-12-02 21:02:11 +00007411TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7412 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7413 kMappedCorners = {
7414 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7415 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7416 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7417 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7418 };
7419
7420 const ui::Rotation displayRotation = GetParam();
7421
7422 addConfigurationProperty("touch.deviceType", "touchScreen");
7423 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7424
Arpit Singha8c236b2023-04-25 13:56:05 +00007425 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00007426
7427 // Ori 270, so width and height swapped
7428 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7429 prepareDisplay(displayRotation);
7430 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7431
7432 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7433
7434 // Test all four corners.
7435 for (int i = 0; i < 4; i++) {
7436 const auto& expected = expectedPoints[i];
7437 const auto& raw = kRawCorners[i];
7438 processDown(mapper, raw.x, raw.y);
7439 processSync(mapper);
7440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7441 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7442 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7443 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7444 << "with display rotation " << ui::toCString(displayRotation)
7445 << ", expected point (" << expected.x << ", " << expected.y << ").";
7446 processUp(mapper);
7447 processSync(mapper);
7448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7449 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7450 WithCoords(expected.x, expected.y))));
7451 }
7452}
7453
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007454TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7455 const ui::Rotation displayRotation = GetParam();
7456
7457 addConfigurationProperty("touch.deviceType", "touchScreen");
7458 prepareDisplay(displayRotation);
7459
7460 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00007461 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007462
7463 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7464 // MotionRanges use display pixels as their units
7465 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7466 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7467
7468 // The MotionRanges should be oriented in the rotated display's coordinate space
7469 const bool displayRotated =
7470 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7471
7472 constexpr float MAX_X = 479.5;
7473 constexpr float MAX_Y = 799.75;
7474 EXPECT_EQ(xRange->min, 0.f);
7475 EXPECT_EQ(yRange->min, 0.f);
7476 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7477 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7478
7479 EXPECT_EQ(xRange->flat, 8.f);
7480 EXPECT_EQ(yRange->flat, 8.f);
7481
7482 EXPECT_EQ(xRange->fuzz, 2.f);
7483 EXPECT_EQ(yRange->fuzz, 2.f);
7484
7485 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7486 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7487}
7488
Prabir Pradhana9df3162022-12-05 23:57:27 +00007489// Run the precision tests for all rotations.
7490INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7491 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7492 ui::ROTATION_270),
7493 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7494 return ftl::enum_string(testParamInfo.param);
7495 });
7496
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007497// --- ExternalStylusFusionTest ---
7498
7499class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7500public:
7501 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7502 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007503 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007504 prepareButtons();
7505 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007506 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007507
7508 mStylusState.when = ARBITRARY_TIME;
7509 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007510 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007511 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007512 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007513 processExternalStylusState(mapper);
7514 return mapper;
7515 }
7516
7517 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7518 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7519 for (const NotifyArgs& args : generatedArgs) {
7520 mFakeListener->notify(args);
7521 }
7522 // Loop the reader to flush the input listener queue.
7523 mReader->loopOnce();
7524 return generatedArgs;
7525 }
7526
7527protected:
7528 StylusState mStylusState{};
7529 static constexpr uint32_t EXPECTED_SOURCE =
7530 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7531
7532 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7533 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007534 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007535
7536 // The first pointer is withheld.
7537 processDown(mapper, 100, 200);
7538 processSync(mapper);
7539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7540 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7541 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7542
7543 // The external stylus reports pressure. The withheld finger pointer is released as a
7544 // stylus.
7545 mStylusState.pressure = 1.f;
7546 processExternalStylusState(mapper);
7547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7548 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7549 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7550
7551 // Subsequent pointer events are not withheld.
7552 processMove(mapper, 101, 201);
7553 processSync(mapper);
7554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7555 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7556
7557 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7559 }
7560
7561 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7562 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7563
7564 // Releasing the touch pointer ends the gesture.
7565 processUp(mapper);
7566 processSync(mapper);
7567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7568 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007569 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007570
7571 mStylusState.pressure = 0.f;
7572 processExternalStylusState(mapper);
7573 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7575 }
7576
7577 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7578 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007579 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007580
7581 // The first pointer is withheld when an external stylus is connected,
7582 // and a timeout is requested.
7583 processDown(mapper, 100, 200);
7584 processSync(mapper);
7585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7586 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7587 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7588
7589 // If the timeout expires early, it is requested again.
7590 handleTimeout(mapper, ARBITRARY_TIME + 1);
7591 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7592 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7593
7594 // When the timeout expires, the withheld touch is released as a finger pointer.
7595 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7597 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7598
7599 // Subsequent pointer events are not withheld.
7600 processMove(mapper, 101, 201);
7601 processSync(mapper);
7602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7603 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7604 processUp(mapper);
7605 processSync(mapper);
7606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7607 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7608
7609 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7611 }
7612
7613private:
7614 InputDeviceInfo mExternalStylusDeviceInfo{};
7615};
7616
7617TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7618 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7619 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7620}
7621
7622TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7623 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7624 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7625}
7626
7627TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7628 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7629 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7630}
7631
7632// Test a successful stylus fusion gesture where the pressure is reported by the external
7633// before the touch is reported by the touchscreen.
7634TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7635 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7636 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007637 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007638
7639 // The external stylus reports pressure first. It is ignored for now.
7640 mStylusState.pressure = 1.f;
7641 processExternalStylusState(mapper);
7642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7643 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7644
7645 // When the touch goes down afterwards, it is reported as a stylus pointer.
7646 processDown(mapper, 100, 200);
7647 processSync(mapper);
7648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7649 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7650 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7651
7652 processMove(mapper, 101, 201);
7653 processSync(mapper);
7654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7655 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7656 processUp(mapper);
7657 processSync(mapper);
7658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7659 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7660
7661 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7663}
7664
7665TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7666 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7667
7668 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7669 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7670
7671 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7672 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7673 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7674 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7675}
7676
7677TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7678 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7679 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007680 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007681
7682 mStylusState.pressure = 0.8f;
7683 processExternalStylusState(mapper);
7684 processDown(mapper, 100, 200);
7685 processSync(mapper);
7686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7687 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7688 WithPressure(0.8f))));
7689 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7690
7691 // The external stylus reports a pressure change. We wait for some time for a touch event.
7692 mStylusState.pressure = 0.6f;
7693 processExternalStylusState(mapper);
7694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7695 ASSERT_NO_FATAL_FAILURE(
7696 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7697
7698 // If a touch is reported within the timeout, it reports the updated pressure.
7699 processMove(mapper, 101, 201);
7700 processSync(mapper);
7701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7702 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7703 WithPressure(0.6f))));
7704 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7705
7706 // There is another pressure change.
7707 mStylusState.pressure = 0.5f;
7708 processExternalStylusState(mapper);
7709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7710 ASSERT_NO_FATAL_FAILURE(
7711 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7712
7713 // If a touch is not reported within the timeout, a move event is generated to report
7714 // the new pressure.
7715 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7717 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7718 WithPressure(0.5f))));
7719
7720 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7721 // repeated indefinitely.
7722 mStylusState.pressure = 0.0f;
7723 processExternalStylusState(mapper);
7724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7725 ASSERT_NO_FATAL_FAILURE(
7726 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7727 processMove(mapper, 102, 202);
7728 processSync(mapper);
7729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7730 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7731 WithPressure(0.5f))));
7732 processMove(mapper, 103, 203);
7733 processSync(mapper);
7734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7735 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7736 WithPressure(0.5f))));
7737
7738 processUp(mapper);
7739 processSync(mapper);
7740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7741 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007742 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007743
7744 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7746}
7747
7748TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7749 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7750 auto source = WithSource(EXPECTED_SOURCE);
7751
7752 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007753 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007754 processExternalStylusState(mapper);
7755 processDown(mapper, 100, 200);
7756 processSync(mapper);
7757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7758 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007759 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007760 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7761
7762 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007763 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007764 processExternalStylusState(mapper);
7765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7766 ASSERT_NO_FATAL_FAILURE(
7767 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7768
7769 // If a touch is reported within the timeout, it reports the updated pressure.
7770 processMove(mapper, 101, 201);
7771 processSync(mapper);
7772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7773 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007774 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007775 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7776
7777 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007778 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007779 processExternalStylusState(mapper);
7780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7781 ASSERT_NO_FATAL_FAILURE(
7782 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7783
7784 // If a touch is not reported within the timeout, a move event is generated to report
7785 // the new tool type.
7786 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7788 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007789 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007790
7791 processUp(mapper);
7792 processSync(mapper);
7793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7794 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007795 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007796
7797 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7799}
7800
7801TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7802 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7803 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007804 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007805
7806 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7807
7808 // The external stylus reports a button change. We wait for some time for a touch event.
7809 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7810 processExternalStylusState(mapper);
7811 ASSERT_NO_FATAL_FAILURE(
7812 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7813
7814 // If a touch is reported within the timeout, it reports the updated button state.
7815 processMove(mapper, 101, 201);
7816 processSync(mapper);
7817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7818 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7819 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7821 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7822 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7823 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7824
7825 // The button is now released.
7826 mStylusState.buttons = 0;
7827 processExternalStylusState(mapper);
7828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7829 ASSERT_NO_FATAL_FAILURE(
7830 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7831
7832 // If a touch is not reported within the timeout, a move event is generated to report
7833 // the new button state.
7834 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7836 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7837 WithButtonState(0))));
7838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007839 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7840 WithButtonState(0))));
7841
7842 processUp(mapper);
7843 processSync(mapper);
7844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007845 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7846
7847 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7849}
7850
Michael Wrightd02c5b62014-02-10 15:10:22 -08007851// --- MultiTouchInputMapperTest ---
7852
7853class MultiTouchInputMapperTest : public TouchInputMapperTest {
7854protected:
7855 void prepareAxes(int axes);
7856
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007857 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7858 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7859 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7860 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7861 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7862 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7863 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7864 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7865 void processId(MultiTouchInputMapper& mapper, int32_t id);
7866 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7867 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7868 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007869 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007870 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007871 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7872 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007873};
7874
7875void MultiTouchInputMapperTest::prepareAxes(int axes) {
7876 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007877 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7878 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007879 }
7880 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007881 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7882 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007883 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007884 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7885 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007886 }
7887 }
7888 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007889 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7890 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007891 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007892 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007893 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007894 }
7895 }
7896 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007897 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7898 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007899 }
7900 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007901 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7902 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007903 }
7904 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007905 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7906 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007907 }
7908 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007909 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7910 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007911 }
7912 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007913 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7914 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007915 }
7916 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007917 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007918 }
7919}
7920
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007921void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7922 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007923 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7924 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007925}
7926
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007927void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7928 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007929 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007930}
7931
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007932void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7933 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007934 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007935}
7936
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007937void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007938 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007939}
7940
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007941void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007943}
7944
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007945void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7946 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007948}
7949
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007950void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007951 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007952}
7953
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007954void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007955 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007956}
7957
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007958void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007960}
7961
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007962void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007964}
7965
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007966void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007967 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007968}
7969
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007970void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7971 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007972 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007973}
7974
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007975void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7976 int32_t value) {
7977 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7978 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7979}
7980
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007981void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007982 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007983}
7984
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007985void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7986 nsecs_t readTime) {
7987 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007988}
7989
Michael Wrightd02c5b62014-02-10 15:10:22 -08007990TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007991 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007992 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007993 prepareAxes(POSITION);
7994 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007995 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007996
arthurhungdcef2dc2020-08-11 14:47:50 +08007997 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007998
7999 NotifyMotionArgs motionArgs;
8000
8001 // Two fingers down at once.
8002 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8003 processPosition(mapper, x1, y1);
8004 processMTSync(mapper);
8005 processPosition(mapper, x2, y2);
8006 processMTSync(mapper);
8007 processSync(mapper);
8008
8009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8010 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8011 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8012 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8013 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8014 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8015 ASSERT_EQ(0, motionArgs.flags);
8016 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8017 ASSERT_EQ(0, motionArgs.buttonState);
8018 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008019 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008020 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008021 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8023 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8024 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8025 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8026 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8027
8028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8029 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8030 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8031 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8032 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008033 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008034 ASSERT_EQ(0, motionArgs.flags);
8035 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8036 ASSERT_EQ(0, motionArgs.buttonState);
8037 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008038 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008039 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008040 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008041 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008042 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8044 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8046 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8047 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8048 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8049 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8050
8051 // Move.
8052 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8053 processPosition(mapper, x1, y1);
8054 processMTSync(mapper);
8055 processPosition(mapper, x2, y2);
8056 processMTSync(mapper);
8057 processSync(mapper);
8058
8059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8060 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8061 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8062 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8063 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8065 ASSERT_EQ(0, motionArgs.flags);
8066 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8067 ASSERT_EQ(0, motionArgs.buttonState);
8068 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008069 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008070 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008071 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008073 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008074 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8075 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8076 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8077 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8078 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8079 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8080 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8081
8082 // First finger up.
8083 x2 += 15; y2 -= 20;
8084 processPosition(mapper, x2, y2);
8085 processMTSync(mapper);
8086 processSync(mapper);
8087
8088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8089 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8090 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8091 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8092 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008093 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008094 ASSERT_EQ(0, motionArgs.flags);
8095 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8096 ASSERT_EQ(0, motionArgs.buttonState);
8097 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008098 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008099 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008100 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008101 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008102 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008103 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8104 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8105 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8106 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8107 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8108 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8109 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8110
8111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8112 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8113 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8114 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8115 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8117 ASSERT_EQ(0, motionArgs.flags);
8118 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8119 ASSERT_EQ(0, motionArgs.buttonState);
8120 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008121 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008122 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 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8127 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8128 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8129
8130 // Move.
8131 x2 += 20; y2 -= 25;
8132 processPosition(mapper, x2, y2);
8133 processMTSync(mapper);
8134 processSync(mapper);
8135
8136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8137 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8138 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8139 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8140 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8142 ASSERT_EQ(0, motionArgs.flags);
8143 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8144 ASSERT_EQ(0, motionArgs.buttonState);
8145 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008146 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008147 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008148 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008149 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8150 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8151 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8152 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8153 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8154
8155 // New finger down.
8156 int32_t x3 = 700, y3 = 300;
8157 processPosition(mapper, x2, y2);
8158 processMTSync(mapper);
8159 processPosition(mapper, x3, y3);
8160 processMTSync(mapper);
8161 processSync(mapper);
8162
8163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8164 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8165 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8166 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8167 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008168 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008169 ASSERT_EQ(0, motionArgs.flags);
8170 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8171 ASSERT_EQ(0, motionArgs.buttonState);
8172 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008173 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008174 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 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8183 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8184 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8185
8186 // Second finger up.
8187 x3 += 30; y3 -= 20;
8188 processPosition(mapper, x3, y3);
8189 processMTSync(mapper);
8190 processSync(mapper);
8191
8192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8193 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8194 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8195 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8196 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008197 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008198 ASSERT_EQ(0, motionArgs.flags);
8199 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8200 ASSERT_EQ(0, motionArgs.buttonState);
8201 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008202 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008203 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008204 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008205 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008206 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008207 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8208 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8210 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8211 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8212 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8213 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8214
8215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8216 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8217 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8218 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8219 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8221 ASSERT_EQ(0, motionArgs.flags);
8222 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8223 ASSERT_EQ(0, motionArgs.buttonState);
8224 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008225 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008226 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008227 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8229 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8230 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8231 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8232 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8233
8234 // Last finger up.
8235 processMTSync(mapper);
8236 processSync(mapper);
8237
8238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8239 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8240 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8241 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8242 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8243 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8244 ASSERT_EQ(0, motionArgs.flags);
8245 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8246 ASSERT_EQ(0, motionArgs.buttonState);
8247 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008248 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008249 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008250 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008251 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8252 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8253 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8254 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8255 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8256
8257 // Should not have sent any more keys or motions.
8258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8260}
8261
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008262TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8263 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008264 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008265
8266 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8267 /*fuzz*/ 0, /*resolution*/ 10);
8268 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8269 /*fuzz*/ 0, /*resolution*/ 11);
8270 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8271 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8272 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8273 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8274 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8275 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8276 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8277 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8278
Arpit Singha8c236b2023-04-25 13:56:05 +00008279 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008280
8281 // X and Y axes
8282 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8283 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8284 // Touch major and minor
8285 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8286 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8287 // Tool major and minor
8288 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8289 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8290}
8291
8292TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8293 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008294 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008295
8296 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8297 /*fuzz*/ 0, /*resolution*/ 10);
8298 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8299 /*fuzz*/ 0, /*resolution*/ 11);
8300
8301 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8302
Arpit Singha8c236b2023-04-25 13:56:05 +00008303 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008304
8305 // Touch major and minor
8306 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8307 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8308 // Tool major and minor
8309 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8310 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8311}
8312
Michael Wrightd02c5b62014-02-10 15:10:22 -08008313TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008314 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008315 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008316 prepareAxes(POSITION | ID);
8317 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008318 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008319
arthurhungdcef2dc2020-08-11 14:47:50 +08008320 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008321
8322 NotifyMotionArgs motionArgs;
8323
8324 // Two fingers down at once.
8325 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8326 processPosition(mapper, x1, y1);
8327 processId(mapper, 1);
8328 processMTSync(mapper);
8329 processPosition(mapper, x2, y2);
8330 processId(mapper, 2);
8331 processMTSync(mapper);
8332 processSync(mapper);
8333
8334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8335 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008336 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008337 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008338 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8340 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8341
8342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008343 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008344 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008345 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008346 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008347 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008348 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8350 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8352 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8353
8354 // Move.
8355 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8356 processPosition(mapper, x1, y1);
8357 processId(mapper, 1);
8358 processMTSync(mapper);
8359 processPosition(mapper, x2, y2);
8360 processId(mapper, 2);
8361 processMTSync(mapper);
8362 processSync(mapper);
8363
8364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8365 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008366 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008367 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008368 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008369 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008370 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8372 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8374 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8375
8376 // First finger up.
8377 x2 += 15; y2 -= 20;
8378 processPosition(mapper, x2, y2);
8379 processId(mapper, 2);
8380 processMTSync(mapper);
8381 processSync(mapper);
8382
8383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008384 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008385 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008386 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008387 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008388 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008389 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8391 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8393 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8394
8395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008397 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008398 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008399 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8401 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8402
8403 // Move.
8404 x2 += 20; y2 -= 25;
8405 processPosition(mapper, x2, y2);
8406 processId(mapper, 2);
8407 processMTSync(mapper);
8408 processSync(mapper);
8409
8410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008412 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008413 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008414 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8416 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8417
8418 // New finger down.
8419 int32_t x3 = 700, y3 = 300;
8420 processPosition(mapper, x2, y2);
8421 processId(mapper, 2);
8422 processMTSync(mapper);
8423 processPosition(mapper, x3, y3);
8424 processId(mapper, 3);
8425 processMTSync(mapper);
8426 processSync(mapper);
8427
8428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008429 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008430 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008431 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008432 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008433 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008434 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8436 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8437 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8438 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8439
8440 // Second finger up.
8441 x3 += 30; y3 -= 20;
8442 processPosition(mapper, x3, y3);
8443 processId(mapper, 3);
8444 processMTSync(mapper);
8445 processSync(mapper);
8446
8447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008448 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008449 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008450 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008451 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008452 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008453 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8455 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8457 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8458
8459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008461 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008462 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008463 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8465 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8466
8467 // Last finger up.
8468 processMTSync(mapper);
8469 processSync(mapper);
8470
8471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8472 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008473 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008474 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008475 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8477 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8478
8479 // Should not have sent any more keys or motions.
8480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8482}
8483
8484TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008485 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008486 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008487 prepareAxes(POSITION | ID | SLOT);
8488 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008489 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008490
arthurhungdcef2dc2020-08-11 14:47:50 +08008491 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008492
8493 NotifyMotionArgs motionArgs;
8494
8495 // Two fingers down at once.
8496 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8497 processPosition(mapper, x1, y1);
8498 processId(mapper, 1);
8499 processSlot(mapper, 1);
8500 processPosition(mapper, x2, y2);
8501 processId(mapper, 2);
8502 processSync(mapper);
8503
8504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8505 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008506 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008507 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008508 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8510 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8511
8512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008513 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008514 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008515 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008516 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008517 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008518 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8520 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8522 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8523
8524 // Move.
8525 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8526 processSlot(mapper, 0);
8527 processPosition(mapper, x1, y1);
8528 processSlot(mapper, 1);
8529 processPosition(mapper, x2, y2);
8530 processSync(mapper);
8531
8532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008534 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008535 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008536 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008537 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008538 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8540 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8542 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8543
8544 // First finger up.
8545 x2 += 15; y2 -= 20;
8546 processSlot(mapper, 0);
8547 processId(mapper, -1);
8548 processSlot(mapper, 1);
8549 processPosition(mapper, x2, y2);
8550 processSync(mapper);
8551
8552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008553 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008554 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008555 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008556 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008557 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008558 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8560 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8562 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8563
8564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008566 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008567 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008568 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8570 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8571
8572 // Move.
8573 x2 += 20; y2 -= 25;
8574 processPosition(mapper, x2, y2);
8575 processSync(mapper);
8576
8577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8578 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008579 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008580 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008581 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8583 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8584
8585 // New finger down.
8586 int32_t x3 = 700, y3 = 300;
8587 processPosition(mapper, x2, y2);
8588 processSlot(mapper, 0);
8589 processId(mapper, 3);
8590 processPosition(mapper, x3, y3);
8591 processSync(mapper);
8592
8593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008594 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008595 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008596 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008597 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008598 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008599 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8601 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8603 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8604
8605 // Second finger up.
8606 x3 += 30; y3 -= 20;
8607 processSlot(mapper, 1);
8608 processId(mapper, -1);
8609 processSlot(mapper, 0);
8610 processPosition(mapper, x3, y3);
8611 processSync(mapper);
8612
8613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008614 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008615 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008616 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008617 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008618 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008619 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8621 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8623 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8624
8625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008627 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008628 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008629 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8631 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8632
8633 // Last finger up.
8634 processId(mapper, -1);
8635 processSync(mapper);
8636
8637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8638 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008639 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008640 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008641 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008642 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8643 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8644
8645 // Should not have sent any more keys or motions.
8646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8648}
8649
8650TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008651 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008652 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008653 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008654 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008655
8656 // These calculations are based on the input device calibration documentation.
8657 int32_t rawX = 100;
8658 int32_t rawY = 200;
8659 int32_t rawTouchMajor = 7;
8660 int32_t rawTouchMinor = 6;
8661 int32_t rawToolMajor = 9;
8662 int32_t rawToolMinor = 8;
8663 int32_t rawPressure = 11;
8664 int32_t rawDistance = 0;
8665 int32_t rawOrientation = 3;
8666 int32_t id = 5;
8667
8668 float x = toDisplayX(rawX);
8669 float y = toDisplayY(rawY);
8670 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8671 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8672 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8673 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8674 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8675 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8676 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8677 float distance = float(rawDistance);
8678
8679 processPosition(mapper, rawX, rawY);
8680 processTouchMajor(mapper, rawTouchMajor);
8681 processTouchMinor(mapper, rawTouchMinor);
8682 processToolMajor(mapper, rawToolMajor);
8683 processToolMinor(mapper, rawToolMinor);
8684 processPressure(mapper, rawPressure);
8685 processOrientation(mapper, rawOrientation);
8686 processDistance(mapper, rawDistance);
8687 processId(mapper, id);
8688 processMTSync(mapper);
8689 processSync(mapper);
8690
8691 NotifyMotionArgs args;
8692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8693 ASSERT_EQ(0, args.pointerProperties[0].id);
8694 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8695 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8696 orientation, distance));
8697}
8698
8699TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008700 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008701 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008702 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8703 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00008704 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008705
8706 // These calculations are based on the input device calibration documentation.
8707 int32_t rawX = 100;
8708 int32_t rawY = 200;
8709 int32_t rawTouchMajor = 140;
8710 int32_t rawTouchMinor = 120;
8711 int32_t rawToolMajor = 180;
8712 int32_t rawToolMinor = 160;
8713
8714 float x = toDisplayX(rawX);
8715 float y = toDisplayY(rawY);
8716 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8717 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8718 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8719 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8720 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8721
8722 processPosition(mapper, rawX, rawY);
8723 processTouchMajor(mapper, rawTouchMajor);
8724 processTouchMinor(mapper, rawTouchMinor);
8725 processToolMajor(mapper, rawToolMajor);
8726 processToolMinor(mapper, rawToolMinor);
8727 processMTSync(mapper);
8728 processSync(mapper);
8729
8730 NotifyMotionArgs args;
8731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8733 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8734}
8735
8736TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008737 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008738 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008739 prepareAxes(POSITION | TOUCH | TOOL);
8740 addConfigurationProperty("touch.size.calibration", "diameter");
8741 addConfigurationProperty("touch.size.scale", "10");
8742 addConfigurationProperty("touch.size.bias", "160");
8743 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00008744 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008745
8746 // These calculations are based on the input device calibration documentation.
8747 // Note: We only provide a single common touch/tool value because the device is assumed
8748 // not to emit separate values for each pointer (isSummed = 1).
8749 int32_t rawX = 100;
8750 int32_t rawY = 200;
8751 int32_t rawX2 = 150;
8752 int32_t rawY2 = 250;
8753 int32_t rawTouchMajor = 5;
8754 int32_t rawToolMajor = 8;
8755
8756 float x = toDisplayX(rawX);
8757 float y = toDisplayY(rawY);
8758 float x2 = toDisplayX(rawX2);
8759 float y2 = toDisplayY(rawY2);
8760 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8761 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8762 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8763
8764 processPosition(mapper, rawX, rawY);
8765 processTouchMajor(mapper, rawTouchMajor);
8766 processToolMajor(mapper, rawToolMajor);
8767 processMTSync(mapper);
8768 processPosition(mapper, rawX2, rawY2);
8769 processTouchMajor(mapper, rawTouchMajor);
8770 processToolMajor(mapper, rawToolMajor);
8771 processMTSync(mapper);
8772 processSync(mapper);
8773
8774 NotifyMotionArgs args;
8775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8776 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8777
8778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008779 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008780 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8782 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8784 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8785}
8786
8787TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008788 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008789 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008790 prepareAxes(POSITION | TOUCH | TOOL);
8791 addConfigurationProperty("touch.size.calibration", "area");
8792 addConfigurationProperty("touch.size.scale", "43");
8793 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00008794 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008795
8796 // These calculations are based on the input device calibration documentation.
8797 int32_t rawX = 100;
8798 int32_t rawY = 200;
8799 int32_t rawTouchMajor = 5;
8800 int32_t rawToolMajor = 8;
8801
8802 float x = toDisplayX(rawX);
8803 float y = toDisplayY(rawY);
8804 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8805 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8806 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8807
8808 processPosition(mapper, rawX, rawY);
8809 processTouchMajor(mapper, rawTouchMajor);
8810 processToolMajor(mapper, rawToolMajor);
8811 processMTSync(mapper);
8812 processSync(mapper);
8813
8814 NotifyMotionArgs args;
8815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8817 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8818}
8819
8820TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008821 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008822 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008823 prepareAxes(POSITION | PRESSURE);
8824 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8825 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00008826 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008827
Michael Wrightaa449c92017-12-13 21:21:43 +00008828 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008829 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008830 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8831 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8832 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8833
Michael Wrightd02c5b62014-02-10 15:10:22 -08008834 // These calculations are based on the input device calibration documentation.
8835 int32_t rawX = 100;
8836 int32_t rawY = 200;
8837 int32_t rawPressure = 60;
8838
8839 float x = toDisplayX(rawX);
8840 float y = toDisplayY(rawY);
8841 float pressure = float(rawPressure) * 0.01f;
8842
8843 processPosition(mapper, rawX, rawY);
8844 processPressure(mapper, rawPressure);
8845 processMTSync(mapper);
8846 processSync(mapper);
8847
8848 NotifyMotionArgs args;
8849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8851 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8852}
8853
8854TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008855 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008856 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008857 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008858 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008859
8860 NotifyMotionArgs motionArgs;
8861 NotifyKeyArgs keyArgs;
8862
8863 processId(mapper, 1);
8864 processPosition(mapper, 100, 200);
8865 processSync(mapper);
8866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8867 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8868 ASSERT_EQ(0, motionArgs.buttonState);
8869
8870 // press BTN_LEFT, release BTN_LEFT
8871 processKey(mapper, BTN_LEFT, 1);
8872 processSync(mapper);
8873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8875 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8876
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8878 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8879 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8880
Michael Wrightd02c5b62014-02-10 15:10:22 -08008881 processKey(mapper, BTN_LEFT, 0);
8882 processSync(mapper);
8883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008884 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008885 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008886
8887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008888 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008889 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008890
8891 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8892 processKey(mapper, BTN_RIGHT, 1);
8893 processKey(mapper, BTN_MIDDLE, 1);
8894 processSync(mapper);
8895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8897 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8898 motionArgs.buttonState);
8899
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8901 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8902 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8903
8904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8905 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8906 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8907 motionArgs.buttonState);
8908
Michael Wrightd02c5b62014-02-10 15:10:22 -08008909 processKey(mapper, BTN_RIGHT, 0);
8910 processSync(mapper);
8911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008912 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008913 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008914
8915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008916 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008917 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008918
8919 processKey(mapper, BTN_MIDDLE, 0);
8920 processSync(mapper);
8921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008922 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008923 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008924
8925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008926 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008927 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008928
8929 // press BTN_BACK, release BTN_BACK
8930 processKey(mapper, BTN_BACK, 1);
8931 processSync(mapper);
8932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8933 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8934 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008935
Michael Wrightd02c5b62014-02-10 15:10:22 -08008936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008938 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8939
8940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8941 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8942 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008943
8944 processKey(mapper, BTN_BACK, 0);
8945 processSync(mapper);
8946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008947 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008948 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008949
8950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008952 ASSERT_EQ(0, motionArgs.buttonState);
8953
Michael Wrightd02c5b62014-02-10 15:10:22 -08008954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8955 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8956 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8957
8958 // press BTN_SIDE, release BTN_SIDE
8959 processKey(mapper, BTN_SIDE, 1);
8960 processSync(mapper);
8961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8962 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8963 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008964
Michael Wrightd02c5b62014-02-10 15:10:22 -08008965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008967 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8968
8969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8970 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8971 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008972
8973 processKey(mapper, BTN_SIDE, 0);
8974 processSync(mapper);
8975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008976 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008977 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008978
8979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008980 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008981 ASSERT_EQ(0, motionArgs.buttonState);
8982
Michael Wrightd02c5b62014-02-10 15:10:22 -08008983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8984 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8985 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8986
8987 // press BTN_FORWARD, release BTN_FORWARD
8988 processKey(mapper, BTN_FORWARD, 1);
8989 processSync(mapper);
8990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8991 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8992 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008993
Michael Wrightd02c5b62014-02-10 15:10:22 -08008994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008996 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8997
8998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8999 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9000 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009001
9002 processKey(mapper, BTN_FORWARD, 0);
9003 processSync(mapper);
9004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009005 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009006 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009007
9008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009009 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009010 ASSERT_EQ(0, motionArgs.buttonState);
9011
Michael Wrightd02c5b62014-02-10 15:10:22 -08009012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9013 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9014 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9015
9016 // press BTN_EXTRA, release BTN_EXTRA
9017 processKey(mapper, BTN_EXTRA, 1);
9018 processSync(mapper);
9019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9020 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9021 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009022
Michael Wrightd02c5b62014-02-10 15:10:22 -08009023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009025 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9026
9027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9028 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9029 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009030
9031 processKey(mapper, BTN_EXTRA, 0);
9032 processSync(mapper);
9033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009034 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009035 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009036
9037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009039 ASSERT_EQ(0, motionArgs.buttonState);
9040
Michael Wrightd02c5b62014-02-10 15:10:22 -08009041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9042 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9043 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9044
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9046
Michael Wrightd02c5b62014-02-10 15:10:22 -08009047 // press BTN_STYLUS, release BTN_STYLUS
9048 processKey(mapper, BTN_STYLUS, 1);
9049 processSync(mapper);
9050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009052 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
9053
9054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9055 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9056 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009057
9058 processKey(mapper, BTN_STYLUS, 0);
9059 processSync(mapper);
9060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009061 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009062 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009063
9064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009065 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009066 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009067
9068 // press BTN_STYLUS2, release BTN_STYLUS2
9069 processKey(mapper, BTN_STYLUS2, 1);
9070 processSync(mapper);
9071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009073 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
9074
9075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9076 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9077 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009078
9079 processKey(mapper, BTN_STYLUS2, 0);
9080 processSync(mapper);
9081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009082 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009083 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009084
9085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009087 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009088
9089 // release touch
9090 processId(mapper, -1);
9091 processSync(mapper);
9092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9093 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9094 ASSERT_EQ(0, motionArgs.buttonState);
9095}
9096
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009097TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
9098 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009099 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009100 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00009101 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009102
9103 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
9104 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
9105
9106 // Touch down.
9107 processId(mapper, 1);
9108 processPosition(mapper, 100, 200);
9109 processSync(mapper);
9110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9111 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
9112
9113 // Press and release button mapped to the primary stylus button.
9114 processKey(mapper, BTN_A, 1);
9115 processSync(mapper);
9116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9117 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9118 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9120 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9121 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9122
9123 processKey(mapper, BTN_A, 0);
9124 processSync(mapper);
9125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9126 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9128 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9129
9130 // Press and release the HID usage mapped to the secondary stylus button.
9131 processHidUsage(mapper, 0xabcd, 1);
9132 processSync(mapper);
9133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9134 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9135 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9137 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9138 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9139
9140 processHidUsage(mapper, 0xabcd, 0);
9141 processSync(mapper);
9142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9143 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9145 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9146
9147 // Release touch.
9148 processId(mapper, -1);
9149 processSync(mapper);
9150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9151 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
9152}
9153
Michael Wrightd02c5b62014-02-10 15:10:22 -08009154TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009155 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009156 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009157 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009158 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009159
9160 NotifyMotionArgs motionArgs;
9161
9162 // default tool type is finger
9163 processId(mapper, 1);
9164 processPosition(mapper, 100, 200);
9165 processSync(mapper);
9166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9167 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009168 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009169
9170 // eraser
9171 processKey(mapper, BTN_TOOL_RUBBER, 1);
9172 processSync(mapper);
9173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009175 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009176
9177 // stylus
9178 processKey(mapper, BTN_TOOL_RUBBER, 0);
9179 processKey(mapper, BTN_TOOL_PEN, 1);
9180 processSync(mapper);
9181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009183 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009184
9185 // brush
9186 processKey(mapper, BTN_TOOL_PEN, 0);
9187 processKey(mapper, BTN_TOOL_BRUSH, 1);
9188 processSync(mapper);
9189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009191 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009192
9193 // pencil
9194 processKey(mapper, BTN_TOOL_BRUSH, 0);
9195 processKey(mapper, BTN_TOOL_PENCIL, 1);
9196 processSync(mapper);
9197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009199 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009200
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009201 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009202 processKey(mapper, BTN_TOOL_PENCIL, 0);
9203 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
9204 processSync(mapper);
9205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9206 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009207 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009208
9209 // mouse
9210 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9211 processKey(mapper, BTN_TOOL_MOUSE, 1);
9212 processSync(mapper);
9213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009215 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009216
9217 // lens
9218 processKey(mapper, BTN_TOOL_MOUSE, 0);
9219 processKey(mapper, BTN_TOOL_LENS, 1);
9220 processSync(mapper);
9221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009223 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009224
9225 // double-tap
9226 processKey(mapper, BTN_TOOL_LENS, 0);
9227 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9228 processSync(mapper);
9229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9230 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009231 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009232
9233 // triple-tap
9234 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9235 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9236 processSync(mapper);
9237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9238 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009239 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009240
9241 // quad-tap
9242 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9243 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9244 processSync(mapper);
9245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009247 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009248
9249 // finger
9250 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9251 processKey(mapper, BTN_TOOL_FINGER, 1);
9252 processSync(mapper);
9253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9254 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009255 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009256
9257 // stylus trumps finger
9258 processKey(mapper, BTN_TOOL_PEN, 1);
9259 processSync(mapper);
9260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009262 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009263
9264 // eraser trumps stylus
9265 processKey(mapper, BTN_TOOL_RUBBER, 1);
9266 processSync(mapper);
9267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009269 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009270
9271 // mouse trumps eraser
9272 processKey(mapper, BTN_TOOL_MOUSE, 1);
9273 processSync(mapper);
9274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009276 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009277
9278 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9279 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9280 processSync(mapper);
9281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009283 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009284
9285 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9286 processToolType(mapper, MT_TOOL_PEN);
9287 processSync(mapper);
9288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009290 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009291
9292 // back to default tool type
9293 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9294 processKey(mapper, BTN_TOOL_MOUSE, 0);
9295 processKey(mapper, BTN_TOOL_RUBBER, 0);
9296 processKey(mapper, BTN_TOOL_PEN, 0);
9297 processKey(mapper, BTN_TOOL_FINGER, 0);
9298 processSync(mapper);
9299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009301 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009302}
9303
9304TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009305 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009306 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009307 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009308 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009309 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009310
9311 NotifyMotionArgs motionArgs;
9312
9313 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9314 processId(mapper, 1);
9315 processPosition(mapper, 100, 200);
9316 processSync(mapper);
9317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9318 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9319 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9320 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9321
9322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9323 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9324 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9325 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9326
9327 // move a little
9328 processPosition(mapper, 150, 250);
9329 processSync(mapper);
9330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9331 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9333 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9334
9335 // down when BTN_TOUCH is pressed, pressure defaults to 1
9336 processKey(mapper, BTN_TOUCH, 1);
9337 processSync(mapper);
9338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9339 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9341 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9342
9343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9344 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9346 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9347
9348 // up when BTN_TOUCH is released, hover restored
9349 processKey(mapper, BTN_TOUCH, 0);
9350 processSync(mapper);
9351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9352 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9354 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9355
9356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9357 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9359 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9360
9361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9362 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9364 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9365
9366 // exit hover when pointer goes away
9367 processId(mapper, -1);
9368 processSync(mapper);
9369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9370 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9372 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9373}
9374
9375TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009376 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009377 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009378 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009379 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009380
9381 NotifyMotionArgs motionArgs;
9382
9383 // initially hovering because pressure is 0
9384 processId(mapper, 1);
9385 processPosition(mapper, 100, 200);
9386 processPressure(mapper, 0);
9387 processSync(mapper);
9388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9389 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9391 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9392
9393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9394 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9396 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9397
9398 // move a little
9399 processPosition(mapper, 150, 250);
9400 processSync(mapper);
9401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9402 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9404 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9405
9406 // down when pressure becomes non-zero
9407 processPressure(mapper, RAW_PRESSURE_MAX);
9408 processSync(mapper);
9409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9410 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9412 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9413
9414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9415 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9417 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9418
9419 // up when pressure becomes 0, hover restored
9420 processPressure(mapper, 0);
9421 processSync(mapper);
9422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9423 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9425 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9426
9427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9428 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9429 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9430 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9431
9432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9433 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9435 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9436
9437 // exit hover when pointer goes away
9438 processId(mapper, -1);
9439 processSync(mapper);
9440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9441 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9443 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9444}
9445
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009446/**
9447 * Set the input device port <--> display port associations, and check that the
9448 * events are routed to the display that matches the display port.
9449 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9450 */
9451TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009452 const std::string usb2 = "USB2";
9453 const uint8_t hdmi1 = 0;
9454 const uint8_t hdmi2 = 1;
9455 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009456 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009457
9458 addConfigurationProperty("touch.deviceType", "touchScreen");
9459 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009460 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009461
9462 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9463 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9464
9465 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9466 // for this input device is specified, and the matching viewport is not present,
9467 // the input device should be disabled (at the mapper level).
9468
9469 // Add viewport for display 2 on hdmi2
9470 prepareSecondaryDisplay(type, hdmi2);
9471 // Send a touch event
9472 processPosition(mapper, 100, 100);
9473 processSync(mapper);
9474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9475
9476 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009477 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009478 // Send a touch event again
9479 processPosition(mapper, 100, 100);
9480 processSync(mapper);
9481
9482 NotifyMotionArgs args;
9483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9484 ASSERT_EQ(DISPLAY_ID, args.displayId);
9485}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009486
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009487TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9488 addConfigurationProperty("touch.deviceType", "touchScreen");
9489 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009490 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009491
9492 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9493
Michael Wrighta9cf4192022-12-01 23:46:39 +00009494 prepareDisplay(ui::ROTATION_0);
9495 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009496
9497 // Send a touch event
9498 processPosition(mapper, 100, 100);
9499 processSync(mapper);
9500
9501 NotifyMotionArgs args;
9502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9503 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9504}
9505
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009506TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009507 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009508 std::shared_ptr<FakePointerController> fakePointerController =
9509 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009510 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009511 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009512 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009513
Garfield Tan888a6a42020-01-09 11:39:16 -08009514 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009515 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009516
Michael Wrighta9cf4192022-12-01 23:46:39 +00009517 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009518 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009519 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009520
Josep del Río2d8c79a2023-01-23 19:33:50 +00009521 // Check source is mouse that would obtain the PointerController.
9522 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009523
9524 NotifyMotionArgs motionArgs;
9525 processPosition(mapper, 100, 100);
9526 processSync(mapper);
9527
9528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9529 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9530 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9531}
9532
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009533/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009534 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9535 */
9536TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9537 addConfigurationProperty("touch.deviceType", "touchScreen");
9538 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009539 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009540
Michael Wrighta9cf4192022-12-01 23:46:39 +00009541 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009542 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9543 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9544 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9545 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009546
9547 NotifyMotionArgs args;
9548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9549 ASSERT_EQ(26, args.readTime);
9550
Harry Cutts33476232023-01-30 19:57:29 +00009551 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9552 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9553 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009554
9555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9556 ASSERT_EQ(33, args.readTime);
9557}
9558
9559/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009560 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9561 * events should not be delivered to the listener.
9562 */
9563TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9564 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009565 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009566 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009567 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009568 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009569 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009570 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009571
9572 NotifyMotionArgs motionArgs;
9573 processPosition(mapper, 100, 100);
9574 processSync(mapper);
9575
9576 mFakeListener->assertNotifyMotionWasNotCalled();
9577}
9578
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009579/**
9580 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9581 * the touch mapper can process the events and the events can be delivered to the listener.
9582 */
9583TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9584 addConfigurationProperty("touch.deviceType", "touchScreen");
9585 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009586 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009587 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009588 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009589 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009590 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009591
9592 NotifyMotionArgs motionArgs;
9593 processPosition(mapper, 100, 100);
9594 processSync(mapper);
9595
9596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9597 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9598}
9599
Josh Thielene986aed2023-06-01 14:17:30 +00009600/**
9601 * When the viewport is deactivated (isActive transitions from true to false),
9602 * and touch.enableForInactiveViewport is false, touches prior to the transition
9603 * should be cancelled.
9604 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08009605TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9606 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009607 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009608 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009609 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009610 std::optional<DisplayViewport> optionalDisplayViewport =
9611 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9612 ASSERT_TRUE(optionalDisplayViewport.has_value());
9613 DisplayViewport displayViewport = *optionalDisplayViewport;
9614
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009615 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009616 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009617 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08009618
9619 // Finger down
9620 int32_t x = 100, y = 100;
9621 processPosition(mapper, x, y);
9622 processSync(mapper);
9623
9624 NotifyMotionArgs motionArgs;
9625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9626 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9627
9628 // Deactivate display viewport
9629 displayViewport.isActive = false;
9630 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009631 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009632
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009633 // The ongoing touch should be canceled immediately
9634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9635 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9636
9637 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009638 x += 10, y += 10;
9639 processPosition(mapper, x, y);
9640 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009642
9643 // Reactivate display viewport
9644 displayViewport.isActive = true;
9645 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009646 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009647
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009648 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009649 x += 10, y += 10;
9650 processPosition(mapper, x, y);
9651 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9653 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009654}
9655
Josh Thielene986aed2023-06-01 14:17:30 +00009656/**
9657 * When the viewport is deactivated (isActive transitions from true to false),
9658 * and touch.enableForInactiveViewport is true, touches prior to the transition
9659 * should not be cancelled.
9660 */
9661TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
9662 addConfigurationProperty("touch.deviceType", "touchScreen");
9663 addConfigurationProperty("touch.enableForInactiveViewport", "1");
9664 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
9665 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
9666 std::optional<DisplayViewport> optionalDisplayViewport =
9667 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9668 ASSERT_TRUE(optionalDisplayViewport.has_value());
9669 DisplayViewport displayViewport = *optionalDisplayViewport;
9670
9671 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9672 prepareAxes(POSITION);
9673 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
9674
9675 // Finger down
9676 int32_t x = 100, y = 100;
9677 processPosition(mapper, x, y);
9678 processSync(mapper);
9679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9680 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9681
9682 // Deactivate display viewport
9683 displayViewport.isActive = false;
9684 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9685 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9686
9687 // The ongoing touch should not be canceled
9688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9689
9690 // Finger move is not ignored
9691 x += 10, y += 10;
9692 processPosition(mapper, x, y);
9693 processSync(mapper);
9694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9695 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
9696
9697 // Reactivate display viewport
9698 displayViewport.isActive = true;
9699 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9700 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9701
9702 // Finger move continues and does not start new gesture
9703 x += 10, y += 10;
9704 processPosition(mapper, x, y);
9705 processSync(mapper);
9706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9707 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
9708}
9709
Arthur Hung7c645402019-01-25 17:45:42 +08009710TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9711 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009712 prepareAxes(POSITION | ID | SLOT);
9713 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009714 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009715
9716 // Create the second touch screen device, and enable multi fingers.
9717 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009718 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009719 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009720 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009721 std::shared_ptr<InputDevice> device2 =
9722 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009723 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009724
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009725 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009726 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009727 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009728 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009729 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009730 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009731 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009732 /*flat=*/0, /*fuzz=*/0);
9733 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009734 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9735 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009736
9737 // Setup the second touch screen device.
Arpit Singha8c236b2023-04-25 13:56:05 +00009738 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
9739 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
9740 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009741 std::list<NotifyArgs> unused =
9742 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009743 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009744 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009745
9746 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009747 std::shared_ptr<FakePointerController> fakePointerController =
9748 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009749 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009750
9751 // Setup policy for associated displays and show touches.
9752 const uint8_t hdmi1 = 0;
9753 const uint8_t hdmi2 = 1;
9754 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9755 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9756 mFakePolicy->setShowTouches(true);
9757
9758 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009759 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009760 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009761
9762 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009763 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009764 InputReaderConfiguration::Change::DISPLAY_INFO |
9765 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009766
9767 // Two fingers down at default display.
9768 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9769 processPosition(mapper, x1, y1);
9770 processId(mapper, 1);
9771 processSlot(mapper, 1);
9772 processPosition(mapper, x2, y2);
9773 processId(mapper, 2);
9774 processSync(mapper);
9775
9776 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9777 fakePointerController->getSpots().find(DISPLAY_ID);
9778 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9779 ASSERT_EQ(size_t(2), iter->second.size());
9780
9781 // Two fingers down at second display.
9782 processPosition(mapper2, x1, y1);
9783 processId(mapper2, 1);
9784 processSlot(mapper2, 1);
9785 processPosition(mapper2, x2, y2);
9786 processId(mapper2, 2);
9787 processSync(mapper2);
9788
9789 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9790 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9791 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009792
9793 // Disable the show touches configuration and ensure the spots are cleared.
9794 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009795 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009796 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009797
9798 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009799}
9800
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009801TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009802 prepareAxes(POSITION);
9803 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009804 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009805 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009806
9807 NotifyMotionArgs motionArgs;
9808 // Unrotated video frame
9809 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9810 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009811 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009812 processPosition(mapper, 100, 200);
9813 processSync(mapper);
9814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9815 ASSERT_EQ(frames, motionArgs.videoFrames);
9816
9817 // Subsequent touch events should not have any videoframes
9818 // This is implemented separately in FakeEventHub,
9819 // but that should match the behaviour of TouchVideoDevice.
9820 processPosition(mapper, 200, 200);
9821 processSync(mapper);
9822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9823 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9824}
9825
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009826TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009827 prepareAxes(POSITION);
9828 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009829 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009830 // Unrotated video frame
9831 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9832 NotifyMotionArgs motionArgs;
9833
9834 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009835 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009836 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9837 clearViewports();
9838 prepareDisplay(orientation);
9839 std::vector<TouchVideoFrame> frames{frame};
9840 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9841 processPosition(mapper, 100, 200);
9842 processSync(mapper);
9843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9844 ASSERT_EQ(frames, motionArgs.videoFrames);
9845 }
9846}
9847
9848TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9849 prepareAxes(POSITION);
9850 addConfigurationProperty("touch.deviceType", "touchScreen");
9851 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9852 // orientation-aware are affected by display rotation.
9853 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009854 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009855 // Unrotated video frame
9856 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9857 NotifyMotionArgs motionArgs;
9858
9859 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009860 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009861 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9862 clearViewports();
9863 prepareDisplay(orientation);
9864 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009865 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009866 processPosition(mapper, 100, 200);
9867 processSync(mapper);
9868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009869 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9870 // compared to the display. This is so that when the window transform (which contains the
9871 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9872 // window's coordinate space.
9873 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009874 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009875
9876 // Release finger.
9877 processSync(mapper);
9878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009879 }
9880}
9881
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009882TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009883 prepareAxes(POSITION);
9884 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009885 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009886 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9887 // so mix these.
9888 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9889 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9890 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9891 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9892 NotifyMotionArgs motionArgs;
9893
Michael Wrighta9cf4192022-12-01 23:46:39 +00009894 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009895 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009896 processPosition(mapper, 100, 200);
9897 processSync(mapper);
9898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009899 ASSERT_EQ(frames, motionArgs.videoFrames);
9900}
9901
9902TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9903 prepareAxes(POSITION);
9904 addConfigurationProperty("touch.deviceType", "touchScreen");
9905 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9906 // orientation-aware are affected by display rotation.
9907 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009908 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009909 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9910 // so mix these.
9911 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9912 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9913 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9914 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9915 NotifyMotionArgs motionArgs;
9916
Michael Wrighta9cf4192022-12-01 23:46:39 +00009917 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009918 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9919 processPosition(mapper, 100, 200);
9920 processSync(mapper);
9921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9922 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9923 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9924 // compared to the display. This is so that when the window transform (which contains the
9925 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9926 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009927 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009928 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009929 ASSERT_EQ(frames, motionArgs.videoFrames);
9930}
9931
Arthur Hung9da14732019-09-02 16:16:58 +08009932/**
9933 * If we had defined port associations, but the viewport is not ready, the touch device would be
9934 * expected to be disabled, and it should be enabled after the viewport has found.
9935 */
9936TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009937 constexpr uint8_t hdmi2 = 1;
9938 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009939 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009940
9941 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9942
9943 addConfigurationProperty("touch.deviceType", "touchScreen");
9944 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009945 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009946
9947 ASSERT_EQ(mDevice->isEnabled(), false);
9948
9949 // Add display on hdmi2, the device should be enabled and can receive touch event.
9950 prepareSecondaryDisplay(type, hdmi2);
9951 ASSERT_EQ(mDevice->isEnabled(), true);
9952
9953 // Send a touch event.
9954 processPosition(mapper, 100, 100);
9955 processSync(mapper);
9956
9957 NotifyMotionArgs args;
9958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9959 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9960}
9961
Arthur Hung421eb1c2020-01-16 00:09:42 +08009962TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009963 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009964 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009965 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009966 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009967
9968 NotifyMotionArgs motionArgs;
9969
9970 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9971 // finger down
9972 processId(mapper, 1);
9973 processPosition(mapper, x1, y1);
9974 processSync(mapper);
9975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9976 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009977 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009978
9979 // finger move
9980 processId(mapper, 1);
9981 processPosition(mapper, x2, y2);
9982 processSync(mapper);
9983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9984 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009985 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009986
9987 // finger up.
9988 processId(mapper, -1);
9989 processSync(mapper);
9990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9991 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009992 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009993
9994 // new finger down
9995 processId(mapper, 1);
9996 processPosition(mapper, x3, y3);
9997 processSync(mapper);
9998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9999 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010000 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010001}
10002
10003/**
arthurhungcc7f9802020-04-30 17:55:40 +080010004 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
10005 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +080010006 */
arthurhungcc7f9802020-04-30 17:55:40 +080010007TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010008 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010009 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010010 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010011 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010012
10013 NotifyMotionArgs motionArgs;
10014
10015 // default tool type is finger
10016 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +080010017 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010018 processPosition(mapper, x1, y1);
10019 processSync(mapper);
10020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10021 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010022 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010023
10024 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
10025 processToolType(mapper, MT_TOOL_PALM);
10026 processSync(mapper);
10027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10028 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10029
10030 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +080010031 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010032 processPosition(mapper, x2, y2);
10033 processSync(mapper);
10034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10035
10036 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +080010037 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010038 processSync(mapper);
10039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10040
10041 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +080010042 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010043 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010044 processPosition(mapper, x3, y3);
10045 processSync(mapper);
10046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10047 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010048 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010049}
10050
arthurhungbf89a482020-04-17 17:37:55 +080010051/**
arthurhungcc7f9802020-04-30 17:55:40 +080010052 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10053 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +080010054 */
arthurhungcc7f9802020-04-30 17:55:40 +080010055TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +080010056 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010057 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +080010058 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010059 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +080010060
10061 NotifyMotionArgs motionArgs;
10062
10063 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +080010064 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10065 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010066 processPosition(mapper, x1, y1);
10067 processSync(mapper);
10068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10069 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010070 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +080010071
10072 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +080010073 processSlot(mapper, SECOND_SLOT);
10074 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010075 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +080010076 processSync(mapper);
10077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010078 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010079 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010080
10081 // If the tool type of the first finger changes to MT_TOOL_PALM,
10082 // we expect to receive ACTION_POINTER_UP with cancel flag.
10083 processSlot(mapper, FIRST_SLOT);
10084 processId(mapper, FIRST_TRACKING_ID);
10085 processToolType(mapper, MT_TOOL_PALM);
10086 processSync(mapper);
10087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010088 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010089 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10090
10091 // The following MOVE events of second finger should be processed.
10092 processSlot(mapper, SECOND_SLOT);
10093 processId(mapper, SECOND_TRACKING_ID);
10094 processPosition(mapper, x2 + 1, y2 + 1);
10095 processSync(mapper);
10096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10097 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010098 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010099
10100 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
10101 // it. Second finger receive move.
10102 processSlot(mapper, FIRST_SLOT);
10103 processId(mapper, INVALID_TRACKING_ID);
10104 processSync(mapper);
10105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010107 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010108
10109 // Second finger keeps moving.
10110 processSlot(mapper, SECOND_SLOT);
10111 processId(mapper, SECOND_TRACKING_ID);
10112 processPosition(mapper, x2 + 2, y2 + 2);
10113 processSync(mapper);
10114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010116 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010117
10118 // Second finger up.
10119 processId(mapper, INVALID_TRACKING_ID);
10120 processSync(mapper);
10121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10122 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10123 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10124}
10125
10126/**
10127 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
10128 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
10129 */
10130TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
10131 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010132 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010133 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010134 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010135
10136 NotifyMotionArgs motionArgs;
10137
10138 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10139 // First finger down.
10140 processId(mapper, FIRST_TRACKING_ID);
10141 processPosition(mapper, x1, y1);
10142 processSync(mapper);
10143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10144 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010145 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010146
10147 // Second finger down.
10148 processSlot(mapper, SECOND_SLOT);
10149 processId(mapper, SECOND_TRACKING_ID);
10150 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +080010151 processSync(mapper);
10152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010153 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010154 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +080010155
arthurhungcc7f9802020-04-30 17:55:40 +080010156 // If the tool type of the first finger changes to MT_TOOL_PALM,
10157 // we expect to receive ACTION_POINTER_UP with cancel flag.
10158 processSlot(mapper, FIRST_SLOT);
10159 processId(mapper, FIRST_TRACKING_ID);
10160 processToolType(mapper, MT_TOOL_PALM);
10161 processSync(mapper);
10162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010163 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010164 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10165
10166 // Second finger keeps moving.
10167 processSlot(mapper, SECOND_SLOT);
10168 processId(mapper, SECOND_TRACKING_ID);
10169 processPosition(mapper, x2 + 1, y2 + 1);
10170 processSync(mapper);
10171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10173
10174 // second finger becomes palm, receive cancel due to only 1 finger is active.
10175 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010176 processToolType(mapper, MT_TOOL_PALM);
10177 processSync(mapper);
10178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10179 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10180
arthurhungcc7f9802020-04-30 17:55:40 +080010181 // third finger down.
10182 processSlot(mapper, THIRD_SLOT);
10183 processId(mapper, THIRD_TRACKING_ID);
10184 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +080010185 processPosition(mapper, x3, y3);
10186 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +080010187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10188 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010189 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010190 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010191
10192 // third finger move
10193 processId(mapper, THIRD_TRACKING_ID);
10194 processPosition(mapper, x3 + 1, y3 + 1);
10195 processSync(mapper);
10196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10198
10199 // first finger up, third finger receive move.
10200 processSlot(mapper, FIRST_SLOT);
10201 processId(mapper, INVALID_TRACKING_ID);
10202 processSync(mapper);
10203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010205 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010206
10207 // second finger up, third finger receive move.
10208 processSlot(mapper, SECOND_SLOT);
10209 processId(mapper, INVALID_TRACKING_ID);
10210 processSync(mapper);
10211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010213 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010214
10215 // third finger up.
10216 processSlot(mapper, THIRD_SLOT);
10217 processId(mapper, INVALID_TRACKING_ID);
10218 processSync(mapper);
10219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10220 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10221 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10222}
10223
10224/**
10225 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10226 * and the active finger could still be allowed to receive the events
10227 */
10228TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10229 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010230 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010231 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010232 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010233
10234 NotifyMotionArgs motionArgs;
10235
10236 // default tool type is finger
10237 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10238 processId(mapper, FIRST_TRACKING_ID);
10239 processPosition(mapper, x1, y1);
10240 processSync(mapper);
10241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10242 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010243 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010244
10245 // Second finger down.
10246 processSlot(mapper, SECOND_SLOT);
10247 processId(mapper, SECOND_TRACKING_ID);
10248 processPosition(mapper, x2, y2);
10249 processSync(mapper);
10250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010251 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010252 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010253
10254 // If the tool type of the second finger changes to MT_TOOL_PALM,
10255 // we expect to receive ACTION_POINTER_UP with cancel flag.
10256 processId(mapper, SECOND_TRACKING_ID);
10257 processToolType(mapper, MT_TOOL_PALM);
10258 processSync(mapper);
10259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010260 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010261 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10262
10263 // The following MOVE event should be processed.
10264 processSlot(mapper, FIRST_SLOT);
10265 processId(mapper, FIRST_TRACKING_ID);
10266 processPosition(mapper, x1 + 1, y1 + 1);
10267 processSync(mapper);
10268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010270 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010271
10272 // second finger up.
10273 processSlot(mapper, SECOND_SLOT);
10274 processId(mapper, INVALID_TRACKING_ID);
10275 processSync(mapper);
10276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10278
10279 // first finger keep moving
10280 processSlot(mapper, FIRST_SLOT);
10281 processId(mapper, FIRST_TRACKING_ID);
10282 processPosition(mapper, x1 + 2, y1 + 2);
10283 processSync(mapper);
10284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10286
10287 // first finger up.
10288 processId(mapper, INVALID_TRACKING_ID);
10289 processSync(mapper);
10290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10291 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10292 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010293}
10294
Arthur Hung9ad18942021-06-19 02:04:46 +000010295/**
10296 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10297 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10298 * cause slot be valid again.
10299 */
10300TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10301 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010302 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +000010303 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010304 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +000010305
10306 NotifyMotionArgs motionArgs;
10307
10308 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10309 // First finger down.
10310 processId(mapper, FIRST_TRACKING_ID);
10311 processPosition(mapper, x1, y1);
10312 processPressure(mapper, RAW_PRESSURE_MAX);
10313 processSync(mapper);
10314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10315 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010316 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010317
10318 // First finger move.
10319 processId(mapper, FIRST_TRACKING_ID);
10320 processPosition(mapper, x1 + 1, y1 + 1);
10321 processPressure(mapper, RAW_PRESSURE_MAX);
10322 processSync(mapper);
10323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10324 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010325 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010326
10327 // Second finger down.
10328 processSlot(mapper, SECOND_SLOT);
10329 processId(mapper, SECOND_TRACKING_ID);
10330 processPosition(mapper, x2, y2);
10331 processPressure(mapper, RAW_PRESSURE_MAX);
10332 processSync(mapper);
10333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010334 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010335 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010336
10337 // second finger up with some unexpected data.
10338 processSlot(mapper, SECOND_SLOT);
10339 processId(mapper, INVALID_TRACKING_ID);
10340 processPosition(mapper, x2, y2);
10341 processSync(mapper);
10342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010343 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010344 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010345
10346 // first finger up with some unexpected data.
10347 processSlot(mapper, FIRST_SLOT);
10348 processId(mapper, INVALID_TRACKING_ID);
10349 processPosition(mapper, x2, y2);
10350 processPressure(mapper, RAW_PRESSURE_MAX);
10351 processSync(mapper);
10352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10353 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010354 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010355}
10356
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010357TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10358 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010359 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010360 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010361 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010362
10363 // First finger down.
10364 processId(mapper, FIRST_TRACKING_ID);
10365 processPosition(mapper, 100, 200);
10366 processPressure(mapper, RAW_PRESSURE_MAX);
10367 processSync(mapper);
10368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10369 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10370
10371 // Second finger down.
10372 processSlot(mapper, SECOND_SLOT);
10373 processId(mapper, SECOND_TRACKING_ID);
10374 processPosition(mapper, 300, 400);
10375 processPressure(mapper, RAW_PRESSURE_MAX);
10376 processSync(mapper);
10377 ASSERT_NO_FATAL_FAILURE(
10378 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10379
10380 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010381 // preserved. Resetting should cancel the ongoing gesture.
10382 resetMapper(mapper, ARBITRARY_TIME);
10383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10384 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010385
10386 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10387 // the existing touch state to generate a down event.
10388 processPosition(mapper, 301, 302);
10389 processSync(mapper);
10390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10391 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10393 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10394
10395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10396}
10397
10398TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10399 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010400 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010401 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010402 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010403
10404 // First finger touches down and releases.
10405 processId(mapper, FIRST_TRACKING_ID);
10406 processPosition(mapper, 100, 200);
10407 processPressure(mapper, RAW_PRESSURE_MAX);
10408 processSync(mapper);
10409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10410 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10411 processId(mapper, INVALID_TRACKING_ID);
10412 processSync(mapper);
10413 ASSERT_NO_FATAL_FAILURE(
10414 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10415
10416 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10417 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010418 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10420
10421 // Send an empty sync frame. Since there are no pointers, no events are generated.
10422 processSync(mapper);
10423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10424}
10425
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010426TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010427 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010428 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010429 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010430 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010432
10433 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10434 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10435 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10436 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10437 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10438
10439 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010440 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010441 processId(mapper, FIRST_TRACKING_ID);
10442 processToolType(mapper, MT_TOOL_PEN);
10443 processPosition(mapper, 100, 200);
10444 processPressure(mapper, RAW_PRESSURE_MAX);
10445 processSync(mapper);
10446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10447 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10448 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010449 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010450
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010451 // Now that we know the device supports styluses, ensure that the device is re-configured with
10452 // the stylus source.
10453 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10454 {
10455 const auto& devices = mReader->getInputDevices();
10456 auto deviceInfo =
10457 std::find_if(devices.begin(), devices.end(),
10458 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10459 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10460 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10461 }
10462
10463 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10465
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010466 processId(mapper, INVALID_TRACKING_ID);
10467 processSync(mapper);
10468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10469 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10470 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010471 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010472}
10473
Seunghwan Choi356026c2023-02-01 14:37:25 +090010474TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10475 addConfigurationProperty("touch.deviceType", "touchScreen");
10476 prepareDisplay(ui::ROTATION_0);
10477 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10478 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10479 // indicate stylus presence dynamically.
10480 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10481 std::shared_ptr<FakePointerController> fakePointerController =
10482 std::make_shared<FakePointerController>();
10483 mFakePolicy->setPointerController(fakePointerController);
10484 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +000010485 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010486
10487 processId(mapper, FIRST_TRACKING_ID);
10488 processPressure(mapper, RAW_PRESSURE_MIN);
10489 processPosition(mapper, 100, 200);
10490 processToolType(mapper, MT_TOOL_PEN);
10491 processSync(mapper);
10492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10493 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010494 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010495 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10496 ASSERT_TRUE(fakePointerController->isPointerShown());
10497 ASSERT_NO_FATAL_FAILURE(
10498 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10499}
10500
10501TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10502 addConfigurationProperty("touch.deviceType", "touchScreen");
10503 prepareDisplay(ui::ROTATION_0);
10504 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10505 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10506 // indicate stylus presence dynamically.
10507 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10508 std::shared_ptr<FakePointerController> fakePointerController =
10509 std::make_shared<FakePointerController>();
10510 mFakePolicy->setPointerController(fakePointerController);
10511 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010512 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010513
10514 processId(mapper, FIRST_TRACKING_ID);
10515 processPressure(mapper, RAW_PRESSURE_MIN);
10516 processPosition(mapper, 100, 200);
10517 processToolType(mapper, MT_TOOL_PEN);
10518 processSync(mapper);
10519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10520 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010521 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010522 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10523 ASSERT_FALSE(fakePointerController->isPointerShown());
10524}
10525
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010526// --- MultiTouchInputMapperTest_ExternalDevice ---
10527
10528class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10529protected:
Chris Yea52ade12020-08-27 16:49:20 -070010530 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010531};
10532
10533/**
10534 * Expect fallback to internal viewport if device is external and external viewport is not present.
10535 */
10536TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10537 prepareAxes(POSITION);
10538 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010539 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010540 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010541
10542 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10543
10544 NotifyMotionArgs motionArgs;
10545
10546 // Expect the event to be sent to the internal viewport,
10547 // because an external viewport is not present.
10548 processPosition(mapper, 100, 100);
10549 processSync(mapper);
10550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10551 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10552
10553 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010554 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010555 processPosition(mapper, 100, 100);
10556 processSync(mapper);
10557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10558 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10559}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010560
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010561TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10562 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10563 std::shared_ptr<FakePointerController> fakePointerController =
10564 std::make_shared<FakePointerController>();
10565 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10566 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010567
10568 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010569 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010570 prepareAxes(POSITION | ID | SLOT);
10571 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10572 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10573 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010574 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010575 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010576
10577 // captured touchpad should be a touchpad source
10578 NotifyDeviceResetArgs resetArgs;
10579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10580 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10581
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010582 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010583
10584 const InputDeviceInfo::MotionRange* relRangeX =
10585 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10586 ASSERT_NE(relRangeX, nullptr);
10587 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10588 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10589 const InputDeviceInfo::MotionRange* relRangeY =
10590 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10591 ASSERT_NE(relRangeY, nullptr);
10592 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10593 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10594
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010595 // run captured pointer tests - note that this is unscaled, so input listener events should be
10596 // identical to what the hardware sends (accounting for any
10597 // calibration).
10598 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010599 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010600 processId(mapper, 1);
10601 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10602 processKey(mapper, BTN_TOUCH, 1);
10603 processSync(mapper);
10604
10605 // expect coord[0] to contain initial location of touch 0
10606 NotifyMotionArgs args;
10607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10608 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010609 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010610 ASSERT_EQ(0, args.pointerProperties[0].id);
10611 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10612 ASSERT_NO_FATAL_FAILURE(
10613 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10614
10615 // FINGER 1 DOWN
10616 processSlot(mapper, 1);
10617 processId(mapper, 2);
10618 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10619 processSync(mapper);
10620
10621 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010623 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010624 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010625 ASSERT_EQ(0, args.pointerProperties[0].id);
10626 ASSERT_EQ(1, args.pointerProperties[1].id);
10627 ASSERT_NO_FATAL_FAILURE(
10628 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10629 ASSERT_NO_FATAL_FAILURE(
10630 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10631
10632 // FINGER 1 MOVE
10633 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10634 processSync(mapper);
10635
10636 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10637 // from move
10638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10639 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10640 ASSERT_NO_FATAL_FAILURE(
10641 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10642 ASSERT_NO_FATAL_FAILURE(
10643 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10644
10645 // FINGER 0 MOVE
10646 processSlot(mapper, 0);
10647 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10648 processSync(mapper);
10649
10650 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10652 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10653 ASSERT_NO_FATAL_FAILURE(
10654 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10655 ASSERT_NO_FATAL_FAILURE(
10656 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10657
10658 // BUTTON DOWN
10659 processKey(mapper, BTN_LEFT, 1);
10660 processSync(mapper);
10661
10662 // touchinputmapper design sends a move before button press
10663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10664 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10666 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10667
10668 // BUTTON UP
10669 processKey(mapper, BTN_LEFT, 0);
10670 processSync(mapper);
10671
10672 // touchinputmapper design sends a move after button release
10673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10674 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10676 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10677
10678 // FINGER 0 UP
10679 processId(mapper, -1);
10680 processSync(mapper);
10681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10682 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10683
10684 // FINGER 1 MOVE
10685 processSlot(mapper, 1);
10686 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10687 processSync(mapper);
10688
10689 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10691 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010692 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010693 ASSERT_EQ(1, args.pointerProperties[0].id);
10694 ASSERT_NO_FATAL_FAILURE(
10695 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10696
10697 // FINGER 1 UP
10698 processId(mapper, -1);
10699 processKey(mapper, BTN_TOUCH, 0);
10700 processSync(mapper);
10701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10702 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10703
Josep del Río2d8c79a2023-01-23 19:33:50 +000010704 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010705 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010706 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010708 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010709}
10710
10711TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10712 std::shared_ptr<FakePointerController> fakePointerController =
10713 std::make_shared<FakePointerController>();
10714 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10715 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010716
10717 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010718 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010719 prepareAxes(POSITION | ID | SLOT);
10720 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10721 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010722 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010723 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010724 // run uncaptured pointer tests - pushes out generic events
10725 // FINGER 0 DOWN
10726 processId(mapper, 3);
10727 processPosition(mapper, 100, 100);
10728 processKey(mapper, BTN_TOUCH, 1);
10729 processSync(mapper);
10730
10731 // start at (100,100), cursor should be at (0,0) * scale
10732 NotifyMotionArgs args;
10733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10734 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10735 ASSERT_NO_FATAL_FAILURE(
10736 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10737
10738 // FINGER 0 MOVE
10739 processPosition(mapper, 200, 200);
10740 processSync(mapper);
10741
10742 // compute scaling to help with touch position checking
10743 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10744 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10745 float scale =
10746 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10747
10748 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10750 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10752 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010753
10754 // BUTTON DOWN
10755 processKey(mapper, BTN_LEFT, 1);
10756 processSync(mapper);
10757
10758 // touchinputmapper design sends a move before button press
10759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10760 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10762 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10763
10764 // BUTTON UP
10765 processKey(mapper, BTN_LEFT, 0);
10766 processSync(mapper);
10767
10768 // touchinputmapper design sends a move after button release
10769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10770 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10772 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010773}
10774
10775TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10776 std::shared_ptr<FakePointerController> fakePointerController =
10777 std::make_shared<FakePointerController>();
10778
Michael Wrighta9cf4192022-12-01 23:46:39 +000010779 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010780 prepareAxes(POSITION | ID | SLOT);
10781 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010782 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010783 mFakePolicy->setPointerCapture(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010784 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010785
Josep del Río2d8c79a2023-01-23 19:33:50 +000010786 // uncaptured touchpad should be a pointer device
10787 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010788
Josep del Río2d8c79a2023-01-23 19:33:50 +000010789 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010790 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010791 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010792 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10793}
10794
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010795// --- BluetoothMultiTouchInputMapperTest ---
10796
10797class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10798protected:
10799 void SetUp() override {
10800 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10801 }
10802};
10803
10804TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10805 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010806 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010807 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010808 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010809
10810 nsecs_t kernelEventTime = ARBITRARY_TIME;
10811 nsecs_t expectedEventTime = ARBITRARY_TIME;
10812 // Touch down.
10813 processId(mapper, FIRST_TRACKING_ID);
10814 processPosition(mapper, 100, 200);
10815 processPressure(mapper, RAW_PRESSURE_MAX);
10816 processSync(mapper, ARBITRARY_TIME);
10817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10818 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10819
10820 // Process several events that come in quick succession, according to their timestamps.
10821 for (int i = 0; i < 3; i++) {
10822 constexpr static nsecs_t delta = ms2ns(1);
10823 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10824 kernelEventTime += delta;
10825 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10826
10827 processPosition(mapper, 101 + i, 201 + i);
10828 processSync(mapper, kernelEventTime);
10829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10830 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10831 WithEventTime(expectedEventTime))));
10832 }
10833
10834 // Release the touch.
10835 processId(mapper, INVALID_TRACKING_ID);
10836 processPressure(mapper, RAW_PRESSURE_MIN);
10837 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10839 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10840 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10841}
10842
10843// --- MultiTouchPointerModeTest ---
10844
HQ Liue6983c72022-04-19 22:14:56 +000010845class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10846protected:
10847 float mPointerMovementScale;
10848 float mPointerXZoomScale;
10849 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10850 addConfigurationProperty("touch.deviceType", "pointer");
10851 std::shared_ptr<FakePointerController> fakePointerController =
10852 std::make_shared<FakePointerController>();
10853 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10854 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010855 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010856
10857 prepareAxes(POSITION);
10858 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10859 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10860 // needs to be disabled, and the pointer gesture needs to be enabled.
10861 mFakePolicy->setPointerCapture(false);
10862 mFakePolicy->setPointerGestureEnabled(true);
10863 mFakePolicy->setPointerController(fakePointerController);
10864
10865 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10866 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10867 mPointerMovementScale =
10868 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10869 mPointerXZoomScale =
10870 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10871 }
10872
10873 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10874 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10875 /*flat*/ 0,
10876 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10877 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10878 /*flat*/ 0,
10879 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10880 }
10881};
10882
10883/**
10884 * Two fingers down on a pointer mode touch pad. The width
10885 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10886 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10887 * be greater than the both value to be freeform gesture, so that after two
10888 * fingers start to move downwards, the gesture should be swipe.
10889 */
10890TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10891 // The min freeform gesture width is 25units/mm x 30mm = 750
10892 // which is greater than fraction of the diagnal length of the touchpad (349).
10893 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010894 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010895 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010896 NotifyMotionArgs motionArgs;
10897
10898 // Two fingers down at once.
10899 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10900 // Pointer's initial position is used the [0,0] coordinate.
10901 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10902
10903 processId(mapper, FIRST_TRACKING_ID);
10904 processPosition(mapper, x1, y1);
10905 processMTSync(mapper);
10906 processId(mapper, SECOND_TRACKING_ID);
10907 processPosition(mapper, x2, y2);
10908 processMTSync(mapper);
10909 processSync(mapper);
10910
10911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010912 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010913 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010914 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010915 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010916 ASSERT_NO_FATAL_FAILURE(
10917 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10918
10919 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10920 // that there should be 1 pointer.
10921 int32_t movingDistance = 200;
10922 y1 += movingDistance;
10923 y2 += movingDistance;
10924
10925 processId(mapper, FIRST_TRACKING_ID);
10926 processPosition(mapper, x1, y1);
10927 processMTSync(mapper);
10928 processId(mapper, SECOND_TRACKING_ID);
10929 processPosition(mapper, x2, y2);
10930 processMTSync(mapper);
10931 processSync(mapper);
10932
10933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010934 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010935 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010936 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010937 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10939 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10940 0, 0, 0, 0));
10941}
10942
10943/**
10944 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10945 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10946 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10947 * value to be freeform gesture, so that after two fingers start to move downwards,
10948 * the gesture should be swipe.
10949 */
10950TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10951 // The min freeform gesture width is 5units/mm x 30mm = 150
10952 // which is greater than fraction of the diagnal length of the touchpad (349).
10953 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010954 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +000010955 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010956 NotifyMotionArgs motionArgs;
10957
10958 // Two fingers down at once.
10959 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10960 // Pointer's initial position is used the [0,0] coordinate.
10961 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10962
10963 processId(mapper, FIRST_TRACKING_ID);
10964 processPosition(mapper, x1, y1);
10965 processMTSync(mapper);
10966 processId(mapper, SECOND_TRACKING_ID);
10967 processPosition(mapper, x2, y2);
10968 processMTSync(mapper);
10969 processSync(mapper);
10970
10971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010972 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010973 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010974 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010975 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010976 ASSERT_NO_FATAL_FAILURE(
10977 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10978
10979 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10980 // and there should be 1 pointer.
10981 int32_t movingDistance = 200;
10982 y1 += movingDistance;
10983 y2 += movingDistance;
10984
10985 processId(mapper, FIRST_TRACKING_ID);
10986 processPosition(mapper, x1, y1);
10987 processMTSync(mapper);
10988 processId(mapper, SECOND_TRACKING_ID);
10989 processPosition(mapper, x2, y2);
10990 processMTSync(mapper);
10991 processSync(mapper);
10992
10993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010994 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010996 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010997 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010998 // New coordinate is the scaled relative coordinate from the initial coordinate.
10999 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
11000 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11001 0, 0, 0, 0));
11002}
11003
11004/**
11005 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
11006 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
11007 * freeform gestures after two fingers start to move downwards.
11008 */
11009TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000011010 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000011011 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000011012
11013 NotifyMotionArgs motionArgs;
11014
11015 // Two fingers down at once. Wider than the max swipe width.
11016 // The gesture is expected to be PRESS, then transformed to FREEFORM
11017 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
11018
11019 processId(mapper, FIRST_TRACKING_ID);
11020 processPosition(mapper, x1, y1);
11021 processMTSync(mapper);
11022 processId(mapper, SECOND_TRACKING_ID);
11023 processPosition(mapper, x2, y2);
11024 processMTSync(mapper);
11025 processSync(mapper);
11026
11027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011028 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011029 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011030 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011031 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011032 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
11033 ASSERT_NO_FATAL_FAILURE(
11034 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11035
11036 int32_t movingDistance = 200;
11037
11038 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
11039 // then two down events for two pointers.
11040 y1 += movingDistance;
11041 y2 += movingDistance;
11042
11043 processId(mapper, FIRST_TRACKING_ID);
11044 processPosition(mapper, x1, y1);
11045 processMTSync(mapper);
11046 processId(mapper, SECOND_TRACKING_ID);
11047 processPosition(mapper, x2, y2);
11048 processMTSync(mapper);
11049 processSync(mapper);
11050
11051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11052 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011053 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011054 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
11055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011056 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011057 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011058 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011060 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011061 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011062 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011063 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011064 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011065 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011066 // Two pointers' scaled relative coordinates from their initial centroid.
11067 // Initial y coordinates are 0 as y1 and y2 have the same value.
11068 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
11069 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
11070 // When pointers move, the new coordinates equal to the initial coordinates plus
11071 // scaled moving distance.
11072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11073 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11074 0, 0, 0, 0));
11075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11076 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11077 0, 0, 0, 0));
11078
11079 // Move two fingers down again, expect one MOVE motion event.
11080 y1 += movingDistance;
11081 y2 += movingDistance;
11082
11083 processId(mapper, FIRST_TRACKING_ID);
11084 processPosition(mapper, x1, y1);
11085 processMTSync(mapper);
11086 processId(mapper, SECOND_TRACKING_ID);
11087 processPosition(mapper, x2, y2);
11088 processMTSync(mapper);
11089 processSync(mapper);
11090
11091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011092 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011093 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011094 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011095 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011096 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11097 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11098 0, 0, 0, 0, 0));
11099 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11100 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11101 0, 0, 0, 0, 0));
11102}
11103
Harry Cutts39b7ca22022-10-05 15:55:48 +000011104TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000011105 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000011106 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000011107 NotifyMotionArgs motionArgs;
11108
11109 // Place two fingers down.
11110 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
11111
11112 processId(mapper, FIRST_TRACKING_ID);
11113 processPosition(mapper, x1, y1);
11114 processMTSync(mapper);
11115 processId(mapper, SECOND_TRACKING_ID);
11116 processPosition(mapper, x2, y2);
11117 processMTSync(mapper);
11118 processSync(mapper);
11119
11120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011121 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000011122 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11123 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
11124 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
11125 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
11126
11127 // Move the two fingers down and to the left.
11128 int32_t movingDistance = 200;
11129 x1 -= movingDistance;
11130 y1 += movingDistance;
11131 x2 -= movingDistance;
11132 y2 += movingDistance;
11133
11134 processId(mapper, FIRST_TRACKING_ID);
11135 processPosition(mapper, x1, y1);
11136 processMTSync(mapper);
11137 processId(mapper, SECOND_TRACKING_ID);
11138 processPosition(mapper, x2, y2);
11139 processMTSync(mapper);
11140 processSync(mapper);
11141
11142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011143 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000011144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11145 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
11146 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
11147 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
11148}
11149
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011150TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000011151 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011152 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000011153 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
11155
11156 // Start a stylus gesture.
11157 processKey(mapper, BTN_TOOL_PEN, 1);
11158 processId(mapper, FIRST_TRACKING_ID);
11159 processPosition(mapper, 100, 200);
11160 processSync(mapper);
11161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11162 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
11163 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011164 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011165 // TODO(b/257078296): Pointer mode generates extra event.
11166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11167 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11168 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011169 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11171
11172 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
11173 // gesture should be disabled.
11174 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
11175 viewport->isActive = false;
11176 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000011177 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11179 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11180 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011181 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011182 // TODO(b/257078296): Pointer mode generates extra event.
11183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11184 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11185 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011186 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11188}
11189
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011190// --- JoystickInputMapperTest ---
11191
11192class JoystickInputMapperTest : public InputMapperTest {
11193protected:
11194 static const int32_t RAW_X_MIN;
11195 static const int32_t RAW_X_MAX;
11196 static const int32_t RAW_Y_MIN;
11197 static const int32_t RAW_Y_MAX;
11198
11199 void SetUp() override {
11200 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
11201 }
11202 void prepareAxes() {
11203 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
11204 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
11205 }
11206
11207 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
11208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
11209 }
11210
11211 void processSync(JoystickInputMapper& mapper) {
11212 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
11213 }
11214
Michael Wrighta9cf4192022-12-01 23:46:39 +000011215 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011216 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
11217 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
11218 NO_PORT, ViewportType::VIRTUAL);
11219 }
11220};
11221
11222const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
11223const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
11224const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11225const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11226
11227TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11228 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000011229 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011230
11231 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11232
Michael Wrighta9cf4192022-12-01 23:46:39 +000011233 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011234
11235 // Send an axis event
11236 processAxis(mapper, ABS_X, 100);
11237 processSync(mapper);
11238
11239 NotifyMotionArgs args;
11240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11241 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11242
11243 // Send another axis event
11244 processAxis(mapper, ABS_Y, 100);
11245 processSync(mapper);
11246
11247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11248 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11249}
11250
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011251// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011252
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011253class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011254protected:
11255 static const char* DEVICE_NAME;
11256 static const char* DEVICE_LOCATION;
11257 static const int32_t DEVICE_ID;
11258 static const int32_t DEVICE_GENERATION;
11259 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011260 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011261 static const int32_t EVENTHUB_ID;
11262
11263 std::shared_ptr<FakeEventHub> mFakeEventHub;
11264 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011265 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011266 std::unique_ptr<InstrumentedInputReader> mReader;
11267 std::shared_ptr<InputDevice> mDevice;
11268
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011269 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011270 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011271 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011272 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011273 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011274 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011275 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11276 }
11277
11278 void SetUp() override { SetUp(DEVICE_CLASSES); }
11279
11280 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011281 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011282 mFakePolicy.clear();
11283 }
11284
Chris Yee2b1e5c2021-03-10 22:45:12 -080011285 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11286 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011287 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011288 InputDeviceIdentifier identifier;
11289 identifier.name = name;
11290 identifier.location = location;
11291 std::shared_ptr<InputDevice> device =
11292 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11293 identifier);
11294 mReader->pushNextDevice(device);
11295 mFakeEventHub->addDevice(eventHubId, name, classes);
11296 mReader->loopOnce();
11297 return device;
11298 }
11299
11300 template <class T, typename... Args>
11301 T& addControllerAndConfigure(Args... args) {
11302 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11303
11304 return controller;
11305 }
11306};
11307
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011308const char* PeripheralControllerTest::DEVICE_NAME = "device";
11309const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11310const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11311const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11312const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011313const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11314 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011315const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011316
11317// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011318class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011319protected:
11320 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011321 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011322 }
11323};
11324
11325TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011326 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011327
Harry Cuttsa5b71292022-11-28 12:56:17 +000011328 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11329 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11330 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011331}
11332
11333TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011334 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011335
Harry Cuttsa5b71292022-11-28 12:56:17 +000011336 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11337 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11338 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011339}
11340
11341// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011342class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011343protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011344 void SetUp() override {
11345 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11346 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011347};
11348
Chris Ye85758332021-05-16 23:05:17 -070011349TEST_F(LightControllerTest, MonoLight) {
11350 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011351 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011352 .maxBrightness = 255,
11353 .flags = InputLightClass::BRIGHTNESS,
11354 .path = ""};
11355 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011356
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011357 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011358 InputDeviceInfo info;
11359 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011360 std::vector<InputDeviceLightInfo> lights = info.getLights();
11361 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011362 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11363 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11364
11365 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11366 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11367}
11368
11369TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11370 RawLightInfo infoMono = {.id = 1,
11371 .name = "mono_keyboard_backlight",
11372 .maxBrightness = 255,
11373 .flags = InputLightClass::BRIGHTNESS |
11374 InputLightClass::KEYBOARD_BACKLIGHT,
11375 .path = ""};
11376 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11377
11378 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11379 InputDeviceInfo info;
11380 controller.populateDeviceInfo(&info);
11381 std::vector<InputDeviceLightInfo> lights = info.getLights();
11382 ASSERT_EQ(1U, lights.size());
11383 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11384 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011385
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011386 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11387 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011388}
11389
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000011390TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
11391 RawLightInfo infoMono = {.id = 1,
11392 .name = "mono_light",
11393 .maxBrightness = 255,
11394 .flags = InputLightClass::BRIGHTNESS,
11395 .path = ""};
11396 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11397 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11398 "0,100,200");
11399
11400 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11401 std::list<NotifyArgs> unused =
11402 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11403 /*changes=*/{});
11404
11405 InputDeviceInfo info;
11406 controller.populateDeviceInfo(&info);
11407 std::vector<InputDeviceLightInfo> lights = info.getLights();
11408 ASSERT_EQ(1U, lights.size());
11409 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11410}
11411
11412TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
11413 RawLightInfo infoMono = {.id = 1,
11414 .name = "mono_keyboard_backlight",
11415 .maxBrightness = 255,
11416 .flags = InputLightClass::BRIGHTNESS |
11417 InputLightClass::KEYBOARD_BACKLIGHT,
11418 .path = ""};
11419 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11420
11421 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11422 std::list<NotifyArgs> unused =
11423 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11424 /*changes=*/{});
11425
11426 InputDeviceInfo info;
11427 controller.populateDeviceInfo(&info);
11428 std::vector<InputDeviceLightInfo> lights = info.getLights();
11429 ASSERT_EQ(1U, lights.size());
11430 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11431}
11432
11433TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
11434 RawLightInfo infoMono = {.id = 1,
11435 .name = "mono_keyboard_backlight",
11436 .maxBrightness = 255,
11437 .flags = InputLightClass::BRIGHTNESS |
11438 InputLightClass::KEYBOARD_BACKLIGHT,
11439 .path = ""};
11440 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11441 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11442 "0,100,200");
11443
11444 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11445 std::list<NotifyArgs> unused =
11446 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11447 /*changes=*/{});
11448
11449 InputDeviceInfo info;
11450 controller.populateDeviceInfo(&info);
11451 std::vector<InputDeviceLightInfo> lights = info.getLights();
11452 ASSERT_EQ(1U, lights.size());
11453 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
11454 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
11455 ASSERT_EQ(BrightnessLevel(0), *it);
11456 std::advance(it, 1);
11457 ASSERT_EQ(BrightnessLevel(100), *it);
11458 std::advance(it, 1);
11459 ASSERT_EQ(BrightnessLevel(200), *it);
11460}
11461
11462TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
11463 RawLightInfo infoMono = {.id = 1,
11464 .name = "mono_keyboard_backlight",
11465 .maxBrightness = 255,
11466 .flags = InputLightClass::BRIGHTNESS |
11467 InputLightClass::KEYBOARD_BACKLIGHT,
11468 .path = ""};
11469 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11470 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11471 "0,100,200,300,400,500");
11472
11473 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11474 std::list<NotifyArgs> unused =
11475 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11476 /*changes=*/{});
11477
11478 InputDeviceInfo info;
11479 controller.populateDeviceInfo(&info);
11480 std::vector<InputDeviceLightInfo> lights = info.getLights();
11481 ASSERT_EQ(1U, lights.size());
11482 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11483}
11484
Chris Yee2b1e5c2021-03-10 22:45:12 -080011485TEST_F(LightControllerTest, RGBLight) {
11486 RawLightInfo infoRed = {.id = 1,
11487 .name = "red",
11488 .maxBrightness = 255,
11489 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11490 .path = ""};
11491 RawLightInfo infoGreen = {.id = 2,
11492 .name = "green",
11493 .maxBrightness = 255,
11494 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11495 .path = ""};
11496 RawLightInfo infoBlue = {.id = 3,
11497 .name = "blue",
11498 .maxBrightness = 255,
11499 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11500 .path = ""};
11501 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11502 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11503 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11504
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011505 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011506 InputDeviceInfo info;
11507 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011508 std::vector<InputDeviceLightInfo> lights = info.getLights();
11509 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011510 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11511 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11512 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11513
11514 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11515 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11516}
11517
11518TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11519 RawLightInfo infoRed = {.id = 1,
11520 .name = "red_keyboard_backlight",
11521 .maxBrightness = 255,
11522 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11523 InputLightClass::KEYBOARD_BACKLIGHT,
11524 .path = ""};
11525 RawLightInfo infoGreen = {.id = 2,
11526 .name = "green_keyboard_backlight",
11527 .maxBrightness = 255,
11528 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11529 InputLightClass::KEYBOARD_BACKLIGHT,
11530 .path = ""};
11531 RawLightInfo infoBlue = {.id = 3,
11532 .name = "blue_keyboard_backlight",
11533 .maxBrightness = 255,
11534 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11535 InputLightClass::KEYBOARD_BACKLIGHT,
11536 .path = ""};
11537 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11538 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11539 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11540
11541 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11542 InputDeviceInfo info;
11543 controller.populateDeviceInfo(&info);
11544 std::vector<InputDeviceLightInfo> lights = info.getLights();
11545 ASSERT_EQ(1U, lights.size());
11546 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11547 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11548 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11549
11550 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11551 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11552}
11553
11554TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11555 RawLightInfo infoRed = {.id = 1,
11556 .name = "red",
11557 .maxBrightness = 255,
11558 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11559 .path = ""};
11560 RawLightInfo infoGreen = {.id = 2,
11561 .name = "green",
11562 .maxBrightness = 255,
11563 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11564 .path = ""};
11565 RawLightInfo infoBlue = {.id = 3,
11566 .name = "blue",
11567 .maxBrightness = 255,
11568 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11569 .path = ""};
11570 RawLightInfo infoGlobal = {.id = 3,
11571 .name = "global_keyboard_backlight",
11572 .maxBrightness = 255,
11573 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11574 InputLightClass::KEYBOARD_BACKLIGHT,
11575 .path = ""};
11576 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11577 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11578 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11579 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11580
11581 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11582 InputDeviceInfo info;
11583 controller.populateDeviceInfo(&info);
11584 std::vector<InputDeviceLightInfo> lights = info.getLights();
11585 ASSERT_EQ(1U, lights.size());
11586 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11587 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11588 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011589
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011590 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11591 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011592}
11593
11594TEST_F(LightControllerTest, MultiColorRGBLight) {
11595 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011596 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011597 .maxBrightness = 255,
11598 .flags = InputLightClass::BRIGHTNESS |
11599 InputLightClass::MULTI_INTENSITY |
11600 InputLightClass::MULTI_INDEX,
11601 .path = ""};
11602
11603 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11604
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011605 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011606 InputDeviceInfo info;
11607 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011608 std::vector<InputDeviceLightInfo> lights = info.getLights();
11609 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011610 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11611 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11612 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11613
11614 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11615 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11616}
11617
11618TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11619 RawLightInfo infoColor = {.id = 1,
11620 .name = "multi_color_keyboard_backlight",
11621 .maxBrightness = 255,
11622 .flags = InputLightClass::BRIGHTNESS |
11623 InputLightClass::MULTI_INTENSITY |
11624 InputLightClass::MULTI_INDEX |
11625 InputLightClass::KEYBOARD_BACKLIGHT,
11626 .path = ""};
11627
11628 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11629
11630 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11631 InputDeviceInfo info;
11632 controller.populateDeviceInfo(&info);
11633 std::vector<InputDeviceLightInfo> lights = info.getLights();
11634 ASSERT_EQ(1U, lights.size());
11635 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11636 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11637 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011638
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011639 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11640 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011641}
11642
11643TEST_F(LightControllerTest, PlayerIdLight) {
11644 RawLightInfo info1 = {.id = 1,
11645 .name = "player1",
11646 .maxBrightness = 255,
11647 .flags = InputLightClass::BRIGHTNESS,
11648 .path = ""};
11649 RawLightInfo info2 = {.id = 2,
11650 .name = "player2",
11651 .maxBrightness = 255,
11652 .flags = InputLightClass::BRIGHTNESS,
11653 .path = ""};
11654 RawLightInfo info3 = {.id = 3,
11655 .name = "player3",
11656 .maxBrightness = 255,
11657 .flags = InputLightClass::BRIGHTNESS,
11658 .path = ""};
11659 RawLightInfo info4 = {.id = 4,
11660 .name = "player4",
11661 .maxBrightness = 255,
11662 .flags = InputLightClass::BRIGHTNESS,
11663 .path = ""};
11664 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11665 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11666 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11667 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11668
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011669 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011670 InputDeviceInfo info;
11671 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011672 std::vector<InputDeviceLightInfo> lights = info.getLights();
11673 ASSERT_EQ(1U, lights.size());
11674 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011675 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11676 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011677
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011678 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11679 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11680 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011681}
11682
Michael Wrightd02c5b62014-02-10 15:10:22 -080011683} // namespace android