blob: e751c89c4e233f2b4287eaaf371491d82726f7f7 [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);
Harry Cutts33476232023-01-30 19:57:29 +00001346 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1347 /*eventDidNotHappenTimeout=*/30ms);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001348
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001349 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1350 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001351 ASSERT_EQ(mReader->start(), OK);
1352
1353 // Since this test is run on a real device, all the input devices connected
1354 // to the test device will show up in mReader. We wait for those input devices to
1355 // show up before beginning the tests.
1356 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +00001357 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001358 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1359 }
1360
Chris Yea52ade12020-08-27 16:49:20 -07001361 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001362#if !defined(__ANDROID__)
1363 return;
1364#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001365 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001366 mReader.reset();
1367 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001368 mFakePolicy.clear();
1369 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001370
1371 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1372 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1373 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1374 [&name](const InputDeviceInfo& info) {
1375 return info.getIdentifier().name == name;
1376 });
1377 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1378 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001379};
1380
1381TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1382 // An invalid input device that is only used for this test.
1383 class InvalidUinputDevice : public UinputDevice {
1384 public:
Harry Cutts33476232023-01-30 19:57:29 +00001385 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001386
1387 private:
1388 void configureDevice(int fd, uinput_user_dev* device) override {}
1389 };
1390
1391 const size_t numDevices = mFakePolicy->getInputDevices().size();
1392
1393 // UinputDevice does not set any event or key bits, so InputReader should not
1394 // consider it as a valid device.
1395 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1396 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1397 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1398 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1399
1400 invalidDevice.reset();
1401 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1402 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1403 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1404}
1405
1406TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1407 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1408
1409 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1410 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1411 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1412 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1413
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001414 const auto device = findDeviceByName(keyboard->getName());
1415 ASSERT_TRUE(device.has_value());
1416 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1417 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1418 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001419
1420 keyboard.reset();
1421 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1422 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1423 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1424}
1425
1426TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1427 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1428 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1429
1430 NotifyConfigurationChangedArgs configChangedArgs;
1431 ASSERT_NO_FATAL_FAILURE(
1432 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001433 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001434 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1435
1436 NotifyKeyArgs keyArgs;
1437 keyboard->pressAndReleaseHomeKey();
1438 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1439 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001440 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001441 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001442 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001443 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001444 prevTimestamp = keyArgs.eventTime;
1445
1446 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1447 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001448 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001449 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001450 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001451}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001452
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001453TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1454 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1455 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1456
1457 const auto device = findDeviceByName(stylus->getName());
1458 ASSERT_TRUE(device.has_value());
1459
Prabir Pradhana3621852022-10-14 18:57:23 +00001460 // An external stylus with buttons should also be recognized as a keyboard.
1461 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001462 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1463 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1464
1465 const auto DOWN =
1466 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1467 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1468
1469 stylus->pressAndReleaseKey(BTN_STYLUS);
1470 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1471 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1472 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1473 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1474
1475 stylus->pressAndReleaseKey(BTN_STYLUS2);
1476 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1477 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1478 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1479 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1480
1481 stylus->pressAndReleaseKey(BTN_STYLUS3);
1482 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1483 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1484 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1485 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1486}
1487
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001488/**
1489 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1490 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1491 * are passed to the listener.
1492 */
1493static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1494TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1495 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1496 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1497 NotifyKeyArgs keyArgs;
1498
1499 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1500 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1501 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1502 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1503
1504 controller->pressAndReleaseKey(BTN_GEAR_UP);
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_UP, keyArgs.scanCode);
1508}
1509
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001510// --- TouchIntegrationTest ---
1511
Arthur Hungaab25622020-01-16 11:22:11 +08001512class TouchIntegrationTest : public InputReaderIntegrationTest {
1513protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001514 const std::string UNIQUE_ID = "local:0";
1515
Chris Yea52ade12020-08-27 16:49:20 -07001516 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001517#if !defined(__ANDROID__)
1518 GTEST_SKIP();
1519#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001520 InputReaderIntegrationTest::SetUp();
1521 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001522 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1523 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001524
1525 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1526 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1527 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001528 const auto info = findDeviceByName(mDevice->getName());
1529 ASSERT_TRUE(info);
1530 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001531 }
1532
1533 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001534 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001535 std::optional<uint8_t> physicalPort,
1536 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001537 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001538 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001539 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001540 }
1541
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001542 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1543 NotifyMotionArgs args;
1544 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1545 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001546 ASSERT_EQ(points.size(), args.getPointerCount());
1547 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001548 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1549 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1550 }
1551 }
1552
Arthur Hungaab25622020-01-16 11:22:11 +08001553 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001554 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001555};
1556
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001557TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
1558 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1559 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1560 // presses).
1561 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1562 mDeviceInfo.getSources());
1563}
1564
Arthur Hungaab25622020-01-16 11:22:11 +08001565TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1566 NotifyMotionArgs args;
1567 const Point centerPoint = mDevice->getCenterPoint();
1568
1569 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001570 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001571 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001572 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001573 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1574 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1575
1576 // ACTION_MOVE
1577 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001578 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001579 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1581
1582 // ACTION_UP
1583 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001584 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001585 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1586 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1587}
1588
1589TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1590 NotifyMotionArgs args;
1591 const Point centerPoint = mDevice->getCenterPoint();
1592
1593 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001594 mDevice->sendSlot(FIRST_SLOT);
1595 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001596 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001597 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001598 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1599 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1600
1601 // ACTION_POINTER_DOWN (Second slot)
1602 const Point secondPoint = centerPoint + Point(100, 100);
1603 mDevice->sendSlot(SECOND_SLOT);
1604 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001605 mDevice->sendDown(secondPoint);
1606 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001607 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001608 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001609
1610 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001611 mDevice->sendMove(secondPoint + Point(1, 1));
1612 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001613 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1615
1616 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001617 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001618 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001619 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001620 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001621
1622 // ACTION_UP
1623 mDevice->sendSlot(FIRST_SLOT);
1624 mDevice->sendUp();
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_UP, args.action);
1628}
1629
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001630/**
1631 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1632 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1633 * data?
1634 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1635 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1636 * for Pointer 0 only is generated after.
1637 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1638 * events, we will not miss any information.
1639 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1640 * event generated afterwards that contains the newest movement of pointer 0.
1641 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1642 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1643 * losing information about non-palm pointers.
1644 */
1645TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1646 NotifyMotionArgs args;
1647 const Point centerPoint = mDevice->getCenterPoint();
1648
1649 // ACTION_DOWN
1650 mDevice->sendSlot(FIRST_SLOT);
1651 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1652 mDevice->sendDown(centerPoint);
1653 mDevice->sendSync();
1654 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1655
1656 // ACTION_POINTER_DOWN (Second slot)
1657 const Point secondPoint = centerPoint + Point(100, 100);
1658 mDevice->sendSlot(SECOND_SLOT);
1659 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1660 mDevice->sendDown(secondPoint);
1661 mDevice->sendSync();
1662 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1663
1664 // ACTION_MOVE (First slot)
1665 mDevice->sendSlot(FIRST_SLOT);
1666 mDevice->sendMove(centerPoint + Point(5, 5));
1667 // ACTION_POINTER_UP (Second slot)
1668 mDevice->sendSlot(SECOND_SLOT);
1669 mDevice->sendPointerUp();
1670 // Send a single sync for the above 2 pointer updates
1671 mDevice->sendSync();
1672
1673 // First, we should get POINTER_UP for the second pointer
1674 assertReceivedMotion(ACTION_POINTER_1_UP,
1675 {/*first pointer */ centerPoint + Point(5, 5),
1676 /*second pointer*/ secondPoint});
1677
1678 // Next, the MOVE event for the first pointer
1679 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1680}
1681
1682/**
1683 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1684 * move, and then it will go up, all in the same frame.
1685 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1686 * gets sent to the listener.
1687 */
1688TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1689 NotifyMotionArgs args;
1690 const Point centerPoint = mDevice->getCenterPoint();
1691
1692 // ACTION_DOWN
1693 mDevice->sendSlot(FIRST_SLOT);
1694 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1695 mDevice->sendDown(centerPoint);
1696 mDevice->sendSync();
1697 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1698
1699 // ACTION_POINTER_DOWN (Second slot)
1700 const Point secondPoint = centerPoint + Point(100, 100);
1701 mDevice->sendSlot(SECOND_SLOT);
1702 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1703 mDevice->sendDown(secondPoint);
1704 mDevice->sendSync();
1705 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1706
1707 // ACTION_MOVE (First slot)
1708 mDevice->sendSlot(FIRST_SLOT);
1709 mDevice->sendMove(centerPoint + Point(5, 5));
1710 // ACTION_POINTER_UP (Second slot)
1711 mDevice->sendSlot(SECOND_SLOT);
1712 mDevice->sendMove(secondPoint + Point(6, 6));
1713 mDevice->sendPointerUp();
1714 // Send a single sync for the above 2 pointer updates
1715 mDevice->sendSync();
1716
1717 // First, we should get POINTER_UP for the second pointer
1718 // The movement of the second pointer during the liftoff frame is ignored.
1719 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1720 assertReceivedMotion(ACTION_POINTER_1_UP,
1721 {/*first pointer */ centerPoint + Point(5, 5),
1722 /*second pointer*/ secondPoint});
1723
1724 // Next, the MOVE event for the first pointer
1725 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1726}
1727
Arthur Hungaab25622020-01-16 11:22:11 +08001728TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1729 NotifyMotionArgs args;
1730 const Point centerPoint = mDevice->getCenterPoint();
1731
1732 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001733 mDevice->sendSlot(FIRST_SLOT);
1734 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001735 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001736 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001737 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1738 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1739
arthurhungcc7f9802020-04-30 17:55:40 +08001740 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001741 const Point secondPoint = centerPoint + Point(100, 100);
1742 mDevice->sendSlot(SECOND_SLOT);
1743 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1744 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001745 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001746 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001747 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001748
arthurhungcc7f9802020-04-30 17:55:40 +08001749 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001750 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001751 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001752 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1753 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1754
arthurhungcc7f9802020-04-30 17:55:40 +08001755 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1756 // a palm event.
1757 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001758 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001759 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001760 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001761 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001762 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001763
arthurhungcc7f9802020-04-30 17:55:40 +08001764 // Send up to second slot, expect first slot send moving.
1765 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001766 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001767 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1768 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001769
arthurhungcc7f9802020-04-30 17:55:40 +08001770 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001771 mDevice->sendSlot(FIRST_SLOT);
1772 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001773 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001774
arthurhungcc7f9802020-04-30 17:55:40 +08001775 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1776 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001777}
1778
Prabir Pradhanda20b172022-09-26 17:01:18 +00001779TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
1780 const Point centerPoint = mDevice->getCenterPoint();
1781
1782 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1783 mDevice->sendSlot(FIRST_SLOT);
1784 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1785 mDevice->sendToolType(MT_TOOL_PEN);
1786 mDevice->sendDown(centerPoint);
1787 mDevice->sendSync();
1788 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1789 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001790 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001791
1792 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1793
1794 // Release the stylus touch.
1795 mDevice->sendUp();
1796 mDevice->sendSync();
1797 ASSERT_NO_FATAL_FAILURE(
1798 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1799
1800 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1801
1802 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1803 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1804 mDevice->sendToolType(MT_TOOL_FINGER);
1805 mDevice->sendDown(centerPoint);
1806 mDevice->sendSync();
1807 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1808 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001809 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001810
1811 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1812
1813 mDevice->sendUp();
1814 mDevice->sendSync();
1815 ASSERT_NO_FATAL_FAILURE(
1816 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1817
1818 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1819 // The policy should be notified of the stylus presence.
1820 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1821 mDevice->sendToolType(MT_TOOL_PEN);
1822 mDevice->sendMove(centerPoint);
1823 mDevice->sendSync();
1824 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1825 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001826 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001827
1828 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1829}
1830
Prabir Pradhan124ea442022-10-28 20:27:44 +00001831// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001832
Prabir Pradhan124ea442022-10-28 20:27:44 +00001833// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1834// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1835// stylus.
1836template <typename UinputStylusDevice>
1837class StylusButtonIntegrationTest : public TouchIntegrationTest {
1838protected:
1839 void SetUp() override {
1840#if !defined(__ANDROID__)
1841 GTEST_SKIP();
1842#endif
1843 TouchIntegrationTest::SetUp();
1844 mTouchscreen = mDevice.get();
1845 mTouchscreenInfo = mDeviceInfo;
1846
1847 setUpStylusDevice();
1848 }
1849
1850 UinputStylusDevice* mStylus{nullptr};
1851 InputDeviceInfo mStylusInfo{};
1852
1853 UinputTouchScreen* mTouchscreen{nullptr};
1854 InputDeviceInfo mTouchscreenInfo{};
1855
1856private:
1857 // When we are attempting to test stylus button events that are sent from the touchscreen,
1858 // use the same Uinput device for the touchscreen and the stylus.
1859 template <typename T = UinputStylusDevice>
1860 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1861 mStylus = mDevice.get();
1862 mStylusInfo = mDeviceInfo;
1863 }
1864
1865 // When we are attempting to stylus buttons from an external stylus being merged with touches
1866 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1867 template <typename T = UinputStylusDevice>
1868 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1869 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1870 mStylus = mStylusDeviceLifecycleTracker.get();
1871 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1872 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1873 const auto info = findDeviceByName(mStylus->getName());
1874 ASSERT_TRUE(info);
1875 mStylusInfo = *info;
1876 }
1877
1878 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1879
1880 // Hide the base class's device to expose it with a different name for readability.
1881 using TouchIntegrationTest::mDevice;
1882 using TouchIntegrationTest::mDeviceInfo;
1883};
1884
1885using StylusButtonIntegrationTestTypes =
1886 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1887TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1888
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001889TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001890 const auto stylusId = TestFixture::mStylusInfo.getId();
1891
1892 TestFixture::mStylus->pressKey(BTN_STYLUS);
1893 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1894 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1895 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1896
1897 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1898 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001899 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001900 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001901}
1902
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001903TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001904 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1905 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1906 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001907
1908 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001909 TestFixture::mStylus->pressKey(BTN_STYLUS);
1910 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001911 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001912 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001913
1914 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001915 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1916 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1917 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1918 TestFixture::mTouchscreen->sendDown(centerPoint);
1919 TestFixture::mTouchscreen->sendSync();
1920 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001921 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001922 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001923 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1924 WithDeviceId(touchscreenId))));
1925 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001926 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001927 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001928 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1929 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001930
Prabir Pradhan124ea442022-10-28 20:27:44 +00001931 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1932 TestFixture::mTouchscreen->sendSync();
1933 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001934 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001935 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001936 WithDeviceId(touchscreenId))));
1937 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001938 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001939 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001940 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001941
1942 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001943 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1944 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001945 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001946 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001947}
1948
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001949TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001950 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1951 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1952 const auto stylusId = TestFixture::mStylusInfo.getId();
1953 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001954 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001955
1956 // Press the stylus button.
1957 TestFixture::mStylus->pressKey(BTN_STYLUS);
1958 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1959 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1960 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1961
1962 // Start hovering with the stylus.
1963 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1964 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1965 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1966 TestFixture::mTouchscreen->sendMove(centerPoint);
1967 TestFixture::mTouchscreen->sendSync();
1968 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1969 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1970 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1971 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1972 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1973 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1974 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1975 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1976 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1977
1978 // Touch down with the stylus.
1979 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1980 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1981 TestFixture::mTouchscreen->sendDown(centerPoint);
1982 TestFixture::mTouchscreen->sendSync();
1983 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1984 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1985 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1986
1987 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1988 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1989 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1990
1991 // Stop touching with the stylus, and start hovering.
1992 TestFixture::mTouchscreen->sendUp();
1993 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1994 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1995 TestFixture::mTouchscreen->sendMove(centerPoint);
1996 TestFixture::mTouchscreen->sendSync();
1997 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1998 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
1999 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2000 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2001 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2002 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2003 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2004 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2005 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2006
2007 // Stop hovering.
2008 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2009 TestFixture::mTouchscreen->sendSync();
2010 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2011 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2012 WithButtonState(0))));
2013 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2014 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2015 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2016 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2017
2018 // Release the stylus button.
2019 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2020 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2021 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2022 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2023}
2024
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002025TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002026 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2027 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2028 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002029
2030 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002031 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2032 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2033 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2034 TestFixture::mTouchscreen->sendDown(centerPoint);
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_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002038 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002039 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002040
2041 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002042 TestFixture::mStylus->pressKey(BTN_STYLUS);
2043 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002044 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002045 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2046 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002047 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002048 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002049 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2050 WithDeviceId(touchscreenId))));
2051 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002052 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002053 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002054 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2055 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002056
Prabir Pradhan124ea442022-10-28 20:27:44 +00002057 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2058 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002059 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002060 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2061 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002062 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002063 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002064 WithDeviceId(touchscreenId))));
2065 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002066 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002067 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002068 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002069
2070 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002071 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2072 TestFixture::mTouchscreen->sendSync();
2073 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002074 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002075 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002076 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002077}
2078
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002079TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002080 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2081 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002082 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002083
2084 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2085 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2086 const auto stylusId = TestFixture::mStylusInfo.getId();
2087
2088 // Start a stylus gesture. By the time this event is processed, the configuration change that
2089 // was requested is guaranteed to be completed.
2090 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2091 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2092 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2093 TestFixture::mTouchscreen->sendDown(centerPoint);
2094 TestFixture::mTouchscreen->sendSync();
2095 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2096 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002097 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002098 WithDeviceId(touchscreenId))));
2099
2100 // Press and release a stylus button. Each change only generates a MOVE motion event.
2101 // Key events are unaffected.
2102 TestFixture::mStylus->pressKey(BTN_STYLUS);
2103 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2104 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2105 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2106 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2107 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002108 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002109 WithDeviceId(touchscreenId))));
2110
2111 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2112 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2113 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2114 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2115 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2116 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002117 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002118 WithDeviceId(touchscreenId))));
2119
2120 // Finish the stylus gesture.
2121 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2122 TestFixture::mTouchscreen->sendSync();
2123 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2124 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002125 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002126 WithDeviceId(touchscreenId))));
2127}
2128
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002129// --- ExternalStylusIntegrationTest ---
2130
2131// Verify the behavior of an external stylus. An external stylus can report pressure or button
2132// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2133// ongoing stylus gesture that is being emitted by the touchscreen.
2134using ExternalStylusIntegrationTest = TouchIntegrationTest;
2135
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002136TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002137 const Point centerPoint = mDevice->getCenterPoint();
2138
2139 // Create an external stylus capable of reporting pressure data that
2140 // should be fused with a touch pointer.
2141 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2142 createUinputDevice<UinputExternalStylusWithPressure>();
2143 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2144 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2145 const auto stylusInfo = findDeviceByName(stylus->getName());
2146 ASSERT_TRUE(stylusInfo);
2147
2148 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2149
2150 const auto touchscreenId = mDeviceInfo.getId();
2151
2152 // Set a pressure value on the stylus. It doesn't generate any events.
2153 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2154 stylus->setPressure(100);
2155 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2156
2157 // Start a finger gesture, and ensure it shows up as stylus gesture
2158 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002159 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002160 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002161 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002162 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002163 mDevice->sendSync();
2164 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2165 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002166 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002167 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002168
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002169 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2170 // event with the updated pressure.
2171 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002172 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2173 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002174 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002175 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002176
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002177 // The external stylus did not generate any events.
2178 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2179 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2180}
2181
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002182TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002183 const Point centerPoint = mDevice->getCenterPoint();
2184
2185 // Create an external stylus capable of reporting pressure data that
2186 // should be fused with a touch pointer.
2187 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2188 createUinputDevice<UinputExternalStylusWithPressure>();
2189 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2190 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2191 const auto stylusInfo = findDeviceByName(stylus->getName());
2192 ASSERT_TRUE(stylusInfo);
2193
2194 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2195
2196 const auto touchscreenId = mDeviceInfo.getId();
2197
2198 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2199 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002200 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2201 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002202 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002203 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002204
2205 // Start a finger gesture. The touch device will withhold generating any touches for
2206 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2207 mDevice->sendSlot(FIRST_SLOT);
2208 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2209 mDevice->sendToolType(MT_TOOL_FINGER);
2210 mDevice->sendDown(centerPoint);
2211 auto waitUntil = std::chrono::system_clock::now() +
2212 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002213 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002214 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002215
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002216 // Since the external stylus did not report a pressure value within the timeout,
2217 // it shows up as a finger pointer.
2218 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2219 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002220 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002221 WithPressure(1.f))));
2222
2223 // Change the pressure on the external stylus. Since the pressure was not present at the start
2224 // of the gesture, it is ignored for now.
2225 stylus->setPressure(200);
2226 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2227
2228 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002229 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2230 mDevice->sendSync();
2231 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2232 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002233 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002234
2235 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2236 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2237 mDevice->sendToolType(MT_TOOL_FINGER);
2238 mDevice->sendDown(centerPoint);
2239 mDevice->sendSync();
2240 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2241 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002242 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002243 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2244
2245 // The external stylus did not generate any events.
2246 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2247 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002248}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002249
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002250TEST_F(ExternalStylusIntegrationTest, DISABLED_UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002251 const Point centerPoint = mDevice->getCenterPoint();
2252
2253 // Create an external stylus device that does not support pressure. It should not affect any
2254 // touch pointers.
2255 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2256 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2257 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2258 const auto stylusInfo = findDeviceByName(stylus->getName());
2259 ASSERT_TRUE(stylusInfo);
2260
2261 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2262
2263 const auto touchscreenId = mDeviceInfo.getId();
2264
2265 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2266 // pressure data from the external stylus.
2267 mDevice->sendSlot(FIRST_SLOT);
2268 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2269 mDevice->sendToolType(MT_TOOL_FINGER);
2270 mDevice->sendDown(centerPoint);
2271 auto waitUntil = std::chrono::system_clock::now() +
2272 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2273 mDevice->sendSync();
2274 ASSERT_NO_FATAL_FAILURE(
2275 mTestListener
2276 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2277 WithToolType(
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002278 ToolType::FINGER),
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002279 WithButtonState(0),
2280 WithDeviceId(touchscreenId),
2281 WithPressure(1.f)),
2282 waitUntil));
2283
2284 // The external stylus did not generate any events.
2285 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2286 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2287}
2288
Michael Wrightd02c5b62014-02-10 15:10:22 -08002289// --- InputDeviceTest ---
2290class InputDeviceTest : public testing::Test {
2291protected:
2292 static const char* DEVICE_NAME;
2293 static const char* DEVICE_LOCATION;
2294 static const int32_t DEVICE_ID;
2295 static const int32_t DEVICE_GENERATION;
2296 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002297 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002298 static const int32_t EVENTHUB_ID;
2299 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2300
2301 std::shared_ptr<FakeEventHub> mFakeEventHub;
2302 sp<FakeInputReaderPolicy> mFakePolicy;
2303 std::unique_ptr<TestInputListener> mFakeListener;
2304 std::unique_ptr<InstrumentedInputReader> mReader;
2305 std::shared_ptr<InputDevice> mDevice;
2306
2307 void SetUp() override {
2308 mFakeEventHub = std::make_unique<FakeEventHub>();
2309 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2310 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002311 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002312 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002313 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002314 identifier.name = DEVICE_NAME;
2315 identifier.location = DEVICE_LOCATION;
2316 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2317 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2318 identifier);
2319 mReader->pushNextDevice(mDevice);
2320 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002321 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002322 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002323
2324 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002325 mFakeListener.reset();
2326 mFakePolicy.clear();
2327 }
2328};
2329
2330const char* InputDeviceTest::DEVICE_NAME = "device";
2331const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2332const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2333const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002334const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002335const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2336 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002337const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002338const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2339
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002340TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002341 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002342 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2343 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002344}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002345
Michael Wrightd02c5b62014-02-10 15:10:22 -08002346TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2347 ASSERT_EQ(mDevice->isEnabled(), false);
2348}
2349
2350TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2351 // Configuration.
2352 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002353 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002354
2355 // Reset.
2356 unused += mDevice->reset(ARBITRARY_TIME);
2357
2358 NotifyDeviceResetArgs resetArgs;
2359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2360 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2361 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2362
2363 // Metadata.
2364 ASSERT_TRUE(mDevice->isIgnored());
2365 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2366
2367 InputDeviceInfo info = mDevice->getDeviceInfo();
2368 ASSERT_EQ(DEVICE_ID, info.getId());
2369 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2370 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2371 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2372
2373 // State queries.
2374 ASSERT_EQ(0, mDevice->getMetaState());
2375
2376 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2377 << "Ignored device should return unknown key code state.";
2378 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2379 << "Ignored device should return unknown scan code state.";
2380 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2381 << "Ignored device should return unknown switch state.";
2382
2383 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2384 uint8_t flags[2] = { 0, 1 };
2385 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2386 << "Ignored device should never mark any key codes.";
2387 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2388 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2389}
2390
2391TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2392 // Configuration.
2393 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2394
2395 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002396 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2397 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002398 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2399 mapper1.setMetaState(AMETA_ALT_ON);
2400 mapper1.addSupportedKeyCode(AKEYCODE_A);
2401 mapper1.addSupportedKeyCode(AKEYCODE_B);
2402 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2403 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2404 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2405 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2406 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2407
2408 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002409 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2410 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002411 mapper2.setMetaState(AMETA_SHIFT_ON);
2412
2413 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002414 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002415
Harry Cuttsf13161a2023-03-08 14:15:49 +00002416 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2417 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002418 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002419 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002420
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002421 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2422 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002423
2424 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002425 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002426 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2427 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002428
2429 NotifyDeviceResetArgs resetArgs;
2430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2431 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2432 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2433
2434 // Metadata.
2435 ASSERT_FALSE(mDevice->isIgnored());
2436 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2437
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002438 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002439 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002440 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002441 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2442 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2443
2444 // State queries.
2445 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2446 << "Should query mappers and combine meta states.";
2447
2448 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2449 << "Should return unknown key code state when source not supported.";
2450 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2451 << "Should return unknown scan code state when source not supported.";
2452 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2453 << "Should return unknown switch state when source not supported.";
2454
2455 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2456 << "Should query mapper when source is supported.";
2457 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2458 << "Should query mapper when source is supported.";
2459 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2460 << "Should query mapper when source is supported.";
2461
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002462 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002463 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002464 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002465 << "Should do nothing when source is unsupported.";
2466 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2467 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2468 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2469 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2470
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002471 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002472 << "Should query mapper when source is supported.";
2473 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2474 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2475 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2476 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2477
2478 // Event handling.
2479 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002480 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002481 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002482
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002483 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2484 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002485}
2486
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -07002487TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2488 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2489 FakeInputMapper& mapper =
2490 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2491 AINPUT_SOURCE_KEYBOARD);
2492 NotifyMotionArgs args1;
2493 NotifySwitchArgs args2;
2494 NotifyKeyArgs args3;
2495 mapper.setProcessResult({args1, args2, args3});
2496
2497 InputReaderConfiguration config;
2498 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2499
2500 RawEvent event;
2501 event.deviceId = EVENTHUB_ID;
2502 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2503
2504 for (auto& arg : notifyArgs) {
2505 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2506 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2507 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2508 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2509 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2510 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2511 }
2512 }
2513}
2514
2515TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2516 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2517 FakeInputMapper& mapper =
2518 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2519 AINPUT_SOURCE_KEYBOARD);
2520 NotifyMotionArgs args;
2521 mapper.setProcessResult({args});
2522
2523 InputReaderConfiguration config;
2524 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2525
2526 RawEvent event;
2527 event.deviceId = EVENTHUB_ID;
2528 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2529
2530 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2531 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2532}
2533
2534TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2535 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2536 FakeInputMapper& mapper =
2537 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2538 AINPUT_SOURCE_KEYBOARD);
2539 NotifyMotionArgs args;
2540 args.policyFlags = POLICY_FLAG_WAKE;
2541 mapper.setProcessResult({args});
2542
2543 InputReaderConfiguration config;
2544 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2545
2546 RawEvent event;
2547 event.deviceId = EVENTHUB_ID;
2548 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2549
2550 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2551 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2552}
2553
Arthur Hung2c9a3342019-07-23 14:18:59 +08002554// A single input device is associated with a specific display. Check that:
2555// 1. Device is disabled if the viewport corresponding to the associated display is not found
2556// 2. Device is disabled when setEnabled API is called
2557TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002558 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2559 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002560
2561 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002562 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002563 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2564 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002565
2566 // Device should be enabled by default.
2567 ASSERT_TRUE(mDevice->isEnabled());
2568
2569 // Prepare associated info.
2570 constexpr uint8_t hdmi = 1;
2571 const std::string UNIQUE_ID = "local:1";
2572
2573 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002574 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002575 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002576 // Device should be disabled because it is associated with a specific display via
2577 // input port <-> display port association, but the corresponding display is not found
2578 ASSERT_FALSE(mDevice->isEnabled());
2579
2580 // Prepare displays.
2581 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002582 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002583 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002584 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002585 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002586 ASSERT_TRUE(mDevice->isEnabled());
2587
2588 // Device should be disabled after set disable.
2589 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002590 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002591 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002592 ASSERT_FALSE(mDevice->isEnabled());
2593
2594 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002595 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002596 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002597 ASSERT_FALSE(mDevice->isEnabled());
2598}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002599
Christine Franks1ba71cc2021-04-07 14:37:42 -07002600TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2601 // Device should be enabled by default.
2602 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002603 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2604 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002605 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002606 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2607 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002608 ASSERT_TRUE(mDevice->isEnabled());
2609
2610 // Device should be disabled because it is associated with a specific display, but the
2611 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002612 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002613 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002614 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002615 ASSERT_FALSE(mDevice->isEnabled());
2616
2617 // Device should be enabled when a display is found.
2618 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002619 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002620 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002621 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002622 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002623 ASSERT_TRUE(mDevice->isEnabled());
2624
2625 // Device should be disabled after set disable.
2626 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002627 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002628 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002629 ASSERT_FALSE(mDevice->isEnabled());
2630
2631 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002632 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002633 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002634 ASSERT_FALSE(mDevice->isEnabled());
2635}
2636
Christine Franks2a2293c2022-01-18 11:51:16 -08002637TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2638 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002639 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2640 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002641 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002642 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2643 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002644
Christine Franks2a2293c2022-01-18 11:51:16 -08002645 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2646 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002647 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002648 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002649 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002650 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002651 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2652}
2653
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002654/**
2655 * This test reproduces a crash caused by a dangling reference that remains after device is added
2656 * and removed. The reference is accessed in InputDevice::dump(..);
2657 */
2658TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2659 constexpr int32_t TEST_EVENTHUB_ID = 10;
2660 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2661
Harry Cutts33476232023-01-30 19:57:29 +00002662 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Siarhei Vishniakouc96ed752023-05-25 00:12:00 +00002663 device.addEventHubDevice(TEST_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002664 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2665 std::string dumpStr, eventHubDevStr;
2666 device.dump(dumpStr, eventHubDevStr);
2667}
2668
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002669TEST_F(InputDeviceTest, GetBluetoothAddress) {
2670 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2671 ASSERT_TRUE(address);
2672 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2673}
2674
Michael Wrightd02c5b62014-02-10 15:10:22 -08002675// --- SwitchInputMapperTest ---
2676
2677class SwitchInputMapperTest : public InputMapperTest {
2678protected:
2679};
2680
2681TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002682 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002683
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002684 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002685}
2686
2687TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002688 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002690 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002691 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002692
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002693 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002694 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002695}
2696
2697TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002698 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002699 std::list<NotifyArgs> out;
2700 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2701 ASSERT_TRUE(out.empty());
2702 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2703 ASSERT_TRUE(out.empty());
2704 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2705 ASSERT_TRUE(out.empty());
2706 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002707
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002708 ASSERT_EQ(1u, out.size());
2709 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002710 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002711 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2712 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002713 args.switchMask);
2714 ASSERT_EQ(uint32_t(0), args.policyFlags);
2715}
2716
Chris Ye87143712020-11-10 05:05:58 +00002717// --- VibratorInputMapperTest ---
2718class VibratorInputMapperTest : public InputMapperTest {
2719protected:
2720 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2721};
2722
2723TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002724 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002725
2726 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2727}
2728
2729TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002730 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002731
2732 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2733}
2734
2735TEST_F(VibratorInputMapperTest, Vibrate) {
2736 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002737 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00002738 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002739
2740 VibrationElement pattern(2);
2741 VibrationSequence sequence(2);
2742 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002743 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2744 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002745 sequence.addElement(pattern);
2746 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002747 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2748 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002749 sequence.addElement(pattern);
2750
2751 std::vector<int64_t> timings = {0, 1};
2752 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2753
2754 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002755 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002756 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002757 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002758 // Verify vibrator state listener was notified.
2759 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002760 ASSERT_EQ(1u, out.size());
2761 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2762 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2763 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002764 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002765 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002766 ASSERT_FALSE(mapper.isVibrating());
2767 // Verify vibrator state listener was notified.
2768 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002769 ASSERT_EQ(1u, out.size());
2770 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2771 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2772 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002773}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002774
Chris Yef59a2f42020-10-16 12:55:26 -07002775// --- SensorInputMapperTest ---
2776
2777class SensorInputMapperTest : public InputMapperTest {
2778protected:
2779 static const int32_t ACCEL_RAW_MIN;
2780 static const int32_t ACCEL_RAW_MAX;
2781 static const int32_t ACCEL_RAW_FUZZ;
2782 static const int32_t ACCEL_RAW_FLAT;
2783 static const int32_t ACCEL_RAW_RESOLUTION;
2784
2785 static const int32_t GYRO_RAW_MIN;
2786 static const int32_t GYRO_RAW_MAX;
2787 static const int32_t GYRO_RAW_FUZZ;
2788 static const int32_t GYRO_RAW_FLAT;
2789 static const int32_t GYRO_RAW_RESOLUTION;
2790
2791 static const float GRAVITY_MS2_UNIT;
2792 static const float DEGREE_RADIAN_UNIT;
2793
2794 void prepareAccelAxes();
2795 void prepareGyroAxes();
2796 void setAccelProperties();
2797 void setGyroProperties();
2798 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2799};
2800
2801const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2802const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2803const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2804const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2805const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2806
2807const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2808const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2809const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2810const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2811const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2812
2813const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2814const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2815
2816void SensorInputMapperTest::prepareAccelAxes() {
2817 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2818 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2819 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2820 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2821 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2822 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2823}
2824
2825void SensorInputMapperTest::prepareGyroAxes() {
2826 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2827 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2828 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2829 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2830 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2831 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2832}
2833
2834void SensorInputMapperTest::setAccelProperties() {
2835 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2836 /* sensorDataIndex */ 0);
2837 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2838 /* sensorDataIndex */ 1);
2839 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2840 /* sensorDataIndex */ 2);
2841 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2842 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2843 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2844 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2845 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2846}
2847
2848void SensorInputMapperTest::setGyroProperties() {
2849 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2850 /* sensorDataIndex */ 0);
2851 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2852 /* sensorDataIndex */ 1);
2853 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2854 /* sensorDataIndex */ 2);
2855 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2856 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2857 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2858 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2859 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2860}
2861
2862TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00002863 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002864
2865 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2866}
2867
2868TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2869 setAccelProperties();
2870 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00002871 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002872
2873 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2874 std::chrono::microseconds(10000),
2875 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002876 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002877 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2878 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2879 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2881 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002882
2883 NotifySensorArgs args;
2884 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2885 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2886 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2887
2888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2889 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2890 ASSERT_EQ(args.deviceId, DEVICE_ID);
2891 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2892 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2893 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2894 ASSERT_EQ(args.values, values);
2895 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2896}
2897
2898TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2899 setGyroProperties();
2900 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00002901 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002902
2903 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2904 std::chrono::microseconds(10000),
2905 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002906 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002907 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2908 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2909 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2910 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2911 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002912
2913 NotifySensorArgs args;
2914 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2915 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2916 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2917
2918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2919 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2920 ASSERT_EQ(args.deviceId, DEVICE_ID);
2921 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2922 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2923 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2924 ASSERT_EQ(args.values, values);
2925 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2926}
2927
Michael Wrightd02c5b62014-02-10 15:10:22 -08002928// --- KeyboardInputMapperTest ---
2929
2930class KeyboardInputMapperTest : public InputMapperTest {
2931protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002932 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002933 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002934 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002935
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002936 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002937 int32_t originalKeyCode, int32_t rotatedKeyCode,
2938 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002939};
2940
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002941/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2942 * orientation.
2943 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002944void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002945 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2946 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002947}
2948
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002949void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002950 int32_t originalScanCode, int32_t originalKeyCode,
2951 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002952 NotifyKeyArgs args;
2953
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002954 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2956 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2957 ASSERT_EQ(originalScanCode, args.scanCode);
2958 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002959 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2963 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2964 ASSERT_EQ(originalScanCode, args.scanCode);
2965 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002966 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002967}
2968
Michael Wrightd02c5b62014-02-10 15:10:22 -08002969TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002970 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00002971 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002972 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002973
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002974 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002975}
2976
2977TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2978 const int32_t USAGE_A = 0x070004;
2979 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002980 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2981 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002982 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2983 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2984 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002985
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002986 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00002987 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002988 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002989 // Initial metastate is AMETA_NONE.
2990 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002991
2992 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994 NotifyKeyArgs args;
2995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2996 ASSERT_EQ(DEVICE_ID, args.deviceId);
2997 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2998 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2999 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3000 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3001 ASSERT_EQ(KEY_HOME, args.scanCode);
3002 ASSERT_EQ(AMETA_NONE, args.metaState);
3003 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3004 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3005 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3006
3007 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003008 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3010 ASSERT_EQ(DEVICE_ID, args.deviceId);
3011 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3012 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3013 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3014 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3015 ASSERT_EQ(KEY_HOME, args.scanCode);
3016 ASSERT_EQ(AMETA_NONE, args.metaState);
3017 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3018 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3019 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3020
3021 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3025 ASSERT_EQ(DEVICE_ID, args.deviceId);
3026 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3027 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3028 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3029 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3030 ASSERT_EQ(0, args.scanCode);
3031 ASSERT_EQ(AMETA_NONE, args.metaState);
3032 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3033 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3034 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3035
3036 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003037 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3038 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3040 ASSERT_EQ(DEVICE_ID, args.deviceId);
3041 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3042 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3043 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3044 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3045 ASSERT_EQ(0, args.scanCode);
3046 ASSERT_EQ(AMETA_NONE, args.metaState);
3047 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3048 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3049 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3050
3051 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3055 ASSERT_EQ(DEVICE_ID, args.deviceId);
3056 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3057 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3058 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3059 ASSERT_EQ(0, args.keyCode);
3060 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3061 ASSERT_EQ(AMETA_NONE, args.metaState);
3062 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3063 ASSERT_EQ(0U, args.policyFlags);
3064 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3065
3066 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3068 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3070 ASSERT_EQ(DEVICE_ID, args.deviceId);
3071 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3072 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3073 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3074 ASSERT_EQ(0, args.keyCode);
3075 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3076 ASSERT_EQ(AMETA_NONE, args.metaState);
3077 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3078 ASSERT_EQ(0U, args.policyFlags);
3079 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3080}
3081
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003082TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3083 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3084 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3085 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3086
3087 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003088 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003089 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3090
3091 // Key down by scan code.
3092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3093 NotifyKeyArgs args;
3094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3095 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3096
3097 // Key up by scan code.
3098 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3100 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3101}
3102
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003103/**
3104 * Ensure that the readTime is set to the time when the EV_KEY is received.
3105 */
3106TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3107 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3108
3109 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003110 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003111 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3112 NotifyKeyArgs args;
3113
3114 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003115 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3117 ASSERT_EQ(12, args.readTime);
3118
3119 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003120 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3122 ASSERT_EQ(15, args.readTime);
3123}
3124
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003126 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3127 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003128 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3129 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3130 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003131
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003132 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003133 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003134 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003135
Arthur Hung95f68612022-04-07 14:08:22 +08003136 // Initial metastate is AMETA_NONE.
3137 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003138
3139 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003141 NotifyKeyArgs args;
3142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3143 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003144 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003145 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003146
3147 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003148 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3150 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003151 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003152
3153 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003154 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3156 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003157 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003158
3159 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003160 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3162 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003163 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003164 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003165}
3166
3167TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003168 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3169 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3170 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3171 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003172
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003173 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003174 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003175 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003176
Michael Wrighta9cf4192022-12-01 23:46:39 +00003177 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003178 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3179 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3180 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3181 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3182 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3183 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3184 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3185 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3186}
3187
3188TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003189 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3190 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3191 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3192 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003193
Michael Wrightd02c5b62014-02-10 15:10:22 -08003194 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003195 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003196 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003197 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003198
Michael Wrighta9cf4192022-12-01 23:46:39 +00003199 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003200 ASSERT_NO_FATAL_FAILURE(
3201 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3202 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3203 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3204 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3205 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3206 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3207 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003208
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003209 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003210 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003211 ASSERT_NO_FATAL_FAILURE(
3212 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3213 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3214 AKEYCODE_DPAD_UP, DISPLAY_ID));
3215 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3216 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3217 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3218 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003219
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003220 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003221 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003222 ASSERT_NO_FATAL_FAILURE(
3223 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3224 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3225 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3226 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3227 AKEYCODE_DPAD_UP, DISPLAY_ID));
3228 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3229 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003230
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003231 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003232 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003233 ASSERT_NO_FATAL_FAILURE(
3234 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3235 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3236 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3237 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3238 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3239 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3240 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003241
3242 // Special case: if orientation changes while key is down, we still emit the same keycode
3243 // in the key up as we did in the key down.
3244 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003245 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003246 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3249 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3250 ASSERT_EQ(KEY_UP, args.scanCode);
3251 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3252
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003253 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003254 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003255 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3257 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3258 ASSERT_EQ(KEY_UP, args.scanCode);
3259 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3260}
3261
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003262TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3263 // If the keyboard is not orientation aware,
3264 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003265 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003266
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003267 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003268 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003269 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003270 NotifyKeyArgs args;
3271
3272 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3277 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3278
Michael Wrighta9cf4192022-12-01 23:46:39 +00003279 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3284 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3285}
3286
3287TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3288 // If the keyboard is orientation aware,
3289 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003290 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003291
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003292 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003293 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003294 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003295 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003296 NotifyKeyArgs args;
3297
3298 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3299 // ^--- already checked by the previous test
3300
Michael Wrighta9cf4192022-12-01 23:46:39 +00003301 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003302 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3307 ASSERT_EQ(DISPLAY_ID, args.displayId);
3308
3309 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003310 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003311 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003312 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3317 ASSERT_EQ(newDisplayId, args.displayId);
3318}
3319
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003321 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003322 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003323 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003324
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003325 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003326 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003328 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003329 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003330}
3331
Philip Junker4af3b3d2021-12-14 10:36:55 +01003332TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3333 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003334 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003335 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3336
3337 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3338 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3339 << "If a mapping is available, the result is equal to the mapping";
3340
3341 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3342 << "If no mapping is available, the result is the key location";
3343}
3344
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003346 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003347 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003348 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003349
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003350 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003351 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003353 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003354 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355}
3356
3357TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003358 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003359 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003360 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003361
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003362 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003363
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003365 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003366 ASSERT_TRUE(flags[0]);
3367 ASSERT_FALSE(flags[1]);
3368}
3369
3370TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003371 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3372 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3373 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3374 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3375 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3376 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003377
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003378 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003379 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003380 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003381 // Initial metastate is AMETA_NONE.
3382 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003383
3384 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003385 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3386 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3387 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003388
3389 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3391 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003392 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3393 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3394 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003395 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003396
3397 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003398 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003400 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3401 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3402 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003403 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003404
3405 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003408 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3409 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3410 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003411 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003412
3413 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003414 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3415 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003416 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3417 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3418 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003419 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003420
3421 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003422 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3423 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003424 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3425 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3426 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003427 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003428
3429 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003432 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3433 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3434 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003435 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003436}
3437
Chris Yea52ade12020-08-27 16:49:20 -07003438TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3439 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3440 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3441 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3442 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3443
3444 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003445 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003446 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3447
Chris Yea52ade12020-08-27 16:49:20 -07003448 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003449 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003450 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3451 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3452 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3453 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3454
3455 NotifyKeyArgs args;
3456 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3459 ASSERT_EQ(AMETA_NONE, args.metaState);
3460 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3461 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3462 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3463
3464 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003465 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3467 ASSERT_EQ(AMETA_NONE, args.metaState);
3468 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3469 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3470 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3471}
3472
Arthur Hung2c9a3342019-07-23 14:18:59 +08003473TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3474 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003475 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3476 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3477 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3478 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003479
3480 // keyboard 2.
3481 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003482 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003483 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003484 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003485 std::shared_ptr<InputDevice> device2 =
3486 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003487 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003488
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003489 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3490 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3491 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3492 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003493
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003494 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003495 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003496 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003497
Arpit Singh67ca6842023-04-26 14:43:16 +00003498 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003499 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003500 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3501 mFakePolicy
3502 ->getReaderConfiguration(),
3503 AINPUT_SOURCE_KEYBOARD,
3504 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003505 std::list<NotifyArgs> unused =
3506 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003507 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003508 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003509
3510 // Prepared displays and associated info.
3511 constexpr uint8_t hdmi1 = 0;
3512 constexpr uint8_t hdmi2 = 1;
3513 const std::string SECONDARY_UNIQUE_ID = "local:1";
3514
3515 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3516 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3517
3518 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003519 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003520 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003521 ASSERT_FALSE(device2->isEnabled());
3522
3523 // Prepare second display.
3524 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003525 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003526 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003527 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003528 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003529 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003530 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003531 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003532
3533 // Device should be enabled after the associated display is found.
3534 ASSERT_TRUE(mDevice->isEnabled());
3535 ASSERT_TRUE(device2->isEnabled());
3536
3537 // Test pad key events
3538 ASSERT_NO_FATAL_FAILURE(
3539 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3540 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3541 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3542 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3543 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3544 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3545 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3546
3547 ASSERT_NO_FATAL_FAILURE(
3548 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3549 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3550 AKEYCODE_DPAD_RIGHT, newDisplayId));
3551 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3552 AKEYCODE_DPAD_DOWN, newDisplayId));
3553 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3554 AKEYCODE_DPAD_LEFT, newDisplayId));
3555}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003556
arthurhungc903df12020-08-11 15:08:42 +08003557TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3558 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3559 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3560 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3561 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3562 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3563 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3564
3565 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003566 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003567 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003568 // Initial metastate is AMETA_NONE.
3569 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003570
3571 // Initialization should have turned all of the lights off.
3572 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3573 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3574 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3575
3576 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3578 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003579 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3580 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3581
3582 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003583 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3584 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003585 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3586 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3587
3588 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3590 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003591 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3592 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3593
3594 mFakeEventHub->removeDevice(EVENTHUB_ID);
3595 mReader->loopOnce();
3596
3597 // keyboard 2 should default toggle keys.
3598 const std::string USB2 = "USB2";
3599 const std::string DEVICE_NAME2 = "KEYBOARD2";
3600 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3601 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3602 std::shared_ptr<InputDevice> device2 =
3603 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003604 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003605 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3606 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3607 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3608 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3609 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3610 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3611
Arpit Singh67ca6842023-04-26 14:43:16 +00003612 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003613 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003614 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3615 mFakePolicy
3616 ->getReaderConfiguration(),
3617 AINPUT_SOURCE_KEYBOARD,
3618 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003619 std::list<NotifyArgs> unused =
3620 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003621 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003622 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003623
3624 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3625 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3626 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003627 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3628 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003629}
3630
Arthur Hungcb40a002021-08-03 14:31:01 +00003631TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3632 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3633 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3634 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3635
3636 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh67ca6842023-04-26 14:43:16 +00003637 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003638 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3639 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003640 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003641 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003642 // Initial metastate is AMETA_NONE.
3643 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003644
3645 mReader->toggleCapsLockState(DEVICE_ID);
3646 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3647}
3648
Arthur Hungfb3cc112022-04-13 07:39:50 +00003649TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3650 // keyboard 1.
3651 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3652 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3653 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3654 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3655 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3656 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3657
3658 KeyboardInputMapper& mapper1 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003659 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003660 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3661
3662 // keyboard 2.
3663 const std::string USB2 = "USB2";
3664 const std::string DEVICE_NAME2 = "KEYBOARD2";
3665 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3666 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3667 std::shared_ptr<InputDevice> device2 =
3668 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3669 ftl::Flags<InputDeviceClass>(0));
3670 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3671 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3672 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3673 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3674 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3675 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3676
Arpit Singh67ca6842023-04-26 14:43:16 +00003677 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003678 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003679 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3680 mFakePolicy
3681 ->getReaderConfiguration(),
3682 AINPUT_SOURCE_KEYBOARD,
3683 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003684 std::list<NotifyArgs> unused =
3685 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003686 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003687 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003688
Arthur Hung95f68612022-04-07 14:08:22 +08003689 // Initial metastate is AMETA_NONE.
3690 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3691 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3692
3693 // Toggle num lock on and off.
3694 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3695 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003696 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3697 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3698 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3699
3700 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3701 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3702 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3703 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3704 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3705
3706 // Toggle caps lock on and off.
3707 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3708 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3709 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3710 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3711 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3712
3713 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3714 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3715 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3716 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3717 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3718
3719 // Toggle scroll lock on and off.
3720 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3721 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3722 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3723 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3724 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3725
3726 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3727 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3728 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3729 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3730 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3731}
3732
Arthur Hung2141d542022-08-23 07:45:21 +00003733TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3734 const int32_t USAGE_A = 0x070004;
3735 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3736 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3737
3738 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003739 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00003740 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3741 // Key down by scan code.
3742 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3743 NotifyKeyArgs args;
3744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3745 ASSERT_EQ(DEVICE_ID, args.deviceId);
3746 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3747 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3748 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3749 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3750 ASSERT_EQ(KEY_HOME, args.scanCode);
3751 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3752
3753 // Disable device, it should synthesize cancellation events for down events.
3754 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003755 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00003756
3757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3758 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3759 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3760 ASSERT_EQ(KEY_HOME, args.scanCode);
3761 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3762}
3763
Zixuan Qufecb6062022-11-12 04:44:31 +00003764TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh67ca6842023-04-26 14:43:16 +00003765 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3766 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00003767 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003768 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3769 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00003770
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003771 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00003772 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3773
3774 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003775 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00003776
3777 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3778 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3779 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3780 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3781 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003782 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
3783
3784 // Call change layout association with the same values: Generation shouldn't change
3785 generation = mReader->getContext()->getGeneration();
3786 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3787 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3788 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
3789 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00003790}
3791
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003792TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3793 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3794 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3795
3796 // Configuration
Arpit Singh67ca6842023-04-26 14:43:16 +00003797 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003798 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3799 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003800 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003801
3802 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3803 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3804}
3805
Justin Chung71ddb432023-03-27 04:29:07 +00003806TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
3807 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
3808 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003809 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Justin Chung71ddb432023-03-27 04:29:07 +00003810 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3811 NotifyKeyArgs args;
3812
3813 // Key down
3814 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
3815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3816 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
3817}
3818
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003819// --- KeyboardInputMapperTest_ExternalDevice ---
3820
3821class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3822protected:
Chris Yea52ade12020-08-27 16:49:20 -07003823 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003824};
3825
3826TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003827 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3828 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003829
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003830 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3831 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3832 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3833 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003834
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003835 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003836 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003837 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003838
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003840 NotifyKeyArgs args;
3841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3842 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3843
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003844 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3846 ASSERT_EQ(uint32_t(0), args.policyFlags);
3847
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003848 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003850 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003851
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003852 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3854 ASSERT_EQ(uint32_t(0), args.policyFlags);
3855
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3858 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3859
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003860 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3862 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3863}
3864
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003865TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003866 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003867
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003868 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3869 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3870 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003871
Powei Fengd041c5d2019-05-03 17:11:33 -07003872 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003873 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003874 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003875 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003876
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003877 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003878 NotifyKeyArgs args;
3879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3880 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3881
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003882 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3884 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3885
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003886 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3888 ASSERT_EQ(uint32_t(0), args.policyFlags);
3889
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003890 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3892 ASSERT_EQ(uint32_t(0), args.policyFlags);
3893
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003894 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3896 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3897
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003898 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3900 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3901}
3902
Michael Wrightd02c5b62014-02-10 15:10:22 -08003903// --- CursorInputMapperTest ---
3904
3905class CursorInputMapperTest : public InputMapperTest {
3906protected:
3907 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3908
Michael Wright17db18e2020-06-26 20:51:44 +01003909 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003910
Chris Yea52ade12020-08-27 16:49:20 -07003911 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003912 InputMapperTest::SetUp();
3913
Michael Wright17db18e2020-06-26 20:51:44 +01003914 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003915 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003916 }
3917
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003918 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3919 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003920
Michael Wrighta9cf4192022-12-01 23:46:39 +00003921 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003922 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3923 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3924 }
3925
3926 void prepareSecondaryDisplay() {
3927 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003928 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003929 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003930 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003931
3932 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3933 float pressure) {
3934 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3935 0.0f, 0.0f, 0.0f, EPSILON));
3936 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003937};
3938
3939const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3940
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003941void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3942 int32_t originalY, int32_t rotatedX,
3943 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003944 NotifyMotionArgs args;
3945
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003951 ASSERT_NO_FATAL_FAILURE(
3952 assertCursorPointerCoords(args.pointerCoords[0],
3953 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3954 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955}
3956
3957TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003958 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00003959 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003960
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003961 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003962}
3963
3964TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003965 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00003966 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003967
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003968 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003969}
3970
3971TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003972 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00003973 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003974
3975 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003976 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003977
3978 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003979 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3980 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003981 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3982 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3983
3984 // When the bounds are set, then there should be a valid motion range.
3985 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3986
3987 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003988 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003989
3990 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3991 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3992 1, 800 - 1, 0.0f, 0.0f));
3993 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3994 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3995 2, 480 - 1, 0.0f, 0.0f));
3996 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3997 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3998 0.0f, 1.0f, 0.0f, 0.0f));
3999}
4000
4001TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004002 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004003 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004004
4005 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004006 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007
4008 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4009 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4010 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4011 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4012 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4013 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4014 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4015 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4016 0.0f, 1.0f, 0.0f, 0.0f));
4017}
4018
4019TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004020 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004021 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004022
arthurhungdcef2dc2020-08-11 14:47:50 +08004023 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004024
4025 NotifyMotionArgs args;
4026
4027 // Button press.
4028 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4030 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4032 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4033 ASSERT_EQ(DEVICE_ID, args.deviceId);
4034 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4035 ASSERT_EQ(uint32_t(0), args.policyFlags);
4036 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4037 ASSERT_EQ(0, args.flags);
4038 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4039 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4040 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004041 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004042 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004043 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004044 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004045 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4046 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4047 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4048
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4050 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4051 ASSERT_EQ(DEVICE_ID, args.deviceId);
4052 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4053 ASSERT_EQ(uint32_t(0), args.policyFlags);
4054 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4055 ASSERT_EQ(0, args.flags);
4056 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4057 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4058 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004059 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004060 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004061 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004062 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004063 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4064 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4065 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4066
Michael Wrightd02c5b62014-02-10 15:10:22 -08004067 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004068 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4069 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4071 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4072 ASSERT_EQ(DEVICE_ID, args.deviceId);
4073 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4074 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004075 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4076 ASSERT_EQ(0, args.flags);
4077 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4078 ASSERT_EQ(0, args.buttonState);
4079 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004080 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004081 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004082 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004083 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004084 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4085 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4086 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4087
4088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4089 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4090 ASSERT_EQ(DEVICE_ID, args.deviceId);
4091 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4092 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004093 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4094 ASSERT_EQ(0, args.flags);
4095 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4096 ASSERT_EQ(0, args.buttonState);
4097 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004098 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004099 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004100 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004101 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004102 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4103 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4104 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4105}
4106
4107TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004108 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004109 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110
4111 NotifyMotionArgs args;
4112
4113 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004114 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004118 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4119 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4120 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004121
4122 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004127 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4128 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004129}
4130
4131TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004132 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004133 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004134
4135 NotifyMotionArgs args;
4136
4137 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4141 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004142 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4145 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004146 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004147
Michael Wrightd02c5b62014-02-10 15:10:22 -08004148 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004152 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004153 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004154
4155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004156 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004157 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004158}
4159
4160TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004161 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004162 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004163
4164 NotifyMotionArgs args;
4165
4166 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004167 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4172 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004173 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4174 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4175 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004176
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4178 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004179 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4180 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4181 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004182
Michael Wrightd02c5b62014-02-10 15:10:22 -08004183 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004189 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4190 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4191 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004192
4193 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004194 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4195 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004197 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004198 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004199
4200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004202 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004203}
4204
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004205TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004206 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004207 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004208 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4209 // need to be rotated.
4210 addConfigurationProperty("cursor.orientationAware", "1");
Arpit Singhe036ad72023-04-27 12:48:15 +00004211 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212
Michael Wrighta9cf4192022-12-01 23:46:39 +00004213 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4215 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4216 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4217 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4218 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4219 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4220 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4221 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4222}
4223
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004224TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004225 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004226 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004227 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4228 // orientation-aware are affected by display rotation.
Arpit Singhe036ad72023-04-27 12:48:15 +00004229 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004231 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004232 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4234 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4235 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4236 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4237 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4238 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4239 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4240 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4241
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004242 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004243 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004244 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4245 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4246 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4247 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4248 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4249 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4250 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4251 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004253 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004254 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004255 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4256 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4257 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4258 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4259 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4260 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4261 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4262 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4263
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004264 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004265 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004266 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4267 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4268 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4269 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4270 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4271 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4272 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4273 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274}
4275
4276TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004278 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279
4280 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4281 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282
4283 NotifyMotionArgs motionArgs;
4284 NotifyKeyArgs keyArgs;
4285
4286 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4290 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4291 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004292 ASSERT_NO_FATAL_FAILURE(
4293 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4296 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4297 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004298 ASSERT_NO_FATAL_FAILURE(
4299 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004300
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004304 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004305 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004306 ASSERT_NO_FATAL_FAILURE(
4307 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308
4309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004310 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004312 ASSERT_NO_FATAL_FAILURE(
4313 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004314
4315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004317 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004318 ASSERT_NO_FATAL_FAILURE(
4319 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320
4321 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004322 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4324 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4326 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4327 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004328 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004329 ASSERT_NO_FATAL_FAILURE(
4330 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4333 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4334 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004335 ASSERT_NO_FATAL_FAILURE(
4336 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004337
4338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4339 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4340 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004341 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004342 ASSERT_NO_FATAL_FAILURE(
4343 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004344
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004345 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4346 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004348 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004350 ASSERT_NO_FATAL_FAILURE(
4351 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004352
4353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004355 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004356 ASSERT_NO_FATAL_FAILURE(
4357 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004359 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4360 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004362 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4363 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004364 ASSERT_NO_FATAL_FAILURE(
4365 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004366 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004368
4369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004372 ASSERT_NO_FATAL_FAILURE(
4373 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004374
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4376 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004378 ASSERT_NO_FATAL_FAILURE(
4379 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380
4381 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004382 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4385 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4386 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004387
Michael Wrightd02c5b62014-02-10 15:10:22 -08004388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004389 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004391 ASSERT_NO_FATAL_FAILURE(
4392 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004393
4394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4395 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4396 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004397 ASSERT_NO_FATAL_FAILURE(
4398 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004399
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004403 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004405 ASSERT_NO_FATAL_FAILURE(
4406 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004407
4408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004410 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004411
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004412 ASSERT_NO_FATAL_FAILURE(
4413 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4415 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4416 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4417
4418 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4422 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4423 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004424
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004426 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004428 ASSERT_NO_FATAL_FAILURE(
4429 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004430
4431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4432 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4433 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004434 ASSERT_NO_FATAL_FAILURE(
4435 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004436
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004437 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4438 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004440 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004442 ASSERT_NO_FATAL_FAILURE(
4443 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004444
4445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4446 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4447 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004448 ASSERT_NO_FATAL_FAILURE(
4449 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004450
Michael Wrightd02c5b62014-02-10 15:10:22 -08004451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4452 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4453 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4454
4455 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4459 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4460 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004461
Michael Wrightd02c5b62014-02-10 15:10:22 -08004462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004463 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004465 ASSERT_NO_FATAL_FAILURE(
4466 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004467
4468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4469 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4470 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004471 ASSERT_NO_FATAL_FAILURE(
4472 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004477 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004478 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004479 ASSERT_NO_FATAL_FAILURE(
4480 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004481
4482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4483 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4484 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004485 ASSERT_NO_FATAL_FAILURE(
4486 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004487
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4489 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4490 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4491
4492 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004493 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4494 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4496 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4497 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004498
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004500 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004501 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004502 ASSERT_NO_FATAL_FAILURE(
4503 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004504
4505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4506 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4507 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004510
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004511 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4512 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004514 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004516 ASSERT_NO_FATAL_FAILURE(
4517 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004518
4519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4520 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4521 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004522 ASSERT_NO_FATAL_FAILURE(
4523 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004524
Michael Wrightd02c5b62014-02-10 15:10:22 -08004525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4526 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4527 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4528}
4529
4530TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004532 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533
4534 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4535 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004536
4537 NotifyMotionArgs args;
4538
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004539 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
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(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004543 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4544 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4546 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 +00004547 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004548}
4549
4550TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004551 addConfigurationProperty("cursor.mode", "pointer");
4552 mFakePolicy->setPointerCapture(true);
Arpit Singhe036ad72023-04-27 12:48:15 +00004553 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004554
4555 NotifyDeviceResetArgs resetArgs;
4556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4557 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4558 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4559
4560 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4561 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004562
4563 NotifyMotionArgs args;
4564
4565 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4570 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4573 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 +00004574 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004575
4576 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4578 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4580 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4581 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4583 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4585 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4586 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4588 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4589
4590 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004591 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4592 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4594 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4595 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4597 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4599 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4600 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4602 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4603
4604 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004605 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4606 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4607 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4609 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4612 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 +00004613 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004614
4615 // Disable pointer capture and check that the device generation got bumped
4616 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004617 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004618 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004619 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004620 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004621
4622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004623 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4624
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004625 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4629 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004630 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4632 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 +00004633 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004634}
4635
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004636/**
4637 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4638 * pointer acceleration or speed processing should not be applied.
4639 */
4640TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4641 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004642 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4643 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004644 mFakePolicy->setVelocityControlParams(testParams);
Arpit Singhe036ad72023-04-27 12:48:15 +00004645 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004646
4647 NotifyDeviceResetArgs resetArgs;
4648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4649 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4650 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4651
4652 NotifyMotionArgs args;
4653
4654 // Move and verify scale is applied.
4655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4659 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4660 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4661 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4662 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4663 ASSERT_GT(relX, 10);
4664 ASSERT_GT(relY, 20);
4665
4666 // Enable Pointer Capture
4667 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004668 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004669 NotifyPointerCaptureChangedArgs captureArgs;
4670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4671 ASSERT_TRUE(captureArgs.request.enable);
4672
4673 // Move and verify scale is not applied.
4674 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4675 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4676 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4678 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4679 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4680 ASSERT_EQ(10, args.pointerCoords[0].getX());
4681 ASSERT_EQ(20, args.pointerCoords[0].getY());
4682}
4683
Prabir Pradhan208360b2022-06-24 18:37:04 +00004684TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4685 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004686 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan208360b2022-06-24 18:37:04 +00004687
4688 NotifyDeviceResetArgs resetArgs;
4689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4690 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4691 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4692
4693 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004694 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004695
4696 NotifyMotionArgs args;
4697
4698 // Verify that the coordinates are rotated.
4699 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4701 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4703 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4704 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4705 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4706 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4707
4708 // Enable Pointer Capture.
4709 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004710 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004711 NotifyPointerCaptureChangedArgs captureArgs;
4712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4713 ASSERT_TRUE(captureArgs.request.enable);
4714
4715 // Move and verify rotation is not applied.
4716 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4717 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4718 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4720 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4721 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4722 ASSERT_EQ(10, args.pointerCoords[0].getX());
4723 ASSERT_EQ(20, args.pointerCoords[0].getY());
4724}
4725
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004726TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004727 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004728
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004729 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004730 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004731
4732 // Set up the secondary display as the display on which the pointer should be shown.
4733 // The InputDevice is not associated with any display.
4734 prepareSecondaryDisplay();
4735 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004736 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004737
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004738 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004739 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004740
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004741 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004742 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4743 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4744 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004746 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4747 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4748 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004749 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004750}
4751
4752TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004753 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004754
4755 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004756 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004757
4758 // Set up the secondary display as the display on which the pointer should be shown,
4759 // and associate the InputDevice with the secondary display.
4760 prepareSecondaryDisplay();
4761 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4762 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004763 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004764
4765 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4766 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004767
4768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004772 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4773 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4774 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004775 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004776}
4777
4778TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004779 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004780
4781 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004782 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004783 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4784
4785 // Associate the InputDevice with the secondary display.
4786 prepareSecondaryDisplay();
4787 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004788 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004789
4790 // The mapper should not generate any events because it is associated with a display that is
4791 // different from the pointer display.
4792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004796}
4797
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004798// --- BluetoothCursorInputMapperTest ---
4799
4800class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4801protected:
4802 void SetUp() override {
4803 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4804
4805 mFakePointerController = std::make_shared<FakePointerController>();
4806 mFakePolicy->setPointerController(mFakePointerController);
4807 }
4808};
4809
4810TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4811 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004812 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004813
4814 nsecs_t kernelEventTime = ARBITRARY_TIME;
4815 nsecs_t expectedEventTime = ARBITRARY_TIME;
4816 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4817 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4819 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4820 WithEventTime(expectedEventTime))));
4821
4822 // Process several events that come in quick succession, according to their timestamps.
4823 for (int i = 0; i < 3; i++) {
4824 constexpr static nsecs_t delta = ms2ns(1);
4825 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4826 kernelEventTime += delta;
4827 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4828
4829 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4830 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4832 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4833 WithEventTime(expectedEventTime))));
4834 }
4835}
4836
4837TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4838 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004839 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004840
4841 nsecs_t expectedEventTime = ARBITRARY_TIME;
4842 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4843 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4845 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4846 WithEventTime(expectedEventTime))));
4847
4848 // Process several events with the same timestamp from the kernel.
4849 // Ensure that we do not generate events too far into the future.
4850 constexpr static int32_t numEvents =
4851 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4852 for (int i = 0; i < numEvents; i++) {
4853 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4854
4855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4858 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4859 WithEventTime(expectedEventTime))));
4860 }
4861
4862 // By processing more events with the same timestamp, we should not generate events with a
4863 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4864 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4865 for (int i = 0; i < 3; i++) {
4866 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4867 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4869 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4870 WithEventTime(cappedEventTime))));
4871 }
4872}
4873
4874TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4875 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004876 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004877
4878 nsecs_t kernelEventTime = ARBITRARY_TIME;
4879 nsecs_t expectedEventTime = ARBITRARY_TIME;
4880 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4881 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4883 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4884 WithEventTime(expectedEventTime))));
4885
4886 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4887 // smoothening is not needed, its timestamp is not affected.
4888 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4889 expectedEventTime = kernelEventTime;
4890
4891 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4892 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4894 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4895 WithEventTime(expectedEventTime))));
4896}
4897
Michael Wrightd02c5b62014-02-10 15:10:22 -08004898// --- TouchInputMapperTest ---
4899
4900class TouchInputMapperTest : public InputMapperTest {
4901protected:
4902 static const int32_t RAW_X_MIN;
4903 static const int32_t RAW_X_MAX;
4904 static const int32_t RAW_Y_MIN;
4905 static const int32_t RAW_Y_MAX;
4906 static const int32_t RAW_TOUCH_MIN;
4907 static const int32_t RAW_TOUCH_MAX;
4908 static const int32_t RAW_TOOL_MIN;
4909 static const int32_t RAW_TOOL_MAX;
4910 static const int32_t RAW_PRESSURE_MIN;
4911 static const int32_t RAW_PRESSURE_MAX;
4912 static const int32_t RAW_ORIENTATION_MIN;
4913 static const int32_t RAW_ORIENTATION_MAX;
4914 static const int32_t RAW_DISTANCE_MIN;
4915 static const int32_t RAW_DISTANCE_MAX;
4916 static const int32_t RAW_TILT_MIN;
4917 static const int32_t RAW_TILT_MAX;
4918 static const int32_t RAW_ID_MIN;
4919 static const int32_t RAW_ID_MAX;
4920 static const int32_t RAW_SLOT_MIN;
4921 static const int32_t RAW_SLOT_MAX;
4922 static const float X_PRECISION;
4923 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004924 static const float X_PRECISION_VIRTUAL;
4925 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004926
4927 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004928 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004929
4930 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4931
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004932 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004933 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004934
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935 enum Axes {
4936 POSITION = 1 << 0,
4937 TOUCH = 1 << 1,
4938 TOOL = 1 << 2,
4939 PRESSURE = 1 << 3,
4940 ORIENTATION = 1 << 4,
4941 MINOR = 1 << 5,
4942 ID = 1 << 6,
4943 DISTANCE = 1 << 7,
4944 TILT = 1 << 8,
4945 SLOT = 1 << 9,
4946 TOOL_TYPE = 1 << 10,
4947 };
4948
Michael Wrighta9cf4192022-12-01 23:46:39 +00004949 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004950 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004951 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004952 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004953 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004954 int32_t toRawX(float displayX);
4955 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004956 int32_t toRotatedRawX(float displayX);
4957 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004958 float toCookedX(float rawX, float rawY);
4959 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004960 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004961 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004962 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004963 float toDisplayY(int32_t rawY, int32_t displayHeight);
4964
Michael Wrightd02c5b62014-02-10 15:10:22 -08004965};
4966
4967const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4968const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4969const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4970const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4971const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4972const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4973const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4974const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004975const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4976const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004977const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4978const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4979const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4980const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4981const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4982const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4983const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4984const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4985const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4986const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4987const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4988const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004989const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4990 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4991const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4992 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004993const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4994 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995
4996const float TouchInputMapperTest::GEOMETRIC_SCALE =
4997 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4998 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4999
5000const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5001 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5002 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5003};
5004
Michael Wrighta9cf4192022-12-01 23:46:39 +00005005void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005006 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5007 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005008}
5009
5010void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5011 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00005012 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013}
5014
Michael Wrighta9cf4192022-12-01 23:46:39 +00005015void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005016 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5017 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5018 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005019}
5020
Michael Wrightd02c5b62014-02-10 15:10:22 -08005021void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005022 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5023 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5024 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5025 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005026}
5027
Jason Gerecke489fda82012-09-07 17:19:40 -07005028void TouchInputMapperTest::prepareLocationCalibration() {
5029 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5030}
5031
Michael Wrightd02c5b62014-02-10 15:10:22 -08005032int32_t TouchInputMapperTest::toRawX(float displayX) {
5033 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5034}
5035
5036int32_t TouchInputMapperTest::toRawY(float displayY) {
5037 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5038}
5039
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005040int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5041 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5042}
5043
5044int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5045 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5046}
5047
Jason Gerecke489fda82012-09-07 17:19:40 -07005048float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5049 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5050 return rawX;
5051}
5052
5053float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5054 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5055 return rawY;
5056}
5057
Michael Wrightd02c5b62014-02-10 15:10:22 -08005058float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005059 return toDisplayX(rawX, DISPLAY_WIDTH);
5060}
5061
5062float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5063 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005064}
5065
5066float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005067 return toDisplayY(rawY, DISPLAY_HEIGHT);
5068}
5069
5070float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5071 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005072}
5073
5074
5075// --- SingleTouchInputMapperTest ---
5076
5077class SingleTouchInputMapperTest : public TouchInputMapperTest {
5078protected:
5079 void prepareButtons();
5080 void prepareAxes(int axes);
5081
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005082 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5083 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5084 void processUp(SingleTouchInputMapper& mappery);
5085 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5086 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5087 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5088 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5089 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5090 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005091};
5092
5093void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005094 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005095}
5096
5097void SingleTouchInputMapperTest::prepareAxes(int axes) {
5098 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005099 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5100 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005101 }
5102 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005103 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5104 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105 }
5106 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005107 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5108 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005109 }
5110 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005111 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5112 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113 }
5114 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005115 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5116 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005117 }
5118}
5119
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005120void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005124}
5125
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005126void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005127 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5128 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129}
5130
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005131void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005133}
5134
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005135void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005137}
5138
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005139void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5140 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005142}
5143
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005144void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005146}
5147
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005148void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5149 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005152}
5153
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005154void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5155 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005157}
5158
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005159void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005160 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005161}
5162
Michael Wrightd02c5b62014-02-10 15:10:22 -08005163TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005164 prepareButtons();
5165 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005166 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005167
Josep del Río2d8c79a2023-01-23 19:33:50 +00005168 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005169}
5170
Michael Wrightd02c5b62014-02-10 15:10:22 -08005171TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005172 prepareButtons();
5173 prepareAxes(POSITION);
5174 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00005175 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005176
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005177 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005178}
5179
5180TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005181 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005182 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005183 prepareButtons();
5184 prepareAxes(POSITION);
5185 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005186 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005187
5188 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005189 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005190
5191 // Virtual key is down.
5192 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5193 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5194 processDown(mapper, x, y);
5195 processSync(mapper);
5196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5197
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005198 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005199
5200 // Virtual key is up.
5201 processUp(mapper);
5202 processSync(mapper);
5203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5204
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005205 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005206}
5207
5208TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005210 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005211 prepareButtons();
5212 prepareAxes(POSITION);
5213 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005214 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005215
5216 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005217 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005218
5219 // Virtual key is down.
5220 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5221 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5222 processDown(mapper, x, y);
5223 processSync(mapper);
5224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5225
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005226 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005227
5228 // Virtual key is up.
5229 processUp(mapper);
5230 processSync(mapper);
5231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5232
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005233 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005234}
5235
5236TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005237 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005238 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005239 prepareButtons();
5240 prepareAxes(POSITION);
5241 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005242 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005243
Michael Wrightd02c5b62014-02-10 15:10:22 -08005244 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005245 ASSERT_TRUE(
5246 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005247 ASSERT_TRUE(flags[0]);
5248 ASSERT_FALSE(flags[1]);
5249}
5250
5251TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005252 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005253 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005254 prepareButtons();
5255 prepareAxes(POSITION);
5256 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005257 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005258
arthurhungdcef2dc2020-08-11 14:47:50 +08005259 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005260
5261 NotifyKeyArgs args;
5262
5263 // Press virtual key.
5264 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5265 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5266 processDown(mapper, x, y);
5267 processSync(mapper);
5268
5269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5270 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5271 ASSERT_EQ(DEVICE_ID, args.deviceId);
5272 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5273 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5274 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5275 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5276 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5277 ASSERT_EQ(KEY_HOME, args.scanCode);
5278 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5279 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5280
5281 // Release virtual key.
5282 processUp(mapper);
5283 processSync(mapper);
5284
5285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5286 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5287 ASSERT_EQ(DEVICE_ID, args.deviceId);
5288 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5289 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5290 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5291 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5292 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5293 ASSERT_EQ(KEY_HOME, args.scanCode);
5294 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5295 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5296
5297 // Should not have sent any motions.
5298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5299}
5300
5301TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005302 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005303 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005304 prepareButtons();
5305 prepareAxes(POSITION);
5306 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005307 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005308
arthurhungdcef2dc2020-08-11 14:47:50 +08005309 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005310
5311 NotifyKeyArgs keyArgs;
5312
5313 // Press virtual key.
5314 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5315 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5316 processDown(mapper, x, y);
5317 processSync(mapper);
5318
5319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5320 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5321 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5322 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5323 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5324 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5325 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5326 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5327 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5328 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5329 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5330
5331 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5332 // into the display area.
5333 y -= 100;
5334 processMove(mapper, x, y);
5335 processSync(mapper);
5336
5337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5338 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5339 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5340 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5341 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5342 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5343 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5344 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5345 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5346 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5347 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5348 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5349
5350 NotifyMotionArgs motionArgs;
5351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5352 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5353 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5354 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5355 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5356 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5357 ASSERT_EQ(0, motionArgs.flags);
5358 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5359 ASSERT_EQ(0, motionArgs.buttonState);
5360 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005361 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005362 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005363 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5365 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5366 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5367 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5368 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5369
5370 // Keep moving out of bounds. Should generate a pointer move.
5371 y -= 50;
5372 processMove(mapper, x, y);
5373 processSync(mapper);
5374
5375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5376 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5377 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5378 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5379 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5380 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5381 ASSERT_EQ(0, motionArgs.flags);
5382 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5383 ASSERT_EQ(0, motionArgs.buttonState);
5384 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005385 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005386 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005387 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5389 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5390 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5391 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5392 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5393
5394 // Release out of bounds. Should generate a pointer up.
5395 processUp(mapper);
5396 processSync(mapper);
5397
5398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5399 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5400 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5401 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5402 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5403 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5404 ASSERT_EQ(0, motionArgs.flags);
5405 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5406 ASSERT_EQ(0, motionArgs.buttonState);
5407 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005408 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005409 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005410 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5412 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5413 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5414 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5415 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5416
5417 // Should not have sent any more keys or motions.
5418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5420}
5421
5422TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005423 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005424 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005425 prepareButtons();
5426 prepareAxes(POSITION);
5427 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005428 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005429
arthurhungdcef2dc2020-08-11 14:47:50 +08005430 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005431
5432 NotifyMotionArgs motionArgs;
5433
5434 // Initially go down out of bounds.
5435 int32_t x = -10;
5436 int32_t y = -10;
5437 processDown(mapper, x, y);
5438 processSync(mapper);
5439
5440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5441
5442 // Move into the display area. Should generate a pointer down.
5443 x = 50;
5444 y = 75;
5445 processMove(mapper, x, y);
5446 processSync(mapper);
5447
5448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5449 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5450 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5451 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5452 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5453 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5454 ASSERT_EQ(0, motionArgs.flags);
5455 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5456 ASSERT_EQ(0, motionArgs.buttonState);
5457 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005458 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005459 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005460 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5462 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5463 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5464 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5465 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5466
5467 // Release. Should generate a pointer up.
5468 processUp(mapper);
5469 processSync(mapper);
5470
5471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5472 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5473 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5474 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5475 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5476 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5477 ASSERT_EQ(0, motionArgs.flags);
5478 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5479 ASSERT_EQ(0, motionArgs.buttonState);
5480 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005481 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005482 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005483 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5485 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5486 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5487 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5488 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5489
5490 // Should not have sent any more keys or motions.
5491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5493}
5494
Santos Cordonfa5cf462017-04-05 10:37:00 -07005495TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005496 addConfigurationProperty("touch.deviceType", "touchScreen");
5497 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5498
Michael Wrighta9cf4192022-12-01 23:46:39 +00005499 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005500 prepareButtons();
5501 prepareAxes(POSITION);
5502 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005503 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005504
arthurhungdcef2dc2020-08-11 14:47:50 +08005505 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005506
5507 NotifyMotionArgs motionArgs;
5508
5509 // Down.
5510 int32_t x = 100;
5511 int32_t y = 125;
5512 processDown(mapper, x, y);
5513 processSync(mapper);
5514
5515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5516 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5517 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5518 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5519 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5520 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5521 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5522 ASSERT_EQ(0, motionArgs.flags);
5523 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5524 ASSERT_EQ(0, motionArgs.buttonState);
5525 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005526 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005527 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005528 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5530 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5531 1, 0, 0, 0, 0, 0, 0, 0));
5532 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5533 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5534 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5535
5536 // Move.
5537 x += 50;
5538 y += 75;
5539 processMove(mapper, x, y);
5540 processSync(mapper);
5541
5542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5543 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5544 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5545 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5546 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5547 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5549 ASSERT_EQ(0, motionArgs.flags);
5550 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5551 ASSERT_EQ(0, motionArgs.buttonState);
5552 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005553 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005554 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005555 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5557 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5558 1, 0, 0, 0, 0, 0, 0, 0));
5559 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5560 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5561 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5562
5563 // Up.
5564 processUp(mapper);
5565 processSync(mapper);
5566
5567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5568 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5569 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5570 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5571 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5572 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5573 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5574 ASSERT_EQ(0, motionArgs.flags);
5575 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5576 ASSERT_EQ(0, motionArgs.buttonState);
5577 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005578 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005579 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005580 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5582 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5583 1, 0, 0, 0, 0, 0, 0, 0));
5584 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5585 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5586 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5587
5588 // Should not have sent any more keys or motions.
5589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5591}
5592
Michael Wrightd02c5b62014-02-10 15:10:22 -08005593TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005594 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005595 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005596 prepareButtons();
5597 prepareAxes(POSITION);
5598 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005599 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005600
arthurhungdcef2dc2020-08-11 14:47:50 +08005601 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005602
5603 NotifyMotionArgs motionArgs;
5604
5605 // Down.
5606 int32_t x = 100;
5607 int32_t y = 125;
5608 processDown(mapper, x, y);
5609 processSync(mapper);
5610
5611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5612 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5613 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5614 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5615 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5616 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5617 ASSERT_EQ(0, motionArgs.flags);
5618 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5619 ASSERT_EQ(0, motionArgs.buttonState);
5620 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005621 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005622 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005623 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5625 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5626 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5627 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5628 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5629
5630 // Move.
5631 x += 50;
5632 y += 75;
5633 processMove(mapper, x, y);
5634 processSync(mapper);
5635
5636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5637 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5638 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5639 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5640 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5641 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5642 ASSERT_EQ(0, motionArgs.flags);
5643 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5644 ASSERT_EQ(0, motionArgs.buttonState);
5645 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005646 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005647 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005648 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5650 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5651 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5652 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5653 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5654
5655 // Up.
5656 processUp(mapper);
5657 processSync(mapper);
5658
5659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5660 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5661 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5662 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5663 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5664 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5665 ASSERT_EQ(0, motionArgs.flags);
5666 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5667 ASSERT_EQ(0, motionArgs.buttonState);
5668 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005669 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005670 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005671 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5673 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5674 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5675 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5676 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5677
5678 // Should not have sent any more keys or motions.
5679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5681}
5682
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005683TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005684 addConfigurationProperty("touch.deviceType", "touchScreen");
5685 prepareButtons();
5686 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005687 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5688 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005689 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005690
5691 NotifyMotionArgs args;
5692
5693 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005694 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005695 processDown(mapper, toRawX(50), toRawY(75));
5696 processSync(mapper);
5697
5698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5699 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5700 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5701
5702 processUp(mapper);
5703 processSync(mapper);
5704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5705}
5706
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005707TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005708 addConfigurationProperty("touch.deviceType", "touchScreen");
5709 prepareButtons();
5710 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005711 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5712 // orientation-aware are affected by display rotation.
5713 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005714 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005715
5716 NotifyMotionArgs args;
5717
5718 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005719 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005720 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005721 processDown(mapper, toRawX(50), toRawY(75));
5722 processSync(mapper);
5723
5724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5725 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5726 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5727
5728 processUp(mapper);
5729 processSync(mapper);
5730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5731
5732 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005733 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005734 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005735 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005736 processSync(mapper);
5737
5738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5739 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5740 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5741
5742 processUp(mapper);
5743 processSync(mapper);
5744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5745
5746 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005747 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005748 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005749 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5750 processSync(mapper);
5751
5752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5753 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5754 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5755
5756 processUp(mapper);
5757 processSync(mapper);
5758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5759
5760 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005761 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005762 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005763 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005764 processSync(mapper);
5765
5766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5767 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5768 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5769
5770 processUp(mapper);
5771 processSync(mapper);
5772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5773}
5774
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005775TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5776 addConfigurationProperty("touch.deviceType", "touchScreen");
5777 prepareButtons();
5778 prepareAxes(POSITION);
5779 addConfigurationProperty("touch.orientationAware", "1");
5780 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5781 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005782 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005783 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005784 NotifyMotionArgs args;
5785
5786 // Orientation 0.
5787 processDown(mapper, toRawX(50), toRawY(75));
5788 processSync(mapper);
5789
5790 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5791 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5792 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5793
5794 processUp(mapper);
5795 processSync(mapper);
5796 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5797}
5798
5799TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5800 addConfigurationProperty("touch.deviceType", "touchScreen");
5801 prepareButtons();
5802 prepareAxes(POSITION);
5803 addConfigurationProperty("touch.orientationAware", "1");
5804 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5805 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005806 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005807 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005808 NotifyMotionArgs args;
5809
5810 // Orientation 90.
5811 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5812 processSync(mapper);
5813
5814 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5815 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5816 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5817
5818 processUp(mapper);
5819 processSync(mapper);
5820 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5821}
5822
5823TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5824 addConfigurationProperty("touch.deviceType", "touchScreen");
5825 prepareButtons();
5826 prepareAxes(POSITION);
5827 addConfigurationProperty("touch.orientationAware", "1");
5828 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5829 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005830 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005831 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005832 NotifyMotionArgs args;
5833
5834 // Orientation 180.
5835 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5836 processSync(mapper);
5837
5838 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5839 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5840 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5841
5842 processUp(mapper);
5843 processSync(mapper);
5844 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5845}
5846
5847TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5848 addConfigurationProperty("touch.deviceType", "touchScreen");
5849 prepareButtons();
5850 prepareAxes(POSITION);
5851 addConfigurationProperty("touch.orientationAware", "1");
5852 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5853 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005854 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005855 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005856 NotifyMotionArgs args;
5857
5858 // Orientation 270.
5859 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5860 processSync(mapper);
5861
5862 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5863 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5864 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5865
5866 processUp(mapper);
5867 processSync(mapper);
5868 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5869}
5870
5871TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5872 addConfigurationProperty("touch.deviceType", "touchScreen");
5873 prepareButtons();
5874 prepareAxes(POSITION);
5875 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5876 // orientation-aware are affected by display rotation.
5877 addConfigurationProperty("touch.orientationAware", "0");
5878 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00005879 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005880
5881 NotifyMotionArgs args;
5882
5883 // Orientation 90, Rotation 0.
5884 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005885 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005886 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5887 processSync(mapper);
5888
5889 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5890 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5891 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5892
5893 processUp(mapper);
5894 processSync(mapper);
5895 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5896
5897 // Orientation 90, Rotation 90.
5898 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005899 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005900 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005901 processSync(mapper);
5902
5903 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5904 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5905 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5906
5907 processUp(mapper);
5908 processSync(mapper);
5909 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5910
5911 // Orientation 90, Rotation 180.
5912 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005913 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005914 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
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 // Orientation 90, Rotation 270.
5926 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005927 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005928 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 -07005929 processSync(mapper);
5930
5931 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5932 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5933 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5934
5935 processUp(mapper);
5936 processSync(mapper);
5937 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5938}
5939
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005940TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5941 addConfigurationProperty("touch.deviceType", "touchScreen");
5942 prepareButtons();
5943 prepareAxes(POSITION);
5944 addConfigurationProperty("touch.orientationAware", "1");
5945 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005946 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005947
5948 // Set a physical frame in the display viewport.
5949 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5950 viewport->physicalLeft = 20;
5951 viewport->physicalTop = 600;
5952 viewport->physicalRight = 30;
5953 viewport->physicalBottom = 610;
5954 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005955 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005956
5957 // Start the touch.
5958 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5959 processSync(mapper);
5960
5961 // Expect all input starting outside the physical frame to be ignored.
5962 const std::array<Point, 6> outsidePoints = {
5963 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5964 for (const auto& p : outsidePoints) {
5965 processMove(mapper, toRawX(p.x), toRawY(p.y));
5966 processSync(mapper);
5967 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5968 }
5969
5970 // Move the touch into the physical frame.
5971 processMove(mapper, toRawX(25), toRawY(605));
5972 processSync(mapper);
5973 NotifyMotionArgs args;
5974 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5975 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5976 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5977 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5978
5979 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5980 for (const auto& p : outsidePoints) {
5981 processMove(mapper, toRawX(p.x), toRawY(p.y));
5982 processSync(mapper);
5983 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5984 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5985 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5986 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5987 }
5988
5989 processUp(mapper);
5990 processSync(mapper);
5991 EXPECT_NO_FATAL_FAILURE(
5992 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5993}
5994
Harry Cutts1db43992023-06-19 17:05:07 +00005995TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
5996 std::shared_ptr<FakePointerController> fakePointerController =
5997 std::make_shared<FakePointerController>();
5998 mFakePolicy->setPointerController(fakePointerController);
5999
6000 addConfigurationProperty("touch.deviceType", "pointer");
6001 prepareAxes(POSITION);
6002 prepareDisplay(ui::ROTATION_0);
6003 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
6004
6005 // Set a physical frame in the display viewport.
6006 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6007 viewport->physicalLeft = 20;
6008 viewport->physicalTop = 600;
6009 viewport->physicalRight = 30;
6010 viewport->physicalBottom = 610;
6011 mFakePolicy->updateViewport(*viewport);
6012 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
6013
6014 // Start the touch.
6015 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6016 processSync(mapper);
6017
6018 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
6019 // produced.
6020 const std::array<Point, 6> outsidePoints = {
6021 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6022 for (const auto& p : outsidePoints) {
6023 processMove(mapper, toRawX(p.x), toRawY(p.y));
6024 processSync(mapper);
6025 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6026 }
6027}
6028
Michael Wrightd02c5b62014-02-10 15:10:22 -08006029TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006030 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006031 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006032 prepareButtons();
6033 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00006034 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006035
6036 // These calculations are based on the input device calibration documentation.
6037 int32_t rawX = 100;
6038 int32_t rawY = 200;
6039 int32_t rawPressure = 10;
6040 int32_t rawToolMajor = 12;
6041 int32_t rawDistance = 2;
6042 int32_t rawTiltX = 30;
6043 int32_t rawTiltY = 110;
6044
6045 float x = toDisplayX(rawX);
6046 float y = toDisplayY(rawY);
6047 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6048 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6049 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6050 float distance = float(rawDistance);
6051
6052 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6053 float tiltScale = M_PI / 180;
6054 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6055 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6056 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6057 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6058
6059 processDown(mapper, rawX, rawY);
6060 processPressure(mapper, rawPressure);
6061 processToolMajor(mapper, rawToolMajor);
6062 processDistance(mapper, rawDistance);
6063 processTilt(mapper, rawTiltX, rawTiltY);
6064 processSync(mapper);
6065
6066 NotifyMotionArgs args;
6067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6068 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6069 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6070 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6071}
6072
Jason Gerecke489fda82012-09-07 17:19:40 -07006073TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006074 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006075 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07006076 prepareLocationCalibration();
6077 prepareButtons();
6078 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006079 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006080
6081 int32_t rawX = 100;
6082 int32_t rawY = 200;
6083
6084 float x = toDisplayX(toCookedX(rawX, rawY));
6085 float y = toDisplayY(toCookedY(rawX, rawY));
6086
6087 processDown(mapper, rawX, rawY);
6088 processSync(mapper);
6089
6090 NotifyMotionArgs args;
6091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6093 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6094}
6095
Michael Wrightd02c5b62014-02-10 15:10:22 -08006096TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006097 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006098 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006099 prepareButtons();
6100 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006101 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006102
6103 NotifyMotionArgs motionArgs;
6104 NotifyKeyArgs keyArgs;
6105
6106 processDown(mapper, 100, 200);
6107 processSync(mapper);
6108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6109 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6110 ASSERT_EQ(0, motionArgs.buttonState);
6111
6112 // press BTN_LEFT, release BTN_LEFT
6113 processKey(mapper, BTN_LEFT, 1);
6114 processSync(mapper);
6115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6117 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6118
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6120 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6121 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6122
Michael Wrightd02c5b62014-02-10 15:10:22 -08006123 processKey(mapper, BTN_LEFT, 0);
6124 processSync(mapper);
6125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006126 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006127 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006128
6129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006130 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006131 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006132
6133 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6134 processKey(mapper, BTN_RIGHT, 1);
6135 processKey(mapper, BTN_MIDDLE, 1);
6136 processSync(mapper);
6137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6138 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6139 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6140 motionArgs.buttonState);
6141
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6143 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6144 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6145
6146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6147 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6148 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6149 motionArgs.buttonState);
6150
Michael Wrightd02c5b62014-02-10 15:10:22 -08006151 processKey(mapper, BTN_RIGHT, 0);
6152 processSync(mapper);
6153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006154 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006155 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006156
6157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006159 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006160
6161 processKey(mapper, BTN_MIDDLE, 0);
6162 processSync(mapper);
6163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006164 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006166
6167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006169 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006170
6171 // press BTN_BACK, release BTN_BACK
6172 processKey(mapper, BTN_BACK, 1);
6173 processSync(mapper);
6174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6175 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6176 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006177
Michael Wrightd02c5b62014-02-10 15:10:22 -08006178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006180 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6181
6182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6184 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006185
6186 processKey(mapper, BTN_BACK, 0);
6187 processSync(mapper);
6188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006189 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006190 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006191
6192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006193 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006194 ASSERT_EQ(0, motionArgs.buttonState);
6195
Michael Wrightd02c5b62014-02-10 15:10:22 -08006196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6197 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6198 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6199
6200 // press BTN_SIDE, release BTN_SIDE
6201 processKey(mapper, BTN_SIDE, 1);
6202 processSync(mapper);
6203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6204 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6205 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006206
Michael Wrightd02c5b62014-02-10 15:10:22 -08006207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006209 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6210
6211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6212 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6213 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006214
6215 processKey(mapper, BTN_SIDE, 0);
6216 processSync(mapper);
6217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006218 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006219 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006220
6221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006223 ASSERT_EQ(0, motionArgs.buttonState);
6224
Michael Wrightd02c5b62014-02-10 15:10:22 -08006225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6226 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6227 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6228
6229 // press BTN_FORWARD, release BTN_FORWARD
6230 processKey(mapper, BTN_FORWARD, 1);
6231 processSync(mapper);
6232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6233 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6234 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006235
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006238 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6239
6240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6241 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6242 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006243
6244 processKey(mapper, BTN_FORWARD, 0);
6245 processSync(mapper);
6246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006247 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006248 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006249
6250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006252 ASSERT_EQ(0, motionArgs.buttonState);
6253
Michael Wrightd02c5b62014-02-10 15:10:22 -08006254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6255 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6256 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6257
6258 // press BTN_EXTRA, release BTN_EXTRA
6259 processKey(mapper, BTN_EXTRA, 1);
6260 processSync(mapper);
6261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6262 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6263 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006264
Michael Wrightd02c5b62014-02-10 15:10:22 -08006265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006266 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006267 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6268
6269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6270 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6271 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006272
6273 processKey(mapper, BTN_EXTRA, 0);
6274 processSync(mapper);
6275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006276 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006277 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006278
6279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006280 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006281 ASSERT_EQ(0, motionArgs.buttonState);
6282
Michael Wrightd02c5b62014-02-10 15:10:22 -08006283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6284 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6285 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6286
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6288
Michael Wrightd02c5b62014-02-10 15:10:22 -08006289 // press BTN_STYLUS, release BTN_STYLUS
6290 processKey(mapper, BTN_STYLUS, 1);
6291 processSync(mapper);
6292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006294 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6295
6296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6297 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6298 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006299
6300 processKey(mapper, BTN_STYLUS, 0);
6301 processSync(mapper);
6302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006303 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006305
6306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006307 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006308 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006309
6310 // press BTN_STYLUS2, release BTN_STYLUS2
6311 processKey(mapper, BTN_STYLUS2, 1);
6312 processSync(mapper);
6313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006315 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6316
6317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6318 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6319 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006320
6321 processKey(mapper, BTN_STYLUS2, 0);
6322 processSync(mapper);
6323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006324 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006325 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006326
6327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006329 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006330
6331 // release touch
6332 processUp(mapper);
6333 processSync(mapper);
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6335 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6336 ASSERT_EQ(0, motionArgs.buttonState);
6337}
6338
6339TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006340 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006341 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006342 prepareButtons();
6343 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006344 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006345
6346 NotifyMotionArgs motionArgs;
6347
6348 // default tool type is finger
6349 processDown(mapper, 100, 200);
6350 processSync(mapper);
6351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6352 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006353 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006354
6355 // eraser
6356 processKey(mapper, BTN_TOOL_RUBBER, 1);
6357 processSync(mapper);
6358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006360 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006361
6362 // stylus
6363 processKey(mapper, BTN_TOOL_RUBBER, 0);
6364 processKey(mapper, BTN_TOOL_PEN, 1);
6365 processSync(mapper);
6366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006368 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006369
6370 // brush
6371 processKey(mapper, BTN_TOOL_PEN, 0);
6372 processKey(mapper, BTN_TOOL_BRUSH, 1);
6373 processSync(mapper);
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006376 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006377
6378 // pencil
6379 processKey(mapper, BTN_TOOL_BRUSH, 0);
6380 processKey(mapper, BTN_TOOL_PENCIL, 1);
6381 processSync(mapper);
6382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006384 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006385
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006386 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006387 processKey(mapper, BTN_TOOL_PENCIL, 0);
6388 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6389 processSync(mapper);
6390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6391 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006392 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006393
6394 // mouse
6395 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6396 processKey(mapper, BTN_TOOL_MOUSE, 1);
6397 processSync(mapper);
6398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006400 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006401
6402 // lens
6403 processKey(mapper, BTN_TOOL_MOUSE, 0);
6404 processKey(mapper, BTN_TOOL_LENS, 1);
6405 processSync(mapper);
6406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6407 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006408 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006409
6410 // double-tap
6411 processKey(mapper, BTN_TOOL_LENS, 0);
6412 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6413 processSync(mapper);
6414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6415 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006416 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006417
6418 // triple-tap
6419 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6420 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6421 processSync(mapper);
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6423 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006424 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006425
6426 // quad-tap
6427 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6428 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6429 processSync(mapper);
6430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006432 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006433
6434 // finger
6435 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6436 processKey(mapper, BTN_TOOL_FINGER, 1);
6437 processSync(mapper);
6438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006440 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006441
6442 // stylus trumps finger
6443 processKey(mapper, BTN_TOOL_PEN, 1);
6444 processSync(mapper);
6445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6446 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006447 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006448
6449 // eraser trumps stylus
6450 processKey(mapper, BTN_TOOL_RUBBER, 1);
6451 processSync(mapper);
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006454 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006455
6456 // mouse trumps eraser
6457 processKey(mapper, BTN_TOOL_MOUSE, 1);
6458 processSync(mapper);
6459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006461 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006462
6463 // back to default tool type
6464 processKey(mapper, BTN_TOOL_MOUSE, 0);
6465 processKey(mapper, BTN_TOOL_RUBBER, 0);
6466 processKey(mapper, BTN_TOOL_PEN, 0);
6467 processKey(mapper, BTN_TOOL_FINGER, 0);
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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006472}
6473
6474TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006475 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006476 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006477 prepareButtons();
6478 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006479 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006480 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006481
6482 NotifyMotionArgs motionArgs;
6483
6484 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6485 processKey(mapper, BTN_TOOL_FINGER, 1);
6486 processMove(mapper, 100, 200);
6487 processSync(mapper);
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6489 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6491 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6492
6493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6494 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6496 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6497
6498 // move a little
6499 processMove(mapper, 150, 250);
6500 processSync(mapper);
6501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6502 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6504 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6505
6506 // down when BTN_TOUCH is pressed, pressure defaults to 1
6507 processKey(mapper, BTN_TOUCH, 1);
6508 processSync(mapper);
6509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6510 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6512 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6513
6514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6515 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6517 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6518
6519 // up when BTN_TOUCH is released, hover restored
6520 processKey(mapper, BTN_TOUCH, 0);
6521 processSync(mapper);
6522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6523 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6525 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6526
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6528 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6530 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6531
6532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6533 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6535 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6536
6537 // exit hover when pointer goes away
6538 processKey(mapper, BTN_TOOL_FINGER, 0);
6539 processSync(mapper);
6540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6541 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6543 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6544}
6545
6546TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006547 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006548 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006549 prepareButtons();
6550 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006551 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006552
6553 NotifyMotionArgs motionArgs;
6554
6555 // initially hovering because pressure is 0
6556 processDown(mapper, 100, 200);
6557 processPressure(mapper, 0);
6558 processSync(mapper);
6559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6560 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6562 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6563
6564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6565 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6567 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6568
6569 // move a little
6570 processMove(mapper, 150, 250);
6571 processSync(mapper);
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6573 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6575 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6576
6577 // down when pressure is non-zero
6578 processPressure(mapper, RAW_PRESSURE_MAX);
6579 processSync(mapper);
6580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6581 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6583 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6584
6585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6586 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6588 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6589
6590 // up when pressure becomes 0, hover restored
6591 processPressure(mapper, 0);
6592 processSync(mapper);
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6594 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6596 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6597
6598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6599 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6601 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6602
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6604 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6606 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6607
6608 // exit hover when pointer goes away
6609 processUp(mapper);
6610 processSync(mapper);
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6612 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6614 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6615}
6616
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006617TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6618 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006619 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006620 prepareButtons();
6621 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006622 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006623
6624 // Touch down.
6625 processDown(mapper, 100, 200);
6626 processPressure(mapper, 1);
6627 processSync(mapper);
6628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6629 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6630
6631 // Reset the mapper. This should cancel the ongoing gesture.
6632 resetMapper(mapper, ARBITRARY_TIME);
6633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6634 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6635
6636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6637}
6638
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006639TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6640 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006641 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006642 prepareButtons();
6643 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006644 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006645
6646 // Set the initial state for the touch pointer.
6647 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6648 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6649 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6650 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6651
6652 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006653 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6654 // does not generate any events.
6655 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006656
6657 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6658 // the recreated touch state to generate a down event.
6659 processSync(mapper);
6660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6661 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6662
6663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6664}
6665
lilinnan687e58f2022-07-19 16:00:50 +08006666TEST_F(SingleTouchInputMapperTest,
6667 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6668 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006669 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006670 prepareButtons();
6671 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006672 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006673 NotifyMotionArgs motionArgs;
6674
6675 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006676 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006677 processSync(mapper);
6678
6679 // We should receive a down event
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6681 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6682
6683 // Change display id
6684 clearViewports();
6685 prepareSecondaryDisplay(ViewportType::INTERNAL);
6686
6687 // We should receive a cancel event
6688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6689 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6690 // Then receive reset called
6691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6692}
6693
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006694TEST_F(SingleTouchInputMapperTest,
6695 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6696 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006697 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006698 prepareButtons();
6699 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006700 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6702 NotifyMotionArgs motionArgs;
6703
6704 // Start a new gesture.
6705 processDown(mapper, 100, 200);
6706 processSync(mapper);
6707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6708 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6709
6710 // Make the viewport inactive. This will put the device in disabled mode.
6711 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6712 viewport->isActive = false;
6713 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006714 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006715
6716 // We should receive a cancel event for the ongoing gesture.
6717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6718 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6719 // Then we should be notified that the device was reset.
6720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6721
6722 // No events are generated while the viewport is inactive.
6723 processMove(mapper, 101, 201);
6724 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006725 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006726 processSync(mapper);
6727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6728
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006729 // Start a new gesture while the viewport is still inactive.
6730 processDown(mapper, 300, 400);
6731 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6732 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6733 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6734 processSync(mapper);
6735
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006736 // Make the viewport active again. The device should resume processing events.
6737 viewport->isActive = true;
6738 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006739 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006740
6741 // The device is reset because it changes back to direct mode, without generating any events.
6742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6744
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006745 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006746 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6748 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006749
6750 // No more events.
6751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6753}
6754
Prabir Pradhan211ba622022-10-31 21:09:21 +00006755TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6756 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006757 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006758 prepareButtons();
6759 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006760 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6762
6763 // Press a stylus button.
6764 processKey(mapper, BTN_STYLUS, 1);
6765 processSync(mapper);
6766
6767 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6768 processDown(mapper, 100, 200);
6769 processSync(mapper);
6770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6771 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6772 WithCoords(toDisplayX(100), toDisplayY(200)),
6773 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6775 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6776 WithCoords(toDisplayX(100), toDisplayY(200)),
6777 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6778
6779 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6780 // the button has not actually been released, since there will be no pointers through which the
6781 // button state can be reported. The event is generated at the location of the pointer before
6782 // it went up.
6783 processUp(mapper);
6784 processSync(mapper);
6785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6786 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6787 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6789 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6790 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6791}
6792
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006793TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6794 addConfigurationProperty("touch.deviceType", "touchScreen");
6795 prepareDisplay(ui::ROTATION_0);
6796 prepareButtons();
6797 prepareAxes(POSITION);
6798
6799 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6800
Arpit Singha8c236b2023-04-25 13:56:05 +00006801 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6803
6804 // Press a stylus button.
6805 processKey(mapper, BTN_STYLUS, 1);
6806 processSync(mapper);
6807
6808 // Start a touch gesture and ensure that the stylus button is not reported.
6809 processDown(mapper, 100, 200);
6810 processSync(mapper);
6811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6812 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6813
6814 // Release and press the stylus button again.
6815 processKey(mapper, BTN_STYLUS, 0);
6816 processSync(mapper);
6817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6818 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6819 processKey(mapper, BTN_STYLUS, 1);
6820 processSync(mapper);
6821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6822 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6823
6824 // Release the touch gesture.
6825 processUp(mapper);
6826 processSync(mapper);
6827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6828 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6829
6830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6831}
6832
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006833TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6834 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6835 prepareDisplay(ui::ROTATION_0);
6836 prepareButtons();
6837 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006838 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6840
6841 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6842}
6843
Seunghwan Choi356026c2023-02-01 14:37:25 +09006844TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6845 std::shared_ptr<FakePointerController> fakePointerController =
6846 std::make_shared<FakePointerController>();
6847 addConfigurationProperty("touch.deviceType", "touchScreen");
6848 prepareDisplay(ui::ROTATION_0);
6849 prepareButtons();
6850 prepareAxes(POSITION);
6851 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6852 mFakePolicy->setPointerController(fakePointerController);
6853 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00006854 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006855
6856 processKey(mapper, BTN_TOOL_PEN, 1);
6857 processMove(mapper, 100, 200);
6858 processSync(mapper);
6859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6860 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006861 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006862 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6863 ASSERT_TRUE(fakePointerController->isPointerShown());
6864 ASSERT_NO_FATAL_FAILURE(
6865 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6866}
6867
6868TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6869 std::shared_ptr<FakePointerController> fakePointerController =
6870 std::make_shared<FakePointerController>();
6871 addConfigurationProperty("touch.deviceType", "touchScreen");
6872 prepareDisplay(ui::ROTATION_0);
6873 prepareButtons();
6874 prepareAxes(POSITION);
6875 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6876 mFakePolicy->setPointerController(fakePointerController);
6877 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00006878 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006879
6880 processKey(mapper, BTN_TOOL_PEN, 1);
6881 processMove(mapper, 100, 200);
6882 processSync(mapper);
6883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6884 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006885 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006886 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6887 ASSERT_FALSE(fakePointerController->isPointerShown());
6888}
6889
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006890TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6891 // Initialize the device without setting device source to touch navigation.
6892 addConfigurationProperty("touch.deviceType", "touchScreen");
6893 prepareDisplay(ui::ROTATION_0);
6894 prepareButtons();
6895 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006896 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006897
6898 // Ensure that the device is created as a touchscreen, not touch navigation.
6899 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6900
6901 // Add device type association after the device was created.
6902 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6903
6904 // Send update to the mapper.
6905 std::list<NotifyArgs> unused2 =
6906 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006907 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006908
6909 // Check whether device type update was successful.
6910 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6911}
6912
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006913TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6914 // Initialize the device without setting device source to touch navigation.
6915 addConfigurationProperty("touch.deviceType", "touchScreen");
6916 prepareDisplay(ui::ROTATION_0);
6917 prepareButtons();
6918 prepareAxes(POSITION);
6919 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6920
6921 // Set a physical frame in the display viewport.
6922 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6923 viewport->physicalLeft = 0;
6924 viewport->physicalTop = 0;
6925 viewport->physicalRight = DISPLAY_WIDTH / 2;
6926 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6927 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006928 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006929
Arpit Singha8c236b2023-04-25 13:56:05 +00006930 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006931
6932 // Hovering inside the physical frame produces events.
6933 processKey(mapper, BTN_TOOL_PEN, 1);
6934 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6935 processSync(mapper);
6936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6937 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6939 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6940
6941 // Leaving the physical frame ends the hovering gesture.
6942 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6943 processSync(mapper);
6944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6945 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6946
6947 // Moving outside the physical frame does not produce events.
6948 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6949 processSync(mapper);
6950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6951
6952 // Re-entering the physical frame produces events.
6953 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6954 processSync(mapper);
6955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6956 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6958 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6959}
6960
Prabir Pradhan5632d622021-09-06 07:57:20 -07006961// --- TouchDisplayProjectionTest ---
6962
6963class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6964public:
6965 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6966 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6967 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006968 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6969 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6970 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006971 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006972 auto rotatedWidth = naturalDisplayWidth;
6973 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006974 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006975 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006976 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006977 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006978 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006979 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006980 inverseRotationFlags = ui::Transform::ROT_180;
6981 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006982 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006983 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006984 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006985 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006986 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006987 inverseRotationFlags = ui::Transform::ROT_0;
6988 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006989 }
6990
Prabir Pradhana9df3162022-12-05 23:57:27 +00006991 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006992 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6993
6994 std::optional<DisplayViewport> internalViewport =
6995 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6996 DisplayViewport& v = *internalViewport;
6997 v.displayId = DISPLAY_ID;
6998 v.orientation = orientation;
6999
7000 v.logicalLeft = 0;
7001 v.logicalTop = 0;
7002 v.logicalRight = 100;
7003 v.logicalBottom = 100;
7004
7005 v.physicalLeft = rotatedPhysicalDisplay.left;
7006 v.physicalTop = rotatedPhysicalDisplay.top;
7007 v.physicalRight = rotatedPhysicalDisplay.right;
7008 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7009
Prabir Pradhana9df3162022-12-05 23:57:27 +00007010 v.deviceWidth = rotatedWidth;
7011 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007012
7013 v.isActive = true;
7014 v.uniqueId = UNIQUE_ID;
7015 v.type = ViewportType::INTERNAL;
7016 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007017 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007018 }
7019
7020 void assertReceivedMove(const Point& point) {
7021 NotifyMotionArgs motionArgs;
7022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007024 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007025 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7026 1, 0, 0, 0, 0, 0, 0, 0));
7027 }
7028};
7029
7030TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7031 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007032 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007033
7034 prepareButtons();
7035 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007036 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007037
7038 NotifyMotionArgs motionArgs;
7039
7040 // Configure the DisplayViewport such that the logical display maps to a subsection of
7041 // the display panel called the physical display. Here, the physical display is bounded by the
7042 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7043 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7044 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7045 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7046
Michael Wrighta9cf4192022-12-01 23:46:39 +00007047 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007048 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7049
7050 // Touches outside the physical display should be ignored, and should not generate any
7051 // events. Ensure touches at the following points that lie outside of the physical display
7052 // area do not generate any events.
7053 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7054 processDown(mapper, toRawX(point.x), toRawY(point.y));
7055 processSync(mapper);
7056 processUp(mapper);
7057 processSync(mapper);
7058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7059 << "Unexpected event generated for touch outside physical display at point: "
7060 << point.x << ", " << point.y;
7061 }
7062 }
7063}
7064
7065TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7066 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007067 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007068
7069 prepareButtons();
7070 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007071 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007072
7073 NotifyMotionArgs motionArgs;
7074
7075 // Configure the DisplayViewport such that the logical display maps to a subsection of
7076 // the display panel called the physical display. Here, the physical display is bounded by the
7077 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7078 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7079
Michael Wrighta9cf4192022-12-01 23:46:39 +00007080 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007081 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7082
7083 // Touches that start outside the physical display should be ignored until it enters the
7084 // physical display bounds, at which point it should generate a down event. Start a touch at
7085 // the point (5, 100), which is outside the physical display bounds.
7086 static const Point kOutsidePoint{5, 100};
7087 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7088 processSync(mapper);
7089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7090
7091 // Move the touch into the physical display area. This should generate a pointer down.
7092 processMove(mapper, toRawX(11), toRawY(21));
7093 processSync(mapper);
7094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7095 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007096 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007097 ASSERT_NO_FATAL_FAILURE(
7098 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7099
7100 // Move the touch inside the physical display area. This should generate a pointer move.
7101 processMove(mapper, toRawX(69), toRawY(159));
7102 processSync(mapper);
7103 assertReceivedMove({69, 159});
7104
7105 // Move outside the physical display area. Since the pointer is already down, this should
7106 // now continue generating events.
7107 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7108 processSync(mapper);
7109 assertReceivedMove(kOutsidePoint);
7110
7111 // Release. This should generate a pointer up.
7112 processUp(mapper);
7113 processSync(mapper);
7114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7115 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7117 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7118
7119 // Ensure no more events were generated.
7120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7122 }
7123}
7124
Prabir Pradhana9df3162022-12-05 23:57:27 +00007125// --- TouchscreenPrecisionTests ---
7126
7127// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
7128// in various orientations and with different display rotations. We configure the touchscreen to
7129// have a higher resolution than that of the display by an integer scale factor in each axis so that
7130// we can enforce that coordinates match precisely as expected.
7131class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
7132 public ::testing::WithParamInterface<ui::Rotation> {
7133public:
7134 void SetUp() override {
7135 SingleTouchInputMapperTest::SetUp();
7136
7137 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7138 // four times the resolution of the display in the Y axis.
7139 prepareButtons();
7140 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007141 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7142 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007143 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007144 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7145 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007146 }
7147
7148 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7149 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7150 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7151 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7152
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007153 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7154 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7155
7156 static const int32_t PRECISION_RAW_X_FLAT = 16;
7157 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7158
7159 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7160 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7161
Prabir Pradhana9df3162022-12-05 23:57:27 +00007162 static const std::array<Point, 4> kRawCorners;
7163};
7164
7165const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7166 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7167 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7168 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7169 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7170}};
7171
7172// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7173// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7174// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7175TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7176 enum class Orientation {
7177 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7178 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7179 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7180 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7181 ftl_last = ORIENTATION_270,
7182 };
7183 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7184 Orientation::ORIENTATION_270;
7185 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7186 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7187 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7188 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7189 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7190 };
7191
7192 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7193
7194 // Configure the touchscreen as being installed in the one of the four different orientations
7195 // relative to the display.
7196 addConfigurationProperty("touch.deviceType", "touchScreen");
7197 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7198 prepareDisplay(ui::ROTATION_0);
7199
Arpit Singha8c236b2023-04-25 13:56:05 +00007200 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00007201
7202 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7203 // orientations of either 90 or 270) this means the display's natural resolution will be
7204 // flipped.
7205 const bool displayRotated =
7206 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7207 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7208 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7209 const Rect physicalFrame{0, 0, width, height};
7210 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7211
7212 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7213 const float expectedPrecisionX = displayRotated ? 4 : 2;
7214 const float expectedPrecisionY = displayRotated ? 2 : 4;
7215
7216 // Test all four corners.
7217 for (int i = 0; i < 4; i++) {
7218 const auto& raw = kRawCorners[i];
7219 processDown(mapper, raw.x, raw.y);
7220 processSync(mapper);
7221 const auto& expected = expectedPoints[i];
7222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7223 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7224 WithCoords(expected.x, expected.y),
7225 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7226 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7227 << "with touchscreen orientation "
7228 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7229 << expected.x << ", " << expected.y << ").";
7230 processUp(mapper);
7231 processSync(mapper);
7232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7233 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7234 WithCoords(expected.x, expected.y))));
7235 }
7236}
7237
Prabir Pradhan82687402022-12-06 01:32:53 +00007238TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7239 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7240 kMappedCorners = {
7241 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7242 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7243 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7244 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7245 };
7246
7247 const ui::Rotation displayRotation = GetParam();
7248
7249 addConfigurationProperty("touch.deviceType", "touchScreen");
7250 prepareDisplay(displayRotation);
7251
Arpit Singha8c236b2023-04-25 13:56:05 +00007252 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00007253
7254 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7255
7256 // Test all four corners.
7257 for (int i = 0; i < 4; i++) {
7258 const auto& expected = expectedPoints[i];
7259 const auto& raw = kRawCorners[i];
7260 processDown(mapper, raw.x, raw.y);
7261 processSync(mapper);
7262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7263 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7264 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7265 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7266 << "with display rotation " << ui::toCString(displayRotation)
7267 << ", expected point (" << expected.x << ", " << expected.y << ").";
7268 processUp(mapper);
7269 processSync(mapper);
7270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7271 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7272 WithCoords(expected.x, expected.y))));
7273 }
7274}
7275
Prabir Pradhan3e798762022-12-02 21:02:11 +00007276TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7277 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7278 kMappedCorners = {
7279 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7280 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7281 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7282 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7283 };
7284
7285 const ui::Rotation displayRotation = GetParam();
7286
7287 addConfigurationProperty("touch.deviceType", "touchScreen");
7288 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7289
Arpit Singha8c236b2023-04-25 13:56:05 +00007290 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00007291
7292 // Ori 270, so width and height swapped
7293 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7294 prepareDisplay(displayRotation);
7295 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7296
7297 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7298
7299 // Test all four corners.
7300 for (int i = 0; i < 4; i++) {
7301 const auto& expected = expectedPoints[i];
7302 const auto& raw = kRawCorners[i];
7303 processDown(mapper, raw.x, raw.y);
7304 processSync(mapper);
7305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7306 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7307 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7308 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7309 << "with display rotation " << ui::toCString(displayRotation)
7310 << ", expected point (" << expected.x << ", " << expected.y << ").";
7311 processUp(mapper);
7312 processSync(mapper);
7313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7314 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7315 WithCoords(expected.x, expected.y))));
7316 }
7317}
7318
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007319TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7320 const ui::Rotation displayRotation = GetParam();
7321
7322 addConfigurationProperty("touch.deviceType", "touchScreen");
7323 prepareDisplay(displayRotation);
7324
7325 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00007326 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007327
7328 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7329 // MotionRanges use display pixels as their units
7330 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7331 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7332
7333 // The MotionRanges should be oriented in the rotated display's coordinate space
7334 const bool displayRotated =
7335 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7336
7337 constexpr float MAX_X = 479.5;
7338 constexpr float MAX_Y = 799.75;
7339 EXPECT_EQ(xRange->min, 0.f);
7340 EXPECT_EQ(yRange->min, 0.f);
7341 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7342 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7343
7344 EXPECT_EQ(xRange->flat, 8.f);
7345 EXPECT_EQ(yRange->flat, 8.f);
7346
7347 EXPECT_EQ(xRange->fuzz, 2.f);
7348 EXPECT_EQ(yRange->fuzz, 2.f);
7349
7350 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7351 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7352}
7353
Prabir Pradhana9df3162022-12-05 23:57:27 +00007354// Run the precision tests for all rotations.
7355INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7356 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7357 ui::ROTATION_270),
7358 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7359 return ftl::enum_string(testParamInfo.param);
7360 });
7361
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007362// --- ExternalStylusFusionTest ---
7363
7364class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7365public:
7366 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7367 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007368 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007369 prepareButtons();
7370 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007371 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007372
7373 mStylusState.when = ARBITRARY_TIME;
7374 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007375 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007376 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007377 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007378 processExternalStylusState(mapper);
7379 return mapper;
7380 }
7381
7382 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7383 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7384 for (const NotifyArgs& args : generatedArgs) {
7385 mFakeListener->notify(args);
7386 }
7387 // Loop the reader to flush the input listener queue.
7388 mReader->loopOnce();
7389 return generatedArgs;
7390 }
7391
7392protected:
7393 StylusState mStylusState{};
7394 static constexpr uint32_t EXPECTED_SOURCE =
7395 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7396
7397 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7398 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007399 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007400
7401 // The first pointer is withheld.
7402 processDown(mapper, 100, 200);
7403 processSync(mapper);
7404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7405 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7406 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7407
7408 // The external stylus reports pressure. The withheld finger pointer is released as a
7409 // stylus.
7410 mStylusState.pressure = 1.f;
7411 processExternalStylusState(mapper);
7412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7413 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7414 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7415
7416 // Subsequent pointer events are not withheld.
7417 processMove(mapper, 101, 201);
7418 processSync(mapper);
7419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7420 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7421
7422 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7424 }
7425
7426 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7427 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7428
7429 // Releasing the touch pointer ends the gesture.
7430 processUp(mapper);
7431 processSync(mapper);
7432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7433 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007434 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007435
7436 mStylusState.pressure = 0.f;
7437 processExternalStylusState(mapper);
7438 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7440 }
7441
7442 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7443 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007444 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007445
7446 // The first pointer is withheld when an external stylus is connected,
7447 // and a timeout is requested.
7448 processDown(mapper, 100, 200);
7449 processSync(mapper);
7450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7451 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7452 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7453
7454 // If the timeout expires early, it is requested again.
7455 handleTimeout(mapper, ARBITRARY_TIME + 1);
7456 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7457 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7458
7459 // When the timeout expires, the withheld touch is released as a finger pointer.
7460 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7462 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7463
7464 // Subsequent pointer events are not withheld.
7465 processMove(mapper, 101, 201);
7466 processSync(mapper);
7467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7468 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7469 processUp(mapper);
7470 processSync(mapper);
7471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7472 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7473
7474 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7476 }
7477
7478private:
7479 InputDeviceInfo mExternalStylusDeviceInfo{};
7480};
7481
7482TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7483 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7484 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7485}
7486
7487TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7488 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7489 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7490}
7491
7492TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7493 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7494 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7495}
7496
7497// Test a successful stylus fusion gesture where the pressure is reported by the external
7498// before the touch is reported by the touchscreen.
7499TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7500 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
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 external stylus reports pressure first. It is ignored for now.
7505 mStylusState.pressure = 1.f;
7506 processExternalStylusState(mapper);
7507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7508 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7509
7510 // When the touch goes down afterwards, it is reported as a stylus pointer.
7511 processDown(mapper, 100, 200);
7512 processSync(mapper);
7513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7514 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7515 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7516
7517 processMove(mapper, 101, 201);
7518 processSync(mapper);
7519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7520 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7521 processUp(mapper);
7522 processSync(mapper);
7523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7524 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7525
7526 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7528}
7529
7530TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7531 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7532
7533 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7534 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7535
7536 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7537 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7538 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7539 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7540}
7541
7542TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7543 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7544 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007545 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007546
7547 mStylusState.pressure = 0.8f;
7548 processExternalStylusState(mapper);
7549 processDown(mapper, 100, 200);
7550 processSync(mapper);
7551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7552 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7553 WithPressure(0.8f))));
7554 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7555
7556 // The external stylus reports a pressure change. We wait for some time for a touch event.
7557 mStylusState.pressure = 0.6f;
7558 processExternalStylusState(mapper);
7559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7560 ASSERT_NO_FATAL_FAILURE(
7561 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7562
7563 // If a touch is reported within the timeout, it reports the updated pressure.
7564 processMove(mapper, 101, 201);
7565 processSync(mapper);
7566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7567 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7568 WithPressure(0.6f))));
7569 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7570
7571 // There is another pressure change.
7572 mStylusState.pressure = 0.5f;
7573 processExternalStylusState(mapper);
7574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7575 ASSERT_NO_FATAL_FAILURE(
7576 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7577
7578 // If a touch is not reported within the timeout, a move event is generated to report
7579 // the new pressure.
7580 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7582 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7583 WithPressure(0.5f))));
7584
7585 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7586 // repeated indefinitely.
7587 mStylusState.pressure = 0.0f;
7588 processExternalStylusState(mapper);
7589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7590 ASSERT_NO_FATAL_FAILURE(
7591 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7592 processMove(mapper, 102, 202);
7593 processSync(mapper);
7594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7595 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7596 WithPressure(0.5f))));
7597 processMove(mapper, 103, 203);
7598 processSync(mapper);
7599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7600 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7601 WithPressure(0.5f))));
7602
7603 processUp(mapper);
7604 processSync(mapper);
7605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7606 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007607 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007608
7609 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7611}
7612
7613TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7614 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7615 auto source = WithSource(EXPECTED_SOURCE);
7616
7617 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007618 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007619 processExternalStylusState(mapper);
7620 processDown(mapper, 100, 200);
7621 processSync(mapper);
7622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7623 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007624 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007625 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7626
7627 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007628 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007629 processExternalStylusState(mapper);
7630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7631 ASSERT_NO_FATAL_FAILURE(
7632 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7633
7634 // If a touch is reported within the timeout, it reports the updated pressure.
7635 processMove(mapper, 101, 201);
7636 processSync(mapper);
7637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7638 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007639 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007640 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7641
7642 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007643 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007644 processExternalStylusState(mapper);
7645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7646 ASSERT_NO_FATAL_FAILURE(
7647 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7648
7649 // If a touch is not reported within the timeout, a move event is generated to report
7650 // the new tool type.
7651 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7653 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007654 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007655
7656 processUp(mapper);
7657 processSync(mapper);
7658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7659 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007660 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007661
7662 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7664}
7665
7666TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7667 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7668 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007669 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007670
7671 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7672
7673 // The external stylus reports a button change. We wait for some time for a touch event.
7674 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7675 processExternalStylusState(mapper);
7676 ASSERT_NO_FATAL_FAILURE(
7677 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7678
7679 // If a touch is reported within the timeout, it reports the updated button state.
7680 processMove(mapper, 101, 201);
7681 processSync(mapper);
7682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7683 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7684 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7686 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7687 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7688 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7689
7690 // The button is now released.
7691 mStylusState.buttons = 0;
7692 processExternalStylusState(mapper);
7693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7694 ASSERT_NO_FATAL_FAILURE(
7695 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7696
7697 // If a touch is not reported within the timeout, a move event is generated to report
7698 // the new button state.
7699 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7701 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7702 WithButtonState(0))));
7703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007704 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7705 WithButtonState(0))));
7706
7707 processUp(mapper);
7708 processSync(mapper);
7709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007710 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7711
7712 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7714}
7715
Michael Wrightd02c5b62014-02-10 15:10:22 -08007716// --- MultiTouchInputMapperTest ---
7717
7718class MultiTouchInputMapperTest : public TouchInputMapperTest {
7719protected:
7720 void prepareAxes(int axes);
7721
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007722 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7723 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7724 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7725 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7726 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7727 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7728 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7729 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7730 void processId(MultiTouchInputMapper& mapper, int32_t id);
7731 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7732 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7733 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007734 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007735 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007736 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7737 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007738};
7739
7740void MultiTouchInputMapperTest::prepareAxes(int axes) {
7741 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007742 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7743 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007744 }
7745 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007746 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7747 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007748 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007749 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7750 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007751 }
7752 }
7753 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007754 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7755 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007756 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007757 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007758 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007759 }
7760 }
7761 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007762 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7763 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007764 }
7765 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007766 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7767 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007768 }
7769 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007770 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7771 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007772 }
7773 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007774 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7775 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007776 }
7777 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007778 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7779 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007780 }
7781 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007782 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007783 }
7784}
7785
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007786void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7787 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7789 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007790}
7791
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007792void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7793 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007795}
7796
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007797void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7798 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007800}
7801
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007802void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007804}
7805
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007806void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007808}
7809
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007810void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7811 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007812 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007813}
7814
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007815void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007817}
7818
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007819void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007820 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007821}
7822
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007823void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007825}
7826
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007827void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007829}
7830
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007831void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007833}
7834
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007835void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7836 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007837 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007838}
7839
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007840void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7841 int32_t value) {
7842 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7843 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7844}
7845
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007846void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007847 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007848}
7849
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007850void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7851 nsecs_t readTime) {
7852 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007853}
7854
Michael Wrightd02c5b62014-02-10 15:10:22 -08007855TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007856 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007857 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007858 prepareAxes(POSITION);
7859 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007860 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007861
arthurhungdcef2dc2020-08-11 14:47:50 +08007862 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007863
7864 NotifyMotionArgs motionArgs;
7865
7866 // Two fingers down at once.
7867 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7868 processPosition(mapper, x1, y1);
7869 processMTSync(mapper);
7870 processPosition(mapper, x2, y2);
7871 processMTSync(mapper);
7872 processSync(mapper);
7873
7874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7875 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7876 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7877 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7878 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7879 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7880 ASSERT_EQ(0, motionArgs.flags);
7881 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7882 ASSERT_EQ(0, motionArgs.buttonState);
7883 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007884 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007885 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007886 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7888 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7889 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7890 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7891 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7892
7893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7894 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7895 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7896 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7897 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007898 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007899 ASSERT_EQ(0, motionArgs.flags);
7900 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7901 ASSERT_EQ(0, motionArgs.buttonState);
7902 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007903 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007904 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007905 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007906 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007907 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7909 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7911 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7912 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7913 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7914 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7915
7916 // Move.
7917 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7918 processPosition(mapper, x1, y1);
7919 processMTSync(mapper);
7920 processPosition(mapper, x2, y2);
7921 processMTSync(mapper);
7922 processSync(mapper);
7923
7924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7925 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7926 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7927 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7928 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7929 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7930 ASSERT_EQ(0, motionArgs.flags);
7931 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7932 ASSERT_EQ(0, motionArgs.buttonState);
7933 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007934 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007935 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007936 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007937 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007938 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007939 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7940 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7942 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7943 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7944 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7945 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7946
7947 // First finger up.
7948 x2 += 15; y2 -= 20;
7949 processPosition(mapper, x2, y2);
7950 processMTSync(mapper);
7951 processSync(mapper);
7952
7953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7954 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7955 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7956 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7957 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007958 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007959 ASSERT_EQ(0, motionArgs.flags);
7960 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7961 ASSERT_EQ(0, motionArgs.buttonState);
7962 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007963 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007964 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007965 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007966 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007967 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7969 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7970 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7971 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7972 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7973 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7974 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7975
7976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7977 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7978 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7979 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7980 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7982 ASSERT_EQ(0, motionArgs.flags);
7983 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7984 ASSERT_EQ(0, motionArgs.buttonState);
7985 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007986 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007987 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007988 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007989 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7990 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7991 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7992 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7993 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7994
7995 // Move.
7996 x2 += 20; y2 -= 25;
7997 processPosition(mapper, x2, y2);
7998 processMTSync(mapper);
7999 processSync(mapper);
8000
8001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8002 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8003 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8004 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8005 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8007 ASSERT_EQ(0, motionArgs.flags);
8008 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8009 ASSERT_EQ(0, motionArgs.buttonState);
8010 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008011 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008012 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008013 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8015 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8016 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8017 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8018 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8019
8020 // New finger down.
8021 int32_t x3 = 700, y3 = 300;
8022 processPosition(mapper, x2, y2);
8023 processMTSync(mapper);
8024 processPosition(mapper, x3, y3);
8025 processMTSync(mapper);
8026 processSync(mapper);
8027
8028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8029 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8030 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8031 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8032 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008033 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008034 ASSERT_EQ(0, motionArgs.flags);
8035 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8036 ASSERT_EQ(0, motionArgs.buttonState);
8037 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008038 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008039 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008040 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008041 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008042 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8044 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8046 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8047 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8048 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8049 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8050
8051 // Second finger up.
8052 x3 += 30; y3 -= 20;
8053 processPosition(mapper, x3, y3);
8054 processMTSync(mapper);
8055 processSync(mapper);
8056
8057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8058 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8059 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8060 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8061 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008062 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008063 ASSERT_EQ(0, motionArgs.flags);
8064 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8065 ASSERT_EQ(0, motionArgs.buttonState);
8066 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008067 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008068 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008069 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008070 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008071 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8073 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8074 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8075 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8076 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8077 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8078 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8079
8080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8081 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8082 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8083 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8084 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8085 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8086 ASSERT_EQ(0, motionArgs.flags);
8087 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8088 ASSERT_EQ(0, motionArgs.buttonState);
8089 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008090 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008091 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008092 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8094 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8095 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8096 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8097 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8098
8099 // Last finger up.
8100 processMTSync(mapper);
8101 processSync(mapper);
8102
8103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8104 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8105 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8106 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8107 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8108 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8109 ASSERT_EQ(0, motionArgs.flags);
8110 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8111 ASSERT_EQ(0, motionArgs.buttonState);
8112 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008113 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008114 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008115 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8117 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8118 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8119 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8120 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8121
8122 // Should not have sent any more keys or motions.
8123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8125}
8126
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008127TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8128 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008129 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008130
8131 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8132 /*fuzz*/ 0, /*resolution*/ 10);
8133 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8134 /*fuzz*/ 0, /*resolution*/ 11);
8135 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8136 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8137 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8138 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8139 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8140 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8141 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8142 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8143
Arpit Singha8c236b2023-04-25 13:56:05 +00008144 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008145
8146 // X and Y axes
8147 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8148 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8149 // Touch major and minor
8150 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8151 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8152 // Tool major and minor
8153 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8154 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8155}
8156
8157TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8158 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008159 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008160
8161 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8162 /*fuzz*/ 0, /*resolution*/ 10);
8163 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8164 /*fuzz*/ 0, /*resolution*/ 11);
8165
8166 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8167
Arpit Singha8c236b2023-04-25 13:56:05 +00008168 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008169
8170 // Touch major and minor
8171 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8172 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8173 // Tool major and minor
8174 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8175 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8176}
8177
Michael Wrightd02c5b62014-02-10 15:10:22 -08008178TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008179 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008180 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008181 prepareAxes(POSITION | ID);
8182 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008183 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008184
arthurhungdcef2dc2020-08-11 14:47:50 +08008185 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008186
8187 NotifyMotionArgs motionArgs;
8188
8189 // Two fingers down at once.
8190 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8191 processPosition(mapper, x1, y1);
8192 processId(mapper, 1);
8193 processMTSync(mapper);
8194 processPosition(mapper, x2, y2);
8195 processId(mapper, 2);
8196 processMTSync(mapper);
8197 processSync(mapper);
8198
8199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8200 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008201 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008202 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008203 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008204 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8205 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8206
8207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008208 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008209 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008210 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008211 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008212 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008213 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8215 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8216 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8217 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8218
8219 // Move.
8220 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8221 processPosition(mapper, x1, y1);
8222 processId(mapper, 1);
8223 processMTSync(mapper);
8224 processPosition(mapper, x2, y2);
8225 processId(mapper, 2);
8226 processMTSync(mapper);
8227 processSync(mapper);
8228
8229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8230 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008231 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008232 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008233 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008234 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008235 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8237 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8238 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8239 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8240
8241 // First finger up.
8242 x2 += 15; y2 -= 20;
8243 processPosition(mapper, x2, y2);
8244 processId(mapper, 2);
8245 processMTSync(mapper);
8246 processSync(mapper);
8247
8248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008249 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008250 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008251 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008252 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008253 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008254 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8256 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8258 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8259
8260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008262 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008263 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008264 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008265 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8266 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8267
8268 // Move.
8269 x2 += 20; y2 -= 25;
8270 processPosition(mapper, x2, y2);
8271 processId(mapper, 2);
8272 processMTSync(mapper);
8273 processSync(mapper);
8274
8275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008277 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008278 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008279 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8281 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8282
8283 // New finger down.
8284 int32_t x3 = 700, y3 = 300;
8285 processPosition(mapper, x2, y2);
8286 processId(mapper, 2);
8287 processMTSync(mapper);
8288 processPosition(mapper, x3, y3);
8289 processId(mapper, 3);
8290 processMTSync(mapper);
8291 processSync(mapper);
8292
8293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008294 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008295 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008296 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008297 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008298 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008299 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8301 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8302 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8303 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8304
8305 // Second finger up.
8306 x3 += 30; y3 -= 20;
8307 processPosition(mapper, x3, y3);
8308 processId(mapper, 3);
8309 processMTSync(mapper);
8310 processSync(mapper);
8311
8312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008313 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008314 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008315 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008316 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008317 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008318 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008319 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8320 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8321 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8322 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8323
8324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008326 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008327 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008328 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008329 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8330 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8331
8332 // Last finger up.
8333 processMTSync(mapper);
8334 processSync(mapper);
8335
8336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8337 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008338 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008339 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008340 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8342 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8343
8344 // Should not have sent any more keys or motions.
8345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8347}
8348
8349TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008350 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008351 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008352 prepareAxes(POSITION | ID | SLOT);
8353 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008354 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008355
arthurhungdcef2dc2020-08-11 14:47:50 +08008356 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008357
8358 NotifyMotionArgs motionArgs;
8359
8360 // Two fingers down at once.
8361 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8362 processPosition(mapper, x1, y1);
8363 processId(mapper, 1);
8364 processSlot(mapper, 1);
8365 processPosition(mapper, x2, y2);
8366 processId(mapper, 2);
8367 processSync(mapper);
8368
8369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8370 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008371 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008372 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008373 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8375 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8376
8377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008378 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008379 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008380 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008381 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008382 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008383 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8385 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8386 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8387 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8388
8389 // Move.
8390 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8391 processSlot(mapper, 0);
8392 processPosition(mapper, x1, y1);
8393 processSlot(mapper, 1);
8394 processPosition(mapper, x2, y2);
8395 processSync(mapper);
8396
8397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8398 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008399 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008400 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008401 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008402 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008403 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8405 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8407 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8408
8409 // First finger up.
8410 x2 += 15; y2 -= 20;
8411 processSlot(mapper, 0);
8412 processId(mapper, -1);
8413 processSlot(mapper, 1);
8414 processPosition(mapper, x2, y2);
8415 processSync(mapper);
8416
8417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008418 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008419 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008420 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008421 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008422 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008423 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8425 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8426 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8427 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8428
8429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8430 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008431 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008432 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008433 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8435 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8436
8437 // Move.
8438 x2 += 20; y2 -= 25;
8439 processPosition(mapper, x2, y2);
8440 processSync(mapper);
8441
8442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8443 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008444 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008445 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008446 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8448 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8449
8450 // New finger down.
8451 int32_t x3 = 700, y3 = 300;
8452 processPosition(mapper, x2, y2);
8453 processSlot(mapper, 0);
8454 processId(mapper, 3);
8455 processPosition(mapper, x3, y3);
8456 processSync(mapper);
8457
8458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008459 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008460 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008461 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008462 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008463 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008464 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8466 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8468 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8469
8470 // Second finger up.
8471 x3 += 30; y3 -= 20;
8472 processSlot(mapper, 1);
8473 processId(mapper, -1);
8474 processSlot(mapper, 0);
8475 processPosition(mapper, x3, y3);
8476 processSync(mapper);
8477
8478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008479 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008480 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008481 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008482 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008483 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008484 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8486 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8488 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8489
8490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8491 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008492 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008493 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008494 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8496 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8497
8498 // Last finger up.
8499 processId(mapper, -1);
8500 processSync(mapper);
8501
8502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8503 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008504 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008505 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008506 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8508 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8509
8510 // Should not have sent any more keys or motions.
8511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8513}
8514
8515TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008516 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008517 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008518 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008519 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008520
8521 // These calculations are based on the input device calibration documentation.
8522 int32_t rawX = 100;
8523 int32_t rawY = 200;
8524 int32_t rawTouchMajor = 7;
8525 int32_t rawTouchMinor = 6;
8526 int32_t rawToolMajor = 9;
8527 int32_t rawToolMinor = 8;
8528 int32_t rawPressure = 11;
8529 int32_t rawDistance = 0;
8530 int32_t rawOrientation = 3;
8531 int32_t id = 5;
8532
8533 float x = toDisplayX(rawX);
8534 float y = toDisplayY(rawY);
8535 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8536 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8537 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8538 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8539 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8540 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8541 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8542 float distance = float(rawDistance);
8543
8544 processPosition(mapper, rawX, rawY);
8545 processTouchMajor(mapper, rawTouchMajor);
8546 processTouchMinor(mapper, rawTouchMinor);
8547 processToolMajor(mapper, rawToolMajor);
8548 processToolMinor(mapper, rawToolMinor);
8549 processPressure(mapper, rawPressure);
8550 processOrientation(mapper, rawOrientation);
8551 processDistance(mapper, rawDistance);
8552 processId(mapper, id);
8553 processMTSync(mapper);
8554 processSync(mapper);
8555
8556 NotifyMotionArgs args;
8557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8558 ASSERT_EQ(0, args.pointerProperties[0].id);
8559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8560 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8561 orientation, distance));
8562}
8563
8564TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008565 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008566 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008567 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8568 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00008569 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008570
8571 // These calculations are based on the input device calibration documentation.
8572 int32_t rawX = 100;
8573 int32_t rawY = 200;
8574 int32_t rawTouchMajor = 140;
8575 int32_t rawTouchMinor = 120;
8576 int32_t rawToolMajor = 180;
8577 int32_t rawToolMinor = 160;
8578
8579 float x = toDisplayX(rawX);
8580 float y = toDisplayY(rawY);
8581 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8582 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8583 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8584 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8585 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8586
8587 processPosition(mapper, rawX, rawY);
8588 processTouchMajor(mapper, rawTouchMajor);
8589 processTouchMinor(mapper, rawTouchMinor);
8590 processToolMajor(mapper, rawToolMajor);
8591 processToolMinor(mapper, rawToolMinor);
8592 processMTSync(mapper);
8593 processSync(mapper);
8594
8595 NotifyMotionArgs args;
8596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8598 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8599}
8600
8601TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008602 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008603 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008604 prepareAxes(POSITION | TOUCH | TOOL);
8605 addConfigurationProperty("touch.size.calibration", "diameter");
8606 addConfigurationProperty("touch.size.scale", "10");
8607 addConfigurationProperty("touch.size.bias", "160");
8608 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00008609 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008610
8611 // These calculations are based on the input device calibration documentation.
8612 // Note: We only provide a single common touch/tool value because the device is assumed
8613 // not to emit separate values for each pointer (isSummed = 1).
8614 int32_t rawX = 100;
8615 int32_t rawY = 200;
8616 int32_t rawX2 = 150;
8617 int32_t rawY2 = 250;
8618 int32_t rawTouchMajor = 5;
8619 int32_t rawToolMajor = 8;
8620
8621 float x = toDisplayX(rawX);
8622 float y = toDisplayY(rawY);
8623 float x2 = toDisplayX(rawX2);
8624 float y2 = toDisplayY(rawY2);
8625 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8626 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8627 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8628
8629 processPosition(mapper, rawX, rawY);
8630 processTouchMajor(mapper, rawTouchMajor);
8631 processToolMajor(mapper, rawToolMajor);
8632 processMTSync(mapper);
8633 processPosition(mapper, rawX2, rawY2);
8634 processTouchMajor(mapper, rawTouchMajor);
8635 processToolMajor(mapper, rawToolMajor);
8636 processMTSync(mapper);
8637 processSync(mapper);
8638
8639 NotifyMotionArgs args;
8640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8641 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8642
8643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008644 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008645 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008646 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8647 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8649 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8650}
8651
8652TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008653 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008654 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008655 prepareAxes(POSITION | TOUCH | TOOL);
8656 addConfigurationProperty("touch.size.calibration", "area");
8657 addConfigurationProperty("touch.size.scale", "43");
8658 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00008659 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008660
8661 // These calculations are based on the input device calibration documentation.
8662 int32_t rawX = 100;
8663 int32_t rawY = 200;
8664 int32_t rawTouchMajor = 5;
8665 int32_t rawToolMajor = 8;
8666
8667 float x = toDisplayX(rawX);
8668 float y = toDisplayY(rawY);
8669 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8670 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8671 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8672
8673 processPosition(mapper, rawX, rawY);
8674 processTouchMajor(mapper, rawTouchMajor);
8675 processToolMajor(mapper, rawToolMajor);
8676 processMTSync(mapper);
8677 processSync(mapper);
8678
8679 NotifyMotionArgs args;
8680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8682 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8683}
8684
8685TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008686 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008687 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008688 prepareAxes(POSITION | PRESSURE);
8689 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8690 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00008691 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008692
Michael Wrightaa449c92017-12-13 21:21:43 +00008693 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008694 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008695 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8696 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8697 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8698
Michael Wrightd02c5b62014-02-10 15:10:22 -08008699 // These calculations are based on the input device calibration documentation.
8700 int32_t rawX = 100;
8701 int32_t rawY = 200;
8702 int32_t rawPressure = 60;
8703
8704 float x = toDisplayX(rawX);
8705 float y = toDisplayY(rawY);
8706 float pressure = float(rawPressure) * 0.01f;
8707
8708 processPosition(mapper, rawX, rawY);
8709 processPressure(mapper, rawPressure);
8710 processMTSync(mapper);
8711 processSync(mapper);
8712
8713 NotifyMotionArgs args;
8714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8715 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8716 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8717}
8718
8719TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008720 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008721 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008722 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008723 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008724
8725 NotifyMotionArgs motionArgs;
8726 NotifyKeyArgs keyArgs;
8727
8728 processId(mapper, 1);
8729 processPosition(mapper, 100, 200);
8730 processSync(mapper);
8731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8732 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8733 ASSERT_EQ(0, motionArgs.buttonState);
8734
8735 // press BTN_LEFT, release BTN_LEFT
8736 processKey(mapper, BTN_LEFT, 1);
8737 processSync(mapper);
8738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8739 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8740 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8741
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8743 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8744 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8745
Michael Wrightd02c5b62014-02-10 15:10:22 -08008746 processKey(mapper, BTN_LEFT, 0);
8747 processSync(mapper);
8748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008749 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008750 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008751
8752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008753 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008754 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008755
8756 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8757 processKey(mapper, BTN_RIGHT, 1);
8758 processKey(mapper, BTN_MIDDLE, 1);
8759 processSync(mapper);
8760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8761 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8762 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8763 motionArgs.buttonState);
8764
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8766 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8767 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8768
8769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8770 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8771 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8772 motionArgs.buttonState);
8773
Michael Wrightd02c5b62014-02-10 15:10:22 -08008774 processKey(mapper, BTN_RIGHT, 0);
8775 processSync(mapper);
8776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008777 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008778 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008779
8780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008781 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008782 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008783
8784 processKey(mapper, BTN_MIDDLE, 0);
8785 processSync(mapper);
8786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008787 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008788 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008789
8790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008792 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008793
8794 // press BTN_BACK, release BTN_BACK
8795 processKey(mapper, BTN_BACK, 1);
8796 processSync(mapper);
8797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8798 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8799 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008800
Michael Wrightd02c5b62014-02-10 15:10:22 -08008801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008802 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008803 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8804
8805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8806 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8807 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008808
8809 processKey(mapper, BTN_BACK, 0);
8810 processSync(mapper);
8811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008812 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008813 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008814
8815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008816 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008817 ASSERT_EQ(0, motionArgs.buttonState);
8818
Michael Wrightd02c5b62014-02-10 15:10:22 -08008819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8820 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8821 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8822
8823 // press BTN_SIDE, release BTN_SIDE
8824 processKey(mapper, BTN_SIDE, 1);
8825 processSync(mapper);
8826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8827 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8828 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008829
Michael Wrightd02c5b62014-02-10 15:10:22 -08008830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008832 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8833
8834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8835 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8836 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008837
8838 processKey(mapper, BTN_SIDE, 0);
8839 processSync(mapper);
8840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008841 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008842 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008843
8844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008845 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008846 ASSERT_EQ(0, motionArgs.buttonState);
8847
Michael Wrightd02c5b62014-02-10 15:10:22 -08008848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8849 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8850 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8851
8852 // press BTN_FORWARD, release BTN_FORWARD
8853 processKey(mapper, BTN_FORWARD, 1);
8854 processSync(mapper);
8855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8856 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8857 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008858
Michael Wrightd02c5b62014-02-10 15:10:22 -08008859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008860 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008861 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8862
8863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8864 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8865 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008866
8867 processKey(mapper, BTN_FORWARD, 0);
8868 processSync(mapper);
8869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008870 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008871 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008872
8873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008875 ASSERT_EQ(0, motionArgs.buttonState);
8876
Michael Wrightd02c5b62014-02-10 15:10:22 -08008877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8878 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8879 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8880
8881 // press BTN_EXTRA, release BTN_EXTRA
8882 processKey(mapper, BTN_EXTRA, 1);
8883 processSync(mapper);
8884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8885 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8886 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008887
Michael Wrightd02c5b62014-02-10 15:10:22 -08008888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008889 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008890 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8891
8892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8893 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8894 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008895
8896 processKey(mapper, BTN_EXTRA, 0);
8897 processSync(mapper);
8898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008899 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008900 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008901
8902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008903 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008904 ASSERT_EQ(0, motionArgs.buttonState);
8905
Michael Wrightd02c5b62014-02-10 15:10:22 -08008906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8907 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8908 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8909
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8911
Michael Wrightd02c5b62014-02-10 15:10:22 -08008912 // press BTN_STYLUS, release BTN_STYLUS
8913 processKey(mapper, BTN_STYLUS, 1);
8914 processSync(mapper);
8915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8916 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008917 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8918
8919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8920 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8921 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008922
8923 processKey(mapper, BTN_STYLUS, 0);
8924 processSync(mapper);
8925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008926 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008927 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008928
8929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008930 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008931 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008932
8933 // press BTN_STYLUS2, release BTN_STYLUS2
8934 processKey(mapper, BTN_STYLUS2, 1);
8935 processSync(mapper);
8936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008938 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8939
8940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8941 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8942 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008943
8944 processKey(mapper, BTN_STYLUS2, 0);
8945 processSync(mapper);
8946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008947 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008948 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008949
8950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008952 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008953
8954 // release touch
8955 processId(mapper, -1);
8956 processSync(mapper);
8957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8958 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8959 ASSERT_EQ(0, motionArgs.buttonState);
8960}
8961
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008962TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8963 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008964 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008965 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008966 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008967
8968 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8969 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8970
8971 // Touch down.
8972 processId(mapper, 1);
8973 processPosition(mapper, 100, 200);
8974 processSync(mapper);
8975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8976 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8977
8978 // Press and release button mapped to the primary stylus button.
8979 processKey(mapper, BTN_A, 1);
8980 processSync(mapper);
8981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8982 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8983 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8985 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8986 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8987
8988 processKey(mapper, BTN_A, 0);
8989 processSync(mapper);
8990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8991 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8993 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8994
8995 // Press and release the HID usage mapped to the secondary stylus button.
8996 processHidUsage(mapper, 0xabcd, 1);
8997 processSync(mapper);
8998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8999 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9000 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9002 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9003 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9004
9005 processHidUsage(mapper, 0xabcd, 0);
9006 processSync(mapper);
9007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9008 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9010 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9011
9012 // Release touch.
9013 processId(mapper, -1);
9014 processSync(mapper);
9015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9016 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
9017}
9018
Michael Wrightd02c5b62014-02-10 15:10:22 -08009019TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009020 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009021 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009022 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009023 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009024
9025 NotifyMotionArgs motionArgs;
9026
9027 // default tool type is finger
9028 processId(mapper, 1);
9029 processPosition(mapper, 100, 200);
9030 processSync(mapper);
9031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9032 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009033 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009034
9035 // eraser
9036 processKey(mapper, BTN_TOOL_RUBBER, 1);
9037 processSync(mapper);
9038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9039 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009040 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009041
9042 // stylus
9043 processKey(mapper, BTN_TOOL_RUBBER, 0);
9044 processKey(mapper, BTN_TOOL_PEN, 1);
9045 processSync(mapper);
9046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9047 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009048 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009049
9050 // brush
9051 processKey(mapper, BTN_TOOL_PEN, 0);
9052 processKey(mapper, BTN_TOOL_BRUSH, 1);
9053 processSync(mapper);
9054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9055 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009056 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009057
9058 // pencil
9059 processKey(mapper, BTN_TOOL_BRUSH, 0);
9060 processKey(mapper, BTN_TOOL_PENCIL, 1);
9061 processSync(mapper);
9062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9063 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009064 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009065
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009066 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009067 processKey(mapper, BTN_TOOL_PENCIL, 0);
9068 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
9069 processSync(mapper);
9070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009072 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009073
9074 // mouse
9075 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9076 processKey(mapper, BTN_TOOL_MOUSE, 1);
9077 processSync(mapper);
9078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9079 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009080 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009081
9082 // lens
9083 processKey(mapper, BTN_TOOL_MOUSE, 0);
9084 processKey(mapper, BTN_TOOL_LENS, 1);
9085 processSync(mapper);
9086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9087 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009088 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009089
9090 // double-tap
9091 processKey(mapper, BTN_TOOL_LENS, 0);
9092 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9093 processSync(mapper);
9094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9095 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009096 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009097
9098 // triple-tap
9099 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9100 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9101 processSync(mapper);
9102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9103 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009104 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009105
9106 // quad-tap
9107 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9108 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9109 processSync(mapper);
9110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009112 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009113
9114 // finger
9115 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9116 processKey(mapper, BTN_TOOL_FINGER, 1);
9117 processSync(mapper);
9118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9119 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009120 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009121
9122 // stylus trumps finger
9123 processKey(mapper, BTN_TOOL_PEN, 1);
9124 processSync(mapper);
9125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009127 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009128
9129 // eraser trumps stylus
9130 processKey(mapper, BTN_TOOL_RUBBER, 1);
9131 processSync(mapper);
9132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009134 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009135
9136 // mouse trumps eraser
9137 processKey(mapper, BTN_TOOL_MOUSE, 1);
9138 processSync(mapper);
9139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009141 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009142
9143 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9144 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9145 processSync(mapper);
9146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009148 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009149
9150 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9151 processToolType(mapper, MT_TOOL_PEN);
9152 processSync(mapper);
9153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9154 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009155 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009156
9157 // back to default tool type
9158 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9159 processKey(mapper, BTN_TOOL_MOUSE, 0);
9160 processKey(mapper, BTN_TOOL_RUBBER, 0);
9161 processKey(mapper, BTN_TOOL_PEN, 0);
9162 processKey(mapper, BTN_TOOL_FINGER, 0);
9163 processSync(mapper);
9164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009166 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009167}
9168
9169TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009170 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009171 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009172 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009173 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009174 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009175
9176 NotifyMotionArgs motionArgs;
9177
9178 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9179 processId(mapper, 1);
9180 processPosition(mapper, 100, 200);
9181 processSync(mapper);
9182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9183 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9185 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9186
9187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9188 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9189 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9190 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9191
9192 // move a little
9193 processPosition(mapper, 150, 250);
9194 processSync(mapper);
9195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9196 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9198 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9199
9200 // down when BTN_TOUCH is pressed, pressure defaults to 1
9201 processKey(mapper, BTN_TOUCH, 1);
9202 processSync(mapper);
9203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9204 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9206 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9207
9208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9209 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9211 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9212
9213 // up when BTN_TOUCH is released, hover restored
9214 processKey(mapper, BTN_TOUCH, 0);
9215 processSync(mapper);
9216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9217 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9219 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9220
9221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9222 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9224 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9225
9226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9227 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9229 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9230
9231 // exit hover when pointer goes away
9232 processId(mapper, -1);
9233 processSync(mapper);
9234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9235 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9237 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9238}
9239
9240TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009241 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009242 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009243 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009244 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009245
9246 NotifyMotionArgs motionArgs;
9247
9248 // initially hovering because pressure is 0
9249 processId(mapper, 1);
9250 processPosition(mapper, 100, 200);
9251 processPressure(mapper, 0);
9252 processSync(mapper);
9253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9254 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9256 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9257
9258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9259 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9261 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9262
9263 // move a little
9264 processPosition(mapper, 150, 250);
9265 processSync(mapper);
9266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9267 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9269 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9270
9271 // down when pressure becomes non-zero
9272 processPressure(mapper, RAW_PRESSURE_MAX);
9273 processSync(mapper);
9274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9275 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9277 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9278
9279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9280 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9282 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9283
9284 // up when pressure becomes 0, hover restored
9285 processPressure(mapper, 0);
9286 processSync(mapper);
9287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9288 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9290 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9291
9292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9293 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9295 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9296
9297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9298 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9300 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9301
9302 // exit hover when pointer goes away
9303 processId(mapper, -1);
9304 processSync(mapper);
9305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9306 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9308 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9309}
9310
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009311/**
9312 * Set the input device port <--> display port associations, and check that the
9313 * events are routed to the display that matches the display port.
9314 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9315 */
9316TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009317 const std::string usb2 = "USB2";
9318 const uint8_t hdmi1 = 0;
9319 const uint8_t hdmi2 = 1;
9320 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009321 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009322
9323 addConfigurationProperty("touch.deviceType", "touchScreen");
9324 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009325 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009326
9327 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9328 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9329
9330 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9331 // for this input device is specified, and the matching viewport is not present,
9332 // the input device should be disabled (at the mapper level).
9333
9334 // Add viewport for display 2 on hdmi2
9335 prepareSecondaryDisplay(type, hdmi2);
9336 // Send a touch event
9337 processPosition(mapper, 100, 100);
9338 processSync(mapper);
9339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9340
9341 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009342 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009343 // Send a touch event again
9344 processPosition(mapper, 100, 100);
9345 processSync(mapper);
9346
9347 NotifyMotionArgs args;
9348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9349 ASSERT_EQ(DISPLAY_ID, args.displayId);
9350}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009351
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009352TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9353 addConfigurationProperty("touch.deviceType", "touchScreen");
9354 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009355 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009356
9357 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9358
Michael Wrighta9cf4192022-12-01 23:46:39 +00009359 prepareDisplay(ui::ROTATION_0);
9360 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009361
9362 // Send a touch event
9363 processPosition(mapper, 100, 100);
9364 processSync(mapper);
9365
9366 NotifyMotionArgs args;
9367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9368 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9369}
9370
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009371TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009372 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009373 std::shared_ptr<FakePointerController> fakePointerController =
9374 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009375 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009376 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009377 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009378
Garfield Tan888a6a42020-01-09 11:39:16 -08009379 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009380 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009381
Michael Wrighta9cf4192022-12-01 23:46:39 +00009382 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009383 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009384 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009385
Josep del Río2d8c79a2023-01-23 19:33:50 +00009386 // Check source is mouse that would obtain the PointerController.
9387 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009388
9389 NotifyMotionArgs motionArgs;
9390 processPosition(mapper, 100, 100);
9391 processSync(mapper);
9392
9393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9394 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9395 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9396}
9397
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009398/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009399 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9400 */
9401TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9402 addConfigurationProperty("touch.deviceType", "touchScreen");
9403 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009404 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009405
Michael Wrighta9cf4192022-12-01 23:46:39 +00009406 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009407 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9408 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9409 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9410 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009411
9412 NotifyMotionArgs args;
9413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9414 ASSERT_EQ(26, args.readTime);
9415
Harry Cutts33476232023-01-30 19:57:29 +00009416 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9417 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9418 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009419
9420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9421 ASSERT_EQ(33, args.readTime);
9422}
9423
9424/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009425 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9426 * events should not be delivered to the listener.
9427 */
9428TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9429 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009430 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009431 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009432 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009433 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009434 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009435 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009436
9437 NotifyMotionArgs motionArgs;
9438 processPosition(mapper, 100, 100);
9439 processSync(mapper);
9440
9441 mFakeListener->assertNotifyMotionWasNotCalled();
9442}
9443
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009444/**
9445 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9446 * the touch mapper can process the events and the events can be delivered to the listener.
9447 */
9448TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9449 addConfigurationProperty("touch.deviceType", "touchScreen");
9450 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009451 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009452 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009453 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009454 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009455 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009456
9457 NotifyMotionArgs motionArgs;
9458 processPosition(mapper, 100, 100);
9459 processSync(mapper);
9460
9461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9462 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9463}
9464
Josh Thielene986aed2023-06-01 14:17:30 +00009465/**
9466 * When the viewport is deactivated (isActive transitions from true to false),
9467 * and touch.enableForInactiveViewport is false, touches prior to the transition
9468 * should be cancelled.
9469 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08009470TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9471 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009472 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009473 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009474 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009475 std::optional<DisplayViewport> optionalDisplayViewport =
9476 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9477 ASSERT_TRUE(optionalDisplayViewport.has_value());
9478 DisplayViewport displayViewport = *optionalDisplayViewport;
9479
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009480 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009481 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009482 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08009483
9484 // Finger down
9485 int32_t x = 100, y = 100;
9486 processPosition(mapper, x, y);
9487 processSync(mapper);
9488
9489 NotifyMotionArgs motionArgs;
9490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9491 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9492
9493 // Deactivate display viewport
9494 displayViewport.isActive = false;
9495 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009496 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009497
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009498 // The ongoing touch should be canceled immediately
9499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9500 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9501
9502 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009503 x += 10, y += 10;
9504 processPosition(mapper, x, y);
9505 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009507
9508 // Reactivate display viewport
9509 displayViewport.isActive = true;
9510 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009511 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009512
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009513 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009514 x += 10, y += 10;
9515 processPosition(mapper, x, y);
9516 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9518 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009519}
9520
Josh Thielene986aed2023-06-01 14:17:30 +00009521/**
9522 * When the viewport is deactivated (isActive transitions from true to false),
9523 * and touch.enableForInactiveViewport is true, touches prior to the transition
9524 * should not be cancelled.
9525 */
9526TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
9527 addConfigurationProperty("touch.deviceType", "touchScreen");
9528 addConfigurationProperty("touch.enableForInactiveViewport", "1");
9529 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
9530 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
9531 std::optional<DisplayViewport> optionalDisplayViewport =
9532 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9533 ASSERT_TRUE(optionalDisplayViewport.has_value());
9534 DisplayViewport displayViewport = *optionalDisplayViewport;
9535
9536 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9537 prepareAxes(POSITION);
9538 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
9539
9540 // Finger down
9541 int32_t x = 100, y = 100;
9542 processPosition(mapper, x, y);
9543 processSync(mapper);
9544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9545 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9546
9547 // Deactivate display viewport
9548 displayViewport.isActive = false;
9549 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9550 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9551
9552 // The ongoing touch should not be canceled
9553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9554
9555 // Finger move is not ignored
9556 x += 10, y += 10;
9557 processPosition(mapper, x, y);
9558 processSync(mapper);
9559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9560 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
9561
9562 // Reactivate display viewport
9563 displayViewport.isActive = true;
9564 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9565 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9566
9567 // Finger move continues and does not start new gesture
9568 x += 10, y += 10;
9569 processPosition(mapper, x, y);
9570 processSync(mapper);
9571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9572 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
9573}
9574
Arthur Hung7c645402019-01-25 17:45:42 +08009575TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9576 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009577 prepareAxes(POSITION | ID | SLOT);
9578 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009579 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009580
9581 // Create the second touch screen device, and enable multi fingers.
9582 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009583 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009584 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009585 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009586 std::shared_ptr<InputDevice> device2 =
9587 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009588 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009589
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009590 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009591 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009592 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009593 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009594 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009595 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009596 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009597 /*flat=*/0, /*fuzz=*/0);
9598 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009599 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9600 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009601
9602 // Setup the second touch screen device.
Arpit Singha8c236b2023-04-25 13:56:05 +00009603 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
9604 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
9605 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009606 std::list<NotifyArgs> unused =
9607 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009608 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009609 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009610
9611 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009612 std::shared_ptr<FakePointerController> fakePointerController =
9613 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009614 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009615
9616 // Setup policy for associated displays and show touches.
9617 const uint8_t hdmi1 = 0;
9618 const uint8_t hdmi2 = 1;
9619 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9620 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9621 mFakePolicy->setShowTouches(true);
9622
9623 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009624 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009625 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009626
9627 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009628 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009629 InputReaderConfiguration::Change::DISPLAY_INFO |
9630 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009631
9632 // Two fingers down at default display.
9633 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9634 processPosition(mapper, x1, y1);
9635 processId(mapper, 1);
9636 processSlot(mapper, 1);
9637 processPosition(mapper, x2, y2);
9638 processId(mapper, 2);
9639 processSync(mapper);
9640
9641 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9642 fakePointerController->getSpots().find(DISPLAY_ID);
9643 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9644 ASSERT_EQ(size_t(2), iter->second.size());
9645
9646 // Two fingers down at second display.
9647 processPosition(mapper2, x1, y1);
9648 processId(mapper2, 1);
9649 processSlot(mapper2, 1);
9650 processPosition(mapper2, x2, y2);
9651 processId(mapper2, 2);
9652 processSync(mapper2);
9653
9654 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9655 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9656 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009657
9658 // Disable the show touches configuration and ensure the spots are cleared.
9659 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009660 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009661 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009662
9663 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009664}
9665
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009666TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009667 prepareAxes(POSITION);
9668 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009669 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009670 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009671
9672 NotifyMotionArgs motionArgs;
9673 // Unrotated video frame
9674 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9675 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009676 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009677 processPosition(mapper, 100, 200);
9678 processSync(mapper);
9679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9680 ASSERT_EQ(frames, motionArgs.videoFrames);
9681
9682 // Subsequent touch events should not have any videoframes
9683 // This is implemented separately in FakeEventHub,
9684 // but that should match the behaviour of TouchVideoDevice.
9685 processPosition(mapper, 200, 200);
9686 processSync(mapper);
9687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9688 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9689}
9690
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009691TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009692 prepareAxes(POSITION);
9693 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009694 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009695 // Unrotated video frame
9696 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9697 NotifyMotionArgs motionArgs;
9698
9699 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009700 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009701 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9702 clearViewports();
9703 prepareDisplay(orientation);
9704 std::vector<TouchVideoFrame> frames{frame};
9705 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9706 processPosition(mapper, 100, 200);
9707 processSync(mapper);
9708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9709 ASSERT_EQ(frames, motionArgs.videoFrames);
9710 }
9711}
9712
9713TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9714 prepareAxes(POSITION);
9715 addConfigurationProperty("touch.deviceType", "touchScreen");
9716 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9717 // orientation-aware are affected by display rotation.
9718 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009719 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009720 // Unrotated video frame
9721 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9722 NotifyMotionArgs motionArgs;
9723
9724 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009725 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009726 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9727 clearViewports();
9728 prepareDisplay(orientation);
9729 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009730 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009731 processPosition(mapper, 100, 200);
9732 processSync(mapper);
9733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009734 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9735 // compared to the display. This is so that when the window transform (which contains the
9736 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9737 // window's coordinate space.
9738 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009739 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009740
9741 // Release finger.
9742 processSync(mapper);
9743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009744 }
9745}
9746
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009747TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009748 prepareAxes(POSITION);
9749 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009750 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009751 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9752 // so mix these.
9753 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9754 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9755 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9756 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9757 NotifyMotionArgs motionArgs;
9758
Michael Wrighta9cf4192022-12-01 23:46:39 +00009759 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009760 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009761 processPosition(mapper, 100, 200);
9762 processSync(mapper);
9763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009764 ASSERT_EQ(frames, motionArgs.videoFrames);
9765}
9766
9767TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9768 prepareAxes(POSITION);
9769 addConfigurationProperty("touch.deviceType", "touchScreen");
9770 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9771 // orientation-aware are affected by display rotation.
9772 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009773 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009774 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9775 // so mix these.
9776 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9777 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9778 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9779 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9780 NotifyMotionArgs motionArgs;
9781
Michael Wrighta9cf4192022-12-01 23:46:39 +00009782 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009783 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9784 processPosition(mapper, 100, 200);
9785 processSync(mapper);
9786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9787 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9788 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9789 // compared to the display. This is so that when the window transform (which contains the
9790 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9791 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009792 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009793 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009794 ASSERT_EQ(frames, motionArgs.videoFrames);
9795}
9796
Arthur Hung9da14732019-09-02 16:16:58 +08009797/**
9798 * If we had defined port associations, but the viewport is not ready, the touch device would be
9799 * expected to be disabled, and it should be enabled after the viewport has found.
9800 */
9801TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009802 constexpr uint8_t hdmi2 = 1;
9803 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009804 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009805
9806 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9807
9808 addConfigurationProperty("touch.deviceType", "touchScreen");
9809 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009810 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009811
9812 ASSERT_EQ(mDevice->isEnabled(), false);
9813
9814 // Add display on hdmi2, the device should be enabled and can receive touch event.
9815 prepareSecondaryDisplay(type, hdmi2);
9816 ASSERT_EQ(mDevice->isEnabled(), true);
9817
9818 // Send a touch event.
9819 processPosition(mapper, 100, 100);
9820 processSync(mapper);
9821
9822 NotifyMotionArgs args;
9823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9824 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9825}
9826
Arthur Hung421eb1c2020-01-16 00:09:42 +08009827TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009828 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009829 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009830 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009831 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009832
9833 NotifyMotionArgs motionArgs;
9834
9835 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9836 // finger down
9837 processId(mapper, 1);
9838 processPosition(mapper, x1, y1);
9839 processSync(mapper);
9840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9841 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009842 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009843
9844 // finger move
9845 processId(mapper, 1);
9846 processPosition(mapper, x2, y2);
9847 processSync(mapper);
9848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009850 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009851
9852 // finger up.
9853 processId(mapper, -1);
9854 processSync(mapper);
9855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9856 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009857 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009858
9859 // new finger down
9860 processId(mapper, 1);
9861 processPosition(mapper, x3, y3);
9862 processSync(mapper);
9863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9864 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009865 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009866}
9867
9868/**
arthurhungcc7f9802020-04-30 17:55:40 +08009869 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9870 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009871 */
arthurhungcc7f9802020-04-30 17:55:40 +08009872TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009873 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009874 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009875 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009876 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009877
9878 NotifyMotionArgs motionArgs;
9879
9880 // default tool type is finger
9881 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009882 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009883 processPosition(mapper, x1, y1);
9884 processSync(mapper);
9885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9886 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009887 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009888
9889 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9890 processToolType(mapper, MT_TOOL_PALM);
9891 processSync(mapper);
9892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9893 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9894
9895 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009896 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009897 processPosition(mapper, x2, y2);
9898 processSync(mapper);
9899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9900
9901 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009902 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009903 processSync(mapper);
9904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9905
9906 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009907 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009908 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009909 processPosition(mapper, x3, y3);
9910 processSync(mapper);
9911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9912 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009913 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009914}
9915
arthurhungbf89a482020-04-17 17:37:55 +08009916/**
arthurhungcc7f9802020-04-30 17:55:40 +08009917 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9918 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009919 */
arthurhungcc7f9802020-04-30 17:55:40 +08009920TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009921 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009922 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009923 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009924 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009925
9926 NotifyMotionArgs motionArgs;
9927
9928 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009929 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9930 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009931 processPosition(mapper, x1, y1);
9932 processSync(mapper);
9933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9934 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009935 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009936
9937 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009938 processSlot(mapper, SECOND_SLOT);
9939 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009940 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009941 processSync(mapper);
9942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009943 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009944 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009945
9946 // If the tool type of the first finger changes to MT_TOOL_PALM,
9947 // we expect to receive ACTION_POINTER_UP with cancel flag.
9948 processSlot(mapper, FIRST_SLOT);
9949 processId(mapper, FIRST_TRACKING_ID);
9950 processToolType(mapper, MT_TOOL_PALM);
9951 processSync(mapper);
9952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009953 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009954 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9955
9956 // The following MOVE events of second finger should be processed.
9957 processSlot(mapper, SECOND_SLOT);
9958 processId(mapper, SECOND_TRACKING_ID);
9959 processPosition(mapper, x2 + 1, y2 + 1);
9960 processSync(mapper);
9961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9962 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009963 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009964
9965 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9966 // it. Second finger receive move.
9967 processSlot(mapper, FIRST_SLOT);
9968 processId(mapper, INVALID_TRACKING_ID);
9969 processSync(mapper);
9970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9971 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009972 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009973
9974 // Second finger keeps moving.
9975 processSlot(mapper, SECOND_SLOT);
9976 processId(mapper, SECOND_TRACKING_ID);
9977 processPosition(mapper, x2 + 2, y2 + 2);
9978 processSync(mapper);
9979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9980 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009981 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009982
9983 // Second finger up.
9984 processId(mapper, INVALID_TRACKING_ID);
9985 processSync(mapper);
9986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9987 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9988 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9989}
9990
9991/**
9992 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9993 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9994 */
9995TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9996 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009997 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009998 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009999 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010000
10001 NotifyMotionArgs motionArgs;
10002
10003 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10004 // First finger down.
10005 processId(mapper, FIRST_TRACKING_ID);
10006 processPosition(mapper, x1, y1);
10007 processSync(mapper);
10008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10009 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010010 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010011
10012 // Second finger down.
10013 processSlot(mapper, SECOND_SLOT);
10014 processId(mapper, SECOND_TRACKING_ID);
10015 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +080010016 processSync(mapper);
10017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010018 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010019 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +080010020
arthurhungcc7f9802020-04-30 17:55:40 +080010021 // If the tool type of the first finger changes to MT_TOOL_PALM,
10022 // we expect to receive ACTION_POINTER_UP with cancel flag.
10023 processSlot(mapper, FIRST_SLOT);
10024 processId(mapper, FIRST_TRACKING_ID);
10025 processToolType(mapper, MT_TOOL_PALM);
10026 processSync(mapper);
10027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010028 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010029 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10030
10031 // Second finger keeps moving.
10032 processSlot(mapper, SECOND_SLOT);
10033 processId(mapper, SECOND_TRACKING_ID);
10034 processPosition(mapper, x2 + 1, y2 + 1);
10035 processSync(mapper);
10036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10038
10039 // second finger becomes palm, receive cancel due to only 1 finger is active.
10040 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010041 processToolType(mapper, MT_TOOL_PALM);
10042 processSync(mapper);
10043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10044 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10045
arthurhungcc7f9802020-04-30 17:55:40 +080010046 // third finger down.
10047 processSlot(mapper, THIRD_SLOT);
10048 processId(mapper, THIRD_TRACKING_ID);
10049 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +080010050 processPosition(mapper, x3, y3);
10051 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +080010052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10053 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010054 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010055 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010056
10057 // third finger move
10058 processId(mapper, THIRD_TRACKING_ID);
10059 processPosition(mapper, x3 + 1, y3 + 1);
10060 processSync(mapper);
10061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10063
10064 // first finger up, third finger receive move.
10065 processSlot(mapper, FIRST_SLOT);
10066 processId(mapper, INVALID_TRACKING_ID);
10067 processSync(mapper);
10068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10069 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010070 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010071
10072 // second finger up, third finger receive move.
10073 processSlot(mapper, SECOND_SLOT);
10074 processId(mapper, INVALID_TRACKING_ID);
10075 processSync(mapper);
10076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10077 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010078 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010079
10080 // third finger up.
10081 processSlot(mapper, THIRD_SLOT);
10082 processId(mapper, INVALID_TRACKING_ID);
10083 processSync(mapper);
10084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10085 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10086 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10087}
10088
10089/**
10090 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10091 * and the active finger could still be allowed to receive the events
10092 */
10093TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10094 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010095 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010096 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010097 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010098
10099 NotifyMotionArgs motionArgs;
10100
10101 // default tool type is finger
10102 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10103 processId(mapper, FIRST_TRACKING_ID);
10104 processPosition(mapper, x1, y1);
10105 processSync(mapper);
10106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10107 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010108 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010109
10110 // Second finger down.
10111 processSlot(mapper, SECOND_SLOT);
10112 processId(mapper, SECOND_TRACKING_ID);
10113 processPosition(mapper, x2, y2);
10114 processSync(mapper);
10115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010116 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010117 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010118
10119 // If the tool type of the second finger changes to MT_TOOL_PALM,
10120 // we expect to receive ACTION_POINTER_UP with cancel flag.
10121 processId(mapper, SECOND_TRACKING_ID);
10122 processToolType(mapper, MT_TOOL_PALM);
10123 processSync(mapper);
10124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010125 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010126 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10127
10128 // The following MOVE event should be processed.
10129 processSlot(mapper, FIRST_SLOT);
10130 processId(mapper, FIRST_TRACKING_ID);
10131 processPosition(mapper, x1 + 1, y1 + 1);
10132 processSync(mapper);
10133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010135 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010136
10137 // second finger up.
10138 processSlot(mapper, SECOND_SLOT);
10139 processId(mapper, INVALID_TRACKING_ID);
10140 processSync(mapper);
10141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10143
10144 // first finger keep moving
10145 processSlot(mapper, FIRST_SLOT);
10146 processId(mapper, FIRST_TRACKING_ID);
10147 processPosition(mapper, x1 + 2, y1 + 2);
10148 processSync(mapper);
10149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10151
10152 // first finger up.
10153 processId(mapper, INVALID_TRACKING_ID);
10154 processSync(mapper);
10155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10156 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10157 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010158}
10159
Arthur Hung9ad18942021-06-19 02:04:46 +000010160/**
10161 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10162 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10163 * cause slot be valid again.
10164 */
10165TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10166 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010167 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +000010168 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010169 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +000010170
10171 NotifyMotionArgs motionArgs;
10172
10173 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10174 // First finger down.
10175 processId(mapper, FIRST_TRACKING_ID);
10176 processPosition(mapper, x1, y1);
10177 processPressure(mapper, RAW_PRESSURE_MAX);
10178 processSync(mapper);
10179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10180 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010181 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010182
10183 // First finger move.
10184 processId(mapper, FIRST_TRACKING_ID);
10185 processPosition(mapper, x1 + 1, y1 + 1);
10186 processPressure(mapper, RAW_PRESSURE_MAX);
10187 processSync(mapper);
10188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10189 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010190 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010191
10192 // Second finger down.
10193 processSlot(mapper, SECOND_SLOT);
10194 processId(mapper, SECOND_TRACKING_ID);
10195 processPosition(mapper, x2, y2);
10196 processPressure(mapper, RAW_PRESSURE_MAX);
10197 processSync(mapper);
10198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010199 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010200 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010201
10202 // second finger up with some unexpected data.
10203 processSlot(mapper, SECOND_SLOT);
10204 processId(mapper, INVALID_TRACKING_ID);
10205 processPosition(mapper, x2, y2);
10206 processSync(mapper);
10207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010208 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010209 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010210
10211 // first finger up with some unexpected data.
10212 processSlot(mapper, FIRST_SLOT);
10213 processId(mapper, INVALID_TRACKING_ID);
10214 processPosition(mapper, x2, y2);
10215 processPressure(mapper, RAW_PRESSURE_MAX);
10216 processSync(mapper);
10217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10218 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010219 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010220}
10221
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010222TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10223 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010224 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010225 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010226 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010227
10228 // First finger down.
10229 processId(mapper, FIRST_TRACKING_ID);
10230 processPosition(mapper, 100, 200);
10231 processPressure(mapper, RAW_PRESSURE_MAX);
10232 processSync(mapper);
10233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10234 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10235
10236 // Second finger down.
10237 processSlot(mapper, SECOND_SLOT);
10238 processId(mapper, SECOND_TRACKING_ID);
10239 processPosition(mapper, 300, 400);
10240 processPressure(mapper, RAW_PRESSURE_MAX);
10241 processSync(mapper);
10242 ASSERT_NO_FATAL_FAILURE(
10243 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10244
10245 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010246 // preserved. Resetting should cancel the ongoing gesture.
10247 resetMapper(mapper, ARBITRARY_TIME);
10248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10249 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010250
10251 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10252 // the existing touch state to generate a down event.
10253 processPosition(mapper, 301, 302);
10254 processSync(mapper);
10255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10256 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10258 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10259
10260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10261}
10262
10263TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10264 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010265 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010266 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010267 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010268
10269 // First finger touches down and releases.
10270 processId(mapper, FIRST_TRACKING_ID);
10271 processPosition(mapper, 100, 200);
10272 processPressure(mapper, RAW_PRESSURE_MAX);
10273 processSync(mapper);
10274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10275 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10276 processId(mapper, INVALID_TRACKING_ID);
10277 processSync(mapper);
10278 ASSERT_NO_FATAL_FAILURE(
10279 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10280
10281 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10282 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010283 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10285
10286 // Send an empty sync frame. Since there are no pointers, no events are generated.
10287 processSync(mapper);
10288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10289}
10290
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010291TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010292 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010293 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010294 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010295 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010297
10298 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10299 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10300 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10301 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10302 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10303
10304 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010305 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010306 processId(mapper, FIRST_TRACKING_ID);
10307 processToolType(mapper, MT_TOOL_PEN);
10308 processPosition(mapper, 100, 200);
10309 processPressure(mapper, RAW_PRESSURE_MAX);
10310 processSync(mapper);
10311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10312 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10313 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010314 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010315
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010316 // Now that we know the device supports styluses, ensure that the device is re-configured with
10317 // the stylus source.
10318 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10319 {
10320 const auto& devices = mReader->getInputDevices();
10321 auto deviceInfo =
10322 std::find_if(devices.begin(), devices.end(),
10323 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10324 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10325 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10326 }
10327
10328 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10330
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010331 processId(mapper, INVALID_TRACKING_ID);
10332 processSync(mapper);
10333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10334 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10335 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010336 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010337}
10338
Seunghwan Choi356026c2023-02-01 14:37:25 +090010339TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10340 addConfigurationProperty("touch.deviceType", "touchScreen");
10341 prepareDisplay(ui::ROTATION_0);
10342 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10343 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10344 // indicate stylus presence dynamically.
10345 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10346 std::shared_ptr<FakePointerController> fakePointerController =
10347 std::make_shared<FakePointerController>();
10348 mFakePolicy->setPointerController(fakePointerController);
10349 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +000010350 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010351
10352 processId(mapper, FIRST_TRACKING_ID);
10353 processPressure(mapper, RAW_PRESSURE_MIN);
10354 processPosition(mapper, 100, 200);
10355 processToolType(mapper, MT_TOOL_PEN);
10356 processSync(mapper);
10357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10358 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010359 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010360 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10361 ASSERT_TRUE(fakePointerController->isPointerShown());
10362 ASSERT_NO_FATAL_FAILURE(
10363 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10364}
10365
10366TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10367 addConfigurationProperty("touch.deviceType", "touchScreen");
10368 prepareDisplay(ui::ROTATION_0);
10369 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10370 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10371 // indicate stylus presence dynamically.
10372 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10373 std::shared_ptr<FakePointerController> fakePointerController =
10374 std::make_shared<FakePointerController>();
10375 mFakePolicy->setPointerController(fakePointerController);
10376 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010377 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010378
10379 processId(mapper, FIRST_TRACKING_ID);
10380 processPressure(mapper, RAW_PRESSURE_MIN);
10381 processPosition(mapper, 100, 200);
10382 processToolType(mapper, MT_TOOL_PEN);
10383 processSync(mapper);
10384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10385 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010386 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010387 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10388 ASSERT_FALSE(fakePointerController->isPointerShown());
10389}
10390
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010391// --- MultiTouchInputMapperTest_ExternalDevice ---
10392
10393class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10394protected:
Chris Yea52ade12020-08-27 16:49:20 -070010395 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010396};
10397
10398/**
10399 * Expect fallback to internal viewport if device is external and external viewport is not present.
10400 */
10401TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10402 prepareAxes(POSITION);
10403 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010404 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010405 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010406
10407 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10408
10409 NotifyMotionArgs motionArgs;
10410
10411 // Expect the event to be sent to the internal viewport,
10412 // because an external viewport is not present.
10413 processPosition(mapper, 100, 100);
10414 processSync(mapper);
10415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10416 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10417
10418 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010419 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010420 processPosition(mapper, 100, 100);
10421 processSync(mapper);
10422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10423 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10424}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010425
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010426TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10427 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10428 std::shared_ptr<FakePointerController> fakePointerController =
10429 std::make_shared<FakePointerController>();
10430 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10431 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010432
10433 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010434 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010435 prepareAxes(POSITION | ID | SLOT);
10436 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10437 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10438 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010439 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010440 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010441
10442 // captured touchpad should be a touchpad source
10443 NotifyDeviceResetArgs resetArgs;
10444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10445 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10446
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010447 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010448
10449 const InputDeviceInfo::MotionRange* relRangeX =
10450 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10451 ASSERT_NE(relRangeX, nullptr);
10452 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10453 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10454 const InputDeviceInfo::MotionRange* relRangeY =
10455 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10456 ASSERT_NE(relRangeY, nullptr);
10457 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10458 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10459
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010460 // run captured pointer tests - note that this is unscaled, so input listener events should be
10461 // identical to what the hardware sends (accounting for any
10462 // calibration).
10463 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010464 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010465 processId(mapper, 1);
10466 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10467 processKey(mapper, BTN_TOUCH, 1);
10468 processSync(mapper);
10469
10470 // expect coord[0] to contain initial location of touch 0
10471 NotifyMotionArgs args;
10472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10473 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010474 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010475 ASSERT_EQ(0, args.pointerProperties[0].id);
10476 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10477 ASSERT_NO_FATAL_FAILURE(
10478 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10479
10480 // FINGER 1 DOWN
10481 processSlot(mapper, 1);
10482 processId(mapper, 2);
10483 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10484 processSync(mapper);
10485
10486 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010488 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010489 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010490 ASSERT_EQ(0, args.pointerProperties[0].id);
10491 ASSERT_EQ(1, args.pointerProperties[1].id);
10492 ASSERT_NO_FATAL_FAILURE(
10493 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10494 ASSERT_NO_FATAL_FAILURE(
10495 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10496
10497 // FINGER 1 MOVE
10498 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10499 processSync(mapper);
10500
10501 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10502 // from move
10503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10504 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10505 ASSERT_NO_FATAL_FAILURE(
10506 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10507 ASSERT_NO_FATAL_FAILURE(
10508 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10509
10510 // FINGER 0 MOVE
10511 processSlot(mapper, 0);
10512 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10513 processSync(mapper);
10514
10515 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10517 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10518 ASSERT_NO_FATAL_FAILURE(
10519 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10520 ASSERT_NO_FATAL_FAILURE(
10521 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10522
10523 // BUTTON DOWN
10524 processKey(mapper, BTN_LEFT, 1);
10525 processSync(mapper);
10526
10527 // touchinputmapper design sends a move before button press
10528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10529 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10531 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10532
10533 // BUTTON UP
10534 processKey(mapper, BTN_LEFT, 0);
10535 processSync(mapper);
10536
10537 // touchinputmapper design sends a move after button release
10538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10539 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10541 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10542
10543 // FINGER 0 UP
10544 processId(mapper, -1);
10545 processSync(mapper);
10546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10547 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10548
10549 // FINGER 1 MOVE
10550 processSlot(mapper, 1);
10551 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10552 processSync(mapper);
10553
10554 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10556 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010557 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010558 ASSERT_EQ(1, args.pointerProperties[0].id);
10559 ASSERT_NO_FATAL_FAILURE(
10560 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10561
10562 // FINGER 1 UP
10563 processId(mapper, -1);
10564 processKey(mapper, BTN_TOUCH, 0);
10565 processSync(mapper);
10566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10567 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10568
Josep del Río2d8c79a2023-01-23 19:33:50 +000010569 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010570 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010571 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010573 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010574}
10575
10576TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10577 std::shared_ptr<FakePointerController> fakePointerController =
10578 std::make_shared<FakePointerController>();
10579 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10580 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010581
10582 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010583 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010584 prepareAxes(POSITION | ID | SLOT);
10585 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10586 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010587 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010588 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010589 // run uncaptured pointer tests - pushes out generic events
10590 // FINGER 0 DOWN
10591 processId(mapper, 3);
10592 processPosition(mapper, 100, 100);
10593 processKey(mapper, BTN_TOUCH, 1);
10594 processSync(mapper);
10595
10596 // start at (100,100), cursor should be at (0,0) * scale
10597 NotifyMotionArgs args;
10598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10599 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10600 ASSERT_NO_FATAL_FAILURE(
10601 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10602
10603 // FINGER 0 MOVE
10604 processPosition(mapper, 200, 200);
10605 processSync(mapper);
10606
10607 // compute scaling to help with touch position checking
10608 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10609 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10610 float scale =
10611 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10612
10613 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10615 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10617 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010618
10619 // BUTTON DOWN
10620 processKey(mapper, BTN_LEFT, 1);
10621 processSync(mapper);
10622
10623 // touchinputmapper design sends a move before button press
10624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10625 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10627 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10628
10629 // BUTTON UP
10630 processKey(mapper, BTN_LEFT, 0);
10631 processSync(mapper);
10632
10633 // touchinputmapper design sends a move after button release
10634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10635 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10637 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010638}
10639
10640TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10641 std::shared_ptr<FakePointerController> fakePointerController =
10642 std::make_shared<FakePointerController>();
10643
Michael Wrighta9cf4192022-12-01 23:46:39 +000010644 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010645 prepareAxes(POSITION | ID | SLOT);
10646 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010647 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010648 mFakePolicy->setPointerCapture(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010649 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010650
Josep del Río2d8c79a2023-01-23 19:33:50 +000010651 // uncaptured touchpad should be a pointer device
10652 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010653
Josep del Río2d8c79a2023-01-23 19:33:50 +000010654 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010655 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010656 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010657 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10658}
10659
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010660// --- BluetoothMultiTouchInputMapperTest ---
10661
10662class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10663protected:
10664 void SetUp() override {
10665 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10666 }
10667};
10668
10669TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10670 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010671 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010672 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010673 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010674
10675 nsecs_t kernelEventTime = ARBITRARY_TIME;
10676 nsecs_t expectedEventTime = ARBITRARY_TIME;
10677 // Touch down.
10678 processId(mapper, FIRST_TRACKING_ID);
10679 processPosition(mapper, 100, 200);
10680 processPressure(mapper, RAW_PRESSURE_MAX);
10681 processSync(mapper, ARBITRARY_TIME);
10682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10683 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10684
10685 // Process several events that come in quick succession, according to their timestamps.
10686 for (int i = 0; i < 3; i++) {
10687 constexpr static nsecs_t delta = ms2ns(1);
10688 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10689 kernelEventTime += delta;
10690 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10691
10692 processPosition(mapper, 101 + i, 201 + i);
10693 processSync(mapper, kernelEventTime);
10694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10695 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10696 WithEventTime(expectedEventTime))));
10697 }
10698
10699 // Release the touch.
10700 processId(mapper, INVALID_TRACKING_ID);
10701 processPressure(mapper, RAW_PRESSURE_MIN);
10702 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10704 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10705 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10706}
10707
10708// --- MultiTouchPointerModeTest ---
10709
HQ Liue6983c72022-04-19 22:14:56 +000010710class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10711protected:
10712 float mPointerMovementScale;
10713 float mPointerXZoomScale;
10714 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10715 addConfigurationProperty("touch.deviceType", "pointer");
10716 std::shared_ptr<FakePointerController> fakePointerController =
10717 std::make_shared<FakePointerController>();
10718 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10719 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010720 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010721
10722 prepareAxes(POSITION);
10723 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10724 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10725 // needs to be disabled, and the pointer gesture needs to be enabled.
10726 mFakePolicy->setPointerCapture(false);
10727 mFakePolicy->setPointerGestureEnabled(true);
10728 mFakePolicy->setPointerController(fakePointerController);
10729
10730 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10731 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10732 mPointerMovementScale =
10733 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10734 mPointerXZoomScale =
10735 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10736 }
10737
10738 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10739 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10740 /*flat*/ 0,
10741 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10742 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10743 /*flat*/ 0,
10744 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10745 }
10746};
10747
10748/**
10749 * Two fingers down on a pointer mode touch pad. The width
10750 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10751 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10752 * be greater than the both value to be freeform gesture, so that after two
10753 * fingers start to move downwards, the gesture should be swipe.
10754 */
10755TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10756 // The min freeform gesture width is 25units/mm x 30mm = 750
10757 // which is greater than fraction of the diagnal length of the touchpad (349).
10758 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010759 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010760 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010761 NotifyMotionArgs motionArgs;
10762
10763 // Two fingers down at once.
10764 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10765 // Pointer's initial position is used the [0,0] coordinate.
10766 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10767
10768 processId(mapper, FIRST_TRACKING_ID);
10769 processPosition(mapper, x1, y1);
10770 processMTSync(mapper);
10771 processId(mapper, SECOND_TRACKING_ID);
10772 processPosition(mapper, x2, y2);
10773 processMTSync(mapper);
10774 processSync(mapper);
10775
10776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010777 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010778 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010779 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010780 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010781 ASSERT_NO_FATAL_FAILURE(
10782 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10783
10784 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10785 // that there should be 1 pointer.
10786 int32_t movingDistance = 200;
10787 y1 += movingDistance;
10788 y2 += movingDistance;
10789
10790 processId(mapper, FIRST_TRACKING_ID);
10791 processPosition(mapper, x1, y1);
10792 processMTSync(mapper);
10793 processId(mapper, SECOND_TRACKING_ID);
10794 processPosition(mapper, x2, y2);
10795 processMTSync(mapper);
10796 processSync(mapper);
10797
10798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010799 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010800 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010801 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010802 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010803 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10804 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10805 0, 0, 0, 0));
10806}
10807
10808/**
10809 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10810 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10811 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10812 * value to be freeform gesture, so that after two fingers start to move downwards,
10813 * the gesture should be swipe.
10814 */
10815TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10816 // The min freeform gesture width is 5units/mm x 30mm = 150
10817 // which is greater than fraction of the diagnal length of the touchpad (349).
10818 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010819 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +000010820 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010821 NotifyMotionArgs motionArgs;
10822
10823 // Two fingers down at once.
10824 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10825 // Pointer's initial position is used the [0,0] coordinate.
10826 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10827
10828 processId(mapper, FIRST_TRACKING_ID);
10829 processPosition(mapper, x1, y1);
10830 processMTSync(mapper);
10831 processId(mapper, SECOND_TRACKING_ID);
10832 processPosition(mapper, x2, y2);
10833 processMTSync(mapper);
10834 processSync(mapper);
10835
10836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010837 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010838 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010839 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010840 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010841 ASSERT_NO_FATAL_FAILURE(
10842 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10843
10844 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10845 // and there should be 1 pointer.
10846 int32_t movingDistance = 200;
10847 y1 += movingDistance;
10848 y2 += movingDistance;
10849
10850 processId(mapper, FIRST_TRACKING_ID);
10851 processPosition(mapper, x1, y1);
10852 processMTSync(mapper);
10853 processId(mapper, SECOND_TRACKING_ID);
10854 processPosition(mapper, x2, y2);
10855 processMTSync(mapper);
10856 processSync(mapper);
10857
10858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010859 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010860 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010861 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010862 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010863 // New coordinate is the scaled relative coordinate from the initial coordinate.
10864 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10865 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10866 0, 0, 0, 0));
10867}
10868
10869/**
10870 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10871 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10872 * freeform gestures after two fingers start to move downwards.
10873 */
10874TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010875 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010876 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010877
10878 NotifyMotionArgs motionArgs;
10879
10880 // Two fingers down at once. Wider than the max swipe width.
10881 // The gesture is expected to be PRESS, then transformed to FREEFORM
10882 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10883
10884 processId(mapper, FIRST_TRACKING_ID);
10885 processPosition(mapper, x1, y1);
10886 processMTSync(mapper);
10887 processId(mapper, SECOND_TRACKING_ID);
10888 processPosition(mapper, x2, y2);
10889 processMTSync(mapper);
10890 processSync(mapper);
10891
10892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010893 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010894 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010895 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010896 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010897 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10898 ASSERT_NO_FATAL_FAILURE(
10899 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10900
10901 int32_t movingDistance = 200;
10902
10903 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10904 // then two down events for two pointers.
10905 y1 += movingDistance;
10906 y2 += movingDistance;
10907
10908 processId(mapper, FIRST_TRACKING_ID);
10909 processPosition(mapper, x1, y1);
10910 processMTSync(mapper);
10911 processId(mapper, SECOND_TRACKING_ID);
10912 processPosition(mapper, x2, y2);
10913 processMTSync(mapper);
10914 processSync(mapper);
10915
10916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10917 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010918 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010919 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010921 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010922 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010923 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010925 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010926 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010927 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010928 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010929 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010930 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010931 // Two pointers' scaled relative coordinates from their initial centroid.
10932 // Initial y coordinates are 0 as y1 and y2 have the same value.
10933 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10934 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10935 // When pointers move, the new coordinates equal to the initial coordinates plus
10936 // scaled moving distance.
10937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10938 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10939 0, 0, 0, 0));
10940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10941 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10942 0, 0, 0, 0));
10943
10944 // Move two fingers down again, expect one MOVE motion event.
10945 y1 += movingDistance;
10946 y2 += movingDistance;
10947
10948 processId(mapper, FIRST_TRACKING_ID);
10949 processPosition(mapper, x1, y1);
10950 processMTSync(mapper);
10951 processId(mapper, SECOND_TRACKING_ID);
10952 processPosition(mapper, x2, y2);
10953 processMTSync(mapper);
10954 processSync(mapper);
10955
10956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010957 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010958 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010959 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010960 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010961 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10962 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10963 0, 0, 0, 0, 0));
10964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10965 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10966 0, 0, 0, 0, 0));
10967}
10968
Harry Cutts39b7ca22022-10-05 15:55:48 +000010969TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010970 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010971 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010972 NotifyMotionArgs motionArgs;
10973
10974 // Place two fingers down.
10975 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10976
10977 processId(mapper, FIRST_TRACKING_ID);
10978 processPosition(mapper, x1, y1);
10979 processMTSync(mapper);
10980 processId(mapper, SECOND_TRACKING_ID);
10981 processPosition(mapper, x2, y2);
10982 processMTSync(mapper);
10983 processSync(mapper);
10984
10985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010986 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010987 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10988 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10989 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10990 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10991
10992 // Move the two fingers down and to the left.
10993 int32_t movingDistance = 200;
10994 x1 -= movingDistance;
10995 y1 += movingDistance;
10996 x2 -= movingDistance;
10997 y2 += movingDistance;
10998
10999 processId(mapper, FIRST_TRACKING_ID);
11000 processPosition(mapper, x1, y1);
11001 processMTSync(mapper);
11002 processId(mapper, SECOND_TRACKING_ID);
11003 processPosition(mapper, x2, y2);
11004 processMTSync(mapper);
11005 processSync(mapper);
11006
11007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011008 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000011009 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11010 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
11011 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
11012 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
11013}
11014
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011015TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000011016 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011017 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000011018 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
11020
11021 // Start a stylus gesture.
11022 processKey(mapper, BTN_TOOL_PEN, 1);
11023 processId(mapper, FIRST_TRACKING_ID);
11024 processPosition(mapper, 100, 200);
11025 processSync(mapper);
11026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11027 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
11028 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011029 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011030 // TODO(b/257078296): Pointer mode generates extra event.
11031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11032 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11033 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011034 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11036
11037 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
11038 // gesture should be disabled.
11039 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
11040 viewport->isActive = false;
11041 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000011042 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11044 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11045 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011046 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011047 // TODO(b/257078296): Pointer mode generates extra event.
11048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11049 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11050 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011051 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11053}
11054
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011055// --- JoystickInputMapperTest ---
11056
11057class JoystickInputMapperTest : public InputMapperTest {
11058protected:
11059 static const int32_t RAW_X_MIN;
11060 static const int32_t RAW_X_MAX;
11061 static const int32_t RAW_Y_MIN;
11062 static const int32_t RAW_Y_MAX;
11063
11064 void SetUp() override {
11065 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
11066 }
11067 void prepareAxes() {
11068 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
11069 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
11070 }
11071
11072 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
11073 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
11074 }
11075
11076 void processSync(JoystickInputMapper& mapper) {
11077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
11078 }
11079
Michael Wrighta9cf4192022-12-01 23:46:39 +000011080 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011081 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
11082 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
11083 NO_PORT, ViewportType::VIRTUAL);
11084 }
11085};
11086
11087const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
11088const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
11089const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11090const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11091
11092TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11093 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000011094 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011095
11096 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11097
Michael Wrighta9cf4192022-12-01 23:46:39 +000011098 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011099
11100 // Send an axis event
11101 processAxis(mapper, ABS_X, 100);
11102 processSync(mapper);
11103
11104 NotifyMotionArgs args;
11105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11106 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11107
11108 // Send another axis event
11109 processAxis(mapper, ABS_Y, 100);
11110 processSync(mapper);
11111
11112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11113 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11114}
11115
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011116// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011117
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011118class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011119protected:
11120 static const char* DEVICE_NAME;
11121 static const char* DEVICE_LOCATION;
11122 static const int32_t DEVICE_ID;
11123 static const int32_t DEVICE_GENERATION;
11124 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011125 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011126 static const int32_t EVENTHUB_ID;
11127
11128 std::shared_ptr<FakeEventHub> mFakeEventHub;
11129 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011130 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011131 std::unique_ptr<InstrumentedInputReader> mReader;
11132 std::shared_ptr<InputDevice> mDevice;
11133
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011134 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011135 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011136 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011137 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011138 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011139 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011140 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11141 }
11142
11143 void SetUp() override { SetUp(DEVICE_CLASSES); }
11144
11145 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011146 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011147 mFakePolicy.clear();
11148 }
11149
Chris Yee2b1e5c2021-03-10 22:45:12 -080011150 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11151 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011152 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011153 InputDeviceIdentifier identifier;
11154 identifier.name = name;
11155 identifier.location = location;
11156 std::shared_ptr<InputDevice> device =
11157 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11158 identifier);
11159 mReader->pushNextDevice(device);
11160 mFakeEventHub->addDevice(eventHubId, name, classes);
11161 mReader->loopOnce();
11162 return device;
11163 }
11164
11165 template <class T, typename... Args>
11166 T& addControllerAndConfigure(Args... args) {
11167 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11168
11169 return controller;
11170 }
11171};
11172
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011173const char* PeripheralControllerTest::DEVICE_NAME = "device";
11174const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11175const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11176const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11177const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011178const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11179 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011180const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011181
11182// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011183class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011184protected:
11185 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011186 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011187 }
11188};
11189
11190TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011191 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011192
Harry Cuttsa5b71292022-11-28 12:56:17 +000011193 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11194 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11195 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011196}
11197
11198TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011199 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011200
Harry Cuttsa5b71292022-11-28 12:56:17 +000011201 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11202 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11203 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011204}
11205
11206// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011207class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011208protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011209 void SetUp() override {
11210 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11211 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011212};
11213
Chris Ye85758332021-05-16 23:05:17 -070011214TEST_F(LightControllerTest, MonoLight) {
11215 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011216 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011217 .maxBrightness = 255,
11218 .flags = InputLightClass::BRIGHTNESS,
11219 .path = ""};
11220 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011221
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011222 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011223 InputDeviceInfo info;
11224 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011225 std::vector<InputDeviceLightInfo> lights = info.getLights();
11226 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011227 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11228 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11229
11230 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11231 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11232}
11233
11234TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11235 RawLightInfo infoMono = {.id = 1,
11236 .name = "mono_keyboard_backlight",
11237 .maxBrightness = 255,
11238 .flags = InputLightClass::BRIGHTNESS |
11239 InputLightClass::KEYBOARD_BACKLIGHT,
11240 .path = ""};
11241 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11242
11243 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11244 InputDeviceInfo info;
11245 controller.populateDeviceInfo(&info);
11246 std::vector<InputDeviceLightInfo> lights = info.getLights();
11247 ASSERT_EQ(1U, lights.size());
11248 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11249 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011250
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011251 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11252 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011253}
11254
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000011255TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
11256 RawLightInfo infoMono = {.id = 1,
11257 .name = "mono_light",
11258 .maxBrightness = 255,
11259 .flags = InputLightClass::BRIGHTNESS,
11260 .path = ""};
11261 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11262 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11263 "0,100,200");
11264
11265 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11266 std::list<NotifyArgs> unused =
11267 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11268 /*changes=*/{});
11269
11270 InputDeviceInfo info;
11271 controller.populateDeviceInfo(&info);
11272 std::vector<InputDeviceLightInfo> lights = info.getLights();
11273 ASSERT_EQ(1U, lights.size());
11274 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11275}
11276
11277TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
11278 RawLightInfo infoMono = {.id = 1,
11279 .name = "mono_keyboard_backlight",
11280 .maxBrightness = 255,
11281 .flags = InputLightClass::BRIGHTNESS |
11282 InputLightClass::KEYBOARD_BACKLIGHT,
11283 .path = ""};
11284 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11285
11286 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11287 std::list<NotifyArgs> unused =
11288 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11289 /*changes=*/{});
11290
11291 InputDeviceInfo info;
11292 controller.populateDeviceInfo(&info);
11293 std::vector<InputDeviceLightInfo> lights = info.getLights();
11294 ASSERT_EQ(1U, lights.size());
11295 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11296}
11297
11298TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
11299 RawLightInfo infoMono = {.id = 1,
11300 .name = "mono_keyboard_backlight",
11301 .maxBrightness = 255,
11302 .flags = InputLightClass::BRIGHTNESS |
11303 InputLightClass::KEYBOARD_BACKLIGHT,
11304 .path = ""};
11305 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11306 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11307 "0,100,200");
11308
11309 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11310 std::list<NotifyArgs> unused =
11311 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11312 /*changes=*/{});
11313
11314 InputDeviceInfo info;
11315 controller.populateDeviceInfo(&info);
11316 std::vector<InputDeviceLightInfo> lights = info.getLights();
11317 ASSERT_EQ(1U, lights.size());
11318 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
11319 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
11320 ASSERT_EQ(BrightnessLevel(0), *it);
11321 std::advance(it, 1);
11322 ASSERT_EQ(BrightnessLevel(100), *it);
11323 std::advance(it, 1);
11324 ASSERT_EQ(BrightnessLevel(200), *it);
11325}
11326
11327TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
11328 RawLightInfo infoMono = {.id = 1,
11329 .name = "mono_keyboard_backlight",
11330 .maxBrightness = 255,
11331 .flags = InputLightClass::BRIGHTNESS |
11332 InputLightClass::KEYBOARD_BACKLIGHT,
11333 .path = ""};
11334 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11335 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11336 "0,100,200,300,400,500");
11337
11338 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11339 std::list<NotifyArgs> unused =
11340 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11341 /*changes=*/{});
11342
11343 InputDeviceInfo info;
11344 controller.populateDeviceInfo(&info);
11345 std::vector<InputDeviceLightInfo> lights = info.getLights();
11346 ASSERT_EQ(1U, lights.size());
11347 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11348}
11349
Chris Yee2b1e5c2021-03-10 22:45:12 -080011350TEST_F(LightControllerTest, RGBLight) {
11351 RawLightInfo infoRed = {.id = 1,
11352 .name = "red",
11353 .maxBrightness = 255,
11354 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11355 .path = ""};
11356 RawLightInfo infoGreen = {.id = 2,
11357 .name = "green",
11358 .maxBrightness = 255,
11359 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11360 .path = ""};
11361 RawLightInfo infoBlue = {.id = 3,
11362 .name = "blue",
11363 .maxBrightness = 255,
11364 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11365 .path = ""};
11366 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11367 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11368 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11369
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011370 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011371 InputDeviceInfo info;
11372 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011373 std::vector<InputDeviceLightInfo> lights = info.getLights();
11374 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011375 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11376 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11377 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11378
11379 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11380 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11381}
11382
11383TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11384 RawLightInfo infoRed = {.id = 1,
11385 .name = "red_keyboard_backlight",
11386 .maxBrightness = 255,
11387 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11388 InputLightClass::KEYBOARD_BACKLIGHT,
11389 .path = ""};
11390 RawLightInfo infoGreen = {.id = 2,
11391 .name = "green_keyboard_backlight",
11392 .maxBrightness = 255,
11393 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11394 InputLightClass::KEYBOARD_BACKLIGHT,
11395 .path = ""};
11396 RawLightInfo infoBlue = {.id = 3,
11397 .name = "blue_keyboard_backlight",
11398 .maxBrightness = 255,
11399 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11400 InputLightClass::KEYBOARD_BACKLIGHT,
11401 .path = ""};
11402 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11403 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11404 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11405
11406 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11407 InputDeviceInfo info;
11408 controller.populateDeviceInfo(&info);
11409 std::vector<InputDeviceLightInfo> lights = info.getLights();
11410 ASSERT_EQ(1U, lights.size());
11411 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11412 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11413 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11414
11415 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11416 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11417}
11418
11419TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11420 RawLightInfo infoRed = {.id = 1,
11421 .name = "red",
11422 .maxBrightness = 255,
11423 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11424 .path = ""};
11425 RawLightInfo infoGreen = {.id = 2,
11426 .name = "green",
11427 .maxBrightness = 255,
11428 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11429 .path = ""};
11430 RawLightInfo infoBlue = {.id = 3,
11431 .name = "blue",
11432 .maxBrightness = 255,
11433 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11434 .path = ""};
11435 RawLightInfo infoGlobal = {.id = 3,
11436 .name = "global_keyboard_backlight",
11437 .maxBrightness = 255,
11438 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11439 InputLightClass::KEYBOARD_BACKLIGHT,
11440 .path = ""};
11441 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11442 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11443 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11444 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11445
11446 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11447 InputDeviceInfo info;
11448 controller.populateDeviceInfo(&info);
11449 std::vector<InputDeviceLightInfo> lights = info.getLights();
11450 ASSERT_EQ(1U, lights.size());
11451 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11452 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11453 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011454
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011455 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11456 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011457}
11458
11459TEST_F(LightControllerTest, MultiColorRGBLight) {
11460 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011461 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011462 .maxBrightness = 255,
11463 .flags = InputLightClass::BRIGHTNESS |
11464 InputLightClass::MULTI_INTENSITY |
11465 InputLightClass::MULTI_INDEX,
11466 .path = ""};
11467
11468 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11469
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011470 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011471 InputDeviceInfo info;
11472 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011473 std::vector<InputDeviceLightInfo> lights = info.getLights();
11474 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011475 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11476 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11477 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11478
11479 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11480 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11481}
11482
11483TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11484 RawLightInfo infoColor = {.id = 1,
11485 .name = "multi_color_keyboard_backlight",
11486 .maxBrightness = 255,
11487 .flags = InputLightClass::BRIGHTNESS |
11488 InputLightClass::MULTI_INTENSITY |
11489 InputLightClass::MULTI_INDEX |
11490 InputLightClass::KEYBOARD_BACKLIGHT,
11491 .path = ""};
11492
11493 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11494
11495 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11496 InputDeviceInfo info;
11497 controller.populateDeviceInfo(&info);
11498 std::vector<InputDeviceLightInfo> lights = info.getLights();
11499 ASSERT_EQ(1U, lights.size());
11500 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11501 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11502 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011503
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011504 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11505 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011506}
11507
11508TEST_F(LightControllerTest, PlayerIdLight) {
11509 RawLightInfo info1 = {.id = 1,
11510 .name = "player1",
11511 .maxBrightness = 255,
11512 .flags = InputLightClass::BRIGHTNESS,
11513 .path = ""};
11514 RawLightInfo info2 = {.id = 2,
11515 .name = "player2",
11516 .maxBrightness = 255,
11517 .flags = InputLightClass::BRIGHTNESS,
11518 .path = ""};
11519 RawLightInfo info3 = {.id = 3,
11520 .name = "player3",
11521 .maxBrightness = 255,
11522 .flags = InputLightClass::BRIGHTNESS,
11523 .path = ""};
11524 RawLightInfo info4 = {.id = 4,
11525 .name = "player4",
11526 .maxBrightness = 255,
11527 .flags = InputLightClass::BRIGHTNESS,
11528 .path = ""};
11529 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11530 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11531 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11532 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11533
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011534 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011535 InputDeviceInfo info;
11536 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011537 std::vector<InputDeviceLightInfo> lights = info.getLights();
11538 ASSERT_EQ(1U, lights.size());
11539 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011540 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11541 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011542
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011543 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11544 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11545 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011546}
11547
Michael Wrightd02c5b62014-02-10 15:10:22 -080011548} // namespace android