blob: 1b1987095ea5dd1a09c12c550f8abeb1d37c7387 [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
Arpit Singh440bf652023-08-09 09:23:43 +00001832TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00001833 const Point centerPoint = mDevice->getCenterPoint();
1834
1835 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1836 mDevice->sendSlot(FIRST_SLOT);
1837 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1838 mDevice->sendToolType(MT_TOOL_PEN);
1839 mDevice->sendDown(centerPoint);
1840 mDevice->sendSync();
1841 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1842 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001843 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001844
1845 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1846
1847 // Release the stylus touch.
1848 mDevice->sendUp();
1849 mDevice->sendSync();
1850 ASSERT_NO_FATAL_FAILURE(
1851 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1852
1853 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1854
1855 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1856 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1857 mDevice->sendToolType(MT_TOOL_FINGER);
1858 mDevice->sendDown(centerPoint);
1859 mDevice->sendSync();
1860 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1861 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001862 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001863
1864 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1865
1866 mDevice->sendUp();
1867 mDevice->sendSync();
1868 ASSERT_NO_FATAL_FAILURE(
1869 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1870
1871 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1872 // The policy should be notified of the stylus presence.
1873 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1874 mDevice->sendToolType(MT_TOOL_PEN);
1875 mDevice->sendMove(centerPoint);
1876 mDevice->sendSync();
1877 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1878 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001879 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001880
1881 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1882}
1883
Arpit Singh440bf652023-08-09 09:23:43 +00001884TEST_P(TouchIntegrationTest, ExternalStylusConnectedDuringTouchGesture) {
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001885 const Point centerPoint = mDevice->getCenterPoint();
1886
1887 // Down
1888 mDevice->sendSlot(FIRST_SLOT);
1889 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1890 mDevice->sendDown(centerPoint);
1891 mDevice->sendSync();
1892 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1893 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1894
1895 // Move
1896 mDevice->sendMove(centerPoint + Point(1, 1));
1897 mDevice->sendSync();
1898 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1899 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1900
1901 // Connecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
1902 auto externalStylus = createUinputDevice<UinputExternalStylus>();
1903 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1904 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1905 const auto stylusInfo = findDeviceByName(externalStylus->getName());
1906 ASSERT_TRUE(stylusInfo);
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001907
1908 // Move
1909 mDevice->sendMove(centerPoint + Point(2, 2));
1910 mDevice->sendSync();
1911 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1912 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1913
1914 // Disconnecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
1915 externalStylus.reset();
1916 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1917 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1918 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
1919
1920 // Up
1921 mDevice->sendUp();
1922 mDevice->sendSync();
1923 ASSERT_NO_FATAL_FAILURE(
1924 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1925
1926 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
1927}
1928
Arpit Singh440bf652023-08-09 09:23:43 +00001929INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
1930 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
1931 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
1932 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
1933
Prabir Pradhan124ea442022-10-28 20:27:44 +00001934// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001935
Prabir Pradhan124ea442022-10-28 20:27:44 +00001936// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1937// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1938// stylus.
1939template <typename UinputStylusDevice>
Arpit Singh440bf652023-08-09 09:23:43 +00001940class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001941protected:
1942 void SetUp() override {
1943#if !defined(__ANDROID__)
1944 GTEST_SKIP();
1945#endif
Arpit Singh440bf652023-08-09 09:23:43 +00001946 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00001947 mTouchscreen = mDevice.get();
1948 mTouchscreenInfo = mDeviceInfo;
1949
1950 setUpStylusDevice();
1951 }
1952
1953 UinputStylusDevice* mStylus{nullptr};
1954 InputDeviceInfo mStylusInfo{};
1955
1956 UinputTouchScreen* mTouchscreen{nullptr};
1957 InputDeviceInfo mTouchscreenInfo{};
1958
1959private:
1960 // When we are attempting to test stylus button events that are sent from the touchscreen,
1961 // use the same Uinput device for the touchscreen and the stylus.
1962 template <typename T = UinputStylusDevice>
1963 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1964 mStylus = mDevice.get();
1965 mStylusInfo = mDeviceInfo;
1966 }
1967
1968 // When we are attempting to stylus buttons from an external stylus being merged with touches
1969 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1970 template <typename T = UinputStylusDevice>
1971 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1972 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1973 mStylus = mStylusDeviceLifecycleTracker.get();
1974 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1975 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1976 const auto info = findDeviceByName(mStylus->getName());
1977 ASSERT_TRUE(info);
1978 mStylusInfo = *info;
1979 }
1980
1981 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1982
1983 // Hide the base class's device to expose it with a different name for readability.
Arpit Singh440bf652023-08-09 09:23:43 +00001984 using BaseTouchIntegrationTest::mDevice;
1985 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00001986};
1987
1988using StylusButtonIntegrationTestTypes =
1989 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1990TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1991
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00001992TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001993 const auto stylusId = TestFixture::mStylusInfo.getId();
1994
1995 TestFixture::mStylus->pressKey(BTN_STYLUS);
1996 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1997 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1998 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1999
2000 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2001 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002002 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002003 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002004}
2005
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002006TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002007 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2008 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2009 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002010
2011 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002012 TestFixture::mStylus->pressKey(BTN_STYLUS);
2013 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002014 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002015 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002016
2017 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002018 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2019 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2020 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2021 TestFixture::mTouchscreen->sendDown(centerPoint);
2022 TestFixture::mTouchscreen->sendSync();
2023 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002024 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002025 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002026 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2027 WithDeviceId(touchscreenId))));
2028 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002029 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002030 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002031 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2032 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002033
Prabir Pradhan124ea442022-10-28 20:27:44 +00002034 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2035 TestFixture::mTouchscreen->sendSync();
2036 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002037 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002038 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002039 WithDeviceId(touchscreenId))));
2040 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002041 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002042 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002043 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002044
2045 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002046 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2047 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002048 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002049 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002050}
2051
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002052TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002053 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2054 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2055 const auto stylusId = TestFixture::mStylusInfo.getId();
2056 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002057 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002058
2059 // Press the stylus button.
2060 TestFixture::mStylus->pressKey(BTN_STYLUS);
2061 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2062 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2063 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2064
2065 // Start hovering with the stylus.
2066 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2067 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2068 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2069 TestFixture::mTouchscreen->sendMove(centerPoint);
2070 TestFixture::mTouchscreen->sendSync();
2071 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2072 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2073 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2074 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2075 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2076 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2077 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2078 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2079 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2080
2081 // Touch down with the stylus.
2082 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2083 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2084 TestFixture::mTouchscreen->sendDown(centerPoint);
2085 TestFixture::mTouchscreen->sendSync();
2086 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2087 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2088 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2089
2090 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2091 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2092 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2093
2094 // Stop touching with the stylus, and start hovering.
2095 TestFixture::mTouchscreen->sendUp();
2096 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2097 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2098 TestFixture::mTouchscreen->sendMove(centerPoint);
2099 TestFixture::mTouchscreen->sendSync();
2100 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2101 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2102 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
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
2110 // Stop hovering.
2111 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2112 TestFixture::mTouchscreen->sendSync();
2113 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2114 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2115 WithButtonState(0))));
2116 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2117 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2118 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2119 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2120
2121 // Release the stylus button.
2122 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2123 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2124 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2125 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2126}
2127
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002128TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002129 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2130 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2131 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002132
2133 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002134 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2135 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2136 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2137 TestFixture::mTouchscreen->sendDown(centerPoint);
2138 TestFixture::mTouchscreen->sendSync();
2139 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002140 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002141 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002142 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002143
2144 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002145 TestFixture::mStylus->pressKey(BTN_STYLUS);
2146 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002147 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002148 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2149 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002150 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002151 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002152 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2153 WithDeviceId(touchscreenId))));
2154 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002155 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002156 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002157 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2158 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002159
Prabir Pradhan124ea442022-10-28 20:27:44 +00002160 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2161 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002162 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002163 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2164 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002165 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002166 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002167 WithDeviceId(touchscreenId))));
2168 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002169 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002170 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002171 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002172
2173 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002174 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2175 TestFixture::mTouchscreen->sendSync();
2176 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002177 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002178 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002179 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002180}
2181
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002182TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002183 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2184 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002185 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002186
2187 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2188 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2189 const auto stylusId = TestFixture::mStylusInfo.getId();
2190
2191 // Start a stylus gesture. By the time this event is processed, the configuration change that
2192 // was requested is guaranteed to be completed.
2193 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2194 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2195 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2196 TestFixture::mTouchscreen->sendDown(centerPoint);
2197 TestFixture::mTouchscreen->sendSync();
2198 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2199 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002200 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002201 WithDeviceId(touchscreenId))));
2202
2203 // Press and release a stylus button. Each change only generates a MOVE motion event.
2204 // Key events are unaffected.
2205 TestFixture::mStylus->pressKey(BTN_STYLUS);
2206 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2207 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2208 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2209 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2210 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002211 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002212 WithDeviceId(touchscreenId))));
2213
2214 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2215 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2216 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2217 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2218 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2219 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002220 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002221 WithDeviceId(touchscreenId))));
2222
2223 // Finish the stylus gesture.
2224 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2225 TestFixture::mTouchscreen->sendSync();
2226 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2227 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002228 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002229 WithDeviceId(touchscreenId))));
2230}
2231
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002232// --- ExternalStylusIntegrationTest ---
2233
2234// Verify the behavior of an external stylus. An external stylus can report pressure or button
2235// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2236// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singh440bf652023-08-09 09:23:43 +00002237using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002238
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002239TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002240 const Point centerPoint = mDevice->getCenterPoint();
2241
2242 // Create an external stylus capable of reporting pressure data that
2243 // should be fused with a touch pointer.
2244 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2245 createUinputDevice<UinputExternalStylusWithPressure>();
2246 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2247 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2248 const auto stylusInfo = findDeviceByName(stylus->getName());
2249 ASSERT_TRUE(stylusInfo);
2250
2251 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2252
2253 const auto touchscreenId = mDeviceInfo.getId();
2254
2255 // Set a pressure value on the stylus. It doesn't generate any events.
2256 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2257 stylus->setPressure(100);
2258 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2259
2260 // Start a finger gesture, and ensure it shows up as stylus gesture
2261 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002262 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002263 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002264 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002265 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002266 mDevice->sendSync();
2267 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2268 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002269 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002270 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002271
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002272 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2273 // event with the updated pressure.
2274 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002275 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2276 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002277 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002278 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002279
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002280 // The external stylus did not generate any events.
2281 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2282 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2283}
2284
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002285TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002286 const Point centerPoint = mDevice->getCenterPoint();
2287
2288 // Create an external stylus capable of reporting pressure data that
2289 // should be fused with a touch pointer.
2290 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2291 createUinputDevice<UinputExternalStylusWithPressure>();
2292 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2293 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2294 const auto stylusInfo = findDeviceByName(stylus->getName());
2295 ASSERT_TRUE(stylusInfo);
2296
2297 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2298
2299 const auto touchscreenId = mDeviceInfo.getId();
2300
2301 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2302 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002303 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2304 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002305 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002306 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002307
2308 // Start a finger gesture. The touch device will withhold generating any touches for
2309 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2310 mDevice->sendSlot(FIRST_SLOT);
2311 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2312 mDevice->sendToolType(MT_TOOL_FINGER);
2313 mDevice->sendDown(centerPoint);
2314 auto waitUntil = std::chrono::system_clock::now() +
2315 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002316 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002317 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002318
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002319 // Since the external stylus did not report a pressure value within the timeout,
2320 // it shows up as a finger pointer.
2321 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2322 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002323 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002324 WithPressure(1.f))));
2325
2326 // Change the pressure on the external stylus. Since the pressure was not present at the start
2327 // of the gesture, it is ignored for now.
2328 stylus->setPressure(200);
2329 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2330
2331 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002332 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2333 mDevice->sendSync();
2334 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2335 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002336 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002337
2338 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2339 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2340 mDevice->sendToolType(MT_TOOL_FINGER);
2341 mDevice->sendDown(centerPoint);
2342 mDevice->sendSync();
2343 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2344 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002345 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002346 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2347
2348 // The external stylus did not generate any events.
2349 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2350 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002351}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002352
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002353TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002354 const Point centerPoint = mDevice->getCenterPoint();
2355
2356 // Create an external stylus device that does not support pressure. It should not affect any
2357 // touch pointers.
2358 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2359 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2360 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2361 const auto stylusInfo = findDeviceByName(stylus->getName());
2362 ASSERT_TRUE(stylusInfo);
2363
2364 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2365
2366 const auto touchscreenId = mDeviceInfo.getId();
2367
2368 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2369 // pressure data from the external stylus.
2370 mDevice->sendSlot(FIRST_SLOT);
2371 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2372 mDevice->sendToolType(MT_TOOL_FINGER);
2373 mDevice->sendDown(centerPoint);
2374 auto waitUntil = std::chrono::system_clock::now() +
2375 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2376 mDevice->sendSync();
2377 ASSERT_NO_FATAL_FAILURE(
2378 mTestListener
2379 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2380 WithToolType(
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002381 ToolType::FINGER),
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002382 WithButtonState(0),
2383 WithDeviceId(touchscreenId),
2384 WithPressure(1.f)),
2385 waitUntil));
2386
2387 // The external stylus did not generate any events.
2388 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2389 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2390}
2391
Michael Wrightd02c5b62014-02-10 15:10:22 -08002392// --- InputDeviceTest ---
2393class InputDeviceTest : public testing::Test {
2394protected:
2395 static const char* DEVICE_NAME;
2396 static const char* DEVICE_LOCATION;
2397 static const int32_t DEVICE_ID;
2398 static const int32_t DEVICE_GENERATION;
2399 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002400 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002401 static const int32_t EVENTHUB_ID;
2402 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2403
2404 std::shared_ptr<FakeEventHub> mFakeEventHub;
2405 sp<FakeInputReaderPolicy> mFakePolicy;
2406 std::unique_ptr<TestInputListener> mFakeListener;
2407 std::unique_ptr<InstrumentedInputReader> mReader;
2408 std::shared_ptr<InputDevice> mDevice;
2409
2410 void SetUp() override {
2411 mFakeEventHub = std::make_unique<FakeEventHub>();
2412 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2413 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002414 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002415 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002416 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002417 identifier.name = DEVICE_NAME;
2418 identifier.location = DEVICE_LOCATION;
2419 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2420 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2421 identifier);
2422 mReader->pushNextDevice(mDevice);
2423 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002424 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002425 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002426
2427 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002428 mFakeListener.reset();
2429 mFakePolicy.clear();
2430 }
2431};
2432
2433const char* InputDeviceTest::DEVICE_NAME = "device";
2434const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2435const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2436const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002437const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002438const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2439 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002440const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002441const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2442
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002443TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002444 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002445 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2446 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002447}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002448
Michael Wrightd02c5b62014-02-10 15:10:22 -08002449TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2450 ASSERT_EQ(mDevice->isEnabled(), false);
2451}
2452
2453TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2454 // Configuration.
2455 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002456 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002457
2458 // Reset.
2459 unused += mDevice->reset(ARBITRARY_TIME);
2460
2461 NotifyDeviceResetArgs resetArgs;
2462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2463 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2464 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2465
2466 // Metadata.
2467 ASSERT_TRUE(mDevice->isIgnored());
2468 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2469
2470 InputDeviceInfo info = mDevice->getDeviceInfo();
2471 ASSERT_EQ(DEVICE_ID, info.getId());
2472 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2473 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2474 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2475
2476 // State queries.
2477 ASSERT_EQ(0, mDevice->getMetaState());
2478
2479 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2480 << "Ignored device should return unknown key code state.";
2481 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2482 << "Ignored device should return unknown scan code state.";
2483 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2484 << "Ignored device should return unknown switch state.";
2485
2486 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2487 uint8_t flags[2] = { 0, 1 };
2488 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2489 << "Ignored device should never mark any key codes.";
2490 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2491 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2492}
2493
2494TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2495 // Configuration.
2496 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2497
2498 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002499 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2500 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002501 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2502 mapper1.setMetaState(AMETA_ALT_ON);
2503 mapper1.addSupportedKeyCode(AKEYCODE_A);
2504 mapper1.addSupportedKeyCode(AKEYCODE_B);
2505 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2506 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2507 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2508 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2509 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2510
2511 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002512 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2513 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002514 mapper2.setMetaState(AMETA_SHIFT_ON);
2515
2516 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002517 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002518
Harry Cuttsf13161a2023-03-08 14:15:49 +00002519 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2520 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002521 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002522 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002523
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002524 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2525 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002526
2527 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002528 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002529 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2530 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002531
2532 NotifyDeviceResetArgs resetArgs;
2533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2534 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2535 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2536
2537 // Metadata.
2538 ASSERT_FALSE(mDevice->isIgnored());
2539 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2540
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002541 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002542 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002543 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002544 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2545 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2546
2547 // State queries.
2548 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2549 << "Should query mappers and combine meta states.";
2550
2551 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2552 << "Should return unknown key code state when source not supported.";
2553 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2554 << "Should return unknown scan code state when source not supported.";
2555 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2556 << "Should return unknown switch state when source not supported.";
2557
2558 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2559 << "Should query mapper when source is supported.";
2560 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2561 << "Should query mapper when source is supported.";
2562 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2563 << "Should query mapper when source is supported.";
2564
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002565 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002566 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002567 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002568 << "Should do nothing when source is unsupported.";
2569 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2570 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2571 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2572 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2573
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002574 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002575 << "Should query mapper when source is supported.";
2576 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2577 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2578 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2579 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2580
2581 // Event handling.
2582 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002583 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002584 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002585
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002586 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2587 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002588}
2589
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -07002590TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2591 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2592 FakeInputMapper& mapper =
2593 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2594 AINPUT_SOURCE_KEYBOARD);
2595 NotifyMotionArgs args1;
2596 NotifySwitchArgs args2;
2597 NotifyKeyArgs args3;
2598 mapper.setProcessResult({args1, args2, args3});
2599
2600 InputReaderConfiguration config;
2601 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2602
2603 RawEvent event;
2604 event.deviceId = EVENTHUB_ID;
2605 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2606
2607 for (auto& arg : notifyArgs) {
2608 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2609 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2610 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2611 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2612 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2613 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2614 }
2615 }
2616}
2617
2618TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2619 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2620 FakeInputMapper& mapper =
2621 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2622 AINPUT_SOURCE_KEYBOARD);
2623 NotifyMotionArgs args;
2624 mapper.setProcessResult({args});
2625
2626 InputReaderConfiguration config;
2627 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2628
2629 RawEvent event;
2630 event.deviceId = EVENTHUB_ID;
2631 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2632
2633 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2634 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2635}
2636
2637TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2638 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2639 FakeInputMapper& mapper =
2640 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2641 AINPUT_SOURCE_KEYBOARD);
2642 NotifyMotionArgs args;
2643 args.policyFlags = POLICY_FLAG_WAKE;
2644 mapper.setProcessResult({args});
2645
2646 InputReaderConfiguration config;
2647 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2648
2649 RawEvent event;
2650 event.deviceId = EVENTHUB_ID;
2651 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2652
2653 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2654 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2655}
2656
Arthur Hung2c9a3342019-07-23 14:18:59 +08002657// A single input device is associated with a specific display. Check that:
2658// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh3e56f7e2023-07-07 13:12:37 +00002659// 2. Device is disabled when setEnabled API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002660TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002661 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2662 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002663
2664 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002665 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002666 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2667 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002668
2669 // Device should be enabled by default.
2670 ASSERT_TRUE(mDevice->isEnabled());
2671
2672 // Prepare associated info.
2673 constexpr uint8_t hdmi = 1;
2674 const std::string UNIQUE_ID = "local:1";
2675
2676 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002677 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002678 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002679 // Device should be disabled because it is associated with a specific display via
2680 // input port <-> display port association, but the corresponding display is not found
2681 ASSERT_FALSE(mDevice->isEnabled());
2682
2683 // Prepare displays.
2684 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002685 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002686 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002687 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002688 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002689 ASSERT_TRUE(mDevice->isEnabled());
2690
2691 // Device should be disabled after set disable.
2692 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002693 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002694 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002695 ASSERT_FALSE(mDevice->isEnabled());
2696
2697 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002698 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002699 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002700 ASSERT_FALSE(mDevice->isEnabled());
2701}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002702
Christine Franks1ba71cc2021-04-07 14:37:42 -07002703TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2704 // Device should be enabled by default.
2705 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002706 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2707 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002708 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002709 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2710 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002711 ASSERT_TRUE(mDevice->isEnabled());
2712
2713 // Device should be disabled because it is associated with a specific display, but the
2714 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002715 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002716 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002717 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002718 ASSERT_FALSE(mDevice->isEnabled());
2719
2720 // Device should be enabled when a display is found.
2721 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002722 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002723 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002724 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002725 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002726 ASSERT_TRUE(mDevice->isEnabled());
2727
2728 // Device should be disabled after set disable.
2729 mFakePolicy->addDisabledDevice(mDevice->getId());
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::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002732 ASSERT_FALSE(mDevice->isEnabled());
2733
2734 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002735 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002736 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002737 ASSERT_FALSE(mDevice->isEnabled());
2738}
2739
Christine Franks2a2293c2022-01-18 11:51:16 -08002740TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2741 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002742 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2743 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002744 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002745 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2746 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002747
Christine Franks2a2293c2022-01-18 11:51:16 -08002748 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2749 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002750 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002751 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002752 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002753 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002754 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2755}
2756
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002757/**
2758 * This test reproduces a crash caused by a dangling reference that remains after device is added
2759 * and removed. The reference is accessed in InputDevice::dump(..);
2760 */
2761TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2762 constexpr int32_t TEST_EVENTHUB_ID = 10;
2763 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2764
Harry Cutts33476232023-01-30 19:57:29 +00002765 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh7f1765e2023-07-07 13:12:37 +00002766 device.addEventHubDevice(TEST_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002767 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2768 std::string dumpStr, eventHubDevStr;
2769 device.dump(dumpStr, eventHubDevStr);
2770}
2771
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002772TEST_F(InputDeviceTest, GetBluetoothAddress) {
2773 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2774 ASSERT_TRUE(address);
2775 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2776}
2777
Michael Wrightd02c5b62014-02-10 15:10:22 -08002778// --- SwitchInputMapperTest ---
2779
2780class SwitchInputMapperTest : public InputMapperTest {
2781protected:
2782};
2783
2784TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002785 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002786
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002787 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002788}
2789
2790TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002791 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002792
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002793 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002794 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002795
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002796 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002797 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002798}
2799
2800TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002801 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002802 std::list<NotifyArgs> out;
2803 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2804 ASSERT_TRUE(out.empty());
2805 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2806 ASSERT_TRUE(out.empty());
2807 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2808 ASSERT_TRUE(out.empty());
2809 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002810
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002811 ASSERT_EQ(1u, out.size());
2812 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002813 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002814 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2815 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002816 args.switchMask);
2817 ASSERT_EQ(uint32_t(0), args.policyFlags);
2818}
2819
Chris Ye87143712020-11-10 05:05:58 +00002820// --- VibratorInputMapperTest ---
2821class VibratorInputMapperTest : public InputMapperTest {
2822protected:
2823 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2824};
2825
2826TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002827 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002828
2829 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2830}
2831
2832TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002833 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002834
2835 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2836}
2837
2838TEST_F(VibratorInputMapperTest, Vibrate) {
2839 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002840 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00002841 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002842
2843 VibrationElement pattern(2);
2844 VibrationSequence sequence(2);
2845 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002846 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2847 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002848 sequence.addElement(pattern);
2849 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002850 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2851 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002852 sequence.addElement(pattern);
2853
2854 std::vector<int64_t> timings = {0, 1};
2855 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2856
2857 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002858 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002859 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002860 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002861 // Verify vibrator state listener was notified.
2862 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002863 ASSERT_EQ(1u, out.size());
2864 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2865 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2866 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002867 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002868 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002869 ASSERT_FALSE(mapper.isVibrating());
2870 // Verify vibrator state listener was notified.
2871 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002872 ASSERT_EQ(1u, out.size());
2873 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2874 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2875 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002876}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002877
Chris Yef59a2f42020-10-16 12:55:26 -07002878// --- SensorInputMapperTest ---
2879
2880class SensorInputMapperTest : public InputMapperTest {
2881protected:
2882 static const int32_t ACCEL_RAW_MIN;
2883 static const int32_t ACCEL_RAW_MAX;
2884 static const int32_t ACCEL_RAW_FUZZ;
2885 static const int32_t ACCEL_RAW_FLAT;
2886 static const int32_t ACCEL_RAW_RESOLUTION;
2887
2888 static const int32_t GYRO_RAW_MIN;
2889 static const int32_t GYRO_RAW_MAX;
2890 static const int32_t GYRO_RAW_FUZZ;
2891 static const int32_t GYRO_RAW_FLAT;
2892 static const int32_t GYRO_RAW_RESOLUTION;
2893
2894 static const float GRAVITY_MS2_UNIT;
2895 static const float DEGREE_RADIAN_UNIT;
2896
2897 void prepareAccelAxes();
2898 void prepareGyroAxes();
2899 void setAccelProperties();
2900 void setGyroProperties();
2901 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2902};
2903
2904const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2905const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2906const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2907const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2908const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2909
2910const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2911const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2912const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2913const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2914const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2915
2916const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2917const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2918
2919void SensorInputMapperTest::prepareAccelAxes() {
2920 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2921 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2922 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2923 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2924 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2925 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2926}
2927
2928void SensorInputMapperTest::prepareGyroAxes() {
2929 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2930 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2931 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2932 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2933 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2934 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2935}
2936
2937void SensorInputMapperTest::setAccelProperties() {
2938 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2939 /* sensorDataIndex */ 0);
2940 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2941 /* sensorDataIndex */ 1);
2942 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2943 /* sensorDataIndex */ 2);
2944 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2945 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2946 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2947 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2948 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2949}
2950
2951void SensorInputMapperTest::setGyroProperties() {
2952 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2953 /* sensorDataIndex */ 0);
2954 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2955 /* sensorDataIndex */ 1);
2956 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2957 /* sensorDataIndex */ 2);
2958 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2959 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2960 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2961 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2962 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2963}
2964
2965TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00002966 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002967
2968 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2969}
2970
2971TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2972 setAccelProperties();
2973 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00002974 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002975
2976 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2977 std::chrono::microseconds(10000),
2978 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002979 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002980 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2981 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2982 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2983 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002985
2986 NotifySensorArgs args;
2987 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2988 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2989 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2990
2991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2992 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2993 ASSERT_EQ(args.deviceId, DEVICE_ID);
2994 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2995 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2996 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2997 ASSERT_EQ(args.values, values);
2998 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2999}
3000
3001TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3002 setGyroProperties();
3003 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003004 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003005
3006 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3007 std::chrono::microseconds(10000),
3008 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003009 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003010 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3011 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3013 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003015
3016 NotifySensorArgs args;
3017 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3018 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3019 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3020
3021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3022 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3023 ASSERT_EQ(args.deviceId, DEVICE_ID);
3024 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3025 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3026 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3027 ASSERT_EQ(args.values, values);
3028 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3029}
3030
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031// --- KeyboardInputMapperTest ---
3032
3033class KeyboardInputMapperTest : public InputMapperTest {
3034protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003035 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003036 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003037 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003038
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003039 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003040 int32_t originalKeyCode, int32_t rotatedKeyCode,
3041 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003042};
3043
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003044/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3045 * orientation.
3046 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003047void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003048 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3049 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003050}
3051
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003052void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003053 int32_t originalScanCode, int32_t originalKeyCode,
3054 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003055 NotifyKeyArgs args;
3056
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003057 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3059 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3060 ASSERT_EQ(originalScanCode, args.scanCode);
3061 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003062 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003064 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3066 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3067 ASSERT_EQ(originalScanCode, args.scanCode);
3068 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003069 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003070}
3071
Michael Wrightd02c5b62014-02-10 15:10:22 -08003072TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003073 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003074 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003075 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003076
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003077 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003078}
3079
3080TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3081 const int32_t USAGE_A = 0x070004;
3082 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003083 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3084 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003085 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3086 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3087 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003090 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003091 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003092 // Initial metastate is AMETA_NONE.
3093 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094
3095 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003097 NotifyKeyArgs args;
3098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3099 ASSERT_EQ(DEVICE_ID, args.deviceId);
3100 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3101 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3102 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3103 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3104 ASSERT_EQ(KEY_HOME, args.scanCode);
3105 ASSERT_EQ(AMETA_NONE, args.metaState);
3106 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3107 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3108 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3109
3110 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003111 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3113 ASSERT_EQ(DEVICE_ID, args.deviceId);
3114 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3115 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3116 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3117 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3118 ASSERT_EQ(KEY_HOME, args.scanCode);
3119 ASSERT_EQ(AMETA_NONE, args.metaState);
3120 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3121 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3122 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3123
3124 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3126 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3128 ASSERT_EQ(DEVICE_ID, args.deviceId);
3129 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3130 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3131 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3132 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3133 ASSERT_EQ(0, args.scanCode);
3134 ASSERT_EQ(AMETA_NONE, args.metaState);
3135 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3136 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3137 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3138
3139 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3141 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3143 ASSERT_EQ(DEVICE_ID, args.deviceId);
3144 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3145 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3146 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3147 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3148 ASSERT_EQ(0, args.scanCode);
3149 ASSERT_EQ(AMETA_NONE, args.metaState);
3150 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3151 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3152 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3153
3154 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3158 ASSERT_EQ(DEVICE_ID, args.deviceId);
3159 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3160 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3161 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3162 ASSERT_EQ(0, args.keyCode);
3163 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3164 ASSERT_EQ(AMETA_NONE, args.metaState);
3165 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3166 ASSERT_EQ(0U, args.policyFlags);
3167 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3168
3169 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3171 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3173 ASSERT_EQ(DEVICE_ID, args.deviceId);
3174 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3175 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3176 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3177 ASSERT_EQ(0, args.keyCode);
3178 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3179 ASSERT_EQ(AMETA_NONE, args.metaState);
3180 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3181 ASSERT_EQ(0U, args.policyFlags);
3182 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3183}
3184
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003185TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3186 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3187 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3188 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3189
3190 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003191 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003192 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3193
3194 // Key down by scan code.
3195 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3196 NotifyKeyArgs args;
3197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3198 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3199
3200 // Key up by scan code.
3201 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3203 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3204}
3205
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003206/**
3207 * Ensure that the readTime is set to the time when the EV_KEY is received.
3208 */
3209TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3210 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3211
3212 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003213 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003214 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3215 NotifyKeyArgs args;
3216
3217 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003218 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3220 ASSERT_EQ(12, args.readTime);
3221
3222 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003223 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3225 ASSERT_EQ(15, args.readTime);
3226}
3227
Michael Wrightd02c5b62014-02-10 15:10:22 -08003228TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003229 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3230 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003231 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3232 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3233 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003234
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003235 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003236 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003237 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003238
Arthur Hung95f68612022-04-07 14:08:22 +08003239 // Initial metastate is AMETA_NONE.
3240 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003241
3242 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003243 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244 NotifyKeyArgs args;
3245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3246 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003247 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003248 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003249
3250 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003251 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3253 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003254 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003255
3256 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003257 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3259 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003260 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261
3262 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003263 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3265 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003266 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003267 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003268}
3269
3270TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003271 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3272 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3273 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3274 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003275
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003276 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003277 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003278 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003279
Michael Wrighta9cf4192022-12-01 23:46:39 +00003280 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003281 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3282 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3283 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3284 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3285 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3286 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3287 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3288 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3289}
3290
3291TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003292 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3293 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3294 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3295 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296
Michael Wrightd02c5b62014-02-10 15:10:22 -08003297 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003298 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003299 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003300 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003301
Michael Wrighta9cf4192022-12-01 23:46:39 +00003302 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003303 ASSERT_NO_FATAL_FAILURE(
3304 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3305 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3306 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3307 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3308 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3309 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3310 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003311
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003312 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003313 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003314 ASSERT_NO_FATAL_FAILURE(
3315 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3316 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3317 AKEYCODE_DPAD_UP, DISPLAY_ID));
3318 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3319 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3320 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3321 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003322
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003323 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003324 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003325 ASSERT_NO_FATAL_FAILURE(
3326 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3327 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3328 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3329 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3330 AKEYCODE_DPAD_UP, DISPLAY_ID));
3331 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3332 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003334 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003335 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003336 ASSERT_NO_FATAL_FAILURE(
3337 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3338 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3339 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3340 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3341 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3342 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3343 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003344
3345 // Special case: if orientation changes while key is down, we still emit the same keycode
3346 // in the key up as we did in the key down.
3347 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003348 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003349 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003350 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3352 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3353 ASSERT_EQ(KEY_UP, args.scanCode);
3354 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3355
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003356 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003357 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3360 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3361 ASSERT_EQ(KEY_UP, args.scanCode);
3362 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3363}
3364
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003365TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3366 // If the keyboard is not orientation aware,
3367 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003368 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003369
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003370 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003371 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003372 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003373 NotifyKeyArgs args;
3374
3375 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3380 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3381
Michael Wrighta9cf4192022-12-01 23:46:39 +00003382 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3387 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3388}
3389
3390TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3391 // If the keyboard is orientation aware,
3392 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003393 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003394
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003395 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003396 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003397 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003398 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003399 NotifyKeyArgs args;
3400
3401 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3402 // ^--- already checked by the previous test
3403
Michael Wrighta9cf4192022-12-01 23:46:39 +00003404 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003405 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3410 ASSERT_EQ(DISPLAY_ID, args.displayId);
3411
3412 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003413 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003414 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003415 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003416 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3420 ASSERT_EQ(newDisplayId, args.displayId);
3421}
3422
Michael Wrightd02c5b62014-02-10 15:10:22 -08003423TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003424 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003425 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003426 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003427
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003428 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003429 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003430
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003431 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003432 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003433}
3434
Philip Junker4af3b3d2021-12-14 10:36:55 +01003435TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3436 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003437 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003438 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3439
3440 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3441 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3442 << "If a mapping is available, the result is equal to the mapping";
3443
3444 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3445 << "If no mapping is available, the result is the key location";
3446}
3447
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003449 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003450 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003451 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003452
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003453 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003454 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003455
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003456 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003457 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003458}
3459
3460TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003461 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003462 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003463 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003464
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003465 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003466
Michael Wrightd02c5b62014-02-10 15:10:22 -08003467 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003468 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003469 ASSERT_TRUE(flags[0]);
3470 ASSERT_FALSE(flags[1]);
3471}
3472
3473TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003474 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3475 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3476 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3477 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3478 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3479 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480
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);
Arthur Hung95f68612022-04-07 14:08:22 +08003484 // Initial metastate is AMETA_NONE.
3485 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003486
3487 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003488 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3489 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3490 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003491
3492 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003493 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3494 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003495 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3496 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3497 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003498 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003499
3500 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003503 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3504 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3505 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003506 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003507
3508 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003511 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3512 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3513 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003514 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003515
3516 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003519 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3520 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3521 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003522 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003523
3524 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003525 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003527 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3528 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3529 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003530 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531
3532 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003533 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3534 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003535 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3536 ASSERT_FALSE(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_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003539}
3540
Chris Yea52ade12020-08-27 16:49:20 -07003541TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3542 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3543 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3544 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3545 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3546
3547 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003548 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003549 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3550
Chris Yea52ade12020-08-27 16:49:20 -07003551 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003552 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003553 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3554 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3555 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3556 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3557
3558 NotifyKeyArgs args;
3559 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003560 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3562 ASSERT_EQ(AMETA_NONE, args.metaState);
3563 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3564 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3565 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3566
3567 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003568 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3570 ASSERT_EQ(AMETA_NONE, args.metaState);
3571 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3572 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3573 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3574}
3575
Arthur Hung2c9a3342019-07-23 14:18:59 +08003576TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3577 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003578 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3579 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3580 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3581 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003582
3583 // keyboard 2.
3584 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003585 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003586 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003587 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003588 std::shared_ptr<InputDevice> device2 =
3589 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003590 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003591
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003592 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3593 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3594 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3595 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003596
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003597 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003598 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003599 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003600
Arpit Singh67ca6842023-04-26 14:43:16 +00003601 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003602 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003603 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3604 mFakePolicy
3605 ->getReaderConfiguration(),
3606 AINPUT_SOURCE_KEYBOARD,
3607 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003608 std::list<NotifyArgs> unused =
3609 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003610 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003611 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003612
3613 // Prepared displays and associated info.
3614 constexpr uint8_t hdmi1 = 0;
3615 constexpr uint8_t hdmi2 = 1;
3616 const std::string SECONDARY_UNIQUE_ID = "local:1";
3617
3618 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3619 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3620
3621 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003622 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003623 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003624 ASSERT_FALSE(device2->isEnabled());
3625
3626 // Prepare second display.
3627 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003628 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003629 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003630 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003631 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003632 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003633 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003634 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003635
3636 // Device should be enabled after the associated display is found.
3637 ASSERT_TRUE(mDevice->isEnabled());
3638 ASSERT_TRUE(device2->isEnabled());
3639
3640 // Test pad key events
3641 ASSERT_NO_FATAL_FAILURE(
3642 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3643 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3644 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3645 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3646 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3647 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3648 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3649
3650 ASSERT_NO_FATAL_FAILURE(
3651 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3652 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3653 AKEYCODE_DPAD_RIGHT, newDisplayId));
3654 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3655 AKEYCODE_DPAD_DOWN, newDisplayId));
3656 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3657 AKEYCODE_DPAD_LEFT, newDisplayId));
3658}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003659
arthurhungc903df12020-08-11 15:08:42 +08003660TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3661 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3662 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3663 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3664 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3665 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3666 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3667
3668 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003669 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003670 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003671 // Initial metastate is AMETA_NONE.
3672 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003673
3674 // Initialization should have turned all of the lights off.
3675 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3676 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3677 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3678
3679 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003680 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3681 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003682 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3683 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3684
3685 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003688 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3689 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3690
3691 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3693 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003694 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3695 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3696
3697 mFakeEventHub->removeDevice(EVENTHUB_ID);
3698 mReader->loopOnce();
3699
3700 // keyboard 2 should default toggle keys.
3701 const std::string USB2 = "USB2";
3702 const std::string DEVICE_NAME2 = "KEYBOARD2";
3703 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3704 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3705 std::shared_ptr<InputDevice> device2 =
3706 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003707 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003708 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3709 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3710 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3711 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3712 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3713 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3714
Arpit Singh67ca6842023-04-26 14:43:16 +00003715 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003716 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003717 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3718 mFakePolicy
3719 ->getReaderConfiguration(),
3720 AINPUT_SOURCE_KEYBOARD,
3721 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003722 std::list<NotifyArgs> unused =
3723 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003724 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003725 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003726
3727 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3728 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3729 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003730 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3731 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003732}
3733
Arthur Hungcb40a002021-08-03 14:31:01 +00003734TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3735 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3736 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3737 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3738
3739 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh67ca6842023-04-26 14:43:16 +00003740 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003741 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3742 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003743 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003744 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003745 // Initial metastate is AMETA_NONE.
3746 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003747
3748 mReader->toggleCapsLockState(DEVICE_ID);
3749 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3750}
3751
Arthur Hungfb3cc112022-04-13 07:39:50 +00003752TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3753 // keyboard 1.
3754 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3755 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3756 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3757 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3758 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3759 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3760
3761 KeyboardInputMapper& mapper1 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003762 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003763 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3764
3765 // keyboard 2.
3766 const std::string USB2 = "USB2";
3767 const std::string DEVICE_NAME2 = "KEYBOARD2";
3768 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3769 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3770 std::shared_ptr<InputDevice> device2 =
3771 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3772 ftl::Flags<InputDeviceClass>(0));
3773 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3774 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3775 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3776 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3777 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3778 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3779
Arpit Singh67ca6842023-04-26 14:43:16 +00003780 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003781 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003782 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3783 mFakePolicy
3784 ->getReaderConfiguration(),
3785 AINPUT_SOURCE_KEYBOARD,
3786 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003787 std::list<NotifyArgs> unused =
3788 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003789 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003790 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003791
Arthur Hung95f68612022-04-07 14:08:22 +08003792 // Initial metastate is AMETA_NONE.
3793 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3794 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3795
3796 // Toggle num lock on and off.
3797 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3798 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003799 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3800 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3801 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3802
3803 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3804 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3805 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3806 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3807 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3808
3809 // Toggle caps lock on and off.
3810 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3811 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3812 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3813 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3814 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3815
3816 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3817 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3818 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3819 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3820 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3821
3822 // Toggle scroll lock on and off.
3823 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3824 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3825 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3826 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3827 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3828
3829 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3830 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3831 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3832 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3833 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3834}
3835
Arthur Hung2141d542022-08-23 07:45:21 +00003836TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3837 const int32_t USAGE_A = 0x070004;
3838 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3839 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3840
3841 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003842 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00003843 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3844 // Key down by scan code.
3845 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3846 NotifyKeyArgs args;
3847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3848 ASSERT_EQ(DEVICE_ID, args.deviceId);
3849 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3850 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3851 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3852 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3853 ASSERT_EQ(KEY_HOME, args.scanCode);
3854 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3855
3856 // Disable device, it should synthesize cancellation events for down events.
3857 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003858 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00003859
3860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3861 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3862 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3863 ASSERT_EQ(KEY_HOME, args.scanCode);
3864 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3865}
3866
Zixuan Qufecb6062022-11-12 04:44:31 +00003867TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh67ca6842023-04-26 14:43:16 +00003868 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3869 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00003870 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003871 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3872 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00003873
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003874 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00003875 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3876
3877 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003878 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00003879
3880 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3881 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3882 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3883 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3884 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003885 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
3886
3887 // Call change layout association with the same values: Generation shouldn't change
3888 generation = mReader->getContext()->getGeneration();
3889 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3890 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3891 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
3892 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00003893}
3894
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003895TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3896 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3897 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3898
3899 // Configuration
Arpit Singh67ca6842023-04-26 14:43:16 +00003900 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003901 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3902 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003903 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003904
3905 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3906 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3907}
3908
Justin Chung71ddb432023-03-27 04:29:07 +00003909TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
3910 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
3911 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003912 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Justin Chung71ddb432023-03-27 04:29:07 +00003913 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3914 NotifyKeyArgs args;
3915
3916 // Key down
3917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
3918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3919 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
3920}
3921
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003922// --- KeyboardInputMapperTest_ExternalDevice ---
3923
3924class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3925protected:
Chris Yea52ade12020-08-27 16:49:20 -07003926 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003927};
3928
3929TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003930 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3931 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003932
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003933 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3934 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3935 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3936 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003937
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003938 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003939 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003940 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003941
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003943 NotifyKeyArgs args;
3944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3945 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3946
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003947 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3949 ASSERT_EQ(uint32_t(0), args.policyFlags);
3950
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003951 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003953 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003954
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003955 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3957 ASSERT_EQ(uint32_t(0), args.policyFlags);
3958
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3961 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3962
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003963 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3965 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3966}
3967
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003968TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003969 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003970
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003971 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3972 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3973 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003974
Powei Fengd041c5d2019-05-03 17:11:33 -07003975 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003976 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003977 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003978 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003979
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003980 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003981 NotifyKeyArgs args;
3982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3983 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3984
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003985 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3987 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3988
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003989 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3991 ASSERT_EQ(uint32_t(0), args.policyFlags);
3992
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003993 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3995 ASSERT_EQ(uint32_t(0), args.policyFlags);
3996
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003997 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3999 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4000
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004001 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4003 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4004}
4005
Michael Wrightd02c5b62014-02-10 15:10:22 -08004006// --- CursorInputMapperTest ---
4007
4008class CursorInputMapperTest : public InputMapperTest {
4009protected:
4010 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4011
Michael Wright17db18e2020-06-26 20:51:44 +01004012 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004013
Chris Yea52ade12020-08-27 16:49:20 -07004014 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004015 InputMapperTest::SetUp();
4016
Michael Wright17db18e2020-06-26 20:51:44 +01004017 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004018 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004019 }
4020
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004021 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4022 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004023
Michael Wrighta9cf4192022-12-01 23:46:39 +00004024 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004025 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4026 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4027 }
4028
4029 void prepareSecondaryDisplay() {
4030 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004031 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004032 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004033 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004034
4035 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4036 float pressure) {
4037 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4038 0.0f, 0.0f, 0.0f, EPSILON));
4039 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004040};
4041
4042const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4043
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004044void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4045 int32_t originalY, int32_t rotatedX,
4046 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004047 NotifyMotionArgs args;
4048
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004049 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4050 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4051 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004054 ASSERT_NO_FATAL_FAILURE(
4055 assertCursorPointerCoords(args.pointerCoords[0],
4056 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4057 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004058}
4059
4060TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004061 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004062 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004063
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004064 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004065}
4066
4067TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004068 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004069 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004070
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004071 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072}
4073
4074TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004076 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004077
4078 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004079 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080
4081 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004082 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4083 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004084 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4085 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4086
4087 // When the bounds are set, then there should be a valid motion range.
4088 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4089
4090 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004091 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092
4093 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4094 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4095 1, 800 - 1, 0.0f, 0.0f));
4096 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4097 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4098 2, 480 - 1, 0.0f, 0.0f));
4099 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4100 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4101 0.0f, 1.0f, 0.0f, 0.0f));
4102}
4103
4104TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004106 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107
4108 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004109 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110
4111 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4112 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4113 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4114 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4115 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4116 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4117 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4118 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4119 0.0f, 1.0f, 0.0f, 0.0f));
4120}
4121
4122TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004123 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004124 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125
arthurhungdcef2dc2020-08-11 14:47:50 +08004126 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004127
4128 NotifyMotionArgs args;
4129
4130 // Button press.
4131 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4135 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4136 ASSERT_EQ(DEVICE_ID, args.deviceId);
4137 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4138 ASSERT_EQ(uint32_t(0), args.policyFlags);
4139 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4140 ASSERT_EQ(0, args.flags);
4141 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4142 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4143 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004144 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004145 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004146 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004147 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004148 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4149 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4150 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4151
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4153 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4154 ASSERT_EQ(DEVICE_ID, args.deviceId);
4155 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4156 ASSERT_EQ(uint32_t(0), args.policyFlags);
4157 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4158 ASSERT_EQ(0, args.flags);
4159 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4160 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4161 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004162 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004163 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004164 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004165 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004166 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4167 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4168 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4169
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004171 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4172 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4174 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4175 ASSERT_EQ(DEVICE_ID, args.deviceId);
4176 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4177 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004178 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4179 ASSERT_EQ(0, args.flags);
4180 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4181 ASSERT_EQ(0, args.buttonState);
4182 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004183 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004184 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004185 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004186 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004187 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4188 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4189 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4190
4191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4192 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4193 ASSERT_EQ(DEVICE_ID, args.deviceId);
4194 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4195 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4197 ASSERT_EQ(0, args.flags);
4198 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4199 ASSERT_EQ(0, args.buttonState);
4200 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004201 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004203 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004204 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004205 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4206 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4207 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4208}
4209
4210TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004211 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004212 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213
4214 NotifyMotionArgs args;
4215
4216 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4218 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004221 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4222 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4223 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004224
4225 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4227 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004230 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4231 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232}
4233
4234TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004236 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237
4238 NotifyMotionArgs args;
4239
4240 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004241 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4242 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4244 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004245 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004246
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4248 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004249 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004250
Michael Wrightd02c5b62014-02-10 15:10:22 -08004251 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004252 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004255 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004256 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004257
4258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004259 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004260 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261}
4262
4263TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004265 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266
4267 NotifyMotionArgs args;
4268
4269 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4275 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004276 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4277 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4278 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4281 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004282 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4283 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4284 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004285
Michael Wrightd02c5b62014-02-10 15:10:22 -08004286 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4291 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004292 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4293 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4294 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295
4296 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004301 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004302
4303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004305 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306}
4307
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004308TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004309 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004311 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4312 // need to be rotated.
4313 addConfigurationProperty("cursor.orientationAware", "1");
Arpit Singhe036ad72023-04-27 12:48:15 +00004314 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004315
Michael Wrighta9cf4192022-12-01 23:46:39 +00004316 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4318 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4319 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4320 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4321 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4322 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4323 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4324 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4325}
4326
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004327TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004328 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004329 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004330 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4331 // orientation-aware are affected by display rotation.
Arpit Singhe036ad72023-04-27 12:48:15 +00004332 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004333
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004334 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004335 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4337 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4338 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4339 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4340 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4341 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4342 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4343 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4344
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004345 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004346 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004347 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4348 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4349 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4350 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4351 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4352 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4353 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4354 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004355
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004356 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004357 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4359 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4360 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4361 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4362 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4363 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4364 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4365 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4366
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004367 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004368 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004369 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4370 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4371 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4372 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4373 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4374 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4375 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4376 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377}
4378
4379TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004381 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382
4383 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4384 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385
4386 NotifyMotionArgs motionArgs;
4387 NotifyKeyArgs keyArgs;
4388
4389 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4391 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4393 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4394 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004395 ASSERT_NO_FATAL_FAILURE(
4396 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4399 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4400 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004401 ASSERT_NO_FATAL_FAILURE(
4402 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004403
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004404 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4405 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004407 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004408 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004409 ASSERT_NO_FATAL_FAILURE(
4410 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411
4412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004413 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004415 ASSERT_NO_FATAL_FAILURE(
4416 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004417
4418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004419 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004420 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004421 ASSERT_NO_FATAL_FAILURE(
4422 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423
4424 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4427 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4429 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4430 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004431 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004432 ASSERT_NO_FATAL_FAILURE(
4433 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4436 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4437 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004438 ASSERT_NO_FATAL_FAILURE(
4439 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004440
4441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4442 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4443 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004444 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004445 ASSERT_NO_FATAL_FAILURE(
4446 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004447
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004451 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004453 ASSERT_NO_FATAL_FAILURE(
4454 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004455
4456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004458 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004459 ASSERT_NO_FATAL_FAILURE(
4460 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004462 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4463 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004465 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4466 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004467 ASSERT_NO_FATAL_FAILURE(
4468 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004469 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4470 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004471
4472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004475 ASSERT_NO_FATAL_FAILURE(
4476 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004477
Michael Wrightd02c5b62014-02-10 15:10:22 -08004478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4479 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004480 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004481 ASSERT_NO_FATAL_FAILURE(
4482 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004483
4484 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004485 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4486 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4488 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4489 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004490
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004492 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004494 ASSERT_NO_FATAL_FAILURE(
4495 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004496
4497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4498 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4499 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004500 ASSERT_NO_FATAL_FAILURE(
4501 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004502
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004503 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4504 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004506 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004507 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004508 ASSERT_NO_FATAL_FAILURE(
4509 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004510
4511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004513 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004514
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004515 ASSERT_NO_FATAL_FAILURE(
4516 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4518 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4519 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4520
4521 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004522 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4523 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4525 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4526 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004527
Michael Wrightd02c5b62014-02-10 15:10:22 -08004528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004529 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004530 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004531 ASSERT_NO_FATAL_FAILURE(
4532 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004533
4534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4535 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4536 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004537 ASSERT_NO_FATAL_FAILURE(
4538 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004539
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004543 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004545 ASSERT_NO_FATAL_FAILURE(
4546 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004547
4548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4549 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4550 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004551 ASSERT_NO_FATAL_FAILURE(
4552 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004553
Michael Wrightd02c5b62014-02-10 15:10:22 -08004554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4555 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4556 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4557
4558 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004559 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4560 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4562 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4563 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004564
Michael Wrightd02c5b62014-02-10 15:10:22 -08004565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004566 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004567 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004568 ASSERT_NO_FATAL_FAILURE(
4569 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004570
4571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4572 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4573 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004574 ASSERT_NO_FATAL_FAILURE(
4575 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004576
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4578 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004580 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004581 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004582 ASSERT_NO_FATAL_FAILURE(
4583 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004584
4585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4586 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4587 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004588 ASSERT_NO_FATAL_FAILURE(
4589 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004590
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4592 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4593 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4594
4595 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004596 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4597 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4599 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4600 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004601
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004603 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004605 ASSERT_NO_FATAL_FAILURE(
4606 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004607
4608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4609 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4610 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004611 ASSERT_NO_FATAL_FAILURE(
4612 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4615 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004617 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004618 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004619 ASSERT_NO_FATAL_FAILURE(
4620 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004621
4622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4623 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4624 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004625 ASSERT_NO_FATAL_FAILURE(
4626 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004627
Michael Wrightd02c5b62014-02-10 15:10:22 -08004628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4629 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4630 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4631}
4632
4633TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004634 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004635 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004636
4637 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4638 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004639
4640 NotifyMotionArgs args;
4641
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004642 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4643 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4644 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004646 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4647 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4649 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 +00004650 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004651}
4652
4653TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004654 addConfigurationProperty("cursor.mode", "pointer");
4655 mFakePolicy->setPointerCapture(true);
Arpit Singhe036ad72023-04-27 12:48:15 +00004656 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004657
4658 NotifyDeviceResetArgs resetArgs;
4659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4660 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4661 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4662
4663 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4664 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004665
4666 NotifyMotionArgs args;
4667
4668 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4671 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4673 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4674 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4676 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 +00004677 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004678
4679 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004680 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4681 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4683 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4684 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4686 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4688 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4689 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4690 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4691 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4692
4693 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004694 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4695 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4697 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4698 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4700 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4702 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4703 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4705 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4706
4707 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004708 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4709 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4710 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4712 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4713 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4714 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4715 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 +00004716 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004717
4718 // Disable pointer capture and check that the device generation got bumped
4719 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004720 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004721 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004722 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004723 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004724
4725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004726 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4727
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004728 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4729 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4730 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4732 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004733 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4735 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 +00004736 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004737}
4738
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004739/**
4740 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4741 * pointer acceleration or speed processing should not be applied.
4742 */
4743TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4744 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004745 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4746 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004747 mFakePolicy->setVelocityControlParams(testParams);
Arpit Singhe036ad72023-04-27 12:48:15 +00004748 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004749
4750 NotifyDeviceResetArgs resetArgs;
4751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4752 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4753 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4754
4755 NotifyMotionArgs args;
4756
4757 // Move and verify scale is applied.
4758 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4760 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4762 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4763 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4764 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4765 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4766 ASSERT_GT(relX, 10);
4767 ASSERT_GT(relY, 20);
4768
4769 // Enable Pointer Capture
4770 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004771 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004772 NotifyPointerCaptureChangedArgs captureArgs;
4773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4774 ASSERT_TRUE(captureArgs.request.enable);
4775
4776 // Move and verify scale is not applied.
4777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4781 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4782 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4783 ASSERT_EQ(10, args.pointerCoords[0].getX());
4784 ASSERT_EQ(20, args.pointerCoords[0].getY());
4785}
4786
Prabir Pradhan208360b2022-06-24 18:37:04 +00004787TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4788 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004789 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan208360b2022-06-24 18:37:04 +00004790
4791 NotifyDeviceResetArgs resetArgs;
4792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4793 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4794 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4795
4796 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004797 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004798
4799 NotifyMotionArgs args;
4800
4801 // Verify that the coordinates are rotated.
4802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4804 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4806 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4807 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4808 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4809 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4810
4811 // Enable Pointer Capture.
4812 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004813 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004814 NotifyPointerCaptureChangedArgs captureArgs;
4815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4816 ASSERT_TRUE(captureArgs.request.enable);
4817
4818 // Move and verify rotation is not applied.
4819 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4820 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4823 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4824 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4825 ASSERT_EQ(10, args.pointerCoords[0].getX());
4826 ASSERT_EQ(20, args.pointerCoords[0].getY());
4827}
4828
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004829TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004830 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004831
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004832 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004833 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004834
4835 // Set up the secondary display as the display on which the pointer should be shown.
4836 // The InputDevice is not associated with any display.
4837 prepareSecondaryDisplay();
4838 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004839 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004840
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004841 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004842 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004843
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004844 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004845 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4846 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4847 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004849 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4850 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4851 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004852 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004853}
4854
4855TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004856 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004857
4858 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004859 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004860
4861 // Set up the secondary display as the display on which the pointer should be shown,
4862 // and associate the InputDevice with the secondary display.
4863 prepareSecondaryDisplay();
4864 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4865 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004866 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004867
4868 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4869 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004870
4871 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4872 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4873 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004875 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4876 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4877 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004878 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004879}
4880
4881TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004882 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004883
4884 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004885 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004886 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4887
4888 // Associate the InputDevice with the secondary display.
4889 prepareSecondaryDisplay();
4890 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004891 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004892
4893 // The mapper should not generate any events because it is associated with a display that is
4894 // different from the pointer display.
4895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4896 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4897 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004899}
4900
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004901// --- BluetoothCursorInputMapperTest ---
4902
4903class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4904protected:
4905 void SetUp() override {
4906 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4907
4908 mFakePointerController = std::make_shared<FakePointerController>();
4909 mFakePolicy->setPointerController(mFakePointerController);
4910 }
4911};
4912
4913TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4914 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004915 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004916
4917 nsecs_t kernelEventTime = ARBITRARY_TIME;
4918 nsecs_t expectedEventTime = ARBITRARY_TIME;
4919 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4920 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4922 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4923 WithEventTime(expectedEventTime))));
4924
4925 // Process several events that come in quick succession, according to their timestamps.
4926 for (int i = 0; i < 3; i++) {
4927 constexpr static nsecs_t delta = ms2ns(1);
4928 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4929 kernelEventTime += delta;
4930 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4931
4932 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4933 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4935 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4936 WithEventTime(expectedEventTime))));
4937 }
4938}
4939
4940TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4941 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004942 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004943
4944 nsecs_t expectedEventTime = ARBITRARY_TIME;
4945 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4948 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4949 WithEventTime(expectedEventTime))));
4950
4951 // Process several events with the same timestamp from the kernel.
4952 // Ensure that we do not generate events too far into the future.
4953 constexpr static int32_t numEvents =
4954 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4955 for (int i = 0; i < numEvents; i++) {
4956 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4957
4958 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4961 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4962 WithEventTime(expectedEventTime))));
4963 }
4964
4965 // By processing more events with the same timestamp, we should not generate events with a
4966 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4967 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4968 for (int i = 0; i < 3; i++) {
4969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4970 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4972 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4973 WithEventTime(cappedEventTime))));
4974 }
4975}
4976
4977TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4978 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004979 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004980
4981 nsecs_t kernelEventTime = ARBITRARY_TIME;
4982 nsecs_t expectedEventTime = ARBITRARY_TIME;
4983 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4984 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4986 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4987 WithEventTime(expectedEventTime))));
4988
4989 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4990 // smoothening is not needed, its timestamp is not affected.
4991 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4992 expectedEventTime = kernelEventTime;
4993
4994 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4995 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4997 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4998 WithEventTime(expectedEventTime))));
4999}
5000
Michael Wrightd02c5b62014-02-10 15:10:22 -08005001// --- TouchInputMapperTest ---
5002
5003class TouchInputMapperTest : public InputMapperTest {
5004protected:
5005 static const int32_t RAW_X_MIN;
5006 static const int32_t RAW_X_MAX;
5007 static const int32_t RAW_Y_MIN;
5008 static const int32_t RAW_Y_MAX;
5009 static const int32_t RAW_TOUCH_MIN;
5010 static const int32_t RAW_TOUCH_MAX;
5011 static const int32_t RAW_TOOL_MIN;
5012 static const int32_t RAW_TOOL_MAX;
5013 static const int32_t RAW_PRESSURE_MIN;
5014 static const int32_t RAW_PRESSURE_MAX;
5015 static const int32_t RAW_ORIENTATION_MIN;
5016 static const int32_t RAW_ORIENTATION_MAX;
5017 static const int32_t RAW_DISTANCE_MIN;
5018 static const int32_t RAW_DISTANCE_MAX;
5019 static const int32_t RAW_TILT_MIN;
5020 static const int32_t RAW_TILT_MAX;
5021 static const int32_t RAW_ID_MIN;
5022 static const int32_t RAW_ID_MAX;
5023 static const int32_t RAW_SLOT_MIN;
5024 static const int32_t RAW_SLOT_MAX;
5025 static const float X_PRECISION;
5026 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005027 static const float X_PRECISION_VIRTUAL;
5028 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005029
5030 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005031 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005032
5033 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5034
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005035 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005036 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005037
Michael Wrightd02c5b62014-02-10 15:10:22 -08005038 enum Axes {
5039 POSITION = 1 << 0,
5040 TOUCH = 1 << 1,
5041 TOOL = 1 << 2,
5042 PRESSURE = 1 << 3,
5043 ORIENTATION = 1 << 4,
5044 MINOR = 1 << 5,
5045 ID = 1 << 6,
5046 DISTANCE = 1 << 7,
5047 TILT = 1 << 8,
5048 SLOT = 1 << 9,
5049 TOOL_TYPE = 1 << 10,
5050 };
5051
Michael Wrighta9cf4192022-12-01 23:46:39 +00005052 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005053 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00005054 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005055 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005056 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005057 int32_t toRawX(float displayX);
5058 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005059 int32_t toRotatedRawX(float displayX);
5060 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005061 float toCookedX(float rawX, float rawY);
5062 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005063 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005064 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005065 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005066 float toDisplayY(int32_t rawY, int32_t displayHeight);
5067
Michael Wrightd02c5b62014-02-10 15:10:22 -08005068};
5069
5070const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5071const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5072const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5073const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5074const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5075const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5076const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5077const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005078const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5079const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5081const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5082const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5083const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5084const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5085const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5086const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5087const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5088const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5089const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5090const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5091const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005092const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5093 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5094const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5095 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005096const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5097 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005098
5099const float TouchInputMapperTest::GEOMETRIC_SCALE =
5100 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5101 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5102
5103const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5104 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5105 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5106};
5107
Michael Wrighta9cf4192022-12-01 23:46:39 +00005108void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005109 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5110 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005111}
5112
5113void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5114 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00005115 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116}
5117
Michael Wrighta9cf4192022-12-01 23:46:39 +00005118void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005119 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5120 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5121 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005122}
5123
Michael Wrightd02c5b62014-02-10 15:10:22 -08005124void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005125 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5126 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5127 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5128 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129}
5130
Jason Gerecke489fda82012-09-07 17:19:40 -07005131void TouchInputMapperTest::prepareLocationCalibration() {
5132 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5133}
5134
Michael Wrightd02c5b62014-02-10 15:10:22 -08005135int32_t TouchInputMapperTest::toRawX(float displayX) {
5136 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5137}
5138
5139int32_t TouchInputMapperTest::toRawY(float displayY) {
5140 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5141}
5142
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005143int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5144 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5145}
5146
5147int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5148 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5149}
5150
Jason Gerecke489fda82012-09-07 17:19:40 -07005151float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5152 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5153 return rawX;
5154}
5155
5156float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5157 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5158 return rawY;
5159}
5160
Michael Wrightd02c5b62014-02-10 15:10:22 -08005161float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005162 return toDisplayX(rawX, DISPLAY_WIDTH);
5163}
5164
5165float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5166 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005167}
5168
5169float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005170 return toDisplayY(rawY, DISPLAY_HEIGHT);
5171}
5172
5173float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5174 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005175}
5176
5177
5178// --- SingleTouchInputMapperTest ---
5179
5180class SingleTouchInputMapperTest : public TouchInputMapperTest {
5181protected:
5182 void prepareButtons();
5183 void prepareAxes(int axes);
5184
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005185 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5186 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5187 void processUp(SingleTouchInputMapper& mappery);
5188 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5189 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5190 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5191 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5192 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5193 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005194};
5195
5196void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005197 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005198}
5199
5200void SingleTouchInputMapperTest::prepareAxes(int axes) {
5201 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005202 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5203 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005204 }
5205 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005206 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5207 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005208 }
5209 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005210 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5211 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005212 }
5213 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005214 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5215 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005216 }
5217 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005218 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5219 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005220 }
5221}
5222
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005223void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005224 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005227}
5228
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005229void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005230 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5231 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005232}
5233
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005234void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005236}
5237
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005238void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005239 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005240}
5241
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005242void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5243 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005244 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005245}
5246
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005247void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005248 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005249}
5250
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005251void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5252 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5254 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005255}
5256
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005257void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5258 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005260}
5261
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005262void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005264}
5265
Michael Wrightd02c5b62014-02-10 15:10:22 -08005266TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005267 prepareButtons();
5268 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005269 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005270
Josep del Río2d8c79a2023-01-23 19:33:50 +00005271 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005272}
5273
Michael Wrightd02c5b62014-02-10 15:10:22 -08005274TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005275 prepareButtons();
5276 prepareAxes(POSITION);
5277 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00005278 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005279
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005280 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005281}
5282
5283TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005284 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005285 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005286 prepareButtons();
5287 prepareAxes(POSITION);
5288 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005289 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005290
5291 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005292 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005293
5294 // Virtual key is down.
5295 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5296 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5297 processDown(mapper, x, y);
5298 processSync(mapper);
5299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5300
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005301 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005302
5303 // Virtual key is up.
5304 processUp(mapper);
5305 processSync(mapper);
5306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5307
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005308 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005309}
5310
5311TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005312 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005313 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314 prepareButtons();
5315 prepareAxes(POSITION);
5316 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005317 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005318
5319 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005320 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005321
5322 // Virtual key is down.
5323 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5324 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5325 processDown(mapper, x, y);
5326 processSync(mapper);
5327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5328
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005329 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330
5331 // Virtual key is up.
5332 processUp(mapper);
5333 processSync(mapper);
5334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5335
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005336 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005337}
5338
5339TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005340 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005341 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005342 prepareButtons();
5343 prepareAxes(POSITION);
5344 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005345 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005346
Michael Wrightd02c5b62014-02-10 15:10:22 -08005347 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005348 ASSERT_TRUE(
5349 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005350 ASSERT_TRUE(flags[0]);
5351 ASSERT_FALSE(flags[1]);
5352}
5353
5354TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005355 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005356 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005357 prepareButtons();
5358 prepareAxes(POSITION);
5359 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005360 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005361
arthurhungdcef2dc2020-08-11 14:47:50 +08005362 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005363
5364 NotifyKeyArgs args;
5365
5366 // Press virtual key.
5367 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5368 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5369 processDown(mapper, x, y);
5370 processSync(mapper);
5371
5372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5373 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5374 ASSERT_EQ(DEVICE_ID, args.deviceId);
5375 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5376 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5377 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5378 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5379 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5380 ASSERT_EQ(KEY_HOME, args.scanCode);
5381 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5382 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5383
5384 // Release virtual key.
5385 processUp(mapper);
5386 processSync(mapper);
5387
5388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5389 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5390 ASSERT_EQ(DEVICE_ID, args.deviceId);
5391 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5392 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5393 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5394 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5395 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5396 ASSERT_EQ(KEY_HOME, args.scanCode);
5397 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5398 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5399
5400 // Should not have sent any motions.
5401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5402}
5403
5404TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005405 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005406 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005407 prepareButtons();
5408 prepareAxes(POSITION);
5409 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005410 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411
arthurhungdcef2dc2020-08-11 14:47:50 +08005412 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413
5414 NotifyKeyArgs keyArgs;
5415
5416 // Press virtual key.
5417 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5418 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5419 processDown(mapper, x, y);
5420 processSync(mapper);
5421
5422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5423 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5424 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5425 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5426 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5427 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5428 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5429 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5430 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5431 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5432 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5433
5434 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5435 // into the display area.
5436 y -= 100;
5437 processMove(mapper, x, y);
5438 processSync(mapper);
5439
5440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5441 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5442 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5443 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5444 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5445 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5446 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5447 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5448 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5449 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5450 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5451 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5452
5453 NotifyMotionArgs motionArgs;
5454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5455 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5456 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5457 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5458 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5459 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5460 ASSERT_EQ(0, motionArgs.flags);
5461 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5462 ASSERT_EQ(0, motionArgs.buttonState);
5463 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005464 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005465 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005466 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5468 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5469 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5470 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5471 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5472
5473 // Keep moving out of bounds. Should generate a pointer move.
5474 y -= 50;
5475 processMove(mapper, x, y);
5476 processSync(mapper);
5477
5478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5479 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5480 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5481 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5482 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5483 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5484 ASSERT_EQ(0, motionArgs.flags);
5485 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5486 ASSERT_EQ(0, motionArgs.buttonState);
5487 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005488 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005489 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005490 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005491 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5492 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5493 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5494 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5495 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5496
5497 // Release out of bounds. Should generate a pointer up.
5498 processUp(mapper);
5499 processSync(mapper);
5500
5501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5502 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5503 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5504 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5505 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5506 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5507 ASSERT_EQ(0, motionArgs.flags);
5508 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5509 ASSERT_EQ(0, motionArgs.buttonState);
5510 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005511 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005512 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005513 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5515 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5516 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5517 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5518 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5519
5520 // Should not have sent any more keys or motions.
5521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5523}
5524
5525TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005526 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005527 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005528 prepareButtons();
5529 prepareAxes(POSITION);
5530 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005531 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005532
arthurhungdcef2dc2020-08-11 14:47:50 +08005533 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005534
5535 NotifyMotionArgs motionArgs;
5536
5537 // Initially go down out of bounds.
5538 int32_t x = -10;
5539 int32_t y = -10;
5540 processDown(mapper, x, y);
5541 processSync(mapper);
5542
5543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5544
5545 // Move into the display area. Should generate a pointer down.
5546 x = 50;
5547 y = 75;
5548 processMove(mapper, x, y);
5549 processSync(mapper);
5550
5551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5552 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5553 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5554 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5555 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5556 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5557 ASSERT_EQ(0, motionArgs.flags);
5558 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5559 ASSERT_EQ(0, motionArgs.buttonState);
5560 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005561 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005562 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005563 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005564 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5565 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5566 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5567 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5568 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5569
5570 // Release. Should generate a pointer up.
5571 processUp(mapper);
5572 processSync(mapper);
5573
5574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5575 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5576 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5577 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5578 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5579 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5580 ASSERT_EQ(0, motionArgs.flags);
5581 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5582 ASSERT_EQ(0, motionArgs.buttonState);
5583 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005584 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005585 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005586 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5588 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5589 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5590 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5591 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5592
5593 // Should not have sent any more keys or motions.
5594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5596}
5597
Santos Cordonfa5cf462017-04-05 10:37:00 -07005598TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005599 addConfigurationProperty("touch.deviceType", "touchScreen");
5600 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5601
Michael Wrighta9cf4192022-12-01 23:46:39 +00005602 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005603 prepareButtons();
5604 prepareAxes(POSITION);
5605 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005606 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005607
arthurhungdcef2dc2020-08-11 14:47:50 +08005608 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005609
5610 NotifyMotionArgs motionArgs;
5611
5612 // Down.
5613 int32_t x = 100;
5614 int32_t y = 125;
5615 processDown(mapper, x, y);
5616 processSync(mapper);
5617
5618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5619 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5620 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5621 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5622 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5623 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5624 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5625 ASSERT_EQ(0, motionArgs.flags);
5626 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5627 ASSERT_EQ(0, motionArgs.buttonState);
5628 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005629 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005630 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005631 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5633 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5634 1, 0, 0, 0, 0, 0, 0, 0));
5635 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5636 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5637 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5638
5639 // Move.
5640 x += 50;
5641 y += 75;
5642 processMove(mapper, x, y);
5643 processSync(mapper);
5644
5645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5646 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5647 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5648 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5649 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5650 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5651 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5652 ASSERT_EQ(0, motionArgs.flags);
5653 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5654 ASSERT_EQ(0, motionArgs.buttonState);
5655 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005656 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005657 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005658 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5660 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5661 1, 0, 0, 0, 0, 0, 0, 0));
5662 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5663 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5664 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5665
5666 // Up.
5667 processUp(mapper);
5668 processSync(mapper);
5669
5670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5671 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5672 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5673 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5674 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5675 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5676 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5677 ASSERT_EQ(0, motionArgs.flags);
5678 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5679 ASSERT_EQ(0, motionArgs.buttonState);
5680 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005681 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005682 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005683 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005684 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5685 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5686 1, 0, 0, 0, 0, 0, 0, 0));
5687 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5688 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5689 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5690
5691 // Should not have sent any more keys or motions.
5692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5694}
5695
Michael Wrightd02c5b62014-02-10 15:10:22 -08005696TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005697 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005698 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005699 prepareButtons();
5700 prepareAxes(POSITION);
5701 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005702 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005703
arthurhungdcef2dc2020-08-11 14:47:50 +08005704 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005705
5706 NotifyMotionArgs motionArgs;
5707
5708 // Down.
5709 int32_t x = 100;
5710 int32_t y = 125;
5711 processDown(mapper, x, y);
5712 processSync(mapper);
5713
5714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5715 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5716 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5717 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5718 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5719 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5720 ASSERT_EQ(0, motionArgs.flags);
5721 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5722 ASSERT_EQ(0, motionArgs.buttonState);
5723 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005724 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005725 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005726 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5728 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5729 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5730 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5731 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5732
5733 // Move.
5734 x += 50;
5735 y += 75;
5736 processMove(mapper, x, y);
5737 processSync(mapper);
5738
5739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5740 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5741 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5742 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5743 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5744 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5745 ASSERT_EQ(0, motionArgs.flags);
5746 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5747 ASSERT_EQ(0, motionArgs.buttonState);
5748 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005749 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005750 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005751 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5753 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5754 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5755 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5756 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5757
5758 // Up.
5759 processUp(mapper);
5760 processSync(mapper);
5761
5762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5763 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5764 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5765 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5766 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5767 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5768 ASSERT_EQ(0, motionArgs.flags);
5769 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5770 ASSERT_EQ(0, motionArgs.buttonState);
5771 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005772 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005773 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005774 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5776 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5777 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5778 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5779 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5780
5781 // Should not have sent any more keys or motions.
5782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5784}
5785
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005786TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005787 addConfigurationProperty("touch.deviceType", "touchScreen");
5788 prepareButtons();
5789 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005790 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5791 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005792 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005793
5794 NotifyMotionArgs args;
5795
5796 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005797 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005798 processDown(mapper, toRawX(50), toRawY(75));
5799 processSync(mapper);
5800
5801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5802 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5803 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5804
5805 processUp(mapper);
5806 processSync(mapper);
5807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5808}
5809
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005810TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005811 addConfigurationProperty("touch.deviceType", "touchScreen");
5812 prepareButtons();
5813 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005814 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5815 // orientation-aware are affected by display rotation.
5816 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005817 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005818
5819 NotifyMotionArgs args;
5820
5821 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005822 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005823 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005824 processDown(mapper, toRawX(50), toRawY(75));
5825 processSync(mapper);
5826
5827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5828 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5829 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5830
5831 processUp(mapper);
5832 processSync(mapper);
5833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5834
5835 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005836 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005837 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005838 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005839 processSync(mapper);
5840
5841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5842 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5843 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5844
5845 processUp(mapper);
5846 processSync(mapper);
5847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5848
5849 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005850 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005851 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005852 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5853 processSync(mapper);
5854
5855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5856 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5857 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5858
5859 processUp(mapper);
5860 processSync(mapper);
5861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5862
5863 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005864 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005865 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005866 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005867 processSync(mapper);
5868
5869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5870 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5871 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5872
5873 processUp(mapper);
5874 processSync(mapper);
5875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5876}
5877
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005878TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5879 addConfigurationProperty("touch.deviceType", "touchScreen");
5880 prepareButtons();
5881 prepareAxes(POSITION);
5882 addConfigurationProperty("touch.orientationAware", "1");
5883 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5884 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005885 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005886 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005887 NotifyMotionArgs args;
5888
5889 // Orientation 0.
5890 processDown(mapper, toRawX(50), toRawY(75));
5891 processSync(mapper);
5892
5893 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5894 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5895 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5896
5897 processUp(mapper);
5898 processSync(mapper);
5899 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5900}
5901
5902TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5903 addConfigurationProperty("touch.deviceType", "touchScreen");
5904 prepareButtons();
5905 prepareAxes(POSITION);
5906 addConfigurationProperty("touch.orientationAware", "1");
5907 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5908 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005909 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005910 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005911 NotifyMotionArgs args;
5912
5913 // Orientation 90.
5914 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5915 processSync(mapper);
5916
5917 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5918 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5919 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5920
5921 processUp(mapper);
5922 processSync(mapper);
5923 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5924}
5925
5926TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5927 addConfigurationProperty("touch.deviceType", "touchScreen");
5928 prepareButtons();
5929 prepareAxes(POSITION);
5930 addConfigurationProperty("touch.orientationAware", "1");
5931 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5932 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005933 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005934 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005935 NotifyMotionArgs args;
5936
5937 // Orientation 180.
5938 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5939 processSync(mapper);
5940
5941 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5942 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5943 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5944
5945 processUp(mapper);
5946 processSync(mapper);
5947 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5948}
5949
5950TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5951 addConfigurationProperty("touch.deviceType", "touchScreen");
5952 prepareButtons();
5953 prepareAxes(POSITION);
5954 addConfigurationProperty("touch.orientationAware", "1");
5955 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5956 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005957 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005958 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005959 NotifyMotionArgs args;
5960
5961 // Orientation 270.
5962 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5963 processSync(mapper);
5964
5965 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5966 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5967 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5968
5969 processUp(mapper);
5970 processSync(mapper);
5971 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5972}
5973
5974TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5975 addConfigurationProperty("touch.deviceType", "touchScreen");
5976 prepareButtons();
5977 prepareAxes(POSITION);
5978 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5979 // orientation-aware are affected by display rotation.
5980 addConfigurationProperty("touch.orientationAware", "0");
5981 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00005982 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005983
5984 NotifyMotionArgs args;
5985
5986 // Orientation 90, Rotation 0.
5987 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005988 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005989 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5990 processSync(mapper);
5991
5992 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5993 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5994 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5995
5996 processUp(mapper);
5997 processSync(mapper);
5998 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5999
6000 // Orientation 90, Rotation 90.
6001 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006002 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006003 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006004 processSync(mapper);
6005
6006 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6007 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6008 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6009
6010 processUp(mapper);
6011 processSync(mapper);
6012 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6013
6014 // Orientation 90, Rotation 180.
6015 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006016 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006017 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6018 processSync(mapper);
6019
6020 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6021 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6022 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6023
6024 processUp(mapper);
6025 processSync(mapper);
6026 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6027
6028 // Orientation 90, Rotation 270.
6029 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006030 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006031 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 -07006032 processSync(mapper);
6033
6034 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6035 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6036 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6037
6038 processUp(mapper);
6039 processSync(mapper);
6040 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6041}
6042
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006043TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
6044 addConfigurationProperty("touch.deviceType", "touchScreen");
6045 prepareButtons();
6046 prepareAxes(POSITION);
6047 addConfigurationProperty("touch.orientationAware", "1");
6048 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006049 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006050
6051 // Set a physical frame in the display viewport.
6052 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6053 viewport->physicalLeft = 20;
6054 viewport->physicalTop = 600;
6055 viewport->physicalRight = 30;
6056 viewport->physicalBottom = 610;
6057 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006058 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006059
6060 // Start the touch.
6061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6062 processSync(mapper);
6063
6064 // Expect all input starting outside the physical frame to be ignored.
6065 const std::array<Point, 6> outsidePoints = {
6066 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6067 for (const auto& p : outsidePoints) {
6068 processMove(mapper, toRawX(p.x), toRawY(p.y));
6069 processSync(mapper);
6070 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6071 }
6072
6073 // Move the touch into the physical frame.
6074 processMove(mapper, toRawX(25), toRawY(605));
6075 processSync(mapper);
6076 NotifyMotionArgs args;
6077 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6078 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6079 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6080 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6081
6082 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
6083 for (const auto& p : outsidePoints) {
6084 processMove(mapper, toRawX(p.x), toRawY(p.y));
6085 processSync(mapper);
6086 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6087 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
6088 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6089 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6090 }
6091
6092 processUp(mapper);
6093 processSync(mapper);
6094 EXPECT_NO_FATAL_FAILURE(
6095 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
6096}
6097
Harry Cutts1db43992023-06-19 17:05:07 +00006098TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
6099 std::shared_ptr<FakePointerController> fakePointerController =
6100 std::make_shared<FakePointerController>();
6101 mFakePolicy->setPointerController(fakePointerController);
6102
6103 addConfigurationProperty("touch.deviceType", "pointer");
6104 prepareAxes(POSITION);
6105 prepareDisplay(ui::ROTATION_0);
6106 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
6107
6108 // Set a physical frame in the display viewport.
6109 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6110 viewport->physicalLeft = 20;
6111 viewport->physicalTop = 600;
6112 viewport->physicalRight = 30;
6113 viewport->physicalBottom = 610;
6114 mFakePolicy->updateViewport(*viewport);
6115 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
6116
6117 // Start the touch.
6118 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6119 processSync(mapper);
6120
6121 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
6122 // produced.
6123 const std::array<Point, 6> outsidePoints = {
6124 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6125 for (const auto& p : outsidePoints) {
6126 processMove(mapper, toRawX(p.x), toRawY(p.y));
6127 processSync(mapper);
6128 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6129 }
6130}
6131
Michael Wrightd02c5b62014-02-10 15:10:22 -08006132TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006133 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006134 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006135 prepareButtons();
6136 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00006137 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006138
6139 // These calculations are based on the input device calibration documentation.
6140 int32_t rawX = 100;
6141 int32_t rawY = 200;
6142 int32_t rawPressure = 10;
6143 int32_t rawToolMajor = 12;
6144 int32_t rawDistance = 2;
6145 int32_t rawTiltX = 30;
6146 int32_t rawTiltY = 110;
6147
6148 float x = toDisplayX(rawX);
6149 float y = toDisplayY(rawY);
6150 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6151 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6152 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6153 float distance = float(rawDistance);
6154
6155 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6156 float tiltScale = M_PI / 180;
6157 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6158 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6159 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6160 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6161
6162 processDown(mapper, rawX, rawY);
6163 processPressure(mapper, rawPressure);
6164 processToolMajor(mapper, rawToolMajor);
6165 processDistance(mapper, rawDistance);
6166 processTilt(mapper, rawTiltX, rawTiltY);
6167 processSync(mapper);
6168
6169 NotifyMotionArgs args;
6170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6172 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6173 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6174}
6175
Jason Gerecke489fda82012-09-07 17:19:40 -07006176TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006177 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006178 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07006179 prepareLocationCalibration();
6180 prepareButtons();
6181 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006182 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006183
6184 int32_t rawX = 100;
6185 int32_t rawY = 200;
6186
6187 float x = toDisplayX(toCookedX(rawX, rawY));
6188 float y = toDisplayY(toCookedY(rawX, rawY));
6189
6190 processDown(mapper, rawX, rawY);
6191 processSync(mapper);
6192
6193 NotifyMotionArgs args;
6194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6195 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6196 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6197}
6198
Michael Wrightd02c5b62014-02-10 15:10:22 -08006199TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006200 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006201 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006202 prepareButtons();
6203 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006204 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006205
6206 NotifyMotionArgs motionArgs;
6207 NotifyKeyArgs keyArgs;
6208
6209 processDown(mapper, 100, 200);
6210 processSync(mapper);
6211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6212 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6213 ASSERT_EQ(0, motionArgs.buttonState);
6214
6215 // press BTN_LEFT, release BTN_LEFT
6216 processKey(mapper, BTN_LEFT, 1);
6217 processSync(mapper);
6218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6219 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6220 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6221
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6223 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6224 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6225
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226 processKey(mapper, BTN_LEFT, 0);
6227 processSync(mapper);
6228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006229 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006230 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006231
6232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006233 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006234 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006235
6236 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6237 processKey(mapper, BTN_RIGHT, 1);
6238 processKey(mapper, BTN_MIDDLE, 1);
6239 processSync(mapper);
6240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6241 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6242 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6243 motionArgs.buttonState);
6244
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6246 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6247 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6248
6249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6250 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6251 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6252 motionArgs.buttonState);
6253
Michael Wrightd02c5b62014-02-10 15:10:22 -08006254 processKey(mapper, BTN_RIGHT, 0);
6255 processSync(mapper);
6256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006257 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006258 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006259
6260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006262 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006263
6264 processKey(mapper, BTN_MIDDLE, 0);
6265 processSync(mapper);
6266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006267 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006268 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006269
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006272 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273
6274 // press BTN_BACK, release BTN_BACK
6275 processKey(mapper, BTN_BACK, 1);
6276 processSync(mapper);
6277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6278 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6279 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006280
Michael Wrightd02c5b62014-02-10 15:10:22 -08006281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006283 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6284
6285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6286 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6287 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006288
6289 processKey(mapper, BTN_BACK, 0);
6290 processSync(mapper);
6291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006292 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006293 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006294
6295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006297 ASSERT_EQ(0, motionArgs.buttonState);
6298
Michael Wrightd02c5b62014-02-10 15:10:22 -08006299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6300 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6301 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6302
6303 // press BTN_SIDE, release BTN_SIDE
6304 processKey(mapper, BTN_SIDE, 1);
6305 processSync(mapper);
6306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6307 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6308 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006309
Michael Wrightd02c5b62014-02-10 15:10:22 -08006310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006312 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6313
6314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6315 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6316 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006317
6318 processKey(mapper, BTN_SIDE, 0);
6319 processSync(mapper);
6320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006321 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006322 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006323
6324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006326 ASSERT_EQ(0, motionArgs.buttonState);
6327
Michael Wrightd02c5b62014-02-10 15:10:22 -08006328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6329 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6330 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6331
6332 // press BTN_FORWARD, release BTN_FORWARD
6333 processKey(mapper, BTN_FORWARD, 1);
6334 processSync(mapper);
6335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6336 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6337 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006338
Michael Wrightd02c5b62014-02-10 15:10:22 -08006339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006340 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006341 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6342
6343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6344 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6345 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006346
6347 processKey(mapper, BTN_FORWARD, 0);
6348 processSync(mapper);
6349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006350 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006351 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006352
6353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006355 ASSERT_EQ(0, motionArgs.buttonState);
6356
Michael Wrightd02c5b62014-02-10 15:10:22 -08006357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6358 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6359 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6360
6361 // press BTN_EXTRA, release BTN_EXTRA
6362 processKey(mapper, BTN_EXTRA, 1);
6363 processSync(mapper);
6364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6365 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6366 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006367
Michael Wrightd02c5b62014-02-10 15:10:22 -08006368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006369 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006370 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6371
6372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6373 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6374 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006375
6376 processKey(mapper, BTN_EXTRA, 0);
6377 processSync(mapper);
6378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006379 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006380 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006381
6382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006384 ASSERT_EQ(0, motionArgs.buttonState);
6385
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6387 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6388 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6389
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6391
Michael Wrightd02c5b62014-02-10 15:10:22 -08006392 // press BTN_STYLUS, release BTN_STYLUS
6393 processKey(mapper, BTN_STYLUS, 1);
6394 processSync(mapper);
6395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006397 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6398
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6400 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6401 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006402
6403 processKey(mapper, BTN_STYLUS, 0);
6404 processSync(mapper);
6405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006406 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006407 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006408
6409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006411 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006412
6413 // press BTN_STYLUS2, release BTN_STYLUS2
6414 processKey(mapper, BTN_STYLUS2, 1);
6415 processSync(mapper);
6416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6417 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006418 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6419
6420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6421 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6422 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006423
6424 processKey(mapper, BTN_STYLUS2, 0);
6425 processSync(mapper);
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006427 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006428 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006429
6430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006432 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006433
6434 // release touch
6435 processUp(mapper);
6436 processSync(mapper);
6437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6438 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6439 ASSERT_EQ(0, motionArgs.buttonState);
6440}
6441
6442TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006443 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006444 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006445 prepareButtons();
6446 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006447 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006448
6449 NotifyMotionArgs motionArgs;
6450
6451 // default tool type is finger
6452 processDown(mapper, 100, 200);
6453 processSync(mapper);
6454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6455 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006456 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006457
6458 // eraser
6459 processKey(mapper, BTN_TOOL_RUBBER, 1);
6460 processSync(mapper);
6461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6462 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006463 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006464
6465 // stylus
6466 processKey(mapper, BTN_TOOL_RUBBER, 0);
6467 processKey(mapper, BTN_TOOL_PEN, 1);
6468 processSync(mapper);
6469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006471 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006472
6473 // brush
6474 processKey(mapper, BTN_TOOL_PEN, 0);
6475 processKey(mapper, BTN_TOOL_BRUSH, 1);
6476 processSync(mapper);
6477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6478 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006479 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006480
6481 // pencil
6482 processKey(mapper, BTN_TOOL_BRUSH, 0);
6483 processKey(mapper, BTN_TOOL_PENCIL, 1);
6484 processSync(mapper);
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6486 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006487 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006488
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006489 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006490 processKey(mapper, BTN_TOOL_PENCIL, 0);
6491 processKey(mapper, BTN_TOOL_AIRBRUSH, 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::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006496
6497 // mouse
6498 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6499 processKey(mapper, BTN_TOOL_MOUSE, 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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006504
6505 // lens
6506 processKey(mapper, BTN_TOOL_MOUSE, 0);
6507 processKey(mapper, BTN_TOOL_LENS, 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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006512
6513 // double-tap
6514 processKey(mapper, BTN_TOOL_LENS, 0);
6515 processKey(mapper, BTN_TOOL_DOUBLETAP, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006520
6521 // triple-tap
6522 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6523 processKey(mapper, BTN_TOOL_TRIPLETAP, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006528
6529 // quad-tap
6530 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6531 processKey(mapper, BTN_TOOL_QUADTAP, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006536
6537 // finger
6538 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6539 processKey(mapper, BTN_TOOL_FINGER, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006544
6545 // stylus trumps finger
6546 processKey(mapper, BTN_TOOL_PEN, 1);
6547 processSync(mapper);
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006550 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006551
6552 // eraser trumps stylus
6553 processKey(mapper, BTN_TOOL_RUBBER, 1);
6554 processSync(mapper);
6555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6556 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006557 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006558
6559 // mouse trumps eraser
6560 processKey(mapper, BTN_TOOL_MOUSE, 1);
6561 processSync(mapper);
6562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6563 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006564 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006565
6566 // back to default tool type
6567 processKey(mapper, BTN_TOOL_MOUSE, 0);
6568 processKey(mapper, BTN_TOOL_RUBBER, 0);
6569 processKey(mapper, BTN_TOOL_PEN, 0);
6570 processKey(mapper, BTN_TOOL_FINGER, 0);
6571 processSync(mapper);
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6573 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006574 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006575}
6576
6577TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006578 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006579 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006580 prepareButtons();
6581 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006582 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006583 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006584
6585 NotifyMotionArgs motionArgs;
6586
6587 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6588 processKey(mapper, BTN_TOOL_FINGER, 1);
6589 processMove(mapper, 100, 200);
6590 processSync(mapper);
6591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6592 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6594 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6595
6596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6597 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6599 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6600
6601 // move a little
6602 processMove(mapper, 150, 250);
6603 processSync(mapper);
6604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6605 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6607 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6608
6609 // down when BTN_TOUCH is pressed, pressure defaults to 1
6610 processKey(mapper, BTN_TOUCH, 1);
6611 processSync(mapper);
6612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6613 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6615 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6616
6617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6618 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6620 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6621
6622 // up when BTN_TOUCH is released, hover restored
6623 processKey(mapper, BTN_TOUCH, 0);
6624 processSync(mapper);
6625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6626 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6628 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6629
6630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6631 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6633 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6634
6635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6636 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6638 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6639
6640 // exit hover when pointer goes away
6641 processKey(mapper, BTN_TOOL_FINGER, 0);
6642 processSync(mapper);
6643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6644 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6645 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6646 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6647}
6648
6649TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006650 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006651 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006652 prepareButtons();
6653 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006654 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006655
6656 NotifyMotionArgs motionArgs;
6657
6658 // initially hovering because pressure is 0
6659 processDown(mapper, 100, 200);
6660 processPressure(mapper, 0);
6661 processSync(mapper);
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(100), toDisplayY(200), 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(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6671
6672 // move a little
6673 processMove(mapper, 150, 250);
6674 processSync(mapper);
6675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6676 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // down when pressure is non-zero
6681 processPressure(mapper, RAW_PRESSURE_MAX);
6682 processSync(mapper);
6683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6684 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6686 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6687
6688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6689 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6690 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6691 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6692
6693 // up when pressure becomes 0, hover restored
6694 processPressure(mapper, 0);
6695 processSync(mapper);
6696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6697 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6699 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6700
6701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6702 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6704 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6705
6706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6707 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6708 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6709 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6710
6711 // exit hover when pointer goes away
6712 processUp(mapper);
6713 processSync(mapper);
6714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6715 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6716 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6717 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6718}
6719
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006720TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6721 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006722 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006723 prepareButtons();
6724 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006725 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006726
6727 // Touch down.
6728 processDown(mapper, 100, 200);
6729 processPressure(mapper, 1);
6730 processSync(mapper);
6731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6732 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6733
6734 // Reset the mapper. This should cancel the ongoing gesture.
6735 resetMapper(mapper, ARBITRARY_TIME);
6736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6737 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6738
6739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6740}
6741
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006742TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6743 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006744 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006745 prepareButtons();
6746 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006747 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006748
6749 // Set the initial state for the touch pointer.
6750 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6751 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6752 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6753 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6754
6755 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006756 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6757 // does not generate any events.
6758 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006759
6760 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6761 // the recreated touch state to generate a down event.
6762 processSync(mapper);
6763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6764 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6765
6766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6767}
6768
lilinnan687e58f2022-07-19 16:00:50 +08006769TEST_F(SingleTouchInputMapperTest,
6770 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6771 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006772 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006773 prepareButtons();
6774 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006775 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006776 NotifyMotionArgs motionArgs;
6777
6778 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006779 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006780 processSync(mapper);
6781
6782 // We should receive a down event
6783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6784 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6785
6786 // Change display id
6787 clearViewports();
6788 prepareSecondaryDisplay(ViewportType::INTERNAL);
6789
6790 // We should receive a cancel event
6791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6792 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6793 // Then receive reset called
6794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6795}
6796
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006797TEST_F(SingleTouchInputMapperTest,
6798 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6799 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006800 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006801 prepareButtons();
6802 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006803 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6805 NotifyMotionArgs motionArgs;
6806
6807 // Start a new gesture.
6808 processDown(mapper, 100, 200);
6809 processSync(mapper);
6810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6811 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6812
6813 // Make the viewport inactive. This will put the device in disabled mode.
6814 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6815 viewport->isActive = false;
6816 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006817 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006818
6819 // We should receive a cancel event for the ongoing gesture.
6820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6821 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6822 // Then we should be notified that the device was reset.
6823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6824
6825 // No events are generated while the viewport is inactive.
6826 processMove(mapper, 101, 201);
6827 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006828 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006829 processSync(mapper);
6830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6831
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006832 // Start a new gesture while the viewport is still inactive.
6833 processDown(mapper, 300, 400);
6834 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6835 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6836 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6837 processSync(mapper);
6838
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006839 // Make the viewport active again. The device should resume processing events.
6840 viewport->isActive = true;
6841 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006842 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006843
6844 // The device is reset because it changes back to direct mode, without generating any events.
6845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6847
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006848 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006849 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6851 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006852
6853 // No more events.
6854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6856}
6857
Prabir Pradhan211ba622022-10-31 21:09:21 +00006858TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6859 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006860 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006861 prepareButtons();
6862 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006863 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6865
6866 // Press a stylus button.
6867 processKey(mapper, BTN_STYLUS, 1);
6868 processSync(mapper);
6869
6870 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6871 processDown(mapper, 100, 200);
6872 processSync(mapper);
6873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6874 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6875 WithCoords(toDisplayX(100), toDisplayY(200)),
6876 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6878 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6879 WithCoords(toDisplayX(100), toDisplayY(200)),
6880 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6881
6882 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6883 // the button has not actually been released, since there will be no pointers through which the
6884 // button state can be reported. The event is generated at the location of the pointer before
6885 // it went up.
6886 processUp(mapper);
6887 processSync(mapper);
6888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6889 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6890 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6892 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6893 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6894}
6895
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006896TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6897 addConfigurationProperty("touch.deviceType", "touchScreen");
6898 prepareDisplay(ui::ROTATION_0);
6899 prepareButtons();
6900 prepareAxes(POSITION);
6901
6902 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6903
Arpit Singha8c236b2023-04-25 13:56:05 +00006904 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6906
6907 // Press a stylus button.
6908 processKey(mapper, BTN_STYLUS, 1);
6909 processSync(mapper);
6910
6911 // Start a touch gesture and ensure that the stylus button is not reported.
6912 processDown(mapper, 100, 200);
6913 processSync(mapper);
6914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6915 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6916
6917 // Release and press the stylus button again.
6918 processKey(mapper, BTN_STYLUS, 0);
6919 processSync(mapper);
6920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6921 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6922 processKey(mapper, BTN_STYLUS, 1);
6923 processSync(mapper);
6924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6925 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6926
6927 // Release the touch gesture.
6928 processUp(mapper);
6929 processSync(mapper);
6930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6931 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6932
6933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6934}
6935
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006936TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6937 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6938 prepareDisplay(ui::ROTATION_0);
6939 prepareButtons();
6940 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006941 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6943
6944 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6945}
6946
Seunghwan Choi356026c2023-02-01 14:37:25 +09006947TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6948 std::shared_ptr<FakePointerController> fakePointerController =
6949 std::make_shared<FakePointerController>();
6950 addConfigurationProperty("touch.deviceType", "touchScreen");
6951 prepareDisplay(ui::ROTATION_0);
6952 prepareButtons();
6953 prepareAxes(POSITION);
6954 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6955 mFakePolicy->setPointerController(fakePointerController);
6956 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00006957 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006958
6959 processKey(mapper, BTN_TOOL_PEN, 1);
6960 processMove(mapper, 100, 200);
6961 processSync(mapper);
6962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6963 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006964 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006965 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6966 ASSERT_TRUE(fakePointerController->isPointerShown());
6967 ASSERT_NO_FATAL_FAILURE(
6968 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6969}
6970
6971TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6972 std::shared_ptr<FakePointerController> fakePointerController =
6973 std::make_shared<FakePointerController>();
6974 addConfigurationProperty("touch.deviceType", "touchScreen");
6975 prepareDisplay(ui::ROTATION_0);
6976 prepareButtons();
6977 prepareAxes(POSITION);
6978 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6979 mFakePolicy->setPointerController(fakePointerController);
6980 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00006981 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006982
6983 processKey(mapper, BTN_TOOL_PEN, 1);
6984 processMove(mapper, 100, 200);
6985 processSync(mapper);
6986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6987 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006988 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006989 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6990 ASSERT_FALSE(fakePointerController->isPointerShown());
6991}
6992
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006993TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6994 // Initialize the device without setting device source to touch navigation.
6995 addConfigurationProperty("touch.deviceType", "touchScreen");
6996 prepareDisplay(ui::ROTATION_0);
6997 prepareButtons();
6998 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006999 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007000
7001 // Ensure that the device is created as a touchscreen, not touch navigation.
7002 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7003
7004 // Add device type association after the device was created.
7005 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
7006
7007 // Send update to the mapper.
7008 std::list<NotifyArgs> unused2 =
7009 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007010 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007011
7012 // Check whether device type update was successful.
7013 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
7014}
7015
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007016TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
7017 // Initialize the device without setting device source to touch navigation.
7018 addConfigurationProperty("touch.deviceType", "touchScreen");
7019 prepareDisplay(ui::ROTATION_0);
7020 prepareButtons();
7021 prepareAxes(POSITION);
7022 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7023
7024 // Set a physical frame in the display viewport.
7025 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7026 viewport->physicalLeft = 0;
7027 viewport->physicalTop = 0;
7028 viewport->physicalRight = DISPLAY_WIDTH / 2;
7029 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
7030 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007031 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007032
Arpit Singha8c236b2023-04-25 13:56:05 +00007033 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007034
7035 // Hovering inside the physical frame produces events.
7036 processKey(mapper, BTN_TOOL_PEN, 1);
7037 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
7038 processSync(mapper);
7039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7040 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
7041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7042 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
7043
7044 // Leaving the physical frame ends the hovering gesture.
7045 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
7046 processSync(mapper);
7047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7048 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
7049
7050 // Moving outside the physical frame does not produce events.
7051 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
7052 processSync(mapper);
7053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7054
7055 // Re-entering the physical frame produces events.
7056 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
7057 processSync(mapper);
7058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7059 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
7060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7061 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
7062}
7063
Prabir Pradhan5632d622021-09-06 07:57:20 -07007064// --- TouchDisplayProjectionTest ---
7065
7066class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7067public:
7068 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7069 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7070 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00007071 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
7072 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
7073 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007074 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007075 auto rotatedWidth = naturalDisplayWidth;
7076 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007077 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00007078 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007079 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007080 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007081 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007082 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007083 inverseRotationFlags = ui::Transform::ROT_180;
7084 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007085 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007086 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007087 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007088 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007089 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007090 inverseRotationFlags = ui::Transform::ROT_0;
7091 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007092 }
7093
Prabir Pradhana9df3162022-12-05 23:57:27 +00007094 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007095 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7096
7097 std::optional<DisplayViewport> internalViewport =
7098 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7099 DisplayViewport& v = *internalViewport;
7100 v.displayId = DISPLAY_ID;
7101 v.orientation = orientation;
7102
7103 v.logicalLeft = 0;
7104 v.logicalTop = 0;
7105 v.logicalRight = 100;
7106 v.logicalBottom = 100;
7107
7108 v.physicalLeft = rotatedPhysicalDisplay.left;
7109 v.physicalTop = rotatedPhysicalDisplay.top;
7110 v.physicalRight = rotatedPhysicalDisplay.right;
7111 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7112
Prabir Pradhana9df3162022-12-05 23:57:27 +00007113 v.deviceWidth = rotatedWidth;
7114 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007115
7116 v.isActive = true;
7117 v.uniqueId = UNIQUE_ID;
7118 v.type = ViewportType::INTERNAL;
7119 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007120 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007121 }
7122
7123 void assertReceivedMove(const Point& point) {
7124 NotifyMotionArgs motionArgs;
7125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007127 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007128 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7129 1, 0, 0, 0, 0, 0, 0, 0));
7130 }
7131};
7132
7133TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7134 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007135 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007136
7137 prepareButtons();
7138 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007139 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007140
7141 NotifyMotionArgs motionArgs;
7142
7143 // Configure the DisplayViewport such that the logical display maps to a subsection of
7144 // the display panel called the physical display. Here, the physical display is bounded by the
7145 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7146 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7147 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7148 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7149
Michael Wrighta9cf4192022-12-01 23:46:39 +00007150 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007151 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7152
7153 // Touches outside the physical display should be ignored, and should not generate any
7154 // events. Ensure touches at the following points that lie outside of the physical display
7155 // area do not generate any events.
7156 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7157 processDown(mapper, toRawX(point.x), toRawY(point.y));
7158 processSync(mapper);
7159 processUp(mapper);
7160 processSync(mapper);
7161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7162 << "Unexpected event generated for touch outside physical display at point: "
7163 << point.x << ", " << point.y;
7164 }
7165 }
7166}
7167
7168TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7169 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007170 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007171
7172 prepareButtons();
7173 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007174 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007175
7176 NotifyMotionArgs motionArgs;
7177
7178 // Configure the DisplayViewport such that the logical display maps to a subsection of
7179 // the display panel called the physical display. Here, the physical display is bounded by the
7180 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7181 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7182
Michael Wrighta9cf4192022-12-01 23:46:39 +00007183 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007184 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7185
7186 // Touches that start outside the physical display should be ignored until it enters the
7187 // physical display bounds, at which point it should generate a down event. Start a touch at
7188 // the point (5, 100), which is outside the physical display bounds.
7189 static const Point kOutsidePoint{5, 100};
7190 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7191 processSync(mapper);
7192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7193
7194 // Move the touch into the physical display area. This should generate a pointer down.
7195 processMove(mapper, toRawX(11), toRawY(21));
7196 processSync(mapper);
7197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7198 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007199 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007200 ASSERT_NO_FATAL_FAILURE(
7201 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7202
7203 // Move the touch inside the physical display area. This should generate a pointer move.
7204 processMove(mapper, toRawX(69), toRawY(159));
7205 processSync(mapper);
7206 assertReceivedMove({69, 159});
7207
7208 // Move outside the physical display area. Since the pointer is already down, this should
7209 // now continue generating events.
7210 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7211 processSync(mapper);
7212 assertReceivedMove(kOutsidePoint);
7213
7214 // Release. This should generate a pointer up.
7215 processUp(mapper);
7216 processSync(mapper);
7217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7218 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7220 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7221
7222 // Ensure no more events were generated.
7223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7225 }
7226}
7227
Prabir Pradhana9df3162022-12-05 23:57:27 +00007228// --- TouchscreenPrecisionTests ---
7229
7230// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
7231// in various orientations and with different display rotations. We configure the touchscreen to
7232// have a higher resolution than that of the display by an integer scale factor in each axis so that
7233// we can enforce that coordinates match precisely as expected.
7234class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
7235 public ::testing::WithParamInterface<ui::Rotation> {
7236public:
7237 void SetUp() override {
7238 SingleTouchInputMapperTest::SetUp();
7239
7240 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7241 // four times the resolution of the display in the Y axis.
7242 prepareButtons();
7243 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007244 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7245 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007246 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007247 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7248 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007249 }
7250
7251 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7252 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7253 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7254 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7255
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007256 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7257 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7258
7259 static const int32_t PRECISION_RAW_X_FLAT = 16;
7260 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7261
7262 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7263 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7264
Prabir Pradhana9df3162022-12-05 23:57:27 +00007265 static const std::array<Point, 4> kRawCorners;
7266};
7267
7268const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7269 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7270 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7271 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7272 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7273}};
7274
7275// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7276// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7277// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7278TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7279 enum class Orientation {
7280 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7281 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7282 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7283 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7284 ftl_last = ORIENTATION_270,
7285 };
7286 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7287 Orientation::ORIENTATION_270;
7288 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7289 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7290 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7291 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7292 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7293 };
7294
7295 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7296
7297 // Configure the touchscreen as being installed in the one of the four different orientations
7298 // relative to the display.
7299 addConfigurationProperty("touch.deviceType", "touchScreen");
7300 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7301 prepareDisplay(ui::ROTATION_0);
7302
Arpit Singha8c236b2023-04-25 13:56:05 +00007303 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00007304
7305 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7306 // orientations of either 90 or 270) this means the display's natural resolution will be
7307 // flipped.
7308 const bool displayRotated =
7309 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7310 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7311 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7312 const Rect physicalFrame{0, 0, width, height};
7313 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7314
7315 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7316 const float expectedPrecisionX = displayRotated ? 4 : 2;
7317 const float expectedPrecisionY = displayRotated ? 2 : 4;
7318
7319 // Test all four corners.
7320 for (int i = 0; i < 4; i++) {
7321 const auto& raw = kRawCorners[i];
7322 processDown(mapper, raw.x, raw.y);
7323 processSync(mapper);
7324 const auto& expected = expectedPoints[i];
7325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7326 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7327 WithCoords(expected.x, expected.y),
7328 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7329 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7330 << "with touchscreen orientation "
7331 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7332 << expected.x << ", " << expected.y << ").";
7333 processUp(mapper);
7334 processSync(mapper);
7335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7336 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7337 WithCoords(expected.x, expected.y))));
7338 }
7339}
7340
Prabir Pradhan82687402022-12-06 01:32:53 +00007341TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7342 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7343 kMappedCorners = {
7344 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7345 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7346 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7347 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7348 };
7349
7350 const ui::Rotation displayRotation = GetParam();
7351
7352 addConfigurationProperty("touch.deviceType", "touchScreen");
7353 prepareDisplay(displayRotation);
7354
Arpit Singha8c236b2023-04-25 13:56:05 +00007355 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00007356
7357 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7358
7359 // Test all four corners.
7360 for (int i = 0; i < 4; i++) {
7361 const auto& expected = expectedPoints[i];
7362 const auto& raw = kRawCorners[i];
7363 processDown(mapper, raw.x, raw.y);
7364 processSync(mapper);
7365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7366 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7367 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7368 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7369 << "with display rotation " << ui::toCString(displayRotation)
7370 << ", expected point (" << expected.x << ", " << expected.y << ").";
7371 processUp(mapper);
7372 processSync(mapper);
7373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7374 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7375 WithCoords(expected.x, expected.y))));
7376 }
7377}
7378
Prabir Pradhan3e798762022-12-02 21:02:11 +00007379TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7380 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7381 kMappedCorners = {
7382 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7383 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7384 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7385 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7386 };
7387
7388 const ui::Rotation displayRotation = GetParam();
7389
7390 addConfigurationProperty("touch.deviceType", "touchScreen");
7391 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7392
Arpit Singha8c236b2023-04-25 13:56:05 +00007393 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00007394
7395 // Ori 270, so width and height swapped
7396 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7397 prepareDisplay(displayRotation);
7398 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7399
7400 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7401
7402 // Test all four corners.
7403 for (int i = 0; i < 4; i++) {
7404 const auto& expected = expectedPoints[i];
7405 const auto& raw = kRawCorners[i];
7406 processDown(mapper, raw.x, raw.y);
7407 processSync(mapper);
7408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7409 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7410 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7411 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7412 << "with display rotation " << ui::toCString(displayRotation)
7413 << ", expected point (" << expected.x << ", " << expected.y << ").";
7414 processUp(mapper);
7415 processSync(mapper);
7416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7417 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7418 WithCoords(expected.x, expected.y))));
7419 }
7420}
7421
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007422TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7423 const ui::Rotation displayRotation = GetParam();
7424
7425 addConfigurationProperty("touch.deviceType", "touchScreen");
7426 prepareDisplay(displayRotation);
7427
7428 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00007429 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007430
7431 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7432 // MotionRanges use display pixels as their units
7433 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7434 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7435
7436 // The MotionRanges should be oriented in the rotated display's coordinate space
7437 const bool displayRotated =
7438 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7439
7440 constexpr float MAX_X = 479.5;
7441 constexpr float MAX_Y = 799.75;
7442 EXPECT_EQ(xRange->min, 0.f);
7443 EXPECT_EQ(yRange->min, 0.f);
7444 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7445 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7446
7447 EXPECT_EQ(xRange->flat, 8.f);
7448 EXPECT_EQ(yRange->flat, 8.f);
7449
7450 EXPECT_EQ(xRange->fuzz, 2.f);
7451 EXPECT_EQ(yRange->fuzz, 2.f);
7452
7453 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7454 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7455}
7456
Prabir Pradhana9df3162022-12-05 23:57:27 +00007457// Run the precision tests for all rotations.
7458INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7459 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7460 ui::ROTATION_270),
7461 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7462 return ftl::enum_string(testParamInfo.param);
7463 });
7464
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007465// --- ExternalStylusFusionTest ---
7466
7467class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7468public:
7469 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7470 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007471 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007472 prepareButtons();
7473 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007474 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007475
7476 mStylusState.when = ARBITRARY_TIME;
7477 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007478 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007479 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007480 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007481 processExternalStylusState(mapper);
7482 return mapper;
7483 }
7484
7485 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7486 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7487 for (const NotifyArgs& args : generatedArgs) {
7488 mFakeListener->notify(args);
7489 }
7490 // Loop the reader to flush the input listener queue.
7491 mReader->loopOnce();
7492 return generatedArgs;
7493 }
7494
7495protected:
7496 StylusState mStylusState{};
7497 static constexpr uint32_t EXPECTED_SOURCE =
7498 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7499
7500 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7501 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007502 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007503
7504 // The first pointer is withheld.
7505 processDown(mapper, 100, 200);
7506 processSync(mapper);
7507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7508 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7509 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7510
7511 // The external stylus reports pressure. The withheld finger pointer is released as a
7512 // stylus.
7513 mStylusState.pressure = 1.f;
7514 processExternalStylusState(mapper);
7515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7516 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7517 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7518
7519 // Subsequent pointer events are not withheld.
7520 processMove(mapper, 101, 201);
7521 processSync(mapper);
7522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7523 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7524
7525 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7527 }
7528
7529 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7530 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7531
7532 // Releasing the touch pointer ends the gesture.
7533 processUp(mapper);
7534 processSync(mapper);
7535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7536 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007537 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007538
7539 mStylusState.pressure = 0.f;
7540 processExternalStylusState(mapper);
7541 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7543 }
7544
7545 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7546 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007547 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007548
7549 // The first pointer is withheld when an external stylus is connected,
7550 // and a timeout is requested.
7551 processDown(mapper, 100, 200);
7552 processSync(mapper);
7553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7554 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7555 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7556
7557 // If the timeout expires early, it is requested again.
7558 handleTimeout(mapper, ARBITRARY_TIME + 1);
7559 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7560 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7561
7562 // When the timeout expires, the withheld touch is released as a finger pointer.
7563 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7565 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7566
7567 // Subsequent pointer events are not withheld.
7568 processMove(mapper, 101, 201);
7569 processSync(mapper);
7570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7571 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7572 processUp(mapper);
7573 processSync(mapper);
7574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7575 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7576
7577 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7579 }
7580
7581private:
7582 InputDeviceInfo mExternalStylusDeviceInfo{};
7583};
7584
7585TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7586 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7587 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7588}
7589
7590TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7591 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7592 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7593}
7594
7595TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7596 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7597 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7598}
7599
7600// Test a successful stylus fusion gesture where the pressure is reported by the external
7601// before the touch is reported by the touchscreen.
7602TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7603 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7604 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007605 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007606
7607 // The external stylus reports pressure first. It is ignored for now.
7608 mStylusState.pressure = 1.f;
7609 processExternalStylusState(mapper);
7610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7611 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7612
7613 // When the touch goes down afterwards, it is reported as a stylus pointer.
7614 processDown(mapper, 100, 200);
7615 processSync(mapper);
7616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7617 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7618 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7619
7620 processMove(mapper, 101, 201);
7621 processSync(mapper);
7622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7623 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7624 processUp(mapper);
7625 processSync(mapper);
7626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7627 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7628
7629 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7631}
7632
7633TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7634 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7635
7636 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7637 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7638
7639 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7640 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7641 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7642 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7643}
7644
7645TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7646 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7647 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007648 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007649
7650 mStylusState.pressure = 0.8f;
7651 processExternalStylusState(mapper);
7652 processDown(mapper, 100, 200);
7653 processSync(mapper);
7654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7655 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7656 WithPressure(0.8f))));
7657 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7658
7659 // The external stylus reports a pressure change. We wait for some time for a touch event.
7660 mStylusState.pressure = 0.6f;
7661 processExternalStylusState(mapper);
7662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7663 ASSERT_NO_FATAL_FAILURE(
7664 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7665
7666 // If a touch is reported within the timeout, it reports the updated pressure.
7667 processMove(mapper, 101, 201);
7668 processSync(mapper);
7669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7670 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7671 WithPressure(0.6f))));
7672 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7673
7674 // There is another pressure change.
7675 mStylusState.pressure = 0.5f;
7676 processExternalStylusState(mapper);
7677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7678 ASSERT_NO_FATAL_FAILURE(
7679 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7680
7681 // If a touch is not reported within the timeout, a move event is generated to report
7682 // the new pressure.
7683 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7685 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7686 WithPressure(0.5f))));
7687
7688 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7689 // repeated indefinitely.
7690 mStylusState.pressure = 0.0f;
7691 processExternalStylusState(mapper);
7692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7693 ASSERT_NO_FATAL_FAILURE(
7694 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7695 processMove(mapper, 102, 202);
7696 processSync(mapper);
7697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7698 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7699 WithPressure(0.5f))));
7700 processMove(mapper, 103, 203);
7701 processSync(mapper);
7702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7703 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7704 WithPressure(0.5f))));
7705
7706 processUp(mapper);
7707 processSync(mapper);
7708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7709 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007710 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007711
7712 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7714}
7715
7716TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7717 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7718 auto source = WithSource(EXPECTED_SOURCE);
7719
7720 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007721 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007722 processExternalStylusState(mapper);
7723 processDown(mapper, 100, 200);
7724 processSync(mapper);
7725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7726 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007727 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007728 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7729
7730 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007731 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007732 processExternalStylusState(mapper);
7733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7734 ASSERT_NO_FATAL_FAILURE(
7735 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7736
7737 // If a touch is reported within the timeout, it reports the updated pressure.
7738 processMove(mapper, 101, 201);
7739 processSync(mapper);
7740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7741 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007742 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007743 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7744
7745 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007746 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007747 processExternalStylusState(mapper);
7748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7749 ASSERT_NO_FATAL_FAILURE(
7750 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7751
7752 // If a touch is not reported within the timeout, a move event is generated to report
7753 // the new tool type.
7754 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7756 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007757 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007758
7759 processUp(mapper);
7760 processSync(mapper);
7761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7762 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007763 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007764
7765 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7767}
7768
7769TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7770 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7771 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007772 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007773
7774 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7775
7776 // The external stylus reports a button change. We wait for some time for a touch event.
7777 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7778 processExternalStylusState(mapper);
7779 ASSERT_NO_FATAL_FAILURE(
7780 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7781
7782 // If a touch is reported within the timeout, it reports the updated button state.
7783 processMove(mapper, 101, 201);
7784 processSync(mapper);
7785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7786 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7787 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7789 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7790 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7791 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7792
7793 // The button is now released.
7794 mStylusState.buttons = 0;
7795 processExternalStylusState(mapper);
7796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7797 ASSERT_NO_FATAL_FAILURE(
7798 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7799
7800 // If a touch is not reported within the timeout, a move event is generated to report
7801 // the new button state.
7802 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7804 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7805 WithButtonState(0))));
7806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007807 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7808 WithButtonState(0))));
7809
7810 processUp(mapper);
7811 processSync(mapper);
7812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007813 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7814
7815 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7817}
7818
Michael Wrightd02c5b62014-02-10 15:10:22 -08007819// --- MultiTouchInputMapperTest ---
7820
7821class MultiTouchInputMapperTest : public TouchInputMapperTest {
7822protected:
7823 void prepareAxes(int axes);
7824
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007825 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7826 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7827 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7828 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7829 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7830 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7831 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7832 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7833 void processId(MultiTouchInputMapper& mapper, int32_t id);
7834 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7835 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7836 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007837 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007838 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007839 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7840 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007841};
7842
7843void MultiTouchInputMapperTest::prepareAxes(int axes) {
7844 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007845 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7846 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007847 }
7848 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007849 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7850 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007851 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007852 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7853 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007854 }
7855 }
7856 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007857 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7858 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007859 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007860 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007861 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007862 }
7863 }
7864 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007865 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7866 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007867 }
7868 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007869 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7870 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007871 }
7872 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007873 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7874 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007875 }
7876 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007877 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7878 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007879 }
7880 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007881 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7882 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007883 }
7884 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007885 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007886 }
7887}
7888
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007889void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7890 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007891 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007893}
7894
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007895void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7896 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007897 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007898}
7899
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007900void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7901 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007902 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007903}
7904
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007905void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007906 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007907}
7908
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007909void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007910 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007911}
7912
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007913void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7914 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007915 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007916}
7917
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007918void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007919 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007920}
7921
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007922void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007923 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007924}
7925
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007926void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007927 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007928}
7929
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007930void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007931 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007932}
7933
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007934void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007935 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007936}
7937
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007938void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7939 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007940 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007941}
7942
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007943void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7944 int32_t value) {
7945 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7947}
7948
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007949void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007951}
7952
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007953void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7954 nsecs_t readTime) {
7955 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007956}
7957
Michael Wrightd02c5b62014-02-10 15:10:22 -08007958TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007959 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007960 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007961 prepareAxes(POSITION);
7962 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007963 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007964
arthurhungdcef2dc2020-08-11 14:47:50 +08007965 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007966
7967 NotifyMotionArgs motionArgs;
7968
7969 // Two fingers down at once.
7970 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7971 processPosition(mapper, x1, y1);
7972 processMTSync(mapper);
7973 processPosition(mapper, x2, y2);
7974 processMTSync(mapper);
7975 processSync(mapper);
7976
7977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7978 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7979 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7980 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7981 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7982 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7983 ASSERT_EQ(0, motionArgs.flags);
7984 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7985 ASSERT_EQ(0, motionArgs.buttonState);
7986 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007987 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007988 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007989 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7991 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7992 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7993 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7994 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7995
7996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7997 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7998 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7999 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8000 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008001 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008002 ASSERT_EQ(0, motionArgs.flags);
8003 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8004 ASSERT_EQ(0, motionArgs.buttonState);
8005 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008006 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008007 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008008 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008009 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008010 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8012 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8014 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8015 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8016 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8017 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8018
8019 // Move.
8020 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8021 processPosition(mapper, x1, y1);
8022 processMTSync(mapper);
8023 processPosition(mapper, x2, y2);
8024 processMTSync(mapper);
8025 processSync(mapper);
8026
8027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8028 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8029 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8030 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8031 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8032 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8033 ASSERT_EQ(0, motionArgs.flags);
8034 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8035 ASSERT_EQ(0, motionArgs.buttonState);
8036 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008037 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008038 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008039 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008040 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008041 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8043 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8045 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8046 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8047 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8048 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8049
8050 // First finger up.
8051 x2 += 15; y2 -= 20;
8052 processPosition(mapper, x2, y2);
8053 processMTSync(mapper);
8054 processSync(mapper);
8055
8056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8057 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8058 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8059 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8060 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008061 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008062 ASSERT_EQ(0, motionArgs.flags);
8063 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8064 ASSERT_EQ(0, motionArgs.buttonState);
8065 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008066 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008067 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008068 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008069 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008070 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008071 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8072 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8073 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8074 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8075 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8076 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8077 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8078
8079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8080 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8081 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8082 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8083 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8085 ASSERT_EQ(0, motionArgs.flags);
8086 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8087 ASSERT_EQ(0, motionArgs.buttonState);
8088 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008089 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008090 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008091 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8093 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8094 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8095 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8096 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8097
8098 // Move.
8099 x2 += 20; y2 -= 25;
8100 processPosition(mapper, x2, y2);
8101 processMTSync(mapper);
8102 processSync(mapper);
8103
8104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8105 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8106 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8107 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8108 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8109 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8110 ASSERT_EQ(0, motionArgs.flags);
8111 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8112 ASSERT_EQ(0, motionArgs.buttonState);
8113 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008114 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008115 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008116 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008117 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8118 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8119 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8120 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8121 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8122
8123 // New finger down.
8124 int32_t x3 = 700, y3 = 300;
8125 processPosition(mapper, x2, y2);
8126 processMTSync(mapper);
8127 processPosition(mapper, x3, y3);
8128 processMTSync(mapper);
8129 processSync(mapper);
8130
8131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8132 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8133 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8134 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8135 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008136 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008137 ASSERT_EQ(0, motionArgs.flags);
8138 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8139 ASSERT_EQ(0, motionArgs.buttonState);
8140 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008141 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008142 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008143 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008144 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008145 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8147 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8149 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8150 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8151 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8152 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8153
8154 // Second finger up.
8155 x3 += 30; y3 -= 20;
8156 processPosition(mapper, x3, y3);
8157 processMTSync(mapper);
8158 processSync(mapper);
8159
8160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8161 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8162 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8163 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8164 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008165 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008166 ASSERT_EQ(0, motionArgs.flags);
8167 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8168 ASSERT_EQ(0, motionArgs.buttonState);
8169 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008170 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008171 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008172 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008173 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008174 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008175 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8176 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8178 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8179 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8180 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8181 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8182
8183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8184 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8185 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8186 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8187 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8189 ASSERT_EQ(0, motionArgs.flags);
8190 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8191 ASSERT_EQ(0, motionArgs.buttonState);
8192 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008193 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008194 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008195 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008196 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8197 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8198 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8199 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8200 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8201
8202 // Last finger up.
8203 processMTSync(mapper);
8204 processSync(mapper);
8205
8206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8207 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8208 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8209 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8210 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8211 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8212 ASSERT_EQ(0, motionArgs.flags);
8213 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8214 ASSERT_EQ(0, motionArgs.buttonState);
8215 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008216 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008217 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008218 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8220 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8221 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8222 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8223 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8224
8225 // Should not have sent any more keys or motions.
8226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8228}
8229
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008230TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8231 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008232 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008233
8234 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8235 /*fuzz*/ 0, /*resolution*/ 10);
8236 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8237 /*fuzz*/ 0, /*resolution*/ 11);
8238 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8239 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8240 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8241 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8242 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8243 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8244 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8245 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8246
Arpit Singha8c236b2023-04-25 13:56:05 +00008247 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008248
8249 // X and Y axes
8250 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8251 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8252 // Touch major and minor
8253 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8254 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8255 // Tool major and minor
8256 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8257 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8258}
8259
8260TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8261 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008262 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008263
8264 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8265 /*fuzz*/ 0, /*resolution*/ 10);
8266 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8267 /*fuzz*/ 0, /*resolution*/ 11);
8268
8269 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8270
Arpit Singha8c236b2023-04-25 13:56:05 +00008271 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008272
8273 // Touch major and minor
8274 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8275 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8276 // Tool major and minor
8277 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8278 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8279}
8280
Michael Wrightd02c5b62014-02-10 15:10:22 -08008281TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008282 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008283 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008284 prepareAxes(POSITION | ID);
8285 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008286 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008287
arthurhungdcef2dc2020-08-11 14:47:50 +08008288 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008289
8290 NotifyMotionArgs motionArgs;
8291
8292 // Two fingers down at once.
8293 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8294 processPosition(mapper, x1, y1);
8295 processId(mapper, 1);
8296 processMTSync(mapper);
8297 processPosition(mapper, x2, y2);
8298 processId(mapper, 2);
8299 processMTSync(mapper);
8300 processSync(mapper);
8301
8302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8303 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008304 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008305 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008306 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8308 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8309
8310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008311 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008312 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008313 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008314 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008315 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008316 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8318 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8319 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8320 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8321
8322 // Move.
8323 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8324 processPosition(mapper, x1, y1);
8325 processId(mapper, 1);
8326 processMTSync(mapper);
8327 processPosition(mapper, x2, y2);
8328 processId(mapper, 2);
8329 processMTSync(mapper);
8330 processSync(mapper);
8331
8332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008334 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008335 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008336 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008337 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008338 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].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 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8342 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8343
8344 // First finger up.
8345 x2 += 15; y2 -= 20;
8346 processPosition(mapper, x2, y2);
8347 processId(mapper, 2);
8348 processMTSync(mapper);
8349 processSync(mapper);
8350
8351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008352 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008353 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008354 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008355 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008356 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008357 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8359 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8361 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8362
8363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008365 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008366 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008367 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8369 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8370
8371 // Move.
8372 x2 += 20; y2 -= 25;
8373 processPosition(mapper, x2, y2);
8374 processId(mapper, 2);
8375 processMTSync(mapper);
8376 processSync(mapper);
8377
8378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008380 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008381 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008382 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8384 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8385
8386 // New finger down.
8387 int32_t x3 = 700, y3 = 300;
8388 processPosition(mapper, x2, y2);
8389 processId(mapper, 2);
8390 processMTSync(mapper);
8391 processPosition(mapper, x3, y3);
8392 processId(mapper, 3);
8393 processMTSync(mapper);
8394 processSync(mapper);
8395
8396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008397 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008398 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008399 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008400 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008401 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008402 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8404 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8406 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8407
8408 // Second finger up.
8409 x3 += 30; y3 -= 20;
8410 processPosition(mapper, x3, y3);
8411 processId(mapper, 3);
8412 processMTSync(mapper);
8413 processSync(mapper);
8414
8415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008416 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008417 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008418 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008419 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008420 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008421 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8423 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8425 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8426
8427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008429 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008430 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008431 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8433 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8434
8435 // Last finger up.
8436 processMTSync(mapper);
8437 processSync(mapper);
8438
8439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8440 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008441 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008442 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008443 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8445 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8446
8447 // Should not have sent any more keys or motions.
8448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8450}
8451
8452TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008453 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008454 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008455 prepareAxes(POSITION | ID | SLOT);
8456 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008457 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008458
arthurhungdcef2dc2020-08-11 14:47:50 +08008459 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008460
8461 NotifyMotionArgs motionArgs;
8462
8463 // Two fingers down at once.
8464 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8465 processPosition(mapper, x1, y1);
8466 processId(mapper, 1);
8467 processSlot(mapper, 1);
8468 processPosition(mapper, x2, y2);
8469 processId(mapper, 2);
8470 processSync(mapper);
8471
8472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8473 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008474 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008475 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008476 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8478 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8479
8480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008481 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008482 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008483 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008484 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008485 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008486 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8488 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8490 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8491
8492 // Move.
8493 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8494 processSlot(mapper, 0);
8495 processPosition(mapper, x1, y1);
8496 processSlot(mapper, 1);
8497 processPosition(mapper, x2, y2);
8498 processSync(mapper);
8499
8500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008502 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008503 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008504 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008505 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008506 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8508 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8510 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8511
8512 // First finger up.
8513 x2 += 15; y2 -= 20;
8514 processSlot(mapper, 0);
8515 processId(mapper, -1);
8516 processSlot(mapper, 1);
8517 processPosition(mapper, x2, y2);
8518 processSync(mapper);
8519
8520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008521 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008522 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008523 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008524 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008525 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008526 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8528 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8530 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
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(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008535 ASSERT_EQ(1, 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_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8538 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8539
8540 // Move.
8541 x2 += 20; y2 -= 25;
8542 processPosition(mapper, x2, y2);
8543 processSync(mapper);
8544
8545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8546 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008547 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008548 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008549 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8551 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8552
8553 // New finger down.
8554 int32_t x3 = 700, y3 = 300;
8555 processPosition(mapper, x2, y2);
8556 processSlot(mapper, 0);
8557 processId(mapper, 3);
8558 processPosition(mapper, x3, y3);
8559 processSync(mapper);
8560
8561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008562 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008563 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008564 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008565 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008566 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008567 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8569 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8571 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8572
8573 // Second finger up.
8574 x3 += 30; y3 -= 20;
8575 processSlot(mapper, 1);
8576 processId(mapper, -1);
8577 processSlot(mapper, 0);
8578 processPosition(mapper, x3, y3);
8579 processSync(mapper);
8580
8581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008582 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008583 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008584 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008585 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008586 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008587 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8589 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8591 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8592
8593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8594 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008595 ASSERT_EQ(size_t(1), 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_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8599 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8600
8601 // Last finger up.
8602 processId(mapper, -1);
8603 processSync(mapper);
8604
8605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8606 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008607 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008608 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008609 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8611 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8612
8613 // Should not have sent any more keys or motions.
8614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8616}
8617
8618TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008619 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008620 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008621 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008622 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008623
8624 // These calculations are based on the input device calibration documentation.
8625 int32_t rawX = 100;
8626 int32_t rawY = 200;
8627 int32_t rawTouchMajor = 7;
8628 int32_t rawTouchMinor = 6;
8629 int32_t rawToolMajor = 9;
8630 int32_t rawToolMinor = 8;
8631 int32_t rawPressure = 11;
8632 int32_t rawDistance = 0;
8633 int32_t rawOrientation = 3;
8634 int32_t id = 5;
8635
8636 float x = toDisplayX(rawX);
8637 float y = toDisplayY(rawY);
8638 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8639 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8640 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8641 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8642 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8643 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8644 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8645 float distance = float(rawDistance);
8646
8647 processPosition(mapper, rawX, rawY);
8648 processTouchMajor(mapper, rawTouchMajor);
8649 processTouchMinor(mapper, rawTouchMinor);
8650 processToolMajor(mapper, rawToolMajor);
8651 processToolMinor(mapper, rawToolMinor);
8652 processPressure(mapper, rawPressure);
8653 processOrientation(mapper, rawOrientation);
8654 processDistance(mapper, rawDistance);
8655 processId(mapper, id);
8656 processMTSync(mapper);
8657 processSync(mapper);
8658
8659 NotifyMotionArgs args;
8660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8661 ASSERT_EQ(0, args.pointerProperties[0].id);
8662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8663 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8664 orientation, distance));
8665}
8666
8667TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008668 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008669 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008670 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8671 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00008672 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008673
8674 // These calculations are based on the input device calibration documentation.
8675 int32_t rawX = 100;
8676 int32_t rawY = 200;
8677 int32_t rawTouchMajor = 140;
8678 int32_t rawTouchMinor = 120;
8679 int32_t rawToolMajor = 180;
8680 int32_t rawToolMinor = 160;
8681
8682 float x = toDisplayX(rawX);
8683 float y = toDisplayY(rawY);
8684 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8685 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8686 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8687 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8688 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8689
8690 processPosition(mapper, rawX, rawY);
8691 processTouchMajor(mapper, rawTouchMajor);
8692 processTouchMinor(mapper, rawTouchMinor);
8693 processToolMajor(mapper, rawToolMajor);
8694 processToolMinor(mapper, rawToolMinor);
8695 processMTSync(mapper);
8696 processSync(mapper);
8697
8698 NotifyMotionArgs args;
8699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8701 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8702}
8703
8704TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008705 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008706 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008707 prepareAxes(POSITION | TOUCH | TOOL);
8708 addConfigurationProperty("touch.size.calibration", "diameter");
8709 addConfigurationProperty("touch.size.scale", "10");
8710 addConfigurationProperty("touch.size.bias", "160");
8711 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00008712 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008713
8714 // These calculations are based on the input device calibration documentation.
8715 // Note: We only provide a single common touch/tool value because the device is assumed
8716 // not to emit separate values for each pointer (isSummed = 1).
8717 int32_t rawX = 100;
8718 int32_t rawY = 200;
8719 int32_t rawX2 = 150;
8720 int32_t rawY2 = 250;
8721 int32_t rawTouchMajor = 5;
8722 int32_t rawToolMajor = 8;
8723
8724 float x = toDisplayX(rawX);
8725 float y = toDisplayY(rawY);
8726 float x2 = toDisplayX(rawX2);
8727 float y2 = toDisplayY(rawY2);
8728 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8729 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8730 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8731
8732 processPosition(mapper, rawX, rawY);
8733 processTouchMajor(mapper, rawTouchMajor);
8734 processToolMajor(mapper, rawToolMajor);
8735 processMTSync(mapper);
8736 processPosition(mapper, rawX2, rawY2);
8737 processTouchMajor(mapper, rawTouchMajor);
8738 processToolMajor(mapper, rawToolMajor);
8739 processMTSync(mapper);
8740 processSync(mapper);
8741
8742 NotifyMotionArgs args;
8743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8744 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8745
8746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008747 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008748 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8750 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8752 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8753}
8754
8755TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008756 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008757 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008758 prepareAxes(POSITION | TOUCH | TOOL);
8759 addConfigurationProperty("touch.size.calibration", "area");
8760 addConfigurationProperty("touch.size.scale", "43");
8761 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00008762 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008763
8764 // These calculations are based on the input device calibration documentation.
8765 int32_t rawX = 100;
8766 int32_t rawY = 200;
8767 int32_t rawTouchMajor = 5;
8768 int32_t rawToolMajor = 8;
8769
8770 float x = toDisplayX(rawX);
8771 float y = toDisplayY(rawY);
8772 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8773 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8774 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8775
8776 processPosition(mapper, rawX, rawY);
8777 processTouchMajor(mapper, rawTouchMajor);
8778 processToolMajor(mapper, rawToolMajor);
8779 processMTSync(mapper);
8780 processSync(mapper);
8781
8782 NotifyMotionArgs args;
8783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8785 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8786}
8787
8788TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008789 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008790 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008791 prepareAxes(POSITION | PRESSURE);
8792 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8793 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00008794 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008795
Michael Wrightaa449c92017-12-13 21:21:43 +00008796 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008797 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008798 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8799 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8800 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8801
Michael Wrightd02c5b62014-02-10 15:10:22 -08008802 // These calculations are based on the input device calibration documentation.
8803 int32_t rawX = 100;
8804 int32_t rawY = 200;
8805 int32_t rawPressure = 60;
8806
8807 float x = toDisplayX(rawX);
8808 float y = toDisplayY(rawY);
8809 float pressure = float(rawPressure) * 0.01f;
8810
8811 processPosition(mapper, rawX, rawY);
8812 processPressure(mapper, rawPressure);
8813 processMTSync(mapper);
8814 processSync(mapper);
8815
8816 NotifyMotionArgs args;
8817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8818 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8819 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8820}
8821
8822TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008823 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008824 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008825 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008826 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008827
8828 NotifyMotionArgs motionArgs;
8829 NotifyKeyArgs keyArgs;
8830
8831 processId(mapper, 1);
8832 processPosition(mapper, 100, 200);
8833 processSync(mapper);
8834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8835 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8836 ASSERT_EQ(0, motionArgs.buttonState);
8837
8838 // press BTN_LEFT, release BTN_LEFT
8839 processKey(mapper, BTN_LEFT, 1);
8840 processSync(mapper);
8841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8842 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8843 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8844
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8846 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8847 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8848
Michael Wrightd02c5b62014-02-10 15:10:22 -08008849 processKey(mapper, BTN_LEFT, 0);
8850 processSync(mapper);
8851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008852 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008853 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008854
8855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008856 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008857 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008858
8859 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8860 processKey(mapper, BTN_RIGHT, 1);
8861 processKey(mapper, BTN_MIDDLE, 1);
8862 processSync(mapper);
8863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8864 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8865 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8866 motionArgs.buttonState);
8867
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8869 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8870 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8871
8872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8873 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8874 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8875 motionArgs.buttonState);
8876
Michael Wrightd02c5b62014-02-10 15:10:22 -08008877 processKey(mapper, BTN_RIGHT, 0);
8878 processSync(mapper);
8879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008880 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008881 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008882
8883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008884 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008885 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008886
8887 processKey(mapper, BTN_MIDDLE, 0);
8888 processSync(mapper);
8889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008890 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008891 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008892
8893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008894 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008895 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008896
8897 // press BTN_BACK, release BTN_BACK
8898 processKey(mapper, BTN_BACK, 1);
8899 processSync(mapper);
8900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8901 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8902 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008903
Michael Wrightd02c5b62014-02-10 15:10:22 -08008904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008905 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008906 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8907
8908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8909 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8910 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008911
8912 processKey(mapper, BTN_BACK, 0);
8913 processSync(mapper);
8914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008915 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008916 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008917
8918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008919 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008920 ASSERT_EQ(0, motionArgs.buttonState);
8921
Michael Wrightd02c5b62014-02-10 15:10:22 -08008922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8923 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8924 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8925
8926 // press BTN_SIDE, release BTN_SIDE
8927 processKey(mapper, BTN_SIDE, 1);
8928 processSync(mapper);
8929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8930 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8931 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008932
Michael Wrightd02c5b62014-02-10 15:10:22 -08008933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008934 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008935 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8936
8937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8938 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8939 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008940
8941 processKey(mapper, BTN_SIDE, 0);
8942 processSync(mapper);
8943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008944 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008945 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008946
8947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008948 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008949 ASSERT_EQ(0, motionArgs.buttonState);
8950
Michael Wrightd02c5b62014-02-10 15:10:22 -08008951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8952 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8953 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8954
8955 // press BTN_FORWARD, release BTN_FORWARD
8956 processKey(mapper, BTN_FORWARD, 1);
8957 processSync(mapper);
8958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8959 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8960 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008961
Michael Wrightd02c5b62014-02-10 15:10:22 -08008962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008963 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008964 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8965
8966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8967 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8968 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008969
8970 processKey(mapper, BTN_FORWARD, 0);
8971 processSync(mapper);
8972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008973 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008974 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008975
8976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008977 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008978 ASSERT_EQ(0, motionArgs.buttonState);
8979
Michael Wrightd02c5b62014-02-10 15:10:22 -08008980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8981 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8982 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8983
8984 // press BTN_EXTRA, release BTN_EXTRA
8985 processKey(mapper, BTN_EXTRA, 1);
8986 processSync(mapper);
8987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8988 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8989 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008990
Michael Wrightd02c5b62014-02-10 15:10:22 -08008991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008992 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008993 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8994
8995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8996 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8997 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008998
8999 processKey(mapper, BTN_EXTRA, 0);
9000 processSync(mapper);
9001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009002 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009003 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009004
9005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009007 ASSERT_EQ(0, motionArgs.buttonState);
9008
Michael Wrightd02c5b62014-02-10 15:10:22 -08009009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9010 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9011 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9012
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9014
Michael Wrightd02c5b62014-02-10 15:10:22 -08009015 // press BTN_STYLUS, release BTN_STYLUS
9016 processKey(mapper, BTN_STYLUS, 1);
9017 processSync(mapper);
9018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009020 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
9021
9022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9023 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9024 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009025
9026 processKey(mapper, BTN_STYLUS, 0);
9027 processSync(mapper);
9028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009029 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009030 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009031
9032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009033 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009034 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009035
9036 // press BTN_STYLUS2, release BTN_STYLUS2
9037 processKey(mapper, BTN_STYLUS2, 1);
9038 processSync(mapper);
9039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9040 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009041 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
9042
9043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9044 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9045 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009046
9047 processKey(mapper, BTN_STYLUS2, 0);
9048 processSync(mapper);
9049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009050 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009051 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009052
9053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009054 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009055 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009056
9057 // release touch
9058 processId(mapper, -1);
9059 processSync(mapper);
9060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9061 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9062 ASSERT_EQ(0, motionArgs.buttonState);
9063}
9064
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009065TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
9066 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009067 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009068 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00009069 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009070
9071 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
9072 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
9073
9074 // Touch down.
9075 processId(mapper, 1);
9076 processPosition(mapper, 100, 200);
9077 processSync(mapper);
9078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9079 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
9080
9081 // Press and release button mapped to the primary stylus button.
9082 processKey(mapper, BTN_A, 1);
9083 processSync(mapper);
9084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9085 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9086 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9088 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9089 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9090
9091 processKey(mapper, BTN_A, 0);
9092 processSync(mapper);
9093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9094 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9096 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9097
9098 // Press and release the HID usage mapped to the secondary stylus button.
9099 processHidUsage(mapper, 0xabcd, 1);
9100 processSync(mapper);
9101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9102 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9103 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9105 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9106 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9107
9108 processHidUsage(mapper, 0xabcd, 0);
9109 processSync(mapper);
9110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9111 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9113 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9114
9115 // Release touch.
9116 processId(mapper, -1);
9117 processSync(mapper);
9118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9119 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
9120}
9121
Michael Wrightd02c5b62014-02-10 15:10:22 -08009122TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009123 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009124 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009125 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009126 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009127
9128 NotifyMotionArgs motionArgs;
9129
9130 // default tool type is finger
9131 processId(mapper, 1);
9132 processPosition(mapper, 100, 200);
9133 processSync(mapper);
9134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9135 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009136 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009137
9138 // eraser
9139 processKey(mapper, BTN_TOOL_RUBBER, 1);
9140 processSync(mapper);
9141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009143 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009144
9145 // stylus
9146 processKey(mapper, BTN_TOOL_RUBBER, 0);
9147 processKey(mapper, BTN_TOOL_PEN, 1);
9148 processSync(mapper);
9149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009151 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009152
9153 // brush
9154 processKey(mapper, BTN_TOOL_PEN, 0);
9155 processKey(mapper, BTN_TOOL_BRUSH, 1);
9156 processSync(mapper);
9157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009159 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009160
9161 // pencil
9162 processKey(mapper, BTN_TOOL_BRUSH, 0);
9163 processKey(mapper, BTN_TOOL_PENCIL, 1);
9164 processSync(mapper);
9165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009167 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009168
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009169 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009170 processKey(mapper, BTN_TOOL_PENCIL, 0);
9171 processKey(mapper, BTN_TOOL_AIRBRUSH, 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::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009176
9177 // mouse
9178 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9179 processKey(mapper, BTN_TOOL_MOUSE, 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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009184
9185 // lens
9186 processKey(mapper, BTN_TOOL_MOUSE, 0);
9187 processKey(mapper, BTN_TOOL_LENS, 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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009192
9193 // double-tap
9194 processKey(mapper, BTN_TOOL_LENS, 0);
9195 processKey(mapper, BTN_TOOL_DOUBLETAP, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009200
9201 // triple-tap
9202 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9203 processKey(mapper, BTN_TOOL_TRIPLETAP, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009208
9209 // quad-tap
9210 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9211 processKey(mapper, BTN_TOOL_QUADTAP, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009216
9217 // finger
9218 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9219 processKey(mapper, BTN_TOOL_FINGER, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009224
9225 // stylus trumps finger
9226 processKey(mapper, BTN_TOOL_PEN, 1);
9227 processSync(mapper);
9228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009230 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009231
9232 // eraser trumps stylus
9233 processKey(mapper, BTN_TOOL_RUBBER, 1);
9234 processSync(mapper);
9235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009237 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009238
9239 // mouse trumps eraser
9240 processKey(mapper, BTN_TOOL_MOUSE, 1);
9241 processSync(mapper);
9242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9243 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009244 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009245
9246 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9247 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9248 processSync(mapper);
9249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009251 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009252
9253 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9254 processToolType(mapper, MT_TOOL_PEN);
9255 processSync(mapper);
9256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009258 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009259
9260 // back to default tool type
9261 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9262 processKey(mapper, BTN_TOOL_MOUSE, 0);
9263 processKey(mapper, BTN_TOOL_RUBBER, 0);
9264 processKey(mapper, BTN_TOOL_PEN, 0);
9265 processKey(mapper, BTN_TOOL_FINGER, 0);
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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009270}
9271
9272TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009273 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009274 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009275 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009276 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009277 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009278
9279 NotifyMotionArgs motionArgs;
9280
9281 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9282 processId(mapper, 1);
9283 processPosition(mapper, 100, 200);
9284 processSync(mapper);
9285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9286 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9288 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9289
9290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9291 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9293 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9294
9295 // move a little
9296 processPosition(mapper, 150, 250);
9297 processSync(mapper);
9298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9299 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9301 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9302
9303 // down when BTN_TOUCH is pressed, pressure defaults to 1
9304 processKey(mapper, BTN_TOUCH, 1);
9305 processSync(mapper);
9306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9307 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9309 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9310
9311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9312 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9314 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9315
9316 // up when BTN_TOUCH is released, hover restored
9317 processKey(mapper, BTN_TOUCH, 0);
9318 processSync(mapper);
9319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9320 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9321 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9322 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9323
9324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9325 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9327 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9328
9329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9330 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9332 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9333
9334 // exit hover when pointer goes away
9335 processId(mapper, -1);
9336 processSync(mapper);
9337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9338 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9340 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9341}
9342
9343TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009344 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009345 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009346 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009347 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009348
9349 NotifyMotionArgs motionArgs;
9350
9351 // initially hovering because pressure is 0
9352 processId(mapper, 1);
9353 processPosition(mapper, 100, 200);
9354 processPressure(mapper, 0);
9355 processSync(mapper);
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(100), toDisplayY(200), 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(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9365
9366 // move a little
9367 processPosition(mapper, 150, 250);
9368 processSync(mapper);
9369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9370 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // down when pressure becomes non-zero
9375 processPressure(mapper, RAW_PRESSURE_MAX);
9376 processSync(mapper);
9377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9378 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9380 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9381
9382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9383 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9385 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9386
9387 // up when pressure becomes 0, hover restored
9388 processPressure(mapper, 0);
9389 processSync(mapper);
9390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9391 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9393 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9394
9395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9396 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9398 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9399
9400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9401 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9403 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9404
9405 // exit hover when pointer goes away
9406 processId(mapper, -1);
9407 processSync(mapper);
9408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9409 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9411 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9412}
9413
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009414/**
9415 * Set the input device port <--> display port associations, and check that the
9416 * events are routed to the display that matches the display port.
9417 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9418 */
9419TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009420 const std::string usb2 = "USB2";
9421 const uint8_t hdmi1 = 0;
9422 const uint8_t hdmi2 = 1;
9423 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009424 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009425
9426 addConfigurationProperty("touch.deviceType", "touchScreen");
9427 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009428 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009429
9430 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9431 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9432
9433 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9434 // for this input device is specified, and the matching viewport is not present,
9435 // the input device should be disabled (at the mapper level).
9436
9437 // Add viewport for display 2 on hdmi2
9438 prepareSecondaryDisplay(type, hdmi2);
9439 // Send a touch event
9440 processPosition(mapper, 100, 100);
9441 processSync(mapper);
9442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9443
9444 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009445 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009446 // Send a touch event again
9447 processPosition(mapper, 100, 100);
9448 processSync(mapper);
9449
9450 NotifyMotionArgs args;
9451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9452 ASSERT_EQ(DISPLAY_ID, args.displayId);
9453}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009454
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009455TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9456 addConfigurationProperty("touch.deviceType", "touchScreen");
9457 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009458 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009459
9460 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9461
Michael Wrighta9cf4192022-12-01 23:46:39 +00009462 prepareDisplay(ui::ROTATION_0);
9463 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009464
9465 // Send a touch event
9466 processPosition(mapper, 100, 100);
9467 processSync(mapper);
9468
9469 NotifyMotionArgs args;
9470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9471 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9472}
9473
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009474TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009475 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009476 std::shared_ptr<FakePointerController> fakePointerController =
9477 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009478 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009479 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009480 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009481
Garfield Tan888a6a42020-01-09 11:39:16 -08009482 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009483 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009484
Michael Wrighta9cf4192022-12-01 23:46:39 +00009485 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009486 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009487 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009488
Josep del Río2d8c79a2023-01-23 19:33:50 +00009489 // Check source is mouse that would obtain the PointerController.
9490 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009491
9492 NotifyMotionArgs motionArgs;
9493 processPosition(mapper, 100, 100);
9494 processSync(mapper);
9495
9496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9497 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9498 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9499}
9500
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009501/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009502 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9503 */
9504TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9505 addConfigurationProperty("touch.deviceType", "touchScreen");
9506 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009507 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009508
Michael Wrighta9cf4192022-12-01 23:46:39 +00009509 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009510 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9511 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9512 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9513 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009514
9515 NotifyMotionArgs args;
9516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9517 ASSERT_EQ(26, args.readTime);
9518
Harry Cutts33476232023-01-30 19:57:29 +00009519 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9520 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9521 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009522
9523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9524 ASSERT_EQ(33, args.readTime);
9525}
9526
9527/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009528 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9529 * events should not be delivered to the listener.
9530 */
9531TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9532 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009533 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009534 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009535 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009536 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009537 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009538 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009539
9540 NotifyMotionArgs motionArgs;
9541 processPosition(mapper, 100, 100);
9542 processSync(mapper);
9543
9544 mFakeListener->assertNotifyMotionWasNotCalled();
9545}
9546
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009547/**
9548 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9549 * the touch mapper can process the events and the events can be delivered to the listener.
9550 */
9551TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9552 addConfigurationProperty("touch.deviceType", "touchScreen");
9553 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009554 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009555 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009556 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009557 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009558 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009559
9560 NotifyMotionArgs motionArgs;
9561 processPosition(mapper, 100, 100);
9562 processSync(mapper);
9563
9564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9565 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9566}
9567
Josh Thielene986aed2023-06-01 14:17:30 +00009568/**
9569 * When the viewport is deactivated (isActive transitions from true to false),
9570 * and touch.enableForInactiveViewport is false, touches prior to the transition
9571 * should be cancelled.
9572 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08009573TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9574 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009575 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009576 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009577 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009578 std::optional<DisplayViewport> optionalDisplayViewport =
9579 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9580 ASSERT_TRUE(optionalDisplayViewport.has_value());
9581 DisplayViewport displayViewport = *optionalDisplayViewport;
9582
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009583 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009584 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009585 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08009586
9587 // Finger down
9588 int32_t x = 100, y = 100;
9589 processPosition(mapper, x, y);
9590 processSync(mapper);
9591
9592 NotifyMotionArgs motionArgs;
9593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9594 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9595
9596 // Deactivate display viewport
9597 displayViewport.isActive = false;
9598 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009599 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009600
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009601 // The ongoing touch should be canceled immediately
9602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9603 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9604
9605 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009606 x += 10, y += 10;
9607 processPosition(mapper, x, y);
9608 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009610
9611 // Reactivate display viewport
9612 displayViewport.isActive = true;
9613 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009614 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009615
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009616 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009617 x += 10, y += 10;
9618 processPosition(mapper, x, y);
9619 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9621 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009622}
9623
Josh Thielene986aed2023-06-01 14:17:30 +00009624/**
9625 * When the viewport is deactivated (isActive transitions from true to false),
9626 * and touch.enableForInactiveViewport is true, touches prior to the transition
9627 * should not be cancelled.
9628 */
9629TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
9630 addConfigurationProperty("touch.deviceType", "touchScreen");
9631 addConfigurationProperty("touch.enableForInactiveViewport", "1");
9632 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
9633 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
9634 std::optional<DisplayViewport> optionalDisplayViewport =
9635 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9636 ASSERT_TRUE(optionalDisplayViewport.has_value());
9637 DisplayViewport displayViewport = *optionalDisplayViewport;
9638
9639 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9640 prepareAxes(POSITION);
9641 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
9642
9643 // Finger down
9644 int32_t x = 100, y = 100;
9645 processPosition(mapper, x, y);
9646 processSync(mapper);
9647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9648 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9649
9650 // Deactivate display viewport
9651 displayViewport.isActive = false;
9652 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9653 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9654
9655 // The ongoing touch should not be canceled
9656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9657
9658 // Finger move is not ignored
9659 x += 10, y += 10;
9660 processPosition(mapper, x, y);
9661 processSync(mapper);
9662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9663 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
9664
9665 // Reactivate display viewport
9666 displayViewport.isActive = true;
9667 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9668 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9669
9670 // Finger move continues and does not start new gesture
9671 x += 10, y += 10;
9672 processPosition(mapper, x, y);
9673 processSync(mapper);
9674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9675 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
9676}
9677
Arthur Hung7c645402019-01-25 17:45:42 +08009678TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9679 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009680 prepareAxes(POSITION | ID | SLOT);
9681 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009682 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009683
9684 // Create the second touch screen device, and enable multi fingers.
9685 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009686 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009687 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009688 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009689 std::shared_ptr<InputDevice> device2 =
9690 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009691 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009692
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009693 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009694 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009695 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009696 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009697 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009698 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009699 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009700 /*flat=*/0, /*fuzz=*/0);
9701 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009702 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9703 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009704
9705 // Setup the second touch screen device.
Arpit Singha8c236b2023-04-25 13:56:05 +00009706 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
9707 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
9708 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009709 std::list<NotifyArgs> unused =
9710 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009711 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009712 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009713
9714 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009715 std::shared_ptr<FakePointerController> fakePointerController =
9716 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009717 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009718
9719 // Setup policy for associated displays and show touches.
9720 const uint8_t hdmi1 = 0;
9721 const uint8_t hdmi2 = 1;
9722 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9723 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9724 mFakePolicy->setShowTouches(true);
9725
9726 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009727 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009728 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009729
9730 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009731 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009732 InputReaderConfiguration::Change::DISPLAY_INFO |
9733 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009734
9735 // Two fingers down at default display.
9736 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9737 processPosition(mapper, x1, y1);
9738 processId(mapper, 1);
9739 processSlot(mapper, 1);
9740 processPosition(mapper, x2, y2);
9741 processId(mapper, 2);
9742 processSync(mapper);
9743
9744 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9745 fakePointerController->getSpots().find(DISPLAY_ID);
9746 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9747 ASSERT_EQ(size_t(2), iter->second.size());
9748
9749 // Two fingers down at second display.
9750 processPosition(mapper2, x1, y1);
9751 processId(mapper2, 1);
9752 processSlot(mapper2, 1);
9753 processPosition(mapper2, x2, y2);
9754 processId(mapper2, 2);
9755 processSync(mapper2);
9756
9757 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9758 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9759 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009760
9761 // Disable the show touches configuration and ensure the spots are cleared.
9762 mFakePolicy->setShowTouches(false);
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::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009765
9766 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009767}
9768
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009769TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009770 prepareAxes(POSITION);
9771 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009772 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009773 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009774
9775 NotifyMotionArgs motionArgs;
9776 // Unrotated video frame
9777 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9778 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009779 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009780 processPosition(mapper, 100, 200);
9781 processSync(mapper);
9782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9783 ASSERT_EQ(frames, motionArgs.videoFrames);
9784
9785 // Subsequent touch events should not have any videoframes
9786 // This is implemented separately in FakeEventHub,
9787 // but that should match the behaviour of TouchVideoDevice.
9788 processPosition(mapper, 200, 200);
9789 processSync(mapper);
9790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9791 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9792}
9793
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009794TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009795 prepareAxes(POSITION);
9796 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009797 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009798 // Unrotated video frame
9799 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9800 NotifyMotionArgs motionArgs;
9801
9802 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009803 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009804 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9805 clearViewports();
9806 prepareDisplay(orientation);
9807 std::vector<TouchVideoFrame> frames{frame};
9808 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9809 processPosition(mapper, 100, 200);
9810 processSync(mapper);
9811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9812 ASSERT_EQ(frames, motionArgs.videoFrames);
9813 }
9814}
9815
9816TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9817 prepareAxes(POSITION);
9818 addConfigurationProperty("touch.deviceType", "touchScreen");
9819 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9820 // orientation-aware are affected by display rotation.
9821 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009822 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009823 // Unrotated video frame
9824 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9825 NotifyMotionArgs motionArgs;
9826
9827 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009828 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009829 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9830 clearViewports();
9831 prepareDisplay(orientation);
9832 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009833 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009834 processPosition(mapper, 100, 200);
9835 processSync(mapper);
9836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009837 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9838 // compared to the display. This is so that when the window transform (which contains the
9839 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9840 // window's coordinate space.
9841 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009842 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009843
9844 // Release finger.
9845 processSync(mapper);
9846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009847 }
9848}
9849
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009850TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009851 prepareAxes(POSITION);
9852 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009853 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009854 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9855 // so mix these.
9856 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9857 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9858 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9859 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9860 NotifyMotionArgs motionArgs;
9861
Michael Wrighta9cf4192022-12-01 23:46:39 +00009862 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009863 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009864 processPosition(mapper, 100, 200);
9865 processSync(mapper);
9866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009867 ASSERT_EQ(frames, motionArgs.videoFrames);
9868}
9869
9870TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9871 prepareAxes(POSITION);
9872 addConfigurationProperty("touch.deviceType", "touchScreen");
9873 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9874 // orientation-aware are affected by display rotation.
9875 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009876 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009877 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9878 // so mix these.
9879 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9880 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9881 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9882 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9883 NotifyMotionArgs motionArgs;
9884
Michael Wrighta9cf4192022-12-01 23:46:39 +00009885 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009886 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9887 processPosition(mapper, 100, 200);
9888 processSync(mapper);
9889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9890 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9891 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9892 // compared to the display. This is so that when the window transform (which contains the
9893 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9894 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009895 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009896 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009897 ASSERT_EQ(frames, motionArgs.videoFrames);
9898}
9899
Arthur Hung9da14732019-09-02 16:16:58 +08009900/**
9901 * If we had defined port associations, but the viewport is not ready, the touch device would be
9902 * expected to be disabled, and it should be enabled after the viewport has found.
9903 */
9904TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009905 constexpr uint8_t hdmi2 = 1;
9906 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009907 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009908
9909 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9910
9911 addConfigurationProperty("touch.deviceType", "touchScreen");
9912 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009913 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009914
9915 ASSERT_EQ(mDevice->isEnabled(), false);
9916
9917 // Add display on hdmi2, the device should be enabled and can receive touch event.
9918 prepareSecondaryDisplay(type, hdmi2);
9919 ASSERT_EQ(mDevice->isEnabled(), true);
9920
9921 // Send a touch event.
9922 processPosition(mapper, 100, 100);
9923 processSync(mapper);
9924
9925 NotifyMotionArgs args;
9926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9927 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9928}
9929
Arthur Hung421eb1c2020-01-16 00:09:42 +08009930TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009931 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009932 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009933 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009934 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009935
9936 NotifyMotionArgs motionArgs;
9937
9938 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9939 // finger down
9940 processId(mapper, 1);
9941 processPosition(mapper, x1, y1);
9942 processSync(mapper);
9943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9944 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009945 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009946
9947 // finger move
9948 processId(mapper, 1);
9949 processPosition(mapper, x2, y2);
9950 processSync(mapper);
9951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9952 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009953 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009954
9955 // finger up.
9956 processId(mapper, -1);
9957 processSync(mapper);
9958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9959 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009960 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009961
9962 // new finger down
9963 processId(mapper, 1);
9964 processPosition(mapper, x3, y3);
9965 processSync(mapper);
9966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9967 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009968 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009969}
9970
9971/**
arthurhungcc7f9802020-04-30 17:55:40 +08009972 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9973 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009974 */
arthurhungcc7f9802020-04-30 17:55:40 +08009975TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009976 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009977 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009978 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009979 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009980
9981 NotifyMotionArgs motionArgs;
9982
9983 // default tool type is finger
9984 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009985 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009986 processPosition(mapper, x1, y1);
9987 processSync(mapper);
9988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9989 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009990 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009991
9992 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9993 processToolType(mapper, MT_TOOL_PALM);
9994 processSync(mapper);
9995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9996 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9997
9998 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009999 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010000 processPosition(mapper, x2, y2);
10001 processSync(mapper);
10002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10003
10004 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +080010005 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010006 processSync(mapper);
10007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10008
10009 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +080010010 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010011 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010012 processPosition(mapper, x3, y3);
10013 processSync(mapper);
10014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10015 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010016 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010017}
10018
arthurhungbf89a482020-04-17 17:37:55 +080010019/**
arthurhungcc7f9802020-04-30 17:55:40 +080010020 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10021 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +080010022 */
arthurhungcc7f9802020-04-30 17:55:40 +080010023TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +080010024 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010025 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +080010026 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010027 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +080010028
10029 NotifyMotionArgs motionArgs;
10030
10031 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +080010032 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10033 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010034 processPosition(mapper, x1, y1);
10035 processSync(mapper);
10036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10037 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010038 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +080010039
10040 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +080010041 processSlot(mapper, SECOND_SLOT);
10042 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010043 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +080010044 processSync(mapper);
10045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010046 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010047 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010048
10049 // If the tool type of the first finger changes to MT_TOOL_PALM,
10050 // we expect to receive ACTION_POINTER_UP with cancel flag.
10051 processSlot(mapper, FIRST_SLOT);
10052 processId(mapper, FIRST_TRACKING_ID);
10053 processToolType(mapper, MT_TOOL_PALM);
10054 processSync(mapper);
10055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010056 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010057 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10058
10059 // The following MOVE events of second finger should be processed.
10060 processSlot(mapper, SECOND_SLOT);
10061 processId(mapper, SECOND_TRACKING_ID);
10062 processPosition(mapper, x2 + 1, y2 + 1);
10063 processSync(mapper);
10064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10065 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010066 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010067
10068 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
10069 // it. Second finger receive move.
10070 processSlot(mapper, FIRST_SLOT);
10071 processId(mapper, INVALID_TRACKING_ID);
10072 processSync(mapper);
10073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010075 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010076
10077 // Second finger keeps moving.
10078 processSlot(mapper, SECOND_SLOT);
10079 processId(mapper, SECOND_TRACKING_ID);
10080 processPosition(mapper, x2 + 2, y2 + 2);
10081 processSync(mapper);
10082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010084 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010085
10086 // Second finger up.
10087 processId(mapper, INVALID_TRACKING_ID);
10088 processSync(mapper);
10089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10090 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10091 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10092}
10093
10094/**
10095 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
10096 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
10097 */
10098TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
10099 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010100 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010101 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010102 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010103
10104 NotifyMotionArgs motionArgs;
10105
10106 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10107 // First finger down.
10108 processId(mapper, FIRST_TRACKING_ID);
10109 processPosition(mapper, x1, y1);
10110 processSync(mapper);
10111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10112 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010113 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010114
10115 // Second finger down.
10116 processSlot(mapper, SECOND_SLOT);
10117 processId(mapper, SECOND_TRACKING_ID);
10118 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +080010119 processSync(mapper);
10120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010121 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010122 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +080010123
arthurhungcc7f9802020-04-30 17:55:40 +080010124 // If the tool type of the first finger changes to MT_TOOL_PALM,
10125 // we expect to receive ACTION_POINTER_UP with cancel flag.
10126 processSlot(mapper, FIRST_SLOT);
10127 processId(mapper, FIRST_TRACKING_ID);
10128 processToolType(mapper, MT_TOOL_PALM);
10129 processSync(mapper);
10130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010131 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010132 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10133
10134 // Second finger keeps moving.
10135 processSlot(mapper, SECOND_SLOT);
10136 processId(mapper, SECOND_TRACKING_ID);
10137 processPosition(mapper, x2 + 1, y2 + 1);
10138 processSync(mapper);
10139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10141
10142 // second finger becomes palm, receive cancel due to only 1 finger is active.
10143 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010144 processToolType(mapper, MT_TOOL_PALM);
10145 processSync(mapper);
10146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10147 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10148
arthurhungcc7f9802020-04-30 17:55:40 +080010149 // third finger down.
10150 processSlot(mapper, THIRD_SLOT);
10151 processId(mapper, THIRD_TRACKING_ID);
10152 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +080010153 processPosition(mapper, x3, y3);
10154 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +080010155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10156 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010157 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010158 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010159
10160 // third finger move
10161 processId(mapper, THIRD_TRACKING_ID);
10162 processPosition(mapper, x3 + 1, y3 + 1);
10163 processSync(mapper);
10164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10166
10167 // first finger up, third finger receive move.
10168 processSlot(mapper, FIRST_SLOT);
10169 processId(mapper, INVALID_TRACKING_ID);
10170 processSync(mapper);
10171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010173 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010174
10175 // second finger up, third finger receive move.
10176 processSlot(mapper, SECOND_SLOT);
10177 processId(mapper, INVALID_TRACKING_ID);
10178 processSync(mapper);
10179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010181 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010182
10183 // third finger up.
10184 processSlot(mapper, THIRD_SLOT);
10185 processId(mapper, INVALID_TRACKING_ID);
10186 processSync(mapper);
10187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10188 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10189 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10190}
10191
10192/**
10193 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10194 * and the active finger could still be allowed to receive the events
10195 */
10196TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10197 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010198 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010199 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010200 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010201
10202 NotifyMotionArgs motionArgs;
10203
10204 // default tool type is finger
10205 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10206 processId(mapper, FIRST_TRACKING_ID);
10207 processPosition(mapper, x1, y1);
10208 processSync(mapper);
10209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10210 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010211 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010212
10213 // Second finger down.
10214 processSlot(mapper, SECOND_SLOT);
10215 processId(mapper, SECOND_TRACKING_ID);
10216 processPosition(mapper, x2, y2);
10217 processSync(mapper);
10218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010219 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010220 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010221
10222 // If the tool type of the second finger changes to MT_TOOL_PALM,
10223 // we expect to receive ACTION_POINTER_UP with cancel flag.
10224 processId(mapper, SECOND_TRACKING_ID);
10225 processToolType(mapper, MT_TOOL_PALM);
10226 processSync(mapper);
10227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010228 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010229 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10230
10231 // The following MOVE event should be processed.
10232 processSlot(mapper, FIRST_SLOT);
10233 processId(mapper, FIRST_TRACKING_ID);
10234 processPosition(mapper, x1 + 1, y1 + 1);
10235 processSync(mapper);
10236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010238 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010239
10240 // second finger up.
10241 processSlot(mapper, SECOND_SLOT);
10242 processId(mapper, INVALID_TRACKING_ID);
10243 processSync(mapper);
10244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10246
10247 // first finger keep moving
10248 processSlot(mapper, FIRST_SLOT);
10249 processId(mapper, FIRST_TRACKING_ID);
10250 processPosition(mapper, x1 + 2, y1 + 2);
10251 processSync(mapper);
10252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10254
10255 // first finger up.
10256 processId(mapper, INVALID_TRACKING_ID);
10257 processSync(mapper);
10258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10259 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10260 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010261}
10262
Arthur Hung9ad18942021-06-19 02:04:46 +000010263/**
10264 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10265 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10266 * cause slot be valid again.
10267 */
10268TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10269 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010270 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +000010271 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010272 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +000010273
10274 NotifyMotionArgs motionArgs;
10275
10276 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10277 // First finger down.
10278 processId(mapper, FIRST_TRACKING_ID);
10279 processPosition(mapper, x1, y1);
10280 processPressure(mapper, RAW_PRESSURE_MAX);
10281 processSync(mapper);
10282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10283 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010284 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010285
10286 // First finger move.
10287 processId(mapper, FIRST_TRACKING_ID);
10288 processPosition(mapper, x1 + 1, y1 + 1);
10289 processPressure(mapper, RAW_PRESSURE_MAX);
10290 processSync(mapper);
10291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10292 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010293 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010294
10295 // Second finger down.
10296 processSlot(mapper, SECOND_SLOT);
10297 processId(mapper, SECOND_TRACKING_ID);
10298 processPosition(mapper, x2, y2);
10299 processPressure(mapper, RAW_PRESSURE_MAX);
10300 processSync(mapper);
10301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010302 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010303 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010304
10305 // second finger up with some unexpected data.
10306 processSlot(mapper, SECOND_SLOT);
10307 processId(mapper, INVALID_TRACKING_ID);
10308 processPosition(mapper, x2, y2);
10309 processSync(mapper);
10310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010311 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010312 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010313
10314 // first finger up with some unexpected data.
10315 processSlot(mapper, FIRST_SLOT);
10316 processId(mapper, INVALID_TRACKING_ID);
10317 processPosition(mapper, x2, y2);
10318 processPressure(mapper, RAW_PRESSURE_MAX);
10319 processSync(mapper);
10320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10321 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010322 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010323}
10324
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010325TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10326 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010327 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010328 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010329 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010330
10331 // First finger down.
10332 processId(mapper, FIRST_TRACKING_ID);
10333 processPosition(mapper, 100, 200);
10334 processPressure(mapper, RAW_PRESSURE_MAX);
10335 processSync(mapper);
10336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10337 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10338
10339 // Second finger down.
10340 processSlot(mapper, SECOND_SLOT);
10341 processId(mapper, SECOND_TRACKING_ID);
10342 processPosition(mapper, 300, 400);
10343 processPressure(mapper, RAW_PRESSURE_MAX);
10344 processSync(mapper);
10345 ASSERT_NO_FATAL_FAILURE(
10346 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10347
10348 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010349 // preserved. Resetting should cancel the ongoing gesture.
10350 resetMapper(mapper, ARBITRARY_TIME);
10351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10352 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010353
10354 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10355 // the existing touch state to generate a down event.
10356 processPosition(mapper, 301, 302);
10357 processSync(mapper);
10358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10359 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10361 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10362
10363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10364}
10365
10366TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10367 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010368 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010369 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010370 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010371
10372 // First finger touches down and releases.
10373 processId(mapper, FIRST_TRACKING_ID);
10374 processPosition(mapper, 100, 200);
10375 processPressure(mapper, RAW_PRESSURE_MAX);
10376 processSync(mapper);
10377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10378 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10379 processId(mapper, INVALID_TRACKING_ID);
10380 processSync(mapper);
10381 ASSERT_NO_FATAL_FAILURE(
10382 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10383
10384 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10385 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010386 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10388
10389 // Send an empty sync frame. Since there are no pointers, no events are generated.
10390 processSync(mapper);
10391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10392}
10393
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010394TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010395 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010396 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010397 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010398 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010400
10401 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10402 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10403 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10404 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10405 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10406
10407 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010408 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010409 processId(mapper, FIRST_TRACKING_ID);
10410 processToolType(mapper, MT_TOOL_PEN);
10411 processPosition(mapper, 100, 200);
10412 processPressure(mapper, RAW_PRESSURE_MAX);
10413 processSync(mapper);
10414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10415 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10416 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010417 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010418
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010419 // Now that we know the device supports styluses, ensure that the device is re-configured with
10420 // the stylus source.
10421 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10422 {
10423 const auto& devices = mReader->getInputDevices();
10424 auto deviceInfo =
10425 std::find_if(devices.begin(), devices.end(),
10426 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10427 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10428 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10429 }
10430
10431 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10433
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010434 processId(mapper, INVALID_TRACKING_ID);
10435 processSync(mapper);
10436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10437 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10438 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010439 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010440}
10441
Seunghwan Choi356026c2023-02-01 14:37:25 +090010442TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10443 addConfigurationProperty("touch.deviceType", "touchScreen");
10444 prepareDisplay(ui::ROTATION_0);
10445 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10446 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10447 // indicate stylus presence dynamically.
10448 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10449 std::shared_ptr<FakePointerController> fakePointerController =
10450 std::make_shared<FakePointerController>();
10451 mFakePolicy->setPointerController(fakePointerController);
10452 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +000010453 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010454
10455 processId(mapper, FIRST_TRACKING_ID);
10456 processPressure(mapper, RAW_PRESSURE_MIN);
10457 processPosition(mapper, 100, 200);
10458 processToolType(mapper, MT_TOOL_PEN);
10459 processSync(mapper);
10460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10461 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010462 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010463 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10464 ASSERT_TRUE(fakePointerController->isPointerShown());
10465 ASSERT_NO_FATAL_FAILURE(
10466 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10467}
10468
10469TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10470 addConfigurationProperty("touch.deviceType", "touchScreen");
10471 prepareDisplay(ui::ROTATION_0);
10472 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10473 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10474 // indicate stylus presence dynamically.
10475 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10476 std::shared_ptr<FakePointerController> fakePointerController =
10477 std::make_shared<FakePointerController>();
10478 mFakePolicy->setPointerController(fakePointerController);
10479 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010480 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010481
10482 processId(mapper, FIRST_TRACKING_ID);
10483 processPressure(mapper, RAW_PRESSURE_MIN);
10484 processPosition(mapper, 100, 200);
10485 processToolType(mapper, MT_TOOL_PEN);
10486 processSync(mapper);
10487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10488 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010489 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010490 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10491 ASSERT_FALSE(fakePointerController->isPointerShown());
10492}
10493
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010494// --- MultiTouchInputMapperTest_ExternalDevice ---
10495
10496class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10497protected:
Chris Yea52ade12020-08-27 16:49:20 -070010498 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010499};
10500
10501/**
10502 * Expect fallback to internal viewport if device is external and external viewport is not present.
10503 */
10504TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10505 prepareAxes(POSITION);
10506 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010507 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010508 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010509
10510 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10511
10512 NotifyMotionArgs motionArgs;
10513
10514 // Expect the event to be sent to the internal viewport,
10515 // because an external viewport is not present.
10516 processPosition(mapper, 100, 100);
10517 processSync(mapper);
10518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10519 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10520
10521 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010522 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010523 processPosition(mapper, 100, 100);
10524 processSync(mapper);
10525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10526 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10527}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010528
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010529TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10530 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10531 std::shared_ptr<FakePointerController> fakePointerController =
10532 std::make_shared<FakePointerController>();
10533 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10534 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010535
10536 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010537 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010538 prepareAxes(POSITION | ID | SLOT);
10539 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10540 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10541 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010542 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010543 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010544
10545 // captured touchpad should be a touchpad source
10546 NotifyDeviceResetArgs resetArgs;
10547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10548 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10549
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010550 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010551
10552 const InputDeviceInfo::MotionRange* relRangeX =
10553 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10554 ASSERT_NE(relRangeX, nullptr);
10555 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10556 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10557 const InputDeviceInfo::MotionRange* relRangeY =
10558 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10559 ASSERT_NE(relRangeY, nullptr);
10560 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10561 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10562
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010563 // run captured pointer tests - note that this is unscaled, so input listener events should be
10564 // identical to what the hardware sends (accounting for any
10565 // calibration).
10566 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010567 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010568 processId(mapper, 1);
10569 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10570 processKey(mapper, BTN_TOUCH, 1);
10571 processSync(mapper);
10572
10573 // expect coord[0] to contain initial location of touch 0
10574 NotifyMotionArgs args;
10575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10576 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010577 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010578 ASSERT_EQ(0, args.pointerProperties[0].id);
10579 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10580 ASSERT_NO_FATAL_FAILURE(
10581 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10582
10583 // FINGER 1 DOWN
10584 processSlot(mapper, 1);
10585 processId(mapper, 2);
10586 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10587 processSync(mapper);
10588
10589 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010591 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010592 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010593 ASSERT_EQ(0, args.pointerProperties[0].id);
10594 ASSERT_EQ(1, args.pointerProperties[1].id);
10595 ASSERT_NO_FATAL_FAILURE(
10596 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10597 ASSERT_NO_FATAL_FAILURE(
10598 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10599
10600 // FINGER 1 MOVE
10601 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10602 processSync(mapper);
10603
10604 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10605 // from move
10606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10607 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10608 ASSERT_NO_FATAL_FAILURE(
10609 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10610 ASSERT_NO_FATAL_FAILURE(
10611 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10612
10613 // FINGER 0 MOVE
10614 processSlot(mapper, 0);
10615 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10616 processSync(mapper);
10617
10618 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10620 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10621 ASSERT_NO_FATAL_FAILURE(
10622 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10623 ASSERT_NO_FATAL_FAILURE(
10624 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10625
10626 // BUTTON DOWN
10627 processKey(mapper, BTN_LEFT, 1);
10628 processSync(mapper);
10629
10630 // touchinputmapper design sends a move before button press
10631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10632 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10634 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10635
10636 // BUTTON UP
10637 processKey(mapper, BTN_LEFT, 0);
10638 processSync(mapper);
10639
10640 // touchinputmapper design sends a move after button release
10641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10642 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10645
10646 // FINGER 0 UP
10647 processId(mapper, -1);
10648 processSync(mapper);
10649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10650 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10651
10652 // FINGER 1 MOVE
10653 processSlot(mapper, 1);
10654 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10655 processSync(mapper);
10656
10657 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010660 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010661 ASSERT_EQ(1, args.pointerProperties[0].id);
10662 ASSERT_NO_FATAL_FAILURE(
10663 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10664
10665 // FINGER 1 UP
10666 processId(mapper, -1);
10667 processKey(mapper, BTN_TOUCH, 0);
10668 processSync(mapper);
10669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10670 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10671
Josep del Río2d8c79a2023-01-23 19:33:50 +000010672 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010673 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010674 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010676 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010677}
10678
10679TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10680 std::shared_ptr<FakePointerController> fakePointerController =
10681 std::make_shared<FakePointerController>();
10682 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10683 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010684
10685 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010686 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010687 prepareAxes(POSITION | ID | SLOT);
10688 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10689 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010690 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010691 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010692 // run uncaptured pointer tests - pushes out generic events
10693 // FINGER 0 DOWN
10694 processId(mapper, 3);
10695 processPosition(mapper, 100, 100);
10696 processKey(mapper, BTN_TOUCH, 1);
10697 processSync(mapper);
10698
10699 // start at (100,100), cursor should be at (0,0) * scale
10700 NotifyMotionArgs args;
10701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10702 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10703 ASSERT_NO_FATAL_FAILURE(
10704 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10705
10706 // FINGER 0 MOVE
10707 processPosition(mapper, 200, 200);
10708 processSync(mapper);
10709
10710 // compute scaling to help with touch position checking
10711 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10712 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10713 float scale =
10714 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10715
10716 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10718 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10719 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10720 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010721
10722 // BUTTON DOWN
10723 processKey(mapper, BTN_LEFT, 1);
10724 processSync(mapper);
10725
10726 // touchinputmapper design sends a move before button press
10727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10728 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10730 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10731
10732 // BUTTON UP
10733 processKey(mapper, BTN_LEFT, 0);
10734 processSync(mapper);
10735
10736 // touchinputmapper design sends a move after button release
10737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10738 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10740 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010741}
10742
10743TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10744 std::shared_ptr<FakePointerController> fakePointerController =
10745 std::make_shared<FakePointerController>();
10746
Michael Wrighta9cf4192022-12-01 23:46:39 +000010747 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010748 prepareAxes(POSITION | ID | SLOT);
10749 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010750 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010751 mFakePolicy->setPointerCapture(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010752 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010753
Josep del Río2d8c79a2023-01-23 19:33:50 +000010754 // uncaptured touchpad should be a pointer device
10755 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010756
Josep del Río2d8c79a2023-01-23 19:33:50 +000010757 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010758 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010759 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010760 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10761}
10762
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010763// --- BluetoothMultiTouchInputMapperTest ---
10764
10765class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10766protected:
10767 void SetUp() override {
10768 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10769 }
10770};
10771
10772TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10773 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010774 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010775 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010776 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010777
10778 nsecs_t kernelEventTime = ARBITRARY_TIME;
10779 nsecs_t expectedEventTime = ARBITRARY_TIME;
10780 // Touch down.
10781 processId(mapper, FIRST_TRACKING_ID);
10782 processPosition(mapper, 100, 200);
10783 processPressure(mapper, RAW_PRESSURE_MAX);
10784 processSync(mapper, ARBITRARY_TIME);
10785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10786 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10787
10788 // Process several events that come in quick succession, according to their timestamps.
10789 for (int i = 0; i < 3; i++) {
10790 constexpr static nsecs_t delta = ms2ns(1);
10791 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10792 kernelEventTime += delta;
10793 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10794
10795 processPosition(mapper, 101 + i, 201 + i);
10796 processSync(mapper, kernelEventTime);
10797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10798 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10799 WithEventTime(expectedEventTime))));
10800 }
10801
10802 // Release the touch.
10803 processId(mapper, INVALID_TRACKING_ID);
10804 processPressure(mapper, RAW_PRESSURE_MIN);
10805 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10807 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10808 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10809}
10810
10811// --- MultiTouchPointerModeTest ---
10812
HQ Liue6983c72022-04-19 22:14:56 +000010813class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10814protected:
10815 float mPointerMovementScale;
10816 float mPointerXZoomScale;
10817 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10818 addConfigurationProperty("touch.deviceType", "pointer");
10819 std::shared_ptr<FakePointerController> fakePointerController =
10820 std::make_shared<FakePointerController>();
10821 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10822 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010823 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010824
10825 prepareAxes(POSITION);
10826 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10827 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10828 // needs to be disabled, and the pointer gesture needs to be enabled.
10829 mFakePolicy->setPointerCapture(false);
10830 mFakePolicy->setPointerGestureEnabled(true);
10831 mFakePolicy->setPointerController(fakePointerController);
10832
10833 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10834 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10835 mPointerMovementScale =
10836 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10837 mPointerXZoomScale =
10838 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10839 }
10840
10841 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10842 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10843 /*flat*/ 0,
10844 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10845 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10846 /*flat*/ 0,
10847 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10848 }
10849};
10850
10851/**
10852 * Two fingers down on a pointer mode touch pad. The width
10853 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10854 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10855 * be greater than the both value to be freeform gesture, so that after two
10856 * fingers start to move downwards, the gesture should be swipe.
10857 */
10858TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10859 // The min freeform gesture width is 25units/mm x 30mm = 750
10860 // which is greater than fraction of the diagnal length of the touchpad (349).
10861 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010862 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010863 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010864 NotifyMotionArgs motionArgs;
10865
10866 // Two fingers down at once.
10867 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10868 // Pointer's initial position is used the [0,0] coordinate.
10869 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10870
10871 processId(mapper, FIRST_TRACKING_ID);
10872 processPosition(mapper, x1, y1);
10873 processMTSync(mapper);
10874 processId(mapper, SECOND_TRACKING_ID);
10875 processPosition(mapper, x2, y2);
10876 processMTSync(mapper);
10877 processSync(mapper);
10878
10879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010880 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010881 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010882 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010883 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010884 ASSERT_NO_FATAL_FAILURE(
10885 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10886
10887 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10888 // that there should be 1 pointer.
10889 int32_t movingDistance = 200;
10890 y1 += movingDistance;
10891 y2 += movingDistance;
10892
10893 processId(mapper, FIRST_TRACKING_ID);
10894 processPosition(mapper, x1, y1);
10895 processMTSync(mapper);
10896 processId(mapper, SECOND_TRACKING_ID);
10897 processPosition(mapper, x2, y2);
10898 processMTSync(mapper);
10899 processSync(mapper);
10900
10901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010902 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010903 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010904 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010905 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10907 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10908 0, 0, 0, 0));
10909}
10910
10911/**
10912 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10913 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10914 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10915 * value to be freeform gesture, so that after two fingers start to move downwards,
10916 * the gesture should be swipe.
10917 */
10918TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10919 // The min freeform gesture width is 5units/mm x 30mm = 150
10920 // which is greater than fraction of the diagnal length of the touchpad (349).
10921 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010922 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +000010923 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010924 NotifyMotionArgs motionArgs;
10925
10926 // Two fingers down at once.
10927 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10928 // Pointer's initial position is used the [0,0] coordinate.
10929 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10930
10931 processId(mapper, FIRST_TRACKING_ID);
10932 processPosition(mapper, x1, y1);
10933 processMTSync(mapper);
10934 processId(mapper, SECOND_TRACKING_ID);
10935 processPosition(mapper, x2, y2);
10936 processMTSync(mapper);
10937 processSync(mapper);
10938
10939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010940 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010941 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010942 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010943 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010944 ASSERT_NO_FATAL_FAILURE(
10945 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10946
10947 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10948 // and there should be 1 pointer.
10949 int32_t movingDistance = 200;
10950 y1 += movingDistance;
10951 y2 += movingDistance;
10952
10953 processId(mapper, FIRST_TRACKING_ID);
10954 processPosition(mapper, x1, y1);
10955 processMTSync(mapper);
10956 processId(mapper, SECOND_TRACKING_ID);
10957 processPosition(mapper, x2, y2);
10958 processMTSync(mapper);
10959 processSync(mapper);
10960
10961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010962 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010963 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010964 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010965 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010966 // New coordinate is the scaled relative coordinate from the initial coordinate.
10967 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10968 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10969 0, 0, 0, 0));
10970}
10971
10972/**
10973 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10974 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10975 * freeform gestures after two fingers start to move downwards.
10976 */
10977TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010978 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010979 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010980
10981 NotifyMotionArgs motionArgs;
10982
10983 // Two fingers down at once. Wider than the max swipe width.
10984 // The gesture is expected to be PRESS, then transformed to FREEFORM
10985 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10986
10987 processId(mapper, FIRST_TRACKING_ID);
10988 processPosition(mapper, x1, y1);
10989 processMTSync(mapper);
10990 processId(mapper, SECOND_TRACKING_ID);
10991 processPosition(mapper, x2, y2);
10992 processMTSync(mapper);
10993 processSync(mapper);
10994
10995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010996 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010997 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010998 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010999 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011000 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
11001 ASSERT_NO_FATAL_FAILURE(
11002 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11003
11004 int32_t movingDistance = 200;
11005
11006 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
11007 // then two down events for two pointers.
11008 y1 += movingDistance;
11009 y2 += movingDistance;
11010
11011 processId(mapper, FIRST_TRACKING_ID);
11012 processPosition(mapper, x1, y1);
11013 processMTSync(mapper);
11014 processId(mapper, SECOND_TRACKING_ID);
11015 processPosition(mapper, x2, y2);
11016 processMTSync(mapper);
11017 processSync(mapper);
11018
11019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11020 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011021 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011022 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
11023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011024 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011025 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011026 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011028 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011029 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011030 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011031 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011032 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011033 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011034 // Two pointers' scaled relative coordinates from their initial centroid.
11035 // Initial y coordinates are 0 as y1 and y2 have the same value.
11036 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
11037 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
11038 // When pointers move, the new coordinates equal to the initial coordinates plus
11039 // scaled moving distance.
11040 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11041 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11042 0, 0, 0, 0));
11043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11044 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11045 0, 0, 0, 0));
11046
11047 // Move two fingers down again, expect one MOVE motion event.
11048 y1 += movingDistance;
11049 y2 += movingDistance;
11050
11051 processId(mapper, FIRST_TRACKING_ID);
11052 processPosition(mapper, x1, y1);
11053 processMTSync(mapper);
11054 processId(mapper, SECOND_TRACKING_ID);
11055 processPosition(mapper, x2, y2);
11056 processMTSync(mapper);
11057 processSync(mapper);
11058
11059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011060 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011061 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011062 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011063 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11065 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11066 0, 0, 0, 0, 0));
11067 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11068 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11069 0, 0, 0, 0, 0));
11070}
11071
Harry Cutts39b7ca22022-10-05 15:55:48 +000011072TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000011073 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000011074 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000011075 NotifyMotionArgs motionArgs;
11076
11077 // Place two fingers down.
11078 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
11079
11080 processId(mapper, FIRST_TRACKING_ID);
11081 processPosition(mapper, x1, y1);
11082 processMTSync(mapper);
11083 processId(mapper, SECOND_TRACKING_ID);
11084 processPosition(mapper, x2, y2);
11085 processMTSync(mapper);
11086 processSync(mapper);
11087
11088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011089 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000011090 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11091 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
11092 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
11093 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
11094
11095 // Move the two fingers down and to the left.
11096 int32_t movingDistance = 200;
11097 x1 -= movingDistance;
11098 y1 += movingDistance;
11099 x2 -= movingDistance;
11100 y2 += movingDistance;
11101
11102 processId(mapper, FIRST_TRACKING_ID);
11103 processPosition(mapper, x1, y1);
11104 processMTSync(mapper);
11105 processId(mapper, SECOND_TRACKING_ID);
11106 processPosition(mapper, x2, y2);
11107 processMTSync(mapper);
11108 processSync(mapper);
11109
11110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011111 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000011112 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11113 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
11114 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
11115 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
11116}
11117
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011118TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000011119 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011120 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000011121 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
11123
11124 // Start a stylus gesture.
11125 processKey(mapper, BTN_TOOL_PEN, 1);
11126 processId(mapper, FIRST_TRACKING_ID);
11127 processPosition(mapper, 100, 200);
11128 processSync(mapper);
11129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11130 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
11131 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011132 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011133 // TODO(b/257078296): Pointer mode generates extra event.
11134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11135 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11136 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011137 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11139
11140 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
11141 // gesture should be disabled.
11142 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
11143 viewport->isActive = false;
11144 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000011145 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11147 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11148 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011149 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011150 // TODO(b/257078296): Pointer mode generates extra event.
11151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11152 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11153 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011154 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11156}
11157
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011158// --- JoystickInputMapperTest ---
11159
11160class JoystickInputMapperTest : public InputMapperTest {
11161protected:
11162 static const int32_t RAW_X_MIN;
11163 static const int32_t RAW_X_MAX;
11164 static const int32_t RAW_Y_MIN;
11165 static const int32_t RAW_Y_MAX;
11166
11167 void SetUp() override {
11168 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
11169 }
11170 void prepareAxes() {
11171 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
11172 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
11173 }
11174
11175 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
11176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
11177 }
11178
11179 void processSync(JoystickInputMapper& mapper) {
11180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
11181 }
11182
Michael Wrighta9cf4192022-12-01 23:46:39 +000011183 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011184 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
11185 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
11186 NO_PORT, ViewportType::VIRTUAL);
11187 }
11188};
11189
11190const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
11191const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
11192const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11193const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11194
11195TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11196 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000011197 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011198
11199 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11200
Michael Wrighta9cf4192022-12-01 23:46:39 +000011201 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011202
11203 // Send an axis event
11204 processAxis(mapper, ABS_X, 100);
11205 processSync(mapper);
11206
11207 NotifyMotionArgs args;
11208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11209 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11210
11211 // Send another axis event
11212 processAxis(mapper, ABS_Y, 100);
11213 processSync(mapper);
11214
11215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11216 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11217}
11218
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011219// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011220
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011221class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011222protected:
11223 static const char* DEVICE_NAME;
11224 static const char* DEVICE_LOCATION;
11225 static const int32_t DEVICE_ID;
11226 static const int32_t DEVICE_GENERATION;
11227 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011228 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011229 static const int32_t EVENTHUB_ID;
11230
11231 std::shared_ptr<FakeEventHub> mFakeEventHub;
11232 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011233 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011234 std::unique_ptr<InstrumentedInputReader> mReader;
11235 std::shared_ptr<InputDevice> mDevice;
11236
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011237 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011238 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011239 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011240 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011241 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011242 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011243 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11244 }
11245
11246 void SetUp() override { SetUp(DEVICE_CLASSES); }
11247
11248 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011249 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011250 mFakePolicy.clear();
11251 }
11252
Chris Yee2b1e5c2021-03-10 22:45:12 -080011253 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11254 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011255 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011256 InputDeviceIdentifier identifier;
11257 identifier.name = name;
11258 identifier.location = location;
11259 std::shared_ptr<InputDevice> device =
11260 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11261 identifier);
11262 mReader->pushNextDevice(device);
11263 mFakeEventHub->addDevice(eventHubId, name, classes);
11264 mReader->loopOnce();
11265 return device;
11266 }
11267
11268 template <class T, typename... Args>
11269 T& addControllerAndConfigure(Args... args) {
11270 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11271
11272 return controller;
11273 }
11274};
11275
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011276const char* PeripheralControllerTest::DEVICE_NAME = "device";
11277const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11278const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11279const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11280const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011281const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11282 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011283const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011284
11285// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011286class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011287protected:
11288 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011289 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011290 }
11291};
11292
11293TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011294 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011295
Harry Cuttsa5b71292022-11-28 12:56:17 +000011296 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11297 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11298 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011299}
11300
11301TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011302 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011303
Harry Cuttsa5b71292022-11-28 12:56:17 +000011304 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11305 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11306 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011307}
11308
11309// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011310class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011311protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011312 void SetUp() override {
11313 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11314 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011315};
11316
Chris Ye85758332021-05-16 23:05:17 -070011317TEST_F(LightControllerTest, MonoLight) {
11318 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011319 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011320 .maxBrightness = 255,
11321 .flags = InputLightClass::BRIGHTNESS,
11322 .path = ""};
11323 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011324
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011325 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011326 InputDeviceInfo info;
11327 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011328 std::vector<InputDeviceLightInfo> lights = info.getLights();
11329 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011330 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11331 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11332
11333 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11334 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11335}
11336
11337TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11338 RawLightInfo infoMono = {.id = 1,
11339 .name = "mono_keyboard_backlight",
11340 .maxBrightness = 255,
11341 .flags = InputLightClass::BRIGHTNESS |
11342 InputLightClass::KEYBOARD_BACKLIGHT,
11343 .path = ""};
11344 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11345
11346 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11347 InputDeviceInfo info;
11348 controller.populateDeviceInfo(&info);
11349 std::vector<InputDeviceLightInfo> lights = info.getLights();
11350 ASSERT_EQ(1U, lights.size());
11351 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11352 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011353
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011354 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11355 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011356}
11357
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000011358TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
11359 RawLightInfo infoMono = {.id = 1,
11360 .name = "mono_light",
11361 .maxBrightness = 255,
11362 .flags = InputLightClass::BRIGHTNESS,
11363 .path = ""};
11364 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11365 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11366 "0,100,200");
11367
11368 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11369 std::list<NotifyArgs> unused =
11370 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11371 /*changes=*/{});
11372
11373 InputDeviceInfo info;
11374 controller.populateDeviceInfo(&info);
11375 std::vector<InputDeviceLightInfo> lights = info.getLights();
11376 ASSERT_EQ(1U, lights.size());
11377 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11378}
11379
11380TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
11381 RawLightInfo infoMono = {.id = 1,
11382 .name = "mono_keyboard_backlight",
11383 .maxBrightness = 255,
11384 .flags = InputLightClass::BRIGHTNESS |
11385 InputLightClass::KEYBOARD_BACKLIGHT,
11386 .path = ""};
11387 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11388
11389 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11390 std::list<NotifyArgs> unused =
11391 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11392 /*changes=*/{});
11393
11394 InputDeviceInfo info;
11395 controller.populateDeviceInfo(&info);
11396 std::vector<InputDeviceLightInfo> lights = info.getLights();
11397 ASSERT_EQ(1U, lights.size());
11398 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11399}
11400
11401TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
11402 RawLightInfo infoMono = {.id = 1,
11403 .name = "mono_keyboard_backlight",
11404 .maxBrightness = 255,
11405 .flags = InputLightClass::BRIGHTNESS |
11406 InputLightClass::KEYBOARD_BACKLIGHT,
11407 .path = ""};
11408 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11409 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11410 "0,100,200");
11411
11412 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11413 std::list<NotifyArgs> unused =
11414 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11415 /*changes=*/{});
11416
11417 InputDeviceInfo info;
11418 controller.populateDeviceInfo(&info);
11419 std::vector<InputDeviceLightInfo> lights = info.getLights();
11420 ASSERT_EQ(1U, lights.size());
11421 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
11422 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
11423 ASSERT_EQ(BrightnessLevel(0), *it);
11424 std::advance(it, 1);
11425 ASSERT_EQ(BrightnessLevel(100), *it);
11426 std::advance(it, 1);
11427 ASSERT_EQ(BrightnessLevel(200), *it);
11428}
11429
11430TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
11431 RawLightInfo infoMono = {.id = 1,
11432 .name = "mono_keyboard_backlight",
11433 .maxBrightness = 255,
11434 .flags = InputLightClass::BRIGHTNESS |
11435 InputLightClass::KEYBOARD_BACKLIGHT,
11436 .path = ""};
11437 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11438 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11439 "0,100,200,300,400,500");
11440
11441 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11442 std::list<NotifyArgs> unused =
11443 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11444 /*changes=*/{});
11445
11446 InputDeviceInfo info;
11447 controller.populateDeviceInfo(&info);
11448 std::vector<InputDeviceLightInfo> lights = info.getLights();
11449 ASSERT_EQ(1U, lights.size());
11450 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11451}
11452
Chris Yee2b1e5c2021-03-10 22:45:12 -080011453TEST_F(LightControllerTest, RGBLight) {
11454 RawLightInfo infoRed = {.id = 1,
11455 .name = "red",
11456 .maxBrightness = 255,
11457 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11458 .path = ""};
11459 RawLightInfo infoGreen = {.id = 2,
11460 .name = "green",
11461 .maxBrightness = 255,
11462 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11463 .path = ""};
11464 RawLightInfo infoBlue = {.id = 3,
11465 .name = "blue",
11466 .maxBrightness = 255,
11467 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11468 .path = ""};
11469 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11470 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11471 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11472
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011473 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011474 InputDeviceInfo info;
11475 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011476 std::vector<InputDeviceLightInfo> lights = info.getLights();
11477 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011478 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11479 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11480 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11481
11482 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11483 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11484}
11485
11486TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11487 RawLightInfo infoRed = {.id = 1,
11488 .name = "red_keyboard_backlight",
11489 .maxBrightness = 255,
11490 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11491 InputLightClass::KEYBOARD_BACKLIGHT,
11492 .path = ""};
11493 RawLightInfo infoGreen = {.id = 2,
11494 .name = "green_keyboard_backlight",
11495 .maxBrightness = 255,
11496 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11497 InputLightClass::KEYBOARD_BACKLIGHT,
11498 .path = ""};
11499 RawLightInfo infoBlue = {.id = 3,
11500 .name = "blue_keyboard_backlight",
11501 .maxBrightness = 255,
11502 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11503 InputLightClass::KEYBOARD_BACKLIGHT,
11504 .path = ""};
11505 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11506 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11507 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11508
11509 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11510 InputDeviceInfo info;
11511 controller.populateDeviceInfo(&info);
11512 std::vector<InputDeviceLightInfo> lights = info.getLights();
11513 ASSERT_EQ(1U, lights.size());
11514 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11515 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11516 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11517
11518 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11519 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11520}
11521
11522TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11523 RawLightInfo infoRed = {.id = 1,
11524 .name = "red",
11525 .maxBrightness = 255,
11526 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11527 .path = ""};
11528 RawLightInfo infoGreen = {.id = 2,
11529 .name = "green",
11530 .maxBrightness = 255,
11531 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11532 .path = ""};
11533 RawLightInfo infoBlue = {.id = 3,
11534 .name = "blue",
11535 .maxBrightness = 255,
11536 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11537 .path = ""};
11538 RawLightInfo infoGlobal = {.id = 3,
11539 .name = "global_keyboard_backlight",
11540 .maxBrightness = 255,
11541 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11542 InputLightClass::KEYBOARD_BACKLIGHT,
11543 .path = ""};
11544 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11545 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11546 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11547 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11548
11549 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11550 InputDeviceInfo info;
11551 controller.populateDeviceInfo(&info);
11552 std::vector<InputDeviceLightInfo> lights = info.getLights();
11553 ASSERT_EQ(1U, lights.size());
11554 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11555 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11556 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011557
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011558 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11559 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011560}
11561
11562TEST_F(LightControllerTest, MultiColorRGBLight) {
11563 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011564 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011565 .maxBrightness = 255,
11566 .flags = InputLightClass::BRIGHTNESS |
11567 InputLightClass::MULTI_INTENSITY |
11568 InputLightClass::MULTI_INDEX,
11569 .path = ""};
11570
11571 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11572
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011573 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011574 InputDeviceInfo info;
11575 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011576 std::vector<InputDeviceLightInfo> lights = info.getLights();
11577 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011578 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11579 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11580 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11581
11582 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11583 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11584}
11585
11586TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11587 RawLightInfo infoColor = {.id = 1,
11588 .name = "multi_color_keyboard_backlight",
11589 .maxBrightness = 255,
11590 .flags = InputLightClass::BRIGHTNESS |
11591 InputLightClass::MULTI_INTENSITY |
11592 InputLightClass::MULTI_INDEX |
11593 InputLightClass::KEYBOARD_BACKLIGHT,
11594 .path = ""};
11595
11596 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11597
11598 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11599 InputDeviceInfo info;
11600 controller.populateDeviceInfo(&info);
11601 std::vector<InputDeviceLightInfo> lights = info.getLights();
11602 ASSERT_EQ(1U, lights.size());
11603 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11604 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11605 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011606
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011607 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11608 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011609}
11610
11611TEST_F(LightControllerTest, PlayerIdLight) {
11612 RawLightInfo info1 = {.id = 1,
11613 .name = "player1",
11614 .maxBrightness = 255,
11615 .flags = InputLightClass::BRIGHTNESS,
11616 .path = ""};
11617 RawLightInfo info2 = {.id = 2,
11618 .name = "player2",
11619 .maxBrightness = 255,
11620 .flags = InputLightClass::BRIGHTNESS,
11621 .path = ""};
11622 RawLightInfo info3 = {.id = 3,
11623 .name = "player3",
11624 .maxBrightness = 255,
11625 .flags = InputLightClass::BRIGHTNESS,
11626 .path = ""};
11627 RawLightInfo info4 = {.id = 4,
11628 .name = "player4",
11629 .maxBrightness = 255,
11630 .flags = InputLightClass::BRIGHTNESS,
11631 .path = ""};
11632 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11633 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11634 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11635 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11636
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011637 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011638 InputDeviceInfo info;
11639 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011640 std::vector<InputDeviceLightInfo> lights = info.getLights();
11641 ASSERT_EQ(1U, lights.size());
11642 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011643 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11644 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011645
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011646 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11647 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11648 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011649}
11650
Michael Wrightd02c5b62014-02-10 15:10:22 -080011651} // namespace android