blob: 4fd30c0d6c4fa849d91540aad5be8f1f182075d1 [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 Pradhan39d60aa2023-07-13 22:01:04 +00001889TYPED_TEST(StylusButtonIntegrationTest, 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 Pradhan39d60aa2023-07-13 22:01:04 +00001903TYPED_TEST(StylusButtonIntegrationTest, 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 Pradhan39d60aa2023-07-13 22:01:04 +00001949TYPED_TEST(StylusButtonIntegrationTest, 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 Pradhan39d60aa2023-07-13 22:01:04 +00002025TYPED_TEST(StylusButtonIntegrationTest, 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 Pradhan39d60aa2023-07-13 22:01:04 +00002079TYPED_TEST(StylusButtonIntegrationTest, 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 Pradhan39d60aa2023-07-13 22:01:04 +00002136TEST_F(ExternalStylusIntegrationTest, 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 Pradhan39d60aa2023-07-13 22:01:04 +00002182TEST_F(ExternalStylusIntegrationTest, 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 Pradhan39d60aa2023-07-13 22:01:04 +00002250TEST_F(ExternalStylusIntegrationTest, 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
Arpit Singhf4cdbef2023-05-30 14:12:49 +00002556// 2. Device is disabled when configure API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002557TEST_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=*/{});
Arpit Singh4f61f3c2023-06-13 15:05:53 +00002663 auto _ = device.addEventHubDevice(ARBITRARY_TIME, TEST_EVENTHUB_ID,
2664 mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002665 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2666 std::string dumpStr, eventHubDevStr;
2667 device.dump(dumpStr, eventHubDevStr);
2668}
2669
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002670TEST_F(InputDeviceTest, GetBluetoothAddress) {
2671 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2672 ASSERT_TRUE(address);
2673 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2674}
2675
Michael Wrightd02c5b62014-02-10 15:10:22 -08002676// --- SwitchInputMapperTest ---
2677
2678class SwitchInputMapperTest : public InputMapperTest {
2679protected:
2680};
2681
2682TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002683 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002684
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002685 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002686}
2687
2688TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002689 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002690
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002691 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002692 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002693
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002694 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002695 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002696}
2697
2698TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002699 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002700 std::list<NotifyArgs> out;
2701 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2702 ASSERT_TRUE(out.empty());
2703 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2704 ASSERT_TRUE(out.empty());
2705 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2706 ASSERT_TRUE(out.empty());
2707 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002709 ASSERT_EQ(1u, out.size());
2710 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002711 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002712 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2713 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002714 args.switchMask);
2715 ASSERT_EQ(uint32_t(0), args.policyFlags);
2716}
2717
Chris Ye87143712020-11-10 05:05:58 +00002718// --- VibratorInputMapperTest ---
2719class VibratorInputMapperTest : public InputMapperTest {
2720protected:
2721 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2722};
2723
2724TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002725 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002726
2727 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2728}
2729
2730TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002731 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002732
2733 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2734}
2735
2736TEST_F(VibratorInputMapperTest, Vibrate) {
2737 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002738 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00002739 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002740
2741 VibrationElement pattern(2);
2742 VibrationSequence sequence(2);
2743 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002744 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2745 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002746 sequence.addElement(pattern);
2747 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002748 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2749 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002750 sequence.addElement(pattern);
2751
2752 std::vector<int64_t> timings = {0, 1};
2753 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2754
2755 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002756 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002757 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002758 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002759 // Verify vibrator state listener was notified.
2760 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002761 ASSERT_EQ(1u, out.size());
2762 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2763 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2764 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002765 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002766 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002767 ASSERT_FALSE(mapper.isVibrating());
2768 // Verify vibrator state listener was notified.
2769 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002770 ASSERT_EQ(1u, out.size());
2771 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2772 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2773 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002774}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002775
Chris Yef59a2f42020-10-16 12:55:26 -07002776// --- SensorInputMapperTest ---
2777
2778class SensorInputMapperTest : public InputMapperTest {
2779protected:
2780 static const int32_t ACCEL_RAW_MIN;
2781 static const int32_t ACCEL_RAW_MAX;
2782 static const int32_t ACCEL_RAW_FUZZ;
2783 static const int32_t ACCEL_RAW_FLAT;
2784 static const int32_t ACCEL_RAW_RESOLUTION;
2785
2786 static const int32_t GYRO_RAW_MIN;
2787 static const int32_t GYRO_RAW_MAX;
2788 static const int32_t GYRO_RAW_FUZZ;
2789 static const int32_t GYRO_RAW_FLAT;
2790 static const int32_t GYRO_RAW_RESOLUTION;
2791
2792 static const float GRAVITY_MS2_UNIT;
2793 static const float DEGREE_RADIAN_UNIT;
2794
2795 void prepareAccelAxes();
2796 void prepareGyroAxes();
2797 void setAccelProperties();
2798 void setGyroProperties();
2799 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2800};
2801
2802const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2803const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2804const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2805const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2806const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2807
2808const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2809const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2810const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2811const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2812const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2813
2814const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2815const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2816
2817void SensorInputMapperTest::prepareAccelAxes() {
2818 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2819 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2820 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2821 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2822 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2823 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2824}
2825
2826void SensorInputMapperTest::prepareGyroAxes() {
2827 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2828 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2829 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2830 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2831 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2832 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2833}
2834
2835void SensorInputMapperTest::setAccelProperties() {
2836 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2837 /* sensorDataIndex */ 0);
2838 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2839 /* sensorDataIndex */ 1);
2840 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2841 /* sensorDataIndex */ 2);
2842 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2843 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2844 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2845 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2846 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2847}
2848
2849void SensorInputMapperTest::setGyroProperties() {
2850 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2851 /* sensorDataIndex */ 0);
2852 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2853 /* sensorDataIndex */ 1);
2854 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2855 /* sensorDataIndex */ 2);
2856 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2857 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2858 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2859 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2860 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2861}
2862
2863TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00002864 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002865
2866 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2867}
2868
2869TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2870 setAccelProperties();
2871 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00002872 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002873
2874 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2875 std::chrono::microseconds(10000),
2876 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002877 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002878 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2879 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2881 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2882 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002883
2884 NotifySensorArgs args;
2885 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2886 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2887 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2888
2889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2890 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2891 ASSERT_EQ(args.deviceId, DEVICE_ID);
2892 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2893 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2894 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2895 ASSERT_EQ(args.values, values);
2896 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2897}
2898
2899TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2900 setGyroProperties();
2901 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00002902 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002903
2904 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2905 std::chrono::microseconds(10000),
2906 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002907 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002908 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2909 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2910 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2911 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2912 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002913
2914 NotifySensorArgs args;
2915 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2916 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2917 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2918
2919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2920 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2921 ASSERT_EQ(args.deviceId, DEVICE_ID);
2922 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2923 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2924 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2925 ASSERT_EQ(args.values, values);
2926 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2927}
2928
Michael Wrightd02c5b62014-02-10 15:10:22 -08002929// --- KeyboardInputMapperTest ---
2930
2931class KeyboardInputMapperTest : public InputMapperTest {
2932protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002933 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002934 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002935 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002936
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002937 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002938 int32_t originalKeyCode, int32_t rotatedKeyCode,
2939 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002940};
2941
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002942/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2943 * orientation.
2944 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002945void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002946 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2947 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002948}
2949
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002950void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002951 int32_t originalScanCode, int32_t originalKeyCode,
2952 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002953 NotifyKeyArgs args;
2954
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002955 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2957 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2958 ASSERT_EQ(originalScanCode, args.scanCode);
2959 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002960 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002961
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002962 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2964 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2965 ASSERT_EQ(originalScanCode, args.scanCode);
2966 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002967 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002968}
2969
Michael Wrightd02c5b62014-02-10 15:10:22 -08002970TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002971 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00002972 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002973 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002975 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002976}
2977
2978TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2979 const int32_t USAGE_A = 0x070004;
2980 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002981 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2982 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002983 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2984 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2985 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002986
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002987 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00002988 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002989 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002990 // Initial metastate is AMETA_NONE.
2991 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002992
2993 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002995 NotifyKeyArgs args;
2996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2997 ASSERT_EQ(DEVICE_ID, args.deviceId);
2998 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2999 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3000 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3001 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3002 ASSERT_EQ(KEY_HOME, args.scanCode);
3003 ASSERT_EQ(AMETA_NONE, args.metaState);
3004 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3005 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3006 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3007
3008 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003009 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3011 ASSERT_EQ(DEVICE_ID, args.deviceId);
3012 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3013 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3014 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3015 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3016 ASSERT_EQ(KEY_HOME, args.scanCode);
3017 ASSERT_EQ(AMETA_NONE, args.metaState);
3018 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3019 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3020 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3021
3022 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3026 ASSERT_EQ(DEVICE_ID, args.deviceId);
3027 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3028 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3029 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3030 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3031 ASSERT_EQ(0, args.scanCode);
3032 ASSERT_EQ(AMETA_NONE, args.metaState);
3033 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3034 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3035 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3036
3037 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3039 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3041 ASSERT_EQ(DEVICE_ID, args.deviceId);
3042 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3043 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3044 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3045 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3046 ASSERT_EQ(0, args.scanCode);
3047 ASSERT_EQ(AMETA_NONE, args.metaState);
3048 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3049 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3050 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3051
3052 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3054 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3056 ASSERT_EQ(DEVICE_ID, args.deviceId);
3057 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3058 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3059 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3060 ASSERT_EQ(0, args.keyCode);
3061 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3062 ASSERT_EQ(AMETA_NONE, args.metaState);
3063 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3064 ASSERT_EQ(0U, args.policyFlags);
3065 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3066
3067 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003068 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3069 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3071 ASSERT_EQ(DEVICE_ID, args.deviceId);
3072 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3073 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3074 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3075 ASSERT_EQ(0, args.keyCode);
3076 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3077 ASSERT_EQ(AMETA_NONE, args.metaState);
3078 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3079 ASSERT_EQ(0U, args.policyFlags);
3080 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3081}
3082
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003083TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3084 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3085 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3086 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3087
3088 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003089 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003090 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3091
3092 // Key down by scan code.
3093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3094 NotifyKeyArgs args;
3095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3096 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3097
3098 // Key up by scan code.
3099 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3101 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3102}
3103
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003104/**
3105 * Ensure that the readTime is set to the time when the EV_KEY is received.
3106 */
3107TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3108 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3109
3110 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003111 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003112 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3113 NotifyKeyArgs args;
3114
3115 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003116 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3118 ASSERT_EQ(12, args.readTime);
3119
3120 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003121 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3123 ASSERT_EQ(15, args.readTime);
3124}
3125
Michael Wrightd02c5b62014-02-10 15:10:22 -08003126TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003127 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3128 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003129 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3130 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3131 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003132
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003133 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003134 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003135 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003136
Arthur Hung95f68612022-04-07 14:08:22 +08003137 // Initial metastate is AMETA_NONE.
3138 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003139
3140 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003142 NotifyKeyArgs args;
3143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3144 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003145 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003146 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003147
3148 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003149 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3151 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003152 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003153
3154 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003155 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3157 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003158 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003159
3160 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003161 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3163 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003164 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003165 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003166}
3167
3168TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003169 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3170 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3171 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3172 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003173
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003174 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003175 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003176 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003177
Michael Wrighta9cf4192022-12-01 23:46:39 +00003178 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003179 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3180 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3181 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3182 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3183 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3184 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3185 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3186 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3187}
3188
3189TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003190 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3191 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3192 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3193 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003194
Michael Wrightd02c5b62014-02-10 15:10:22 -08003195 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003196 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003197 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003198 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003199
Michael Wrighta9cf4192022-12-01 23:46:39 +00003200 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003201 ASSERT_NO_FATAL_FAILURE(
3202 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3203 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3204 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3205 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3206 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3207 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3208 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003209
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003210 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003211 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003212 ASSERT_NO_FATAL_FAILURE(
3213 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3214 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3215 AKEYCODE_DPAD_UP, DISPLAY_ID));
3216 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3217 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3218 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3219 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003220
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003221 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003222 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003223 ASSERT_NO_FATAL_FAILURE(
3224 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3225 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3226 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3227 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3228 AKEYCODE_DPAD_UP, DISPLAY_ID));
3229 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3230 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003231
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003232 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003233 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003234 ASSERT_NO_FATAL_FAILURE(
3235 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3236 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3237 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3238 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3239 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3240 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3241 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003242
3243 // Special case: if orientation changes while key is down, we still emit the same keycode
3244 // in the key up as we did in the key down.
3245 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003246 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003247 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003248 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3250 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3251 ASSERT_EQ(KEY_UP, args.scanCode);
3252 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3253
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003254 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003255 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003256 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3258 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3259 ASSERT_EQ(KEY_UP, args.scanCode);
3260 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3261}
3262
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003263TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3264 // If the keyboard is not orientation aware,
3265 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003266 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003267
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003268 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003269 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003270 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003271 NotifyKeyArgs args;
3272
3273 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3278 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3279
Michael Wrighta9cf4192022-12-01 23:46:39 +00003280 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003283 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3285 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3286}
3287
3288TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3289 // If the keyboard is orientation aware,
3290 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003291 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003292
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003293 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003294 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003295 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003296 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003297 NotifyKeyArgs args;
3298
3299 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3300 // ^--- already checked by the previous test
3301
Michael Wrighta9cf4192022-12-01 23:46:39 +00003302 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003303 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003304 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3308 ASSERT_EQ(DISPLAY_ID, args.displayId);
3309
3310 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003311 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003312 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003313 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3318 ASSERT_EQ(newDisplayId, args.displayId);
3319}
3320
Michael Wrightd02c5b62014-02-10 15:10:22 -08003321TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003322 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003323 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003324 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003326 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003327 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003329 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003330 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003331}
3332
Philip Junker4af3b3d2021-12-14 10:36:55 +01003333TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3334 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003335 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003336 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3337
3338 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3339 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3340 << "If a mapping is available, the result is equal to the mapping";
3341
3342 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3343 << "If no mapping is available, the result is the key location";
3344}
3345
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003347 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003348 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003349 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003350
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003351 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003352 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003354 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003355 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003356}
3357
3358TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003359 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003360 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003361 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003362
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003363 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364
Michael Wrightd02c5b62014-02-10 15:10:22 -08003365 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003366 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003367 ASSERT_TRUE(flags[0]);
3368 ASSERT_FALSE(flags[1]);
3369}
3370
3371TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003372 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3373 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3374 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3375 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3376 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3377 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003378
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003379 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003380 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003381 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003382 // Initial metastate is AMETA_NONE.
3383 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003384
3385 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003386 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3387 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3388 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003389
3390 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003391 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3392 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003393 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3394 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3395 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003396 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003397
3398 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003401 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3402 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3403 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003404 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003405
3406 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003409 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3410 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3411 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003412 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003413
3414 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003415 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3416 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003417 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3418 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3419 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003420 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003421
3422 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003423 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3424 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003425 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3426 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3427 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003428 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003429
3430 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3432 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003433 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3434 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3435 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003436 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003437}
3438
Chris Yea52ade12020-08-27 16:49:20 -07003439TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3440 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3441 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3442 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3443 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3444
3445 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003446 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003447 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3448
Chris Yea52ade12020-08-27 16:49:20 -07003449 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003450 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003451 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3452 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3453 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3454 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3455
3456 NotifyKeyArgs args;
3457 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3460 ASSERT_EQ(AMETA_NONE, args.metaState);
3461 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3462 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3463 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3464
3465 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003466 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3468 ASSERT_EQ(AMETA_NONE, args.metaState);
3469 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3470 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3471 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3472}
3473
Arthur Hung2c9a3342019-07-23 14:18:59 +08003474TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3475 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003476 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3477 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3478 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3479 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003480
3481 // keyboard 2.
3482 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003483 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003484 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003485 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003486 std::shared_ptr<InputDevice> device2 =
3487 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003488 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003489
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003490 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3491 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3492 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3493 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003494
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003495 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003496 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003497 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003498
Arpit Singh67ca6842023-04-26 14:43:16 +00003499 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003500 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003501 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3502 mFakePolicy
3503 ->getReaderConfiguration(),
3504 AINPUT_SOURCE_KEYBOARD,
3505 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003506 std::list<NotifyArgs> unused =
3507 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003508 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003509 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003510
3511 // Prepared displays and associated info.
3512 constexpr uint8_t hdmi1 = 0;
3513 constexpr uint8_t hdmi2 = 1;
3514 const std::string SECONDARY_UNIQUE_ID = "local:1";
3515
3516 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3517 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3518
3519 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003520 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003521 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003522 ASSERT_FALSE(device2->isEnabled());
3523
3524 // Prepare second display.
3525 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003526 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003527 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003528 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003529 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003530 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003531 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003532 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003533
3534 // Device should be enabled after the associated display is found.
3535 ASSERT_TRUE(mDevice->isEnabled());
3536 ASSERT_TRUE(device2->isEnabled());
3537
3538 // Test pad key events
3539 ASSERT_NO_FATAL_FAILURE(
3540 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3541 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3542 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3543 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3544 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3545 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3546 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3547
3548 ASSERT_NO_FATAL_FAILURE(
3549 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3550 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3551 AKEYCODE_DPAD_RIGHT, newDisplayId));
3552 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3553 AKEYCODE_DPAD_DOWN, newDisplayId));
3554 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3555 AKEYCODE_DPAD_LEFT, newDisplayId));
3556}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557
arthurhungc903df12020-08-11 15:08:42 +08003558TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3559 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3560 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3561 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3562 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3563 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3564 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3565
3566 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003567 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003568 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003569 // Initial metastate is AMETA_NONE.
3570 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003571
3572 // Initialization should have turned all of the lights off.
3573 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3574 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3575 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3576
3577 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003578 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3579 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003580 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3581 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3582
3583 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003584 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003586 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3587 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3588
3589 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003590 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3591 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003592 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3593 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3594
3595 mFakeEventHub->removeDevice(EVENTHUB_ID);
3596 mReader->loopOnce();
3597
3598 // keyboard 2 should default toggle keys.
3599 const std::string USB2 = "USB2";
3600 const std::string DEVICE_NAME2 = "KEYBOARD2";
3601 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3602 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3603 std::shared_ptr<InputDevice> device2 =
3604 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003605 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003606 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3607 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3608 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3609 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3610 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3611 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3612
Arpit Singh67ca6842023-04-26 14:43:16 +00003613 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003614 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003615 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3616 mFakePolicy
3617 ->getReaderConfiguration(),
3618 AINPUT_SOURCE_KEYBOARD,
3619 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003620 std::list<NotifyArgs> unused =
3621 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003622 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003623 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003624
3625 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3626 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3627 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003628 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3629 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003630}
3631
Arthur Hungcb40a002021-08-03 14:31:01 +00003632TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3633 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3634 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3635 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3636
3637 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh67ca6842023-04-26 14:43:16 +00003638 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003639 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3640 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003641 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003642 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003643 // Initial metastate is AMETA_NONE.
3644 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003645
3646 mReader->toggleCapsLockState(DEVICE_ID);
3647 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3648}
3649
Arthur Hungfb3cc112022-04-13 07:39:50 +00003650TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3651 // keyboard 1.
3652 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3653 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3654 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3655 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3656 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3657 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3658
3659 KeyboardInputMapper& mapper1 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003660 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003661 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3662
3663 // keyboard 2.
3664 const std::string USB2 = "USB2";
3665 const std::string DEVICE_NAME2 = "KEYBOARD2";
3666 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3667 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3668 std::shared_ptr<InputDevice> device2 =
3669 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3670 ftl::Flags<InputDeviceClass>(0));
3671 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3672 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3673 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3674 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3675 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3676 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3677
Arpit Singh67ca6842023-04-26 14:43:16 +00003678 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003679 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003680 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3681 mFakePolicy
3682 ->getReaderConfiguration(),
3683 AINPUT_SOURCE_KEYBOARD,
3684 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003685 std::list<NotifyArgs> unused =
3686 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003687 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003688 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003689
Arthur Hung95f68612022-04-07 14:08:22 +08003690 // Initial metastate is AMETA_NONE.
3691 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3692 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3693
3694 // Toggle num lock on and off.
3695 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3696 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003697 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3698 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3699 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3700
3701 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3702 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3703 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3704 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3705 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3706
3707 // Toggle caps lock on and off.
3708 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3709 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3710 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3711 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3712 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3713
3714 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3715 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3716 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3717 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3718 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3719
3720 // Toggle scroll lock on and off.
3721 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3722 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3723 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3724 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3725 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3726
3727 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3728 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3729 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3730 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3731 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3732}
3733
Arthur Hung2141d542022-08-23 07:45:21 +00003734TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3735 const int32_t USAGE_A = 0x070004;
3736 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3737 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3738
3739 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003740 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00003741 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3742 // Key down by scan code.
3743 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3744 NotifyKeyArgs args;
3745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3746 ASSERT_EQ(DEVICE_ID, args.deviceId);
3747 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3748 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3749 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3750 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3751 ASSERT_EQ(KEY_HOME, args.scanCode);
3752 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3753
3754 // Disable device, it should synthesize cancellation events for down events.
3755 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003756 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00003757
3758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3759 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3760 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3761 ASSERT_EQ(KEY_HOME, args.scanCode);
3762 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3763}
3764
Zixuan Qufecb6062022-11-12 04:44:31 +00003765TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh67ca6842023-04-26 14:43:16 +00003766 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3767 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00003768 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003769 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3770 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00003771
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003772 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00003773 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3774
3775 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003776 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00003777
3778 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3779 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3780 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3781 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3782 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003783 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
3784
3785 // Call change layout association with the same values: Generation shouldn't change
3786 generation = mReader->getContext()->getGeneration();
3787 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3788 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3789 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
3790 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00003791}
3792
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003793TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3794 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3795 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3796
3797 // Configuration
Arpit Singh67ca6842023-04-26 14:43:16 +00003798 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003799 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3800 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003801 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003802
3803 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3804 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3805}
3806
Justin Chung71ddb432023-03-27 04:29:07 +00003807TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
3808 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
3809 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003810 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Justin Chung71ddb432023-03-27 04:29:07 +00003811 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3812 NotifyKeyArgs args;
3813
3814 // Key down
3815 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
3816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3817 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
3818}
3819
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003820// --- KeyboardInputMapperTest_ExternalDevice ---
3821
3822class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3823protected:
Chris Yea52ade12020-08-27 16:49:20 -07003824 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003825};
3826
3827TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003828 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3829 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003830
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003831 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3832 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3833 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3834 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003835
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003836 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003837 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003838 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003839
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003841 NotifyKeyArgs args;
3842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3843 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3844
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003845 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3847 ASSERT_EQ(uint32_t(0), args.policyFlags);
3848
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003849 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003851 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003852
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003853 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3855 ASSERT_EQ(uint32_t(0), args.policyFlags);
3856
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3859 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3860
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003861 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3863 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3864}
3865
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003866TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003867 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003868
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003869 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3870 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3871 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003872
Powei Fengd041c5d2019-05-03 17:11:33 -07003873 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003874 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003875 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003876 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003877
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003878 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003879 NotifyKeyArgs args;
3880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3881 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3882
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003883 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3885 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3886
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003887 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3889 ASSERT_EQ(uint32_t(0), args.policyFlags);
3890
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003891 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3893 ASSERT_EQ(uint32_t(0), args.policyFlags);
3894
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3897 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3898
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003899 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3901 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3902}
3903
Michael Wrightd02c5b62014-02-10 15:10:22 -08003904// --- CursorInputMapperTest ---
3905
3906class CursorInputMapperTest : public InputMapperTest {
3907protected:
3908 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3909
Michael Wright17db18e2020-06-26 20:51:44 +01003910 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003911
Chris Yea52ade12020-08-27 16:49:20 -07003912 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913 InputMapperTest::SetUp();
3914
Michael Wright17db18e2020-06-26 20:51:44 +01003915 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003916 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003917 }
3918
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003919 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3920 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003921
Michael Wrighta9cf4192022-12-01 23:46:39 +00003922 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003923 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3924 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3925 }
3926
3927 void prepareSecondaryDisplay() {
3928 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003929 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003930 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003931 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003932
3933 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3934 float pressure) {
3935 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3936 0.0f, 0.0f, 0.0f, EPSILON));
3937 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003938};
3939
3940const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3941
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003942void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3943 int32_t originalY, int32_t rotatedX,
3944 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003945 NotifyMotionArgs args;
3946
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003952 ASSERT_NO_FATAL_FAILURE(
3953 assertCursorPointerCoords(args.pointerCoords[0],
3954 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3955 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003956}
3957
3958TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003959 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00003960 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003961
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003962 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003963}
3964
3965TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003966 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00003967 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003968
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003969 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003970}
3971
3972TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003973 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00003974 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003975
3976 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003977 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003978
3979 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003980 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3981 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003982 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3983 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3984
3985 // When the bounds are set, then there should be a valid motion range.
3986 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3987
3988 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003989 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003990
3991 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3992 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3993 1, 800 - 1, 0.0f, 0.0f));
3994 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3995 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3996 2, 480 - 1, 0.0f, 0.0f));
3997 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3998 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3999 0.0f, 1.0f, 0.0f, 0.0f));
4000}
4001
4002TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004003 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004004 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004005
4006 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004007 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004008
4009 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4010 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4011 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4012 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4013 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4014 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4015 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4016 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4017 0.0f, 1.0f, 0.0f, 0.0f));
4018}
4019
4020TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004022 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004023
arthurhungdcef2dc2020-08-11 14:47:50 +08004024 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004025
4026 NotifyMotionArgs args;
4027
4028 // Button press.
4029 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004030 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4033 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4034 ASSERT_EQ(DEVICE_ID, args.deviceId);
4035 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4036 ASSERT_EQ(uint32_t(0), args.policyFlags);
4037 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4038 ASSERT_EQ(0, args.flags);
4039 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4040 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4041 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004042 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004043 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004044 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004045 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004046 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4047 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4048 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4049
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4051 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4052 ASSERT_EQ(DEVICE_ID, args.deviceId);
4053 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4054 ASSERT_EQ(uint32_t(0), args.policyFlags);
4055 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4056 ASSERT_EQ(0, args.flags);
4057 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4058 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4059 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004060 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004061 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004062 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004063 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004064 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4065 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4066 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4067
Michael Wrightd02c5b62014-02-10 15:10:22 -08004068 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004069 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4070 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4072 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4073 ASSERT_EQ(DEVICE_ID, args.deviceId);
4074 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4075 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004076 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4077 ASSERT_EQ(0, args.flags);
4078 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4079 ASSERT_EQ(0, args.buttonState);
4080 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004081 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004082 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004083 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004084 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004085 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4086 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4087 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4088
4089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4090 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4091 ASSERT_EQ(DEVICE_ID, args.deviceId);
4092 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4093 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4095 ASSERT_EQ(0, args.flags);
4096 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4097 ASSERT_EQ(0, args.buttonState);
4098 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004099 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004100 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004101 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004102 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004103 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4104 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4105 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4106}
4107
4108TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004110 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004111
4112 NotifyMotionArgs args;
4113
4114 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4116 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004119 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4120 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4121 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004122
4123 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4127 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004128 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4129 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130}
4131
4132TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004133 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004134 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004135
4136 NotifyMotionArgs args;
4137
4138 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4142 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004143 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4146 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004147 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004148
Michael Wrightd02c5b62014-02-10 15:10:22 -08004149 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004153 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004154 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004155
4156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004158 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004159}
4160
4161TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004162 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004163 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004164
4165 NotifyMotionArgs args;
4166
4167 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4173 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004174 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4175 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4176 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4179 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004180 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4181 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4182 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004183
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4189 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004190 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4191 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4192 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004193
4194 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004195 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4196 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004198 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004199 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004200
4201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004203 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004204}
4205
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004206TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004207 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004209 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4210 // need to be rotated.
4211 addConfigurationProperty("cursor.orientationAware", "1");
Arpit Singhe036ad72023-04-27 12:48:15 +00004212 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213
Michael Wrighta9cf4192022-12-01 23:46:39 +00004214 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4216 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4217 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4218 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4219 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4220 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4221 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4222 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4223}
4224
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004225TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004226 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004227 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004228 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4229 // orientation-aware are affected by display rotation.
Arpit Singhe036ad72023-04-27 12:48:15 +00004230 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004232 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004233 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004234 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4235 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4236 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4237 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4238 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4239 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4240 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4241 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4242
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004243 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004244 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004245 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4246 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4247 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4248 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4249 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4250 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4251 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4252 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004254 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004255 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004256 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4257 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4258 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4259 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4260 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4261 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4262 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4263 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4264
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004265 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004266 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004267 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4268 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4269 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4270 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4271 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4272 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4273 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4274 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275}
4276
4277TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004279 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280
4281 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4282 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283
4284 NotifyMotionArgs motionArgs;
4285 NotifyKeyArgs keyArgs;
4286
4287 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4291 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4292 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004293 ASSERT_NO_FATAL_FAILURE(
4294 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4297 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4298 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004299 ASSERT_NO_FATAL_FAILURE(
4300 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004301
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004305 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004307 ASSERT_NO_FATAL_FAILURE(
4308 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309
4310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004311 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004313 ASSERT_NO_FATAL_FAILURE(
4314 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004315
4316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004318 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004319 ASSERT_NO_FATAL_FAILURE(
4320 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321
4322 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4324 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4327 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4328 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004329 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004330 ASSERT_NO_FATAL_FAILURE(
4331 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4334 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4335 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004336 ASSERT_NO_FATAL_FAILURE(
4337 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004338
4339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4340 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4341 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004342 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004343 ASSERT_NO_FATAL_FAILURE(
4344 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004345
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004346 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004349 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004351 ASSERT_NO_FATAL_FAILURE(
4352 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004353
4354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004355 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004356 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004357 ASSERT_NO_FATAL_FAILURE(
4358 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004360 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4361 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004363 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4364 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004365 ASSERT_NO_FATAL_FAILURE(
4366 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004369
4370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004373 ASSERT_NO_FATAL_FAILURE(
4374 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004375
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4377 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004378 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004379 ASSERT_NO_FATAL_FAILURE(
4380 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381
4382 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4384 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4386 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4387 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004388
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004390 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004391 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004392 ASSERT_NO_FATAL_FAILURE(
4393 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004394
4395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4396 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4397 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004398 ASSERT_NO_FATAL_FAILURE(
4399 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4402 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004404 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004405 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004406 ASSERT_NO_FATAL_FAILURE(
4407 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004408
4409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004410 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004411 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004412
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004413 ASSERT_NO_FATAL_FAILURE(
4414 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4416 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4417 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4418
4419 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4421 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4423 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4424 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004425
Michael Wrightd02c5b62014-02-10 15:10:22 -08004426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004427 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004428 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004429 ASSERT_NO_FATAL_FAILURE(
4430 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004431
4432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4433 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4434 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004435 ASSERT_NO_FATAL_FAILURE(
4436 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004437
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004438 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004441 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004443 ASSERT_NO_FATAL_FAILURE(
4444 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004445
4446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4447 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4448 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004449 ASSERT_NO_FATAL_FAILURE(
4450 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004451
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4453 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4454 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4455
4456 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4460 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4461 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004462
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004464 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004465 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004466 ASSERT_NO_FATAL_FAILURE(
4467 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004468
4469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4470 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4471 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004472 ASSERT_NO_FATAL_FAILURE(
4473 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004478 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004480 ASSERT_NO_FATAL_FAILURE(
4481 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004482
4483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4484 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4485 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004486 ASSERT_NO_FATAL_FAILURE(
4487 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004488
Michael Wrightd02c5b62014-02-10 15:10:22 -08004489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4490 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4491 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4492
4493 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004494 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4497 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4498 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004499
Michael Wrightd02c5b62014-02-10 15:10:22 -08004500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004501 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004502 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004503 ASSERT_NO_FATAL_FAILURE(
4504 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004505
4506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4507 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4508 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004509 ASSERT_NO_FATAL_FAILURE(
4510 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004512 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4513 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004515 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004517 ASSERT_NO_FATAL_FAILURE(
4518 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004519
4520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4521 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4522 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004523 ASSERT_NO_FATAL_FAILURE(
4524 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004525
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4527 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4528 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4529}
4530
4531TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004532 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004533 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534
4535 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4536 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004537
4538 NotifyMotionArgs args;
4539
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004544 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4545 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4547 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 +00004548 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004549}
4550
4551TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004552 addConfigurationProperty("cursor.mode", "pointer");
4553 mFakePolicy->setPointerCapture(true);
Arpit Singhe036ad72023-04-27 12:48:15 +00004554 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004555
4556 NotifyDeviceResetArgs resetArgs;
4557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4558 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4559 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4560
4561 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4562 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004563
4564 NotifyMotionArgs args;
4565
4566 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4571 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4572 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4574 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 +00004575 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004576
4577 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004578 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4579 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4581 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4582 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4584 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4586 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4587 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4589 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4590
4591 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004592 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4593 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4595 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4596 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4598 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4600 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4601 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4603 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4604
4605 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004606 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4607 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4608 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4610 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4613 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 +00004614 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004615
4616 // Disable pointer capture and check that the device generation got bumped
4617 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004618 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004619 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004620 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004621 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004622
4623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004624 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4625
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4628 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4630 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004631 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4633 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 +00004634 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004635}
4636
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004637/**
4638 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4639 * pointer acceleration or speed processing should not be applied.
4640 */
4641TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4642 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004643 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4644 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004645 mFakePolicy->setVelocityControlParams(testParams);
Arpit Singhe036ad72023-04-27 12:48:15 +00004646 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004647
4648 NotifyDeviceResetArgs resetArgs;
4649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4650 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4651 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4652
4653 NotifyMotionArgs args;
4654
4655 // Move and verify scale is applied.
4656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4658 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4660 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4661 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4662 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4663 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4664 ASSERT_GT(relX, 10);
4665 ASSERT_GT(relY, 20);
4666
4667 // Enable Pointer Capture
4668 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004669 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004670 NotifyPointerCaptureChangedArgs captureArgs;
4671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4672 ASSERT_TRUE(captureArgs.request.enable);
4673
4674 // Move and verify scale is not applied.
4675 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4676 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4679 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4680 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4681 ASSERT_EQ(10, args.pointerCoords[0].getX());
4682 ASSERT_EQ(20, args.pointerCoords[0].getY());
4683}
4684
Prabir Pradhan208360b2022-06-24 18:37:04 +00004685TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4686 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004687 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan208360b2022-06-24 18:37:04 +00004688
4689 NotifyDeviceResetArgs resetArgs;
4690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4691 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4692 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4693
4694 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004695 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004696
4697 NotifyMotionArgs args;
4698
4699 // Verify that the coordinates are rotated.
4700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4701 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4702 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4704 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4705 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4706 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4707 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4708
4709 // Enable Pointer Capture.
4710 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004711 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004712 NotifyPointerCaptureChangedArgs captureArgs;
4713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4714 ASSERT_TRUE(captureArgs.request.enable);
4715
4716 // Move and verify rotation is not applied.
4717 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4718 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4719 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4721 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4722 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4723 ASSERT_EQ(10, args.pointerCoords[0].getX());
4724 ASSERT_EQ(20, args.pointerCoords[0].getY());
4725}
4726
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004727TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004728 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004729
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004730 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004731 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004732
4733 // Set up the secondary display as the display on which the pointer should be shown.
4734 // The InputDevice is not associated with any display.
4735 prepareSecondaryDisplay();
4736 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004737 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004738
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004739 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004740 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004741
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004742 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004743 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4744 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4745 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004747 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4748 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4749 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004750 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004751}
4752
4753TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004754 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004755
4756 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004757 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004758
4759 // Set up the secondary display as the display on which the pointer should be shown,
4760 // and associate the InputDevice with the secondary display.
4761 prepareSecondaryDisplay();
4762 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4763 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004764 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004765
4766 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4767 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004768
4769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4771 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004773 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4774 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4775 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004776 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004777}
4778
4779TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004780 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004781
4782 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004783 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004784 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4785
4786 // Associate the InputDevice with the secondary display.
4787 prepareSecondaryDisplay();
4788 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004789 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004790
4791 // The mapper should not generate any events because it is associated with a display that is
4792 // different from the pointer display.
4793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004797}
4798
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004799// --- BluetoothCursorInputMapperTest ---
4800
4801class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4802protected:
4803 void SetUp() override {
4804 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4805
4806 mFakePointerController = std::make_shared<FakePointerController>();
4807 mFakePolicy->setPointerController(mFakePointerController);
4808 }
4809};
4810
4811TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4812 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004813 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004814
4815 nsecs_t kernelEventTime = ARBITRARY_TIME;
4816 nsecs_t expectedEventTime = ARBITRARY_TIME;
4817 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4818 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4820 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4821 WithEventTime(expectedEventTime))));
4822
4823 // Process several events that come in quick succession, according to their timestamps.
4824 for (int i = 0; i < 3; i++) {
4825 constexpr static nsecs_t delta = ms2ns(1);
4826 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4827 kernelEventTime += delta;
4828 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4829
4830 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4831 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4833 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4834 WithEventTime(expectedEventTime))));
4835 }
4836}
4837
4838TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4839 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004840 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004841
4842 nsecs_t expectedEventTime = ARBITRARY_TIME;
4843 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4844 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4846 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4847 WithEventTime(expectedEventTime))));
4848
4849 // Process several events with the same timestamp from the kernel.
4850 // Ensure that we do not generate events too far into the future.
4851 constexpr static int32_t numEvents =
4852 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4853 for (int i = 0; i < numEvents; i++) {
4854 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4855
4856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4859 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4860 WithEventTime(expectedEventTime))));
4861 }
4862
4863 // By processing more events with the same timestamp, we should not generate events with a
4864 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4865 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4866 for (int i = 0; i < 3; i++) {
4867 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4870 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4871 WithEventTime(cappedEventTime))));
4872 }
4873}
4874
4875TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4876 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004877 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004878
4879 nsecs_t kernelEventTime = ARBITRARY_TIME;
4880 nsecs_t expectedEventTime = ARBITRARY_TIME;
4881 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4882 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4884 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4885 WithEventTime(expectedEventTime))));
4886
4887 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4888 // smoothening is not needed, its timestamp is not affected.
4889 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4890 expectedEventTime = kernelEventTime;
4891
4892 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4893 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4895 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4896 WithEventTime(expectedEventTime))));
4897}
4898
Michael Wrightd02c5b62014-02-10 15:10:22 -08004899// --- TouchInputMapperTest ---
4900
4901class TouchInputMapperTest : public InputMapperTest {
4902protected:
4903 static const int32_t RAW_X_MIN;
4904 static const int32_t RAW_X_MAX;
4905 static const int32_t RAW_Y_MIN;
4906 static const int32_t RAW_Y_MAX;
4907 static const int32_t RAW_TOUCH_MIN;
4908 static const int32_t RAW_TOUCH_MAX;
4909 static const int32_t RAW_TOOL_MIN;
4910 static const int32_t RAW_TOOL_MAX;
4911 static const int32_t RAW_PRESSURE_MIN;
4912 static const int32_t RAW_PRESSURE_MAX;
4913 static const int32_t RAW_ORIENTATION_MIN;
4914 static const int32_t RAW_ORIENTATION_MAX;
4915 static const int32_t RAW_DISTANCE_MIN;
4916 static const int32_t RAW_DISTANCE_MAX;
4917 static const int32_t RAW_TILT_MIN;
4918 static const int32_t RAW_TILT_MAX;
4919 static const int32_t RAW_ID_MIN;
4920 static const int32_t RAW_ID_MAX;
4921 static const int32_t RAW_SLOT_MIN;
4922 static const int32_t RAW_SLOT_MAX;
4923 static const float X_PRECISION;
4924 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004925 static const float X_PRECISION_VIRTUAL;
4926 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927
4928 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004929 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930
4931 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4932
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004933 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004934 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004935
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936 enum Axes {
4937 POSITION = 1 << 0,
4938 TOUCH = 1 << 1,
4939 TOOL = 1 << 2,
4940 PRESSURE = 1 << 3,
4941 ORIENTATION = 1 << 4,
4942 MINOR = 1 << 5,
4943 ID = 1 << 6,
4944 DISTANCE = 1 << 7,
4945 TILT = 1 << 8,
4946 SLOT = 1 << 9,
4947 TOOL_TYPE = 1 << 10,
4948 };
4949
Michael Wrighta9cf4192022-12-01 23:46:39 +00004950 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004951 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004952 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004953 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004954 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004955 int32_t toRawX(float displayX);
4956 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004957 int32_t toRotatedRawX(float displayX);
4958 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004959 float toCookedX(float rawX, float rawY);
4960 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004961 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004962 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004963 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004964 float toDisplayY(int32_t rawY, int32_t displayHeight);
4965
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966};
4967
4968const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4969const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4970const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4971const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4972const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4973const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4974const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4975const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004976const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4977const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004978const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4979const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4980const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4981const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4982const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4983const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4984const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4985const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4986const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4987const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4988const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4989const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004990const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4991 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4992const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4993 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004994const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4995 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004996
4997const float TouchInputMapperTest::GEOMETRIC_SCALE =
4998 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4999 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5000
5001const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5002 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5003 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5004};
5005
Michael Wrighta9cf4192022-12-01 23:46:39 +00005006void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005007 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5008 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005009}
5010
5011void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5012 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00005013 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005014}
5015
Michael Wrighta9cf4192022-12-01 23:46:39 +00005016void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005017 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5018 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5019 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005020}
5021
Michael Wrightd02c5b62014-02-10 15:10:22 -08005022void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005023 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5024 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5025 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5026 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005027}
5028
Jason Gerecke489fda82012-09-07 17:19:40 -07005029void TouchInputMapperTest::prepareLocationCalibration() {
5030 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5031}
5032
Michael Wrightd02c5b62014-02-10 15:10:22 -08005033int32_t TouchInputMapperTest::toRawX(float displayX) {
5034 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5035}
5036
5037int32_t TouchInputMapperTest::toRawY(float displayY) {
5038 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5039}
5040
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005041int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5042 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5043}
5044
5045int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5046 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5047}
5048
Jason Gerecke489fda82012-09-07 17:19:40 -07005049float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5050 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5051 return rawX;
5052}
5053
5054float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5055 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5056 return rawY;
5057}
5058
Michael Wrightd02c5b62014-02-10 15:10:22 -08005059float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005060 return toDisplayX(rawX, DISPLAY_WIDTH);
5061}
5062
5063float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5064 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005065}
5066
5067float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005068 return toDisplayY(rawY, DISPLAY_HEIGHT);
5069}
5070
5071float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5072 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005073}
5074
5075
5076// --- SingleTouchInputMapperTest ---
5077
5078class SingleTouchInputMapperTest : public TouchInputMapperTest {
5079protected:
5080 void prepareButtons();
5081 void prepareAxes(int axes);
5082
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005083 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5084 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5085 void processUp(SingleTouchInputMapper& mappery);
5086 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5087 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5088 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5089 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5090 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5091 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005092};
5093
5094void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005095 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005096}
5097
5098void SingleTouchInputMapperTest::prepareAxes(int axes) {
5099 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005100 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5101 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005102 }
5103 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005104 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5105 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005106 }
5107 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005108 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5109 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110 }
5111 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005112 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5113 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005114 }
5115 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005116 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5117 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118 }
5119}
5120
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005121void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005125}
5126
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005127void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005128 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5129 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130}
5131
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005132void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005134}
5135
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005136void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005138}
5139
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005140void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5141 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005142 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005143}
5144
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005145void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005147}
5148
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005149void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5150 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005153}
5154
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005155void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5156 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005158}
5159
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005160void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005162}
5163
Michael Wrightd02c5b62014-02-10 15:10:22 -08005164TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005165 prepareButtons();
5166 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005167 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005168
Josep del Río2d8c79a2023-01-23 19:33:50 +00005169 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005170}
5171
Michael Wrightd02c5b62014-02-10 15:10:22 -08005172TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005173 prepareButtons();
5174 prepareAxes(POSITION);
5175 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00005176 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005177
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005178 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005179}
5180
5181TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005182 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005183 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005184 prepareButtons();
5185 prepareAxes(POSITION);
5186 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005187 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005188
5189 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005190 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005191
5192 // Virtual key is down.
5193 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5194 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5195 processDown(mapper, x, y);
5196 processSync(mapper);
5197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5198
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005199 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005200
5201 // Virtual key is up.
5202 processUp(mapper);
5203 processSync(mapper);
5204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5205
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005206 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005207}
5208
5209TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005210 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005211 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005212 prepareButtons();
5213 prepareAxes(POSITION);
5214 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005215 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005216
5217 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005218 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005219
5220 // Virtual key is down.
5221 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5222 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5223 processDown(mapper, x, y);
5224 processSync(mapper);
5225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5226
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005227 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005228
5229 // Virtual key is up.
5230 processUp(mapper);
5231 processSync(mapper);
5232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5233
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005234 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005235}
5236
5237TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005238 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005239 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005240 prepareButtons();
5241 prepareAxes(POSITION);
5242 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005243 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005244
Michael Wrightd02c5b62014-02-10 15:10:22 -08005245 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005246 ASSERT_TRUE(
5247 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005248 ASSERT_TRUE(flags[0]);
5249 ASSERT_FALSE(flags[1]);
5250}
5251
5252TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005253 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005254 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005255 prepareButtons();
5256 prepareAxes(POSITION);
5257 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005258 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005259
arthurhungdcef2dc2020-08-11 14:47:50 +08005260 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005261
5262 NotifyKeyArgs args;
5263
5264 // Press virtual key.
5265 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5266 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5267 processDown(mapper, x, y);
5268 processSync(mapper);
5269
5270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5271 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5272 ASSERT_EQ(DEVICE_ID, args.deviceId);
5273 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5274 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5275 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5276 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5277 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5278 ASSERT_EQ(KEY_HOME, args.scanCode);
5279 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5280 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5281
5282 // Release virtual key.
5283 processUp(mapper);
5284 processSync(mapper);
5285
5286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5287 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5288 ASSERT_EQ(DEVICE_ID, args.deviceId);
5289 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5290 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5291 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5292 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5293 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5294 ASSERT_EQ(KEY_HOME, args.scanCode);
5295 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5296 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5297
5298 // Should not have sent any motions.
5299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5300}
5301
5302TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005303 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005304 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005305 prepareButtons();
5306 prepareAxes(POSITION);
5307 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005308 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005309
arthurhungdcef2dc2020-08-11 14:47:50 +08005310 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005311
5312 NotifyKeyArgs keyArgs;
5313
5314 // Press virtual key.
5315 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5316 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5317 processDown(mapper, x, y);
5318 processSync(mapper);
5319
5320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5321 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5322 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5323 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5324 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5325 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5326 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5327 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5328 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5329 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5330 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5331
5332 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5333 // into the display area.
5334 y -= 100;
5335 processMove(mapper, x, y);
5336 processSync(mapper);
5337
5338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5339 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5340 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5341 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5342 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5343 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5344 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5345 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5346 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5347 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5348 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5349 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5350
5351 NotifyMotionArgs motionArgs;
5352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5353 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5354 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5355 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5356 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5357 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5358 ASSERT_EQ(0, motionArgs.flags);
5359 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5360 ASSERT_EQ(0, motionArgs.buttonState);
5361 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005362 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005363 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005364 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5366 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5367 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5368 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5369 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5370
5371 // Keep moving out of bounds. Should generate a pointer move.
5372 y -= 50;
5373 processMove(mapper, x, y);
5374 processSync(mapper);
5375
5376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5377 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5378 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5379 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5380 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5382 ASSERT_EQ(0, motionArgs.flags);
5383 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5384 ASSERT_EQ(0, motionArgs.buttonState);
5385 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005386 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005387 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005388 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5390 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5391 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5392 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5393 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5394
5395 // Release out of bounds. Should generate a pointer up.
5396 processUp(mapper);
5397 processSync(mapper);
5398
5399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5400 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5401 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5402 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5403 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5404 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5405 ASSERT_EQ(0, motionArgs.flags);
5406 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5407 ASSERT_EQ(0, motionArgs.buttonState);
5408 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005409 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005410 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005411 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5413 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5414 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5415 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5416 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5417
5418 // Should not have sent any more keys or motions.
5419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5421}
5422
5423TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005424 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005425 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005426 prepareButtons();
5427 prepareAxes(POSITION);
5428 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005429 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005430
arthurhungdcef2dc2020-08-11 14:47:50 +08005431 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005432
5433 NotifyMotionArgs motionArgs;
5434
5435 // Initially go down out of bounds.
5436 int32_t x = -10;
5437 int32_t y = -10;
5438 processDown(mapper, x, y);
5439 processSync(mapper);
5440
5441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5442
5443 // Move into the display area. Should generate a pointer down.
5444 x = 50;
5445 y = 75;
5446 processMove(mapper, x, y);
5447 processSync(mapper);
5448
5449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5450 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5451 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5452 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5453 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5454 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5455 ASSERT_EQ(0, motionArgs.flags);
5456 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5457 ASSERT_EQ(0, motionArgs.buttonState);
5458 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005459 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005460 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005461 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5463 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5464 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5465 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5466 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5467
5468 // Release. Should generate a pointer up.
5469 processUp(mapper);
5470 processSync(mapper);
5471
5472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5473 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5474 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5475 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5476 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5477 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5478 ASSERT_EQ(0, motionArgs.flags);
5479 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5480 ASSERT_EQ(0, motionArgs.buttonState);
5481 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005482 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005483 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005484 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5486 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5487 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5488 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5489 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5490
5491 // Should not have sent any more keys or motions.
5492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5494}
5495
Santos Cordonfa5cf462017-04-05 10:37:00 -07005496TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005497 addConfigurationProperty("touch.deviceType", "touchScreen");
5498 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5499
Michael Wrighta9cf4192022-12-01 23:46:39 +00005500 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005501 prepareButtons();
5502 prepareAxes(POSITION);
5503 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005504 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005505
arthurhungdcef2dc2020-08-11 14:47:50 +08005506 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005507
5508 NotifyMotionArgs motionArgs;
5509
5510 // Down.
5511 int32_t x = 100;
5512 int32_t y = 125;
5513 processDown(mapper, x, y);
5514 processSync(mapper);
5515
5516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5517 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5518 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5519 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5520 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5521 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5522 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5523 ASSERT_EQ(0, motionArgs.flags);
5524 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5525 ASSERT_EQ(0, motionArgs.buttonState);
5526 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005527 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005528 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005529 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5531 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5532 1, 0, 0, 0, 0, 0, 0, 0));
5533 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5534 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5535 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5536
5537 // Move.
5538 x += 50;
5539 y += 75;
5540 processMove(mapper, x, y);
5541 processSync(mapper);
5542
5543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5544 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5545 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5546 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5547 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5548 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5550 ASSERT_EQ(0, motionArgs.flags);
5551 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5552 ASSERT_EQ(0, motionArgs.buttonState);
5553 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005554 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005555 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005556 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5558 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5559 1, 0, 0, 0, 0, 0, 0, 0));
5560 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5561 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5562 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5563
5564 // Up.
5565 processUp(mapper);
5566 processSync(mapper);
5567
5568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5569 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5570 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5571 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5572 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5573 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5574 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5575 ASSERT_EQ(0, motionArgs.flags);
5576 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5577 ASSERT_EQ(0, motionArgs.buttonState);
5578 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005579 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005580 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005581 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5583 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5584 1, 0, 0, 0, 0, 0, 0, 0));
5585 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5586 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5587 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5588
5589 // Should not have sent any more keys or motions.
5590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5592}
5593
Michael Wrightd02c5b62014-02-10 15:10:22 -08005594TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005595 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005596 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005597 prepareButtons();
5598 prepareAxes(POSITION);
5599 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005600 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005601
arthurhungdcef2dc2020-08-11 14:47:50 +08005602 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005603
5604 NotifyMotionArgs motionArgs;
5605
5606 // Down.
5607 int32_t x = 100;
5608 int32_t y = 125;
5609 processDown(mapper, x, y);
5610 processSync(mapper);
5611
5612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5613 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5614 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5615 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5616 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5617 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5618 ASSERT_EQ(0, motionArgs.flags);
5619 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5620 ASSERT_EQ(0, motionArgs.buttonState);
5621 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005622 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005623 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005624 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5626 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5627 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5628 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5629 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5630
5631 // Move.
5632 x += 50;
5633 y += 75;
5634 processMove(mapper, x, y);
5635 processSync(mapper);
5636
5637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5638 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5639 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5640 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5641 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5642 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5643 ASSERT_EQ(0, motionArgs.flags);
5644 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5645 ASSERT_EQ(0, motionArgs.buttonState);
5646 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005647 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005648 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005649 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5651 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5652 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5653 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5654 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5655
5656 // Up.
5657 processUp(mapper);
5658 processSync(mapper);
5659
5660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5661 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5662 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5663 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5664 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5665 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5666 ASSERT_EQ(0, motionArgs.flags);
5667 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5668 ASSERT_EQ(0, motionArgs.buttonState);
5669 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005670 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005671 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005672 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005673 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5674 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5675 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5676 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5677 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5678
5679 // Should not have sent any more keys or motions.
5680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5682}
5683
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005684TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005685 addConfigurationProperty("touch.deviceType", "touchScreen");
5686 prepareButtons();
5687 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005688 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5689 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005690 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005691
5692 NotifyMotionArgs args;
5693
5694 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005695 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005696 processDown(mapper, toRawX(50), toRawY(75));
5697 processSync(mapper);
5698
5699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5700 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5701 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5702
5703 processUp(mapper);
5704 processSync(mapper);
5705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5706}
5707
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005708TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005709 addConfigurationProperty("touch.deviceType", "touchScreen");
5710 prepareButtons();
5711 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005712 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5713 // orientation-aware are affected by display rotation.
5714 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005715 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005716
5717 NotifyMotionArgs args;
5718
5719 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005720 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005721 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005722 processDown(mapper, toRawX(50), toRawY(75));
5723 processSync(mapper);
5724
5725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5726 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5727 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5728
5729 processUp(mapper);
5730 processSync(mapper);
5731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5732
5733 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005734 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005735 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005736 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005737 processSync(mapper);
5738
5739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5740 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5741 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5742
5743 processUp(mapper);
5744 processSync(mapper);
5745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5746
5747 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005748 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005749 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005750 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5751 processSync(mapper);
5752
5753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5754 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5755 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5756
5757 processUp(mapper);
5758 processSync(mapper);
5759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5760
5761 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005762 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005763 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005764 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005765 processSync(mapper);
5766
5767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5768 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5769 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5770
5771 processUp(mapper);
5772 processSync(mapper);
5773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5774}
5775
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005776TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5777 addConfigurationProperty("touch.deviceType", "touchScreen");
5778 prepareButtons();
5779 prepareAxes(POSITION);
5780 addConfigurationProperty("touch.orientationAware", "1");
5781 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5782 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005783 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005784 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005785 NotifyMotionArgs args;
5786
5787 // Orientation 0.
5788 processDown(mapper, toRawX(50), toRawY(75));
5789 processSync(mapper);
5790
5791 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5792 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5793 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5794
5795 processUp(mapper);
5796 processSync(mapper);
5797 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5798}
5799
5800TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5801 addConfigurationProperty("touch.deviceType", "touchScreen");
5802 prepareButtons();
5803 prepareAxes(POSITION);
5804 addConfigurationProperty("touch.orientationAware", "1");
5805 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5806 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005807 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005808 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005809 NotifyMotionArgs args;
5810
5811 // Orientation 90.
5812 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5813 processSync(mapper);
5814
5815 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5816 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5817 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5818
5819 processUp(mapper);
5820 processSync(mapper);
5821 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5822}
5823
5824TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5825 addConfigurationProperty("touch.deviceType", "touchScreen");
5826 prepareButtons();
5827 prepareAxes(POSITION);
5828 addConfigurationProperty("touch.orientationAware", "1");
5829 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5830 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005831 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005832 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005833 NotifyMotionArgs args;
5834
5835 // Orientation 180.
5836 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5837 processSync(mapper);
5838
5839 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5840 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5841 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5842
5843 processUp(mapper);
5844 processSync(mapper);
5845 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5846}
5847
5848TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5849 addConfigurationProperty("touch.deviceType", "touchScreen");
5850 prepareButtons();
5851 prepareAxes(POSITION);
5852 addConfigurationProperty("touch.orientationAware", "1");
5853 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5854 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005855 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005856 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005857 NotifyMotionArgs args;
5858
5859 // Orientation 270.
5860 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5861 processSync(mapper);
5862
5863 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5864 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5865 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5866
5867 processUp(mapper);
5868 processSync(mapper);
5869 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5870}
5871
5872TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5873 addConfigurationProperty("touch.deviceType", "touchScreen");
5874 prepareButtons();
5875 prepareAxes(POSITION);
5876 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5877 // orientation-aware are affected by display rotation.
5878 addConfigurationProperty("touch.orientationAware", "0");
5879 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00005880 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005881
5882 NotifyMotionArgs args;
5883
5884 // Orientation 90, Rotation 0.
5885 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005886 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005887 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5888 processSync(mapper);
5889
5890 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5891 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5892 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5893
5894 processUp(mapper);
5895 processSync(mapper);
5896 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5897
5898 // Orientation 90, Rotation 90.
5899 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005900 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005901 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005902 processSync(mapper);
5903
5904 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5905 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5906 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5907
5908 processUp(mapper);
5909 processSync(mapper);
5910 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5911
5912 // Orientation 90, Rotation 180.
5913 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005914 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005915 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5916 processSync(mapper);
5917
5918 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5919 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5920 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5921
5922 processUp(mapper);
5923 processSync(mapper);
5924 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5925
5926 // Orientation 90, Rotation 270.
5927 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005928 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005929 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 -07005930 processSync(mapper);
5931
5932 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5933 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5934 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5935
5936 processUp(mapper);
5937 processSync(mapper);
5938 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5939}
5940
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005941TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5942 addConfigurationProperty("touch.deviceType", "touchScreen");
5943 prepareButtons();
5944 prepareAxes(POSITION);
5945 addConfigurationProperty("touch.orientationAware", "1");
5946 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005947 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005948
5949 // Set a physical frame in the display viewport.
5950 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5951 viewport->physicalLeft = 20;
5952 viewport->physicalTop = 600;
5953 viewport->physicalRight = 30;
5954 viewport->physicalBottom = 610;
5955 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005956 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005957
5958 // Start the touch.
5959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5960 processSync(mapper);
5961
5962 // Expect all input starting outside the physical frame to be ignored.
5963 const std::array<Point, 6> outsidePoints = {
5964 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5965 for (const auto& p : outsidePoints) {
5966 processMove(mapper, toRawX(p.x), toRawY(p.y));
5967 processSync(mapper);
5968 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5969 }
5970
5971 // Move the touch into the physical frame.
5972 processMove(mapper, toRawX(25), toRawY(605));
5973 processSync(mapper);
5974 NotifyMotionArgs args;
5975 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5976 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5977 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5978 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5979
5980 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5981 for (const auto& p : outsidePoints) {
5982 processMove(mapper, toRawX(p.x), toRawY(p.y));
5983 processSync(mapper);
5984 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5985 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5986 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5987 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5988 }
5989
5990 processUp(mapper);
5991 processSync(mapper);
5992 EXPECT_NO_FATAL_FAILURE(
5993 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5994}
5995
Harry Cutts1db43992023-06-19 17:05:07 +00005996TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
5997 std::shared_ptr<FakePointerController> fakePointerController =
5998 std::make_shared<FakePointerController>();
5999 mFakePolicy->setPointerController(fakePointerController);
6000
6001 addConfigurationProperty("touch.deviceType", "pointer");
6002 prepareAxes(POSITION);
6003 prepareDisplay(ui::ROTATION_0);
6004 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
6005
6006 // Set a physical frame in the display viewport.
6007 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6008 viewport->physicalLeft = 20;
6009 viewport->physicalTop = 600;
6010 viewport->physicalRight = 30;
6011 viewport->physicalBottom = 610;
6012 mFakePolicy->updateViewport(*viewport);
6013 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
6014
6015 // Start the touch.
6016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6017 processSync(mapper);
6018
6019 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
6020 // produced.
6021 const std::array<Point, 6> outsidePoints = {
6022 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6023 for (const auto& p : outsidePoints) {
6024 processMove(mapper, toRawX(p.x), toRawY(p.y));
6025 processSync(mapper);
6026 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6027 }
6028}
6029
Michael Wrightd02c5b62014-02-10 15:10:22 -08006030TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006031 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006032 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006033 prepareButtons();
6034 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00006035 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006036
6037 // These calculations are based on the input device calibration documentation.
6038 int32_t rawX = 100;
6039 int32_t rawY = 200;
6040 int32_t rawPressure = 10;
6041 int32_t rawToolMajor = 12;
6042 int32_t rawDistance = 2;
6043 int32_t rawTiltX = 30;
6044 int32_t rawTiltY = 110;
6045
6046 float x = toDisplayX(rawX);
6047 float y = toDisplayY(rawY);
6048 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6049 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6050 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6051 float distance = float(rawDistance);
6052
6053 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6054 float tiltScale = M_PI / 180;
6055 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6056 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6057 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6058 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6059
6060 processDown(mapper, rawX, rawY);
6061 processPressure(mapper, rawPressure);
6062 processToolMajor(mapper, rawToolMajor);
6063 processDistance(mapper, rawDistance);
6064 processTilt(mapper, rawTiltX, rawTiltY);
6065 processSync(mapper);
6066
6067 NotifyMotionArgs args;
6068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6069 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6070 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6071 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6072}
6073
Jason Gerecke489fda82012-09-07 17:19:40 -07006074TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006075 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006076 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07006077 prepareLocationCalibration();
6078 prepareButtons();
6079 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006080 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006081
6082 int32_t rawX = 100;
6083 int32_t rawY = 200;
6084
6085 float x = toDisplayX(toCookedX(rawX, rawY));
6086 float y = toDisplayY(toCookedY(rawX, rawY));
6087
6088 processDown(mapper, rawX, rawY);
6089 processSync(mapper);
6090
6091 NotifyMotionArgs args;
6092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6094 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6095}
6096
Michael Wrightd02c5b62014-02-10 15:10:22 -08006097TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006098 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006099 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006100 prepareButtons();
6101 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006102 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006103
6104 NotifyMotionArgs motionArgs;
6105 NotifyKeyArgs keyArgs;
6106
6107 processDown(mapper, 100, 200);
6108 processSync(mapper);
6109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6110 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6111 ASSERT_EQ(0, motionArgs.buttonState);
6112
6113 // press BTN_LEFT, release BTN_LEFT
6114 processKey(mapper, BTN_LEFT, 1);
6115 processSync(mapper);
6116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6118 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6119
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6121 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6122 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6123
Michael Wrightd02c5b62014-02-10 15:10:22 -08006124 processKey(mapper, BTN_LEFT, 0);
6125 processSync(mapper);
6126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006127 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006128 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006129
6130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006132 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006133
6134 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6135 processKey(mapper, BTN_RIGHT, 1);
6136 processKey(mapper, BTN_MIDDLE, 1);
6137 processSync(mapper);
6138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6140 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6141 motionArgs.buttonState);
6142
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6144 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6145 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6146
6147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6148 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6149 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6150 motionArgs.buttonState);
6151
Michael Wrightd02c5b62014-02-10 15:10:22 -08006152 processKey(mapper, BTN_RIGHT, 0);
6153 processSync(mapper);
6154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006155 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006156 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006157
6158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006159 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006160 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006161
6162 processKey(mapper, BTN_MIDDLE, 0);
6163 processSync(mapper);
6164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006165 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006166 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006167
6168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006169 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006170 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006171
6172 // press BTN_BACK, release BTN_BACK
6173 processKey(mapper, BTN_BACK, 1);
6174 processSync(mapper);
6175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6176 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6177 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006178
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006181 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6182
6183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6184 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6185 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006186
6187 processKey(mapper, BTN_BACK, 0);
6188 processSync(mapper);
6189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006190 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006191 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006192
6193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006195 ASSERT_EQ(0, motionArgs.buttonState);
6196
Michael Wrightd02c5b62014-02-10 15:10:22 -08006197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6198 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6199 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6200
6201 // press BTN_SIDE, release BTN_SIDE
6202 processKey(mapper, BTN_SIDE, 1);
6203 processSync(mapper);
6204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6205 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6206 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006207
Michael Wrightd02c5b62014-02-10 15:10:22 -08006208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006209 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006210 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6211
6212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6213 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6214 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006215
6216 processKey(mapper, BTN_SIDE, 0);
6217 processSync(mapper);
6218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006219 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006220 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006221
6222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006223 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006224 ASSERT_EQ(0, motionArgs.buttonState);
6225
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6227 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6228 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6229
6230 // press BTN_FORWARD, release BTN_FORWARD
6231 processKey(mapper, BTN_FORWARD, 1);
6232 processSync(mapper);
6233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6234 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6235 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006236
Michael Wrightd02c5b62014-02-10 15:10:22 -08006237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006238 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006239 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6240
6241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6242 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6243 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006244
6245 processKey(mapper, BTN_FORWARD, 0);
6246 processSync(mapper);
6247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006248 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006249 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006250
6251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006253 ASSERT_EQ(0, motionArgs.buttonState);
6254
Michael Wrightd02c5b62014-02-10 15:10:22 -08006255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6256 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6257 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6258
6259 // press BTN_EXTRA, release BTN_EXTRA
6260 processKey(mapper, BTN_EXTRA, 1);
6261 processSync(mapper);
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6263 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6264 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006265
Michael Wrightd02c5b62014-02-10 15:10:22 -08006266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006268 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6269
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6271 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6272 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273
6274 processKey(mapper, BTN_EXTRA, 0);
6275 processSync(mapper);
6276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006277 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006278 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006279
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006281 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006282 ASSERT_EQ(0, motionArgs.buttonState);
6283
Michael Wrightd02c5b62014-02-10 15:10:22 -08006284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6285 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6286 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6287
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6289
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290 // press BTN_STYLUS, release BTN_STYLUS
6291 processKey(mapper, BTN_STYLUS, 1);
6292 processSync(mapper);
6293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6294 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006295 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6296
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6298 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6299 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006300
6301 processKey(mapper, BTN_STYLUS, 0);
6302 processSync(mapper);
6303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006304 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006305 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006306
6307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006309 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006310
6311 // press BTN_STYLUS2, release BTN_STYLUS2
6312 processKey(mapper, BTN_STYLUS2, 1);
6313 processSync(mapper);
6314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6315 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006316 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6317
6318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6319 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6320 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006321
6322 processKey(mapper, BTN_STYLUS2, 0);
6323 processSync(mapper);
6324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006325 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006326 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006327
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006330 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331
6332 // release touch
6333 processUp(mapper);
6334 processSync(mapper);
6335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6336 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6337 ASSERT_EQ(0, motionArgs.buttonState);
6338}
6339
6340TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006341 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006342 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006343 prepareButtons();
6344 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006345 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006346
6347 NotifyMotionArgs motionArgs;
6348
6349 // default tool type is finger
6350 processDown(mapper, 100, 200);
6351 processSync(mapper);
6352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6353 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006354 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006355
6356 // eraser
6357 processKey(mapper, BTN_TOOL_RUBBER, 1);
6358 processSync(mapper);
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006361 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006362
6363 // stylus
6364 processKey(mapper, BTN_TOOL_RUBBER, 0);
6365 processKey(mapper, BTN_TOOL_PEN, 1);
6366 processSync(mapper);
6367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006369 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006370
6371 // brush
6372 processKey(mapper, BTN_TOOL_PEN, 0);
6373 processKey(mapper, BTN_TOOL_BRUSH, 1);
6374 processSync(mapper);
6375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6376 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006377 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006378
6379 // pencil
6380 processKey(mapper, BTN_TOOL_BRUSH, 0);
6381 processKey(mapper, BTN_TOOL_PENCIL, 1);
6382 processSync(mapper);
6383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006385 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006387 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006388 processKey(mapper, BTN_TOOL_PENCIL, 0);
6389 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6390 processSync(mapper);
6391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006393 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006394
6395 // mouse
6396 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6397 processKey(mapper, BTN_TOOL_MOUSE, 1);
6398 processSync(mapper);
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006401 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006402
6403 // lens
6404 processKey(mapper, BTN_TOOL_MOUSE, 0);
6405 processKey(mapper, BTN_TOOL_LENS, 1);
6406 processSync(mapper);
6407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006409 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006410
6411 // double-tap
6412 processKey(mapper, BTN_TOOL_LENS, 0);
6413 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6414 processSync(mapper);
6415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006417 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006418
6419 // triple-tap
6420 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6421 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6422 processSync(mapper);
6423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6424 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006425 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006426
6427 // quad-tap
6428 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6429 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6430 processSync(mapper);
6431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6432 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006433 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006434
6435 // finger
6436 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6437 processKey(mapper, BTN_TOOL_FINGER, 1);
6438 processSync(mapper);
6439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006441 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006442
6443 // stylus trumps finger
6444 processKey(mapper, BTN_TOOL_PEN, 1);
6445 processSync(mapper);
6446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006448 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006449
6450 // eraser trumps stylus
6451 processKey(mapper, BTN_TOOL_RUBBER, 1);
6452 processSync(mapper);
6453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6454 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006455 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006456
6457 // mouse trumps eraser
6458 processKey(mapper, BTN_TOOL_MOUSE, 1);
6459 processSync(mapper);
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6461 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006462 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006463
6464 // back to default tool type
6465 processKey(mapper, BTN_TOOL_MOUSE, 0);
6466 processKey(mapper, BTN_TOOL_RUBBER, 0);
6467 processKey(mapper, BTN_TOOL_PEN, 0);
6468 processKey(mapper, BTN_TOOL_FINGER, 0);
6469 processSync(mapper);
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006472 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006473}
6474
6475TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006476 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006477 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006478 prepareButtons();
6479 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006480 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006481 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006482
6483 NotifyMotionArgs motionArgs;
6484
6485 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6486 processKey(mapper, BTN_TOOL_FINGER, 1);
6487 processMove(mapper, 100, 200);
6488 processSync(mapper);
6489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6490 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6491 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6492 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6493
6494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6495 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6497 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6498
6499 // move a little
6500 processMove(mapper, 150, 250);
6501 processSync(mapper);
6502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6503 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6504 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6505 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6506
6507 // down when BTN_TOUCH is pressed, pressure defaults to 1
6508 processKey(mapper, BTN_TOUCH, 1);
6509 processSync(mapper);
6510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6511 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6513 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6514
6515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6516 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6518 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6519
6520 // up when BTN_TOUCH is released, hover restored
6521 processKey(mapper, BTN_TOUCH, 0);
6522 processSync(mapper);
6523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6524 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6526 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6527
6528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6529 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6531 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6532
6533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6534 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6536 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6537
6538 // exit hover when pointer goes away
6539 processKey(mapper, BTN_TOOL_FINGER, 0);
6540 processSync(mapper);
6541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6542 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6544 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6545}
6546
6547TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006548 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006549 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006550 prepareButtons();
6551 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006552 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006553
6554 NotifyMotionArgs motionArgs;
6555
6556 // initially hovering because pressure is 0
6557 processDown(mapper, 100, 200);
6558 processPressure(mapper, 0);
6559 processSync(mapper);
6560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6561 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6563 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6564
6565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6566 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6567 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6568 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6569
6570 // move a little
6571 processMove(mapper, 150, 250);
6572 processSync(mapper);
6573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6574 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6576 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6577
6578 // down when pressure is non-zero
6579 processPressure(mapper, RAW_PRESSURE_MAX);
6580 processSync(mapper);
6581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6582 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6584 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6585
6586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6587 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6589 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6590
6591 // up when pressure becomes 0, hover restored
6592 processPressure(mapper, 0);
6593 processSync(mapper);
6594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6595 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6597 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6598
6599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6600 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6602 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6603
6604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6605 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6607 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6608
6609 // exit hover when pointer goes away
6610 processUp(mapper);
6611 processSync(mapper);
6612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6613 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6615 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6616}
6617
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006618TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6619 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006620 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006621 prepareButtons();
6622 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006623 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006624
6625 // Touch down.
6626 processDown(mapper, 100, 200);
6627 processPressure(mapper, 1);
6628 processSync(mapper);
6629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6630 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6631
6632 // Reset the mapper. This should cancel the ongoing gesture.
6633 resetMapper(mapper, ARBITRARY_TIME);
6634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6635 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6636
6637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6638}
6639
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006640TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6641 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006642 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006643 prepareButtons();
6644 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006645 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006646
6647 // Set the initial state for the touch pointer.
6648 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6649 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6650 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6651 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6652
6653 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006654 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6655 // does not generate any events.
6656 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006657
6658 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6659 // the recreated touch state to generate a down event.
6660 processSync(mapper);
6661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6662 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6663
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6665}
6666
lilinnan687e58f2022-07-19 16:00:50 +08006667TEST_F(SingleTouchInputMapperTest,
6668 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6669 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006670 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006671 prepareButtons();
6672 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006673 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006674 NotifyMotionArgs motionArgs;
6675
6676 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006677 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006678 processSync(mapper);
6679
6680 // We should receive a down event
6681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6682 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6683
6684 // Change display id
6685 clearViewports();
6686 prepareSecondaryDisplay(ViewportType::INTERNAL);
6687
6688 // We should receive a cancel event
6689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6690 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6691 // Then receive reset called
6692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6693}
6694
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006695TEST_F(SingleTouchInputMapperTest,
6696 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6697 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006698 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006699 prepareButtons();
6700 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006701 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6703 NotifyMotionArgs motionArgs;
6704
6705 // Start a new gesture.
6706 processDown(mapper, 100, 200);
6707 processSync(mapper);
6708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6709 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6710
6711 // Make the viewport inactive. This will put the device in disabled mode.
6712 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6713 viewport->isActive = false;
6714 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006715 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006716
6717 // We should receive a cancel event for the ongoing gesture.
6718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6719 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6720 // Then we should be notified that the device was reset.
6721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6722
6723 // No events are generated while the viewport is inactive.
6724 processMove(mapper, 101, 201);
6725 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006726 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006727 processSync(mapper);
6728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6729
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006730 // Start a new gesture while the viewport is still inactive.
6731 processDown(mapper, 300, 400);
6732 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6733 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6734 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6735 processSync(mapper);
6736
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006737 // Make the viewport active again. The device should resume processing events.
6738 viewport->isActive = true;
6739 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006740 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006741
6742 // The device is reset because it changes back to direct mode, without generating any events.
6743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6745
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006746 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006747 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6749 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006750
6751 // No more events.
6752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6754}
6755
Prabir Pradhan211ba622022-10-31 21:09:21 +00006756TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6757 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006758 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006759 prepareButtons();
6760 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006761 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6763
6764 // Press a stylus button.
6765 processKey(mapper, BTN_STYLUS, 1);
6766 processSync(mapper);
6767
6768 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6769 processDown(mapper, 100, 200);
6770 processSync(mapper);
6771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6772 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6773 WithCoords(toDisplayX(100), toDisplayY(200)),
6774 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6776 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6777 WithCoords(toDisplayX(100), toDisplayY(200)),
6778 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6779
6780 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6781 // the button has not actually been released, since there will be no pointers through which the
6782 // button state can be reported. The event is generated at the location of the pointer before
6783 // it went up.
6784 processUp(mapper);
6785 processSync(mapper);
6786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6787 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6788 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6790 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6791 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6792}
6793
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006794TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6795 addConfigurationProperty("touch.deviceType", "touchScreen");
6796 prepareDisplay(ui::ROTATION_0);
6797 prepareButtons();
6798 prepareAxes(POSITION);
6799
6800 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6801
Arpit Singha8c236b2023-04-25 13:56:05 +00006802 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6804
6805 // Press a stylus button.
6806 processKey(mapper, BTN_STYLUS, 1);
6807 processSync(mapper);
6808
6809 // Start a touch gesture and ensure that the stylus button is not reported.
6810 processDown(mapper, 100, 200);
6811 processSync(mapper);
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6813 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6814
6815 // Release and press the stylus button again.
6816 processKey(mapper, BTN_STYLUS, 0);
6817 processSync(mapper);
6818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6819 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6820 processKey(mapper, BTN_STYLUS, 1);
6821 processSync(mapper);
6822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6823 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6824
6825 // Release the touch gesture.
6826 processUp(mapper);
6827 processSync(mapper);
6828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6829 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6830
6831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6832}
6833
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006834TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6835 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6836 prepareDisplay(ui::ROTATION_0);
6837 prepareButtons();
6838 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006839 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6841
6842 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6843}
6844
Seunghwan Choi356026c2023-02-01 14:37:25 +09006845TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6846 std::shared_ptr<FakePointerController> fakePointerController =
6847 std::make_shared<FakePointerController>();
6848 addConfigurationProperty("touch.deviceType", "touchScreen");
6849 prepareDisplay(ui::ROTATION_0);
6850 prepareButtons();
6851 prepareAxes(POSITION);
6852 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6853 mFakePolicy->setPointerController(fakePointerController);
6854 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00006855 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006856
6857 processKey(mapper, BTN_TOOL_PEN, 1);
6858 processMove(mapper, 100, 200);
6859 processSync(mapper);
6860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6861 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006862 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006863 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6864 ASSERT_TRUE(fakePointerController->isPointerShown());
6865 ASSERT_NO_FATAL_FAILURE(
6866 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6867}
6868
6869TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6870 std::shared_ptr<FakePointerController> fakePointerController =
6871 std::make_shared<FakePointerController>();
6872 addConfigurationProperty("touch.deviceType", "touchScreen");
6873 prepareDisplay(ui::ROTATION_0);
6874 prepareButtons();
6875 prepareAxes(POSITION);
6876 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6877 mFakePolicy->setPointerController(fakePointerController);
6878 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00006879 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006880
6881 processKey(mapper, BTN_TOOL_PEN, 1);
6882 processMove(mapper, 100, 200);
6883 processSync(mapper);
6884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6885 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006886 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006887 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6888 ASSERT_FALSE(fakePointerController->isPointerShown());
6889}
6890
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006891TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6892 // Initialize the device without setting device source to touch navigation.
6893 addConfigurationProperty("touch.deviceType", "touchScreen");
6894 prepareDisplay(ui::ROTATION_0);
6895 prepareButtons();
6896 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006897 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006898
6899 // Ensure that the device is created as a touchscreen, not touch navigation.
6900 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6901
6902 // Add device type association after the device was created.
6903 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6904
6905 // Send update to the mapper.
6906 std::list<NotifyArgs> unused2 =
6907 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006908 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006909
6910 // Check whether device type update was successful.
6911 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6912}
6913
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006914TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6915 // Initialize the device without setting device source to touch navigation.
6916 addConfigurationProperty("touch.deviceType", "touchScreen");
6917 prepareDisplay(ui::ROTATION_0);
6918 prepareButtons();
6919 prepareAxes(POSITION);
6920 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6921
6922 // Set a physical frame in the display viewport.
6923 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6924 viewport->physicalLeft = 0;
6925 viewport->physicalTop = 0;
6926 viewport->physicalRight = DISPLAY_WIDTH / 2;
6927 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6928 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006929 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006930
Arpit Singha8c236b2023-04-25 13:56:05 +00006931 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006932
6933 // Hovering inside the physical frame produces events.
6934 processKey(mapper, BTN_TOOL_PEN, 1);
6935 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6936 processSync(mapper);
6937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6938 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6940 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6941
6942 // Leaving the physical frame ends the hovering gesture.
6943 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6944 processSync(mapper);
6945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6946 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6947
6948 // Moving outside the physical frame does not produce events.
6949 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6950 processSync(mapper);
6951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6952
6953 // Re-entering the physical frame produces events.
6954 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6955 processSync(mapper);
6956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6957 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6959 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6960}
6961
Prabir Pradhan5632d622021-09-06 07:57:20 -07006962// --- TouchDisplayProjectionTest ---
6963
6964class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6965public:
6966 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6967 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6968 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006969 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6970 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6971 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006972 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006973 auto rotatedWidth = naturalDisplayWidth;
6974 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006975 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006976 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006977 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006978 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006979 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006980 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006981 inverseRotationFlags = ui::Transform::ROT_180;
6982 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006983 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006984 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006985 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006986 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006987 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006988 inverseRotationFlags = ui::Transform::ROT_0;
6989 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006990 }
6991
Prabir Pradhana9df3162022-12-05 23:57:27 +00006992 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006993 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6994
6995 std::optional<DisplayViewport> internalViewport =
6996 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6997 DisplayViewport& v = *internalViewport;
6998 v.displayId = DISPLAY_ID;
6999 v.orientation = orientation;
7000
7001 v.logicalLeft = 0;
7002 v.logicalTop = 0;
7003 v.logicalRight = 100;
7004 v.logicalBottom = 100;
7005
7006 v.physicalLeft = rotatedPhysicalDisplay.left;
7007 v.physicalTop = rotatedPhysicalDisplay.top;
7008 v.physicalRight = rotatedPhysicalDisplay.right;
7009 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7010
Prabir Pradhana9df3162022-12-05 23:57:27 +00007011 v.deviceWidth = rotatedWidth;
7012 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007013
7014 v.isActive = true;
7015 v.uniqueId = UNIQUE_ID;
7016 v.type = ViewportType::INTERNAL;
7017 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007018 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007019 }
7020
7021 void assertReceivedMove(const Point& point) {
7022 NotifyMotionArgs motionArgs;
7023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007025 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007026 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7027 1, 0, 0, 0, 0, 0, 0, 0));
7028 }
7029};
7030
7031TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7032 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007033 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007034
7035 prepareButtons();
7036 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007037 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007038
7039 NotifyMotionArgs motionArgs;
7040
7041 // Configure the DisplayViewport such that the logical display maps to a subsection of
7042 // the display panel called the physical display. Here, the physical display is bounded by the
7043 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7044 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7045 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7046 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7047
Michael Wrighta9cf4192022-12-01 23:46:39 +00007048 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007049 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7050
7051 // Touches outside the physical display should be ignored, and should not generate any
7052 // events. Ensure touches at the following points that lie outside of the physical display
7053 // area do not generate any events.
7054 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7055 processDown(mapper, toRawX(point.x), toRawY(point.y));
7056 processSync(mapper);
7057 processUp(mapper);
7058 processSync(mapper);
7059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7060 << "Unexpected event generated for touch outside physical display at point: "
7061 << point.x << ", " << point.y;
7062 }
7063 }
7064}
7065
7066TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7067 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007068 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007069
7070 prepareButtons();
7071 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007072 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007073
7074 NotifyMotionArgs motionArgs;
7075
7076 // Configure the DisplayViewport such that the logical display maps to a subsection of
7077 // the display panel called the physical display. Here, the physical display is bounded by the
7078 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7079 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7080
Michael Wrighta9cf4192022-12-01 23:46:39 +00007081 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007082 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7083
7084 // Touches that start outside the physical display should be ignored until it enters the
7085 // physical display bounds, at which point it should generate a down event. Start a touch at
7086 // the point (5, 100), which is outside the physical display bounds.
7087 static const Point kOutsidePoint{5, 100};
7088 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7089 processSync(mapper);
7090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7091
7092 // Move the touch into the physical display area. This should generate a pointer down.
7093 processMove(mapper, toRawX(11), toRawY(21));
7094 processSync(mapper);
7095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7096 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007097 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007098 ASSERT_NO_FATAL_FAILURE(
7099 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7100
7101 // Move the touch inside the physical display area. This should generate a pointer move.
7102 processMove(mapper, toRawX(69), toRawY(159));
7103 processSync(mapper);
7104 assertReceivedMove({69, 159});
7105
7106 // Move outside the physical display area. Since the pointer is already down, this should
7107 // now continue generating events.
7108 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7109 processSync(mapper);
7110 assertReceivedMove(kOutsidePoint);
7111
7112 // Release. This should generate a pointer up.
7113 processUp(mapper);
7114 processSync(mapper);
7115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7116 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7117 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7118 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7119
7120 // Ensure no more events were generated.
7121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7123 }
7124}
7125
Prabir Pradhana9df3162022-12-05 23:57:27 +00007126// --- TouchscreenPrecisionTests ---
7127
7128// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
7129// in various orientations and with different display rotations. We configure the touchscreen to
7130// have a higher resolution than that of the display by an integer scale factor in each axis so that
7131// we can enforce that coordinates match precisely as expected.
7132class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
7133 public ::testing::WithParamInterface<ui::Rotation> {
7134public:
7135 void SetUp() override {
7136 SingleTouchInputMapperTest::SetUp();
7137
7138 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7139 // four times the resolution of the display in the Y axis.
7140 prepareButtons();
7141 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007142 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7143 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007144 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007145 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7146 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007147 }
7148
7149 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7150 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7151 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7152 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7153
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007154 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7155 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7156
7157 static const int32_t PRECISION_RAW_X_FLAT = 16;
7158 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7159
7160 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7161 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7162
Prabir Pradhana9df3162022-12-05 23:57:27 +00007163 static const std::array<Point, 4> kRawCorners;
7164};
7165
7166const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7167 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7168 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7169 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7170 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7171}};
7172
7173// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7174// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7175// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7176TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7177 enum class Orientation {
7178 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7179 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7180 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7181 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7182 ftl_last = ORIENTATION_270,
7183 };
7184 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7185 Orientation::ORIENTATION_270;
7186 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7187 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7188 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7189 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7190 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7191 };
7192
7193 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7194
7195 // Configure the touchscreen as being installed in the one of the four different orientations
7196 // relative to the display.
7197 addConfigurationProperty("touch.deviceType", "touchScreen");
7198 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7199 prepareDisplay(ui::ROTATION_0);
7200
Arpit Singha8c236b2023-04-25 13:56:05 +00007201 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00007202
7203 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7204 // orientations of either 90 or 270) this means the display's natural resolution will be
7205 // flipped.
7206 const bool displayRotated =
7207 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7208 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7209 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7210 const Rect physicalFrame{0, 0, width, height};
7211 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7212
7213 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7214 const float expectedPrecisionX = displayRotated ? 4 : 2;
7215 const float expectedPrecisionY = displayRotated ? 2 : 4;
7216
7217 // Test all four corners.
7218 for (int i = 0; i < 4; i++) {
7219 const auto& raw = kRawCorners[i];
7220 processDown(mapper, raw.x, raw.y);
7221 processSync(mapper);
7222 const auto& expected = expectedPoints[i];
7223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7224 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7225 WithCoords(expected.x, expected.y),
7226 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7227 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7228 << "with touchscreen orientation "
7229 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7230 << expected.x << ", " << expected.y << ").";
7231 processUp(mapper);
7232 processSync(mapper);
7233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7234 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7235 WithCoords(expected.x, expected.y))));
7236 }
7237}
7238
Prabir Pradhan82687402022-12-06 01:32:53 +00007239TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7240 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7241 kMappedCorners = {
7242 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7243 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7244 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7245 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7246 };
7247
7248 const ui::Rotation displayRotation = GetParam();
7249
7250 addConfigurationProperty("touch.deviceType", "touchScreen");
7251 prepareDisplay(displayRotation);
7252
Arpit Singha8c236b2023-04-25 13:56:05 +00007253 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00007254
7255 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7256
7257 // Test all four corners.
7258 for (int i = 0; i < 4; i++) {
7259 const auto& expected = expectedPoints[i];
7260 const auto& raw = kRawCorners[i];
7261 processDown(mapper, raw.x, raw.y);
7262 processSync(mapper);
7263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7264 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7265 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7266 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7267 << "with display rotation " << ui::toCString(displayRotation)
7268 << ", expected point (" << expected.x << ", " << expected.y << ").";
7269 processUp(mapper);
7270 processSync(mapper);
7271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7272 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7273 WithCoords(expected.x, expected.y))));
7274 }
7275}
7276
Prabir Pradhan3e798762022-12-02 21:02:11 +00007277TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7278 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7279 kMappedCorners = {
7280 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7281 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7282 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7283 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7284 };
7285
7286 const ui::Rotation displayRotation = GetParam();
7287
7288 addConfigurationProperty("touch.deviceType", "touchScreen");
7289 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7290
Arpit Singha8c236b2023-04-25 13:56:05 +00007291 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00007292
7293 // Ori 270, so width and height swapped
7294 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7295 prepareDisplay(displayRotation);
7296 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7297
7298 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7299
7300 // Test all four corners.
7301 for (int i = 0; i < 4; i++) {
7302 const auto& expected = expectedPoints[i];
7303 const auto& raw = kRawCorners[i];
7304 processDown(mapper, raw.x, raw.y);
7305 processSync(mapper);
7306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7307 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7308 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7309 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7310 << "with display rotation " << ui::toCString(displayRotation)
7311 << ", expected point (" << expected.x << ", " << expected.y << ").";
7312 processUp(mapper);
7313 processSync(mapper);
7314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7315 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7316 WithCoords(expected.x, expected.y))));
7317 }
7318}
7319
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007320TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7321 const ui::Rotation displayRotation = GetParam();
7322
7323 addConfigurationProperty("touch.deviceType", "touchScreen");
7324 prepareDisplay(displayRotation);
7325
7326 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00007327 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007328
7329 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7330 // MotionRanges use display pixels as their units
7331 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7332 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7333
7334 // The MotionRanges should be oriented in the rotated display's coordinate space
7335 const bool displayRotated =
7336 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7337
7338 constexpr float MAX_X = 479.5;
7339 constexpr float MAX_Y = 799.75;
7340 EXPECT_EQ(xRange->min, 0.f);
7341 EXPECT_EQ(yRange->min, 0.f);
7342 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7343 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7344
7345 EXPECT_EQ(xRange->flat, 8.f);
7346 EXPECT_EQ(yRange->flat, 8.f);
7347
7348 EXPECT_EQ(xRange->fuzz, 2.f);
7349 EXPECT_EQ(yRange->fuzz, 2.f);
7350
7351 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7352 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7353}
7354
Prabir Pradhana9df3162022-12-05 23:57:27 +00007355// Run the precision tests for all rotations.
7356INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7357 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7358 ui::ROTATION_270),
7359 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7360 return ftl::enum_string(testParamInfo.param);
7361 });
7362
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007363// --- ExternalStylusFusionTest ---
7364
7365class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7366public:
7367 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7368 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007369 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007370 prepareButtons();
7371 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007372 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007373
7374 mStylusState.when = ARBITRARY_TIME;
7375 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007376 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007377 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007378 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007379 processExternalStylusState(mapper);
7380 return mapper;
7381 }
7382
7383 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7384 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7385 for (const NotifyArgs& args : generatedArgs) {
7386 mFakeListener->notify(args);
7387 }
7388 // Loop the reader to flush the input listener queue.
7389 mReader->loopOnce();
7390 return generatedArgs;
7391 }
7392
7393protected:
7394 StylusState mStylusState{};
7395 static constexpr uint32_t EXPECTED_SOURCE =
7396 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7397
7398 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7399 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007400 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007401
7402 // The first pointer is withheld.
7403 processDown(mapper, 100, 200);
7404 processSync(mapper);
7405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7406 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7407 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7408
7409 // The external stylus reports pressure. The withheld finger pointer is released as a
7410 // stylus.
7411 mStylusState.pressure = 1.f;
7412 processExternalStylusState(mapper);
7413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7414 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7415 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7416
7417 // Subsequent pointer events are not withheld.
7418 processMove(mapper, 101, 201);
7419 processSync(mapper);
7420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7421 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7422
7423 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7425 }
7426
7427 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7428 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7429
7430 // Releasing the touch pointer ends the gesture.
7431 processUp(mapper);
7432 processSync(mapper);
7433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7434 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007435 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007436
7437 mStylusState.pressure = 0.f;
7438 processExternalStylusState(mapper);
7439 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7441 }
7442
7443 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7444 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007445 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007446
7447 // The first pointer is withheld when an external stylus is connected,
7448 // and a timeout is requested.
7449 processDown(mapper, 100, 200);
7450 processSync(mapper);
7451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7452 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7453 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7454
7455 // If the timeout expires early, it is requested again.
7456 handleTimeout(mapper, ARBITRARY_TIME + 1);
7457 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7458 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7459
7460 // When the timeout expires, the withheld touch is released as a finger pointer.
7461 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7463 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7464
7465 // Subsequent pointer events are not withheld.
7466 processMove(mapper, 101, 201);
7467 processSync(mapper);
7468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7469 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7470 processUp(mapper);
7471 processSync(mapper);
7472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7473 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7474
7475 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7477 }
7478
7479private:
7480 InputDeviceInfo mExternalStylusDeviceInfo{};
7481};
7482
7483TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7484 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7485 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7486}
7487
7488TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7489 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7490 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7491}
7492
7493TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7494 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7495 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7496}
7497
7498// Test a successful stylus fusion gesture where the pressure is reported by the external
7499// before the touch is reported by the touchscreen.
7500TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7501 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7502 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007503 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007504
7505 // The external stylus reports pressure first. It is ignored for now.
7506 mStylusState.pressure = 1.f;
7507 processExternalStylusState(mapper);
7508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7509 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7510
7511 // When the touch goes down afterwards, it is reported as a stylus pointer.
7512 processDown(mapper, 100, 200);
7513 processSync(mapper);
7514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7515 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7516 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7517
7518 processMove(mapper, 101, 201);
7519 processSync(mapper);
7520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7521 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7522 processUp(mapper);
7523 processSync(mapper);
7524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7525 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7526
7527 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7529}
7530
7531TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7532 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7533
7534 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7535 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7536
7537 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7538 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7539 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7540 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7541}
7542
7543TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7544 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7545 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007546 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007547
7548 mStylusState.pressure = 0.8f;
7549 processExternalStylusState(mapper);
7550 processDown(mapper, 100, 200);
7551 processSync(mapper);
7552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7553 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7554 WithPressure(0.8f))));
7555 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7556
7557 // The external stylus reports a pressure change. We wait for some time for a touch event.
7558 mStylusState.pressure = 0.6f;
7559 processExternalStylusState(mapper);
7560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7561 ASSERT_NO_FATAL_FAILURE(
7562 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7563
7564 // If a touch is reported within the timeout, it reports the updated pressure.
7565 processMove(mapper, 101, 201);
7566 processSync(mapper);
7567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7568 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7569 WithPressure(0.6f))));
7570 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7571
7572 // There is another pressure change.
7573 mStylusState.pressure = 0.5f;
7574 processExternalStylusState(mapper);
7575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7576 ASSERT_NO_FATAL_FAILURE(
7577 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7578
7579 // If a touch is not reported within the timeout, a move event is generated to report
7580 // the new pressure.
7581 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7583 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7584 WithPressure(0.5f))));
7585
7586 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7587 // repeated indefinitely.
7588 mStylusState.pressure = 0.0f;
7589 processExternalStylusState(mapper);
7590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7591 ASSERT_NO_FATAL_FAILURE(
7592 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7593 processMove(mapper, 102, 202);
7594 processSync(mapper);
7595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7596 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7597 WithPressure(0.5f))));
7598 processMove(mapper, 103, 203);
7599 processSync(mapper);
7600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7601 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7602 WithPressure(0.5f))));
7603
7604 processUp(mapper);
7605 processSync(mapper);
7606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7607 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007608 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007609
7610 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7612}
7613
7614TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7615 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7616 auto source = WithSource(EXPECTED_SOURCE);
7617
7618 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007619 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007620 processExternalStylusState(mapper);
7621 processDown(mapper, 100, 200);
7622 processSync(mapper);
7623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7624 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007625 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007626 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7627
7628 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007629 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007630 processExternalStylusState(mapper);
7631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7632 ASSERT_NO_FATAL_FAILURE(
7633 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7634
7635 // If a touch is reported within the timeout, it reports the updated pressure.
7636 processMove(mapper, 101, 201);
7637 processSync(mapper);
7638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7639 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007640 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007641 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7642
7643 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007644 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007645 processExternalStylusState(mapper);
7646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7647 ASSERT_NO_FATAL_FAILURE(
7648 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7649
7650 // If a touch is not reported within the timeout, a move event is generated to report
7651 // the new tool type.
7652 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7654 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007655 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007656
7657 processUp(mapper);
7658 processSync(mapper);
7659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7660 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007661 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007662
7663 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7665}
7666
7667TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7668 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7669 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007670 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007671
7672 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7673
7674 // The external stylus reports a button change. We wait for some time for a touch event.
7675 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7676 processExternalStylusState(mapper);
7677 ASSERT_NO_FATAL_FAILURE(
7678 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7679
7680 // If a touch is reported within the timeout, it reports the updated button state.
7681 processMove(mapper, 101, 201);
7682 processSync(mapper);
7683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7684 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7685 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7687 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7688 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7689 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7690
7691 // The button is now released.
7692 mStylusState.buttons = 0;
7693 processExternalStylusState(mapper);
7694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7695 ASSERT_NO_FATAL_FAILURE(
7696 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7697
7698 // If a touch is not reported within the timeout, a move event is generated to report
7699 // the new button state.
7700 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7702 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7703 WithButtonState(0))));
7704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007705 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7706 WithButtonState(0))));
7707
7708 processUp(mapper);
7709 processSync(mapper);
7710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007711 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7712
7713 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7715}
7716
Michael Wrightd02c5b62014-02-10 15:10:22 -08007717// --- MultiTouchInputMapperTest ---
7718
7719class MultiTouchInputMapperTest : public TouchInputMapperTest {
7720protected:
7721 void prepareAxes(int axes);
7722
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007723 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7724 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7725 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7726 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7727 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7728 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7729 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7730 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7731 void processId(MultiTouchInputMapper& mapper, int32_t id);
7732 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7733 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7734 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007735 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007736 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007737 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7738 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007739};
7740
7741void MultiTouchInputMapperTest::prepareAxes(int axes) {
7742 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007743 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7744 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007745 }
7746 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007747 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7748 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007749 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007750 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7751 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007752 }
7753 }
7754 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007755 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7756 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007757 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007758 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007759 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007760 }
7761 }
7762 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007763 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7764 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007765 }
7766 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007767 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7768 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007769 }
7770 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007771 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7772 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007773 }
7774 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007775 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7776 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007777 }
7778 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007779 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7780 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007781 }
7782 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007783 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007784 }
7785}
7786
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007787void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7788 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007789 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007791}
7792
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007793void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7794 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007796}
7797
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007798void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7799 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007801}
7802
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007803void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007804 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007805}
7806
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007807void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007809}
7810
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007811void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7812 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007813 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007814}
7815
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007816void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007817 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007818}
7819
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007820void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007822}
7823
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007824void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007825 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007826}
7827
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007828void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007830}
7831
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007832void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007834}
7835
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007836void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7837 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007838 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007839}
7840
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007841void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7842 int32_t value) {
7843 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7844 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7845}
7846
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007847void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007848 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007849}
7850
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007851void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7852 nsecs_t readTime) {
7853 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007854}
7855
Michael Wrightd02c5b62014-02-10 15:10:22 -08007856TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007857 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007858 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007859 prepareAxes(POSITION);
7860 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007861 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007862
arthurhungdcef2dc2020-08-11 14:47:50 +08007863 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007864
7865 NotifyMotionArgs motionArgs;
7866
7867 // Two fingers down at once.
7868 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7869 processPosition(mapper, x1, y1);
7870 processMTSync(mapper);
7871 processPosition(mapper, x2, y2);
7872 processMTSync(mapper);
7873 processSync(mapper);
7874
7875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7876 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7877 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7878 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7879 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7880 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7881 ASSERT_EQ(0, motionArgs.flags);
7882 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7883 ASSERT_EQ(0, motionArgs.buttonState);
7884 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007885 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007886 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007887 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7889 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7890 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7891 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7892 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7893
7894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7895 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7896 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7897 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7898 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007899 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007900 ASSERT_EQ(0, motionArgs.flags);
7901 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7902 ASSERT_EQ(0, motionArgs.buttonState);
7903 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007904 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007905 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007906 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007907 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007908 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007909 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7910 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7912 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7913 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7914 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7915 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7916
7917 // Move.
7918 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7919 processPosition(mapper, x1, y1);
7920 processMTSync(mapper);
7921 processPosition(mapper, x2, y2);
7922 processMTSync(mapper);
7923 processSync(mapper);
7924
7925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7926 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7927 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7928 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7929 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7930 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7931 ASSERT_EQ(0, motionArgs.flags);
7932 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7933 ASSERT_EQ(0, motionArgs.buttonState);
7934 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007935 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007936 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007937 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007938 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007939 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7941 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7943 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7944 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7945 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7946 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7947
7948 // First finger up.
7949 x2 += 15; y2 -= 20;
7950 processPosition(mapper, x2, y2);
7951 processMTSync(mapper);
7952 processSync(mapper);
7953
7954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7955 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7956 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7957 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7958 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007959 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007960 ASSERT_EQ(0, motionArgs.flags);
7961 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7962 ASSERT_EQ(0, motionArgs.buttonState);
7963 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007964 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007965 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007966 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007967 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007968 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7970 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7972 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7973 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7974 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7975 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7976
7977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7978 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7979 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7980 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7981 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7982 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7983 ASSERT_EQ(0, motionArgs.flags);
7984 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7985 ASSERT_EQ(0, motionArgs.buttonState);
7986 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007987 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007988 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007989 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7991 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7992 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7993 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7994 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7995
7996 // Move.
7997 x2 += 20; y2 -= 25;
7998 processPosition(mapper, x2, y2);
7999 processMTSync(mapper);
8000 processSync(mapper);
8001
8002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8003 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8004 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8005 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8006 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8008 ASSERT_EQ(0, motionArgs.flags);
8009 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8010 ASSERT_EQ(0, motionArgs.buttonState);
8011 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008012 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008013 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008014 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008015 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8016 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8017 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8018 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8019 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8020
8021 // New finger down.
8022 int32_t x3 = 700, y3 = 300;
8023 processPosition(mapper, x2, y2);
8024 processMTSync(mapper);
8025 processPosition(mapper, x3, y3);
8026 processMTSync(mapper);
8027 processSync(mapper);
8028
8029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8030 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8031 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8032 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8033 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008034 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008035 ASSERT_EQ(0, motionArgs.flags);
8036 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8037 ASSERT_EQ(0, motionArgs.buttonState);
8038 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008039 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008040 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008041 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008042 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008043 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8045 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8046 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8047 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8048 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8049 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8050 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8051
8052 // Second finger up.
8053 x3 += 30; y3 -= 20;
8054 processPosition(mapper, x3, y3);
8055 processMTSync(mapper);
8056 processSync(mapper);
8057
8058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8059 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8060 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8061 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8062 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008063 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008064 ASSERT_EQ(0, motionArgs.flags);
8065 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8066 ASSERT_EQ(0, motionArgs.buttonState);
8067 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008068 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008069 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008070 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008071 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008072 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008073 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8074 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8076 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8077 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8078 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8079 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8080
8081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8082 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8083 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8084 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8085 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8087 ASSERT_EQ(0, motionArgs.flags);
8088 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8089 ASSERT_EQ(0, motionArgs.buttonState);
8090 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008091 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008092 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008093 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8095 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8096 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8097 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8098 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8099
8100 // Last finger up.
8101 processMTSync(mapper);
8102 processSync(mapper);
8103
8104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8105 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8106 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8107 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8108 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8109 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8110 ASSERT_EQ(0, motionArgs.flags);
8111 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8112 ASSERT_EQ(0, motionArgs.buttonState);
8113 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008114 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008115 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008116 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008117 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8118 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8119 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8120 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8121 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8122
8123 // Should not have sent any more keys or motions.
8124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8126}
8127
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008128TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8129 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008130 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008131
8132 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8133 /*fuzz*/ 0, /*resolution*/ 10);
8134 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8135 /*fuzz*/ 0, /*resolution*/ 11);
8136 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8137 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8138 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8139 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8140 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8141 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8142 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8143 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8144
Arpit Singha8c236b2023-04-25 13:56:05 +00008145 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008146
8147 // X and Y axes
8148 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8149 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8150 // Touch major and minor
8151 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8152 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8153 // Tool major and minor
8154 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8155 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8156}
8157
8158TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8159 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008160 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008161
8162 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8163 /*fuzz*/ 0, /*resolution*/ 10);
8164 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8165 /*fuzz*/ 0, /*resolution*/ 11);
8166
8167 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8168
Arpit Singha8c236b2023-04-25 13:56:05 +00008169 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008170
8171 // Touch major and minor
8172 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8173 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8174 // Tool major and minor
8175 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8176 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8177}
8178
Michael Wrightd02c5b62014-02-10 15:10:22 -08008179TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008180 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008181 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008182 prepareAxes(POSITION | ID);
8183 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008184 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008185
arthurhungdcef2dc2020-08-11 14:47:50 +08008186 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008187
8188 NotifyMotionArgs motionArgs;
8189
8190 // Two fingers down at once.
8191 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8192 processPosition(mapper, x1, y1);
8193 processId(mapper, 1);
8194 processMTSync(mapper);
8195 processPosition(mapper, x2, y2);
8196 processId(mapper, 2);
8197 processMTSync(mapper);
8198 processSync(mapper);
8199
8200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8201 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008202 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008203 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008204 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8206 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8207
8208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008209 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008210 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008211 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008212 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008213 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008214 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008215 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8216 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8217 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8218 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8219
8220 // Move.
8221 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8222 processPosition(mapper, x1, y1);
8223 processId(mapper, 1);
8224 processMTSync(mapper);
8225 processPosition(mapper, x2, y2);
8226 processId(mapper, 2);
8227 processMTSync(mapper);
8228 processSync(mapper);
8229
8230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8231 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008232 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008233 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008234 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008235 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008236 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8238 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8239 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8240 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8241
8242 // First finger up.
8243 x2 += 15; y2 -= 20;
8244 processPosition(mapper, x2, y2);
8245 processId(mapper, 2);
8246 processMTSync(mapper);
8247 processSync(mapper);
8248
8249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008250 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008251 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008252 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008253 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008254 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008255 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008256 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8257 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8259 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8260
8261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8262 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008263 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008264 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008265 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008266 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8267 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8268
8269 // Move.
8270 x2 += 20; y2 -= 25;
8271 processPosition(mapper, x2, y2);
8272 processId(mapper, 2);
8273 processMTSync(mapper);
8274 processSync(mapper);
8275
8276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008278 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008279 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008280 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8282 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8283
8284 // New finger down.
8285 int32_t x3 = 700, y3 = 300;
8286 processPosition(mapper, x2, y2);
8287 processId(mapper, 2);
8288 processMTSync(mapper);
8289 processPosition(mapper, x3, y3);
8290 processId(mapper, 3);
8291 processMTSync(mapper);
8292 processSync(mapper);
8293
8294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008295 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008296 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008297 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008298 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008299 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008300 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8302 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8304 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8305
8306 // Second finger up.
8307 x3 += 30; y3 -= 20;
8308 processPosition(mapper, x3, y3);
8309 processId(mapper, 3);
8310 processMTSync(mapper);
8311 processSync(mapper);
8312
8313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008314 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008315 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008316 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008317 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008318 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008319 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8321 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8323 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8324
8325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008327 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008328 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008329 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8331 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8332
8333 // Last finger up.
8334 processMTSync(mapper);
8335 processSync(mapper);
8336
8337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8338 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008339 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008340 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008341 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008342 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8343 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8344
8345 // Should not have sent any more keys or motions.
8346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8348}
8349
8350TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008351 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008352 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008353 prepareAxes(POSITION | ID | SLOT);
8354 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008355 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008356
arthurhungdcef2dc2020-08-11 14:47:50 +08008357 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008358
8359 NotifyMotionArgs motionArgs;
8360
8361 // Two fingers down at once.
8362 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8363 processPosition(mapper, x1, y1);
8364 processId(mapper, 1);
8365 processSlot(mapper, 1);
8366 processPosition(mapper, x2, y2);
8367 processId(mapper, 2);
8368 processSync(mapper);
8369
8370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8371 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008372 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008373 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008374 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8376 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8377
8378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008379 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008380 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008381 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008382 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008383 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008384 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8386 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8387 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8388 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8389
8390 // Move.
8391 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8392 processSlot(mapper, 0);
8393 processPosition(mapper, x1, y1);
8394 processSlot(mapper, 1);
8395 processPosition(mapper, x2, y2);
8396 processSync(mapper);
8397
8398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008400 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008401 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008402 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008403 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008404 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8406 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8408 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8409
8410 // First finger up.
8411 x2 += 15; y2 -= 20;
8412 processSlot(mapper, 0);
8413 processId(mapper, -1);
8414 processSlot(mapper, 1);
8415 processPosition(mapper, x2, y2);
8416 processSync(mapper);
8417
8418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008419 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008420 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008421 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008422 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008423 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008424 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8426 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8428 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8429
8430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008432 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008433 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008434 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8436 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8437
8438 // Move.
8439 x2 += 20; y2 -= 25;
8440 processPosition(mapper, x2, y2);
8441 processSync(mapper);
8442
8443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008445 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008446 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008447 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008448 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8449 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8450
8451 // New finger down.
8452 int32_t x3 = 700, y3 = 300;
8453 processPosition(mapper, x2, y2);
8454 processSlot(mapper, 0);
8455 processId(mapper, 3);
8456 processPosition(mapper, x3, y3);
8457 processSync(mapper);
8458
8459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008460 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008461 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008462 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008463 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008464 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008465 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8467 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8469 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8470
8471 // Second finger up.
8472 x3 += 30; y3 -= 20;
8473 processSlot(mapper, 1);
8474 processId(mapper, -1);
8475 processSlot(mapper, 0);
8476 processPosition(mapper, x3, y3);
8477 processSync(mapper);
8478
8479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008480 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008481 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008482 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008483 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008484 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008485 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8487 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8488 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8489 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8490
8491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8492 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008493 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008494 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008495 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8497 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8498
8499 // Last finger up.
8500 processId(mapper, -1);
8501 processSync(mapper);
8502
8503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8504 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008505 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008506 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008507 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8509 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8510
8511 // Should not have sent any more keys or motions.
8512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8514}
8515
8516TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008517 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008518 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008519 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008520 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008521
8522 // These calculations are based on the input device calibration documentation.
8523 int32_t rawX = 100;
8524 int32_t rawY = 200;
8525 int32_t rawTouchMajor = 7;
8526 int32_t rawTouchMinor = 6;
8527 int32_t rawToolMajor = 9;
8528 int32_t rawToolMinor = 8;
8529 int32_t rawPressure = 11;
8530 int32_t rawDistance = 0;
8531 int32_t rawOrientation = 3;
8532 int32_t id = 5;
8533
8534 float x = toDisplayX(rawX);
8535 float y = toDisplayY(rawY);
8536 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8537 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8538 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8539 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8540 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8541 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8542 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8543 float distance = float(rawDistance);
8544
8545 processPosition(mapper, rawX, rawY);
8546 processTouchMajor(mapper, rawTouchMajor);
8547 processTouchMinor(mapper, rawTouchMinor);
8548 processToolMajor(mapper, rawToolMajor);
8549 processToolMinor(mapper, rawToolMinor);
8550 processPressure(mapper, rawPressure);
8551 processOrientation(mapper, rawOrientation);
8552 processDistance(mapper, rawDistance);
8553 processId(mapper, id);
8554 processMTSync(mapper);
8555 processSync(mapper);
8556
8557 NotifyMotionArgs args;
8558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8559 ASSERT_EQ(0, args.pointerProperties[0].id);
8560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8561 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8562 orientation, distance));
8563}
8564
8565TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008566 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008567 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008568 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8569 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00008570 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008571
8572 // These calculations are based on the input device calibration documentation.
8573 int32_t rawX = 100;
8574 int32_t rawY = 200;
8575 int32_t rawTouchMajor = 140;
8576 int32_t rawTouchMinor = 120;
8577 int32_t rawToolMajor = 180;
8578 int32_t rawToolMinor = 160;
8579
8580 float x = toDisplayX(rawX);
8581 float y = toDisplayY(rawY);
8582 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8583 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8584 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8585 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8586 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8587
8588 processPosition(mapper, rawX, rawY);
8589 processTouchMajor(mapper, rawTouchMajor);
8590 processTouchMinor(mapper, rawTouchMinor);
8591 processToolMajor(mapper, rawToolMajor);
8592 processToolMinor(mapper, rawToolMinor);
8593 processMTSync(mapper);
8594 processSync(mapper);
8595
8596 NotifyMotionArgs args;
8597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8599 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8600}
8601
8602TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008603 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008604 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008605 prepareAxes(POSITION | TOUCH | TOOL);
8606 addConfigurationProperty("touch.size.calibration", "diameter");
8607 addConfigurationProperty("touch.size.scale", "10");
8608 addConfigurationProperty("touch.size.bias", "160");
8609 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00008610 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008611
8612 // These calculations are based on the input device calibration documentation.
8613 // Note: We only provide a single common touch/tool value because the device is assumed
8614 // not to emit separate values for each pointer (isSummed = 1).
8615 int32_t rawX = 100;
8616 int32_t rawY = 200;
8617 int32_t rawX2 = 150;
8618 int32_t rawY2 = 250;
8619 int32_t rawTouchMajor = 5;
8620 int32_t rawToolMajor = 8;
8621
8622 float x = toDisplayX(rawX);
8623 float y = toDisplayY(rawY);
8624 float x2 = toDisplayX(rawX2);
8625 float y2 = toDisplayY(rawY2);
8626 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8627 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8628 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8629
8630 processPosition(mapper, rawX, rawY);
8631 processTouchMajor(mapper, rawTouchMajor);
8632 processToolMajor(mapper, rawToolMajor);
8633 processMTSync(mapper);
8634 processPosition(mapper, rawX2, rawY2);
8635 processTouchMajor(mapper, rawTouchMajor);
8636 processToolMajor(mapper, rawToolMajor);
8637 processMTSync(mapper);
8638 processSync(mapper);
8639
8640 NotifyMotionArgs args;
8641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8642 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8643
8644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008645 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008646 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8648 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8650 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8651}
8652
8653TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008654 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008655 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008656 prepareAxes(POSITION | TOUCH | TOOL);
8657 addConfigurationProperty("touch.size.calibration", "area");
8658 addConfigurationProperty("touch.size.scale", "43");
8659 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00008660 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008661
8662 // These calculations are based on the input device calibration documentation.
8663 int32_t rawX = 100;
8664 int32_t rawY = 200;
8665 int32_t rawTouchMajor = 5;
8666 int32_t rawToolMajor = 8;
8667
8668 float x = toDisplayX(rawX);
8669 float y = toDisplayY(rawY);
8670 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8671 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8672 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8673
8674 processPosition(mapper, rawX, rawY);
8675 processTouchMajor(mapper, rawTouchMajor);
8676 processToolMajor(mapper, rawToolMajor);
8677 processMTSync(mapper);
8678 processSync(mapper);
8679
8680 NotifyMotionArgs args;
8681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8683 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8684}
8685
8686TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008687 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008688 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008689 prepareAxes(POSITION | PRESSURE);
8690 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8691 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00008692 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008693
Michael Wrightaa449c92017-12-13 21:21:43 +00008694 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008695 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008696 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8697 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8698 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8699
Michael Wrightd02c5b62014-02-10 15:10:22 -08008700 // These calculations are based on the input device calibration documentation.
8701 int32_t rawX = 100;
8702 int32_t rawY = 200;
8703 int32_t rawPressure = 60;
8704
8705 float x = toDisplayX(rawX);
8706 float y = toDisplayY(rawY);
8707 float pressure = float(rawPressure) * 0.01f;
8708
8709 processPosition(mapper, rawX, rawY);
8710 processPressure(mapper, rawPressure);
8711 processMTSync(mapper);
8712 processSync(mapper);
8713
8714 NotifyMotionArgs args;
8715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8716 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8717 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8718}
8719
8720TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008721 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008722 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008723 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008724 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008725
8726 NotifyMotionArgs motionArgs;
8727 NotifyKeyArgs keyArgs;
8728
8729 processId(mapper, 1);
8730 processPosition(mapper, 100, 200);
8731 processSync(mapper);
8732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8733 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8734 ASSERT_EQ(0, motionArgs.buttonState);
8735
8736 // press BTN_LEFT, release BTN_LEFT
8737 processKey(mapper, BTN_LEFT, 1);
8738 processSync(mapper);
8739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8741 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8742
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8744 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8745 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8746
Michael Wrightd02c5b62014-02-10 15:10:22 -08008747 processKey(mapper, BTN_LEFT, 0);
8748 processSync(mapper);
8749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008750 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008751 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008752
8753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008754 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008755 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008756
8757 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8758 processKey(mapper, BTN_RIGHT, 1);
8759 processKey(mapper, BTN_MIDDLE, 1);
8760 processSync(mapper);
8761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8762 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8763 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8764 motionArgs.buttonState);
8765
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8767 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8768 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8769
8770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8771 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8772 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8773 motionArgs.buttonState);
8774
Michael Wrightd02c5b62014-02-10 15:10:22 -08008775 processKey(mapper, BTN_RIGHT, 0);
8776 processSync(mapper);
8777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008778 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008779 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008780
8781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008782 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008783 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008784
8785 processKey(mapper, BTN_MIDDLE, 0);
8786 processSync(mapper);
8787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008788 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008789 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008790
8791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008792 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008793 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008794
8795 // press BTN_BACK, release BTN_BACK
8796 processKey(mapper, BTN_BACK, 1);
8797 processSync(mapper);
8798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8799 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8800 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008801
Michael Wrightd02c5b62014-02-10 15:10:22 -08008802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008803 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008804 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8805
8806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8807 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8808 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008809
8810 processKey(mapper, BTN_BACK, 0);
8811 processSync(mapper);
8812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008813 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008814 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008815
8816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008817 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008818 ASSERT_EQ(0, motionArgs.buttonState);
8819
Michael Wrightd02c5b62014-02-10 15:10:22 -08008820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8821 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8822 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8823
8824 // press BTN_SIDE, release BTN_SIDE
8825 processKey(mapper, BTN_SIDE, 1);
8826 processSync(mapper);
8827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8828 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8829 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008830
Michael Wrightd02c5b62014-02-10 15:10:22 -08008831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008832 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008833 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8834
8835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8836 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8837 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008838
8839 processKey(mapper, BTN_SIDE, 0);
8840 processSync(mapper);
8841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008842 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008843 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008844
8845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008846 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008847 ASSERT_EQ(0, motionArgs.buttonState);
8848
Michael Wrightd02c5b62014-02-10 15:10:22 -08008849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8850 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8851 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8852
8853 // press BTN_FORWARD, release BTN_FORWARD
8854 processKey(mapper, BTN_FORWARD, 1);
8855 processSync(mapper);
8856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8857 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8858 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008859
Michael Wrightd02c5b62014-02-10 15:10:22 -08008860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008861 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008862 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8863
8864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8865 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8866 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008867
8868 processKey(mapper, BTN_FORWARD, 0);
8869 processSync(mapper);
8870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008871 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008872 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008873
8874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008875 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008876 ASSERT_EQ(0, motionArgs.buttonState);
8877
Michael Wrightd02c5b62014-02-10 15:10:22 -08008878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8879 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8880 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8881
8882 // press BTN_EXTRA, release BTN_EXTRA
8883 processKey(mapper, BTN_EXTRA, 1);
8884 processSync(mapper);
8885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8886 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8887 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008888
Michael Wrightd02c5b62014-02-10 15:10:22 -08008889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008890 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008891 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8892
8893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8894 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8895 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008896
8897 processKey(mapper, BTN_EXTRA, 0);
8898 processSync(mapper);
8899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008900 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008901 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008902
8903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008904 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008905 ASSERT_EQ(0, motionArgs.buttonState);
8906
Michael Wrightd02c5b62014-02-10 15:10:22 -08008907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8908 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8909 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8910
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8912
Michael Wrightd02c5b62014-02-10 15:10:22 -08008913 // press BTN_STYLUS, release BTN_STYLUS
8914 processKey(mapper, BTN_STYLUS, 1);
8915 processSync(mapper);
8916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008918 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8919
8920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8921 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8922 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008923
8924 processKey(mapper, BTN_STYLUS, 0);
8925 processSync(mapper);
8926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008927 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008928 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008929
8930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008931 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008932 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008933
8934 // press BTN_STYLUS2, release BTN_STYLUS2
8935 processKey(mapper, BTN_STYLUS2, 1);
8936 processSync(mapper);
8937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8938 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008939 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8940
8941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8942 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8943 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008944
8945 processKey(mapper, BTN_STYLUS2, 0);
8946 processSync(mapper);
8947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008948 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008949 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008950
8951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008952 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008953 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008954
8955 // release touch
8956 processId(mapper, -1);
8957 processSync(mapper);
8958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8959 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8960 ASSERT_EQ(0, motionArgs.buttonState);
8961}
8962
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008963TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8964 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008965 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008966 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008967 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008968
8969 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8970 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8971
8972 // Touch down.
8973 processId(mapper, 1);
8974 processPosition(mapper, 100, 200);
8975 processSync(mapper);
8976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8977 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8978
8979 // Press and release button mapped to the primary stylus button.
8980 processKey(mapper, BTN_A, 1);
8981 processSync(mapper);
8982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8983 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8984 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8986 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8987 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8988
8989 processKey(mapper, BTN_A, 0);
8990 processSync(mapper);
8991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8992 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8994 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8995
8996 // Press and release the HID usage mapped to the secondary stylus button.
8997 processHidUsage(mapper, 0xabcd, 1);
8998 processSync(mapper);
8999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9000 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9001 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9003 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9004 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9005
9006 processHidUsage(mapper, 0xabcd, 0);
9007 processSync(mapper);
9008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9009 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9011 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9012
9013 // Release touch.
9014 processId(mapper, -1);
9015 processSync(mapper);
9016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9017 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
9018}
9019
Michael Wrightd02c5b62014-02-10 15:10:22 -08009020TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009021 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009022 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009023 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009024 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009025
9026 NotifyMotionArgs motionArgs;
9027
9028 // default tool type is finger
9029 processId(mapper, 1);
9030 processPosition(mapper, 100, 200);
9031 processSync(mapper);
9032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9033 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009034 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009035
9036 // eraser
9037 processKey(mapper, BTN_TOOL_RUBBER, 1);
9038 processSync(mapper);
9039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9040 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009041 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009042
9043 // stylus
9044 processKey(mapper, BTN_TOOL_RUBBER, 0);
9045 processKey(mapper, BTN_TOOL_PEN, 1);
9046 processSync(mapper);
9047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9048 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009049 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009050
9051 // brush
9052 processKey(mapper, BTN_TOOL_PEN, 0);
9053 processKey(mapper, BTN_TOOL_BRUSH, 1);
9054 processSync(mapper);
9055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9056 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009057 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009058
9059 // pencil
9060 processKey(mapper, BTN_TOOL_BRUSH, 0);
9061 processKey(mapper, BTN_TOOL_PENCIL, 1);
9062 processSync(mapper);
9063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009065 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009066
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009067 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009068 processKey(mapper, BTN_TOOL_PENCIL, 0);
9069 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
9070 processSync(mapper);
9071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009073 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009074
9075 // mouse
9076 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9077 processKey(mapper, BTN_TOOL_MOUSE, 1);
9078 processSync(mapper);
9079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9080 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009081 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009082
9083 // lens
9084 processKey(mapper, BTN_TOOL_MOUSE, 0);
9085 processKey(mapper, BTN_TOOL_LENS, 1);
9086 processSync(mapper);
9087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9088 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009089 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009090
9091 // double-tap
9092 processKey(mapper, BTN_TOOL_LENS, 0);
9093 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9094 processSync(mapper);
9095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9096 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009097 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009098
9099 // triple-tap
9100 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9101 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9102 processSync(mapper);
9103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009105 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009106
9107 // quad-tap
9108 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9109 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9110 processSync(mapper);
9111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9112 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009113 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009114
9115 // finger
9116 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9117 processKey(mapper, BTN_TOOL_FINGER, 1);
9118 processSync(mapper);
9119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9120 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009121 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009122
9123 // stylus trumps finger
9124 processKey(mapper, BTN_TOOL_PEN, 1);
9125 processSync(mapper);
9126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9127 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009128 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009129
9130 // eraser trumps stylus
9131 processKey(mapper, BTN_TOOL_RUBBER, 1);
9132 processSync(mapper);
9133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009135 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009136
9137 // mouse trumps eraser
9138 processKey(mapper, BTN_TOOL_MOUSE, 1);
9139 processSync(mapper);
9140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009142 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009143
9144 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9145 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9146 processSync(mapper);
9147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9148 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009149 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009150
9151 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9152 processToolType(mapper, MT_TOOL_PEN);
9153 processSync(mapper);
9154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009156 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009157
9158 // back to default tool type
9159 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9160 processKey(mapper, BTN_TOOL_MOUSE, 0);
9161 processKey(mapper, BTN_TOOL_RUBBER, 0);
9162 processKey(mapper, BTN_TOOL_PEN, 0);
9163 processKey(mapper, BTN_TOOL_FINGER, 0);
9164 processSync(mapper);
9165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009167 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009168}
9169
9170TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009171 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009172 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009173 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009174 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009175 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009176
9177 NotifyMotionArgs motionArgs;
9178
9179 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9180 processId(mapper, 1);
9181 processPosition(mapper, 100, 200);
9182 processSync(mapper);
9183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9184 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9186 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9187
9188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9189 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9190 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9191 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9192
9193 // move a little
9194 processPosition(mapper, 150, 250);
9195 processSync(mapper);
9196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9197 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9198 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9199 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9200
9201 // down when BTN_TOUCH is pressed, pressure defaults to 1
9202 processKey(mapper, BTN_TOUCH, 1);
9203 processSync(mapper);
9204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9205 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9206 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9207 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9208
9209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9210 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9212 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9213
9214 // up when BTN_TOUCH is released, hover restored
9215 processKey(mapper, BTN_TOUCH, 0);
9216 processSync(mapper);
9217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9218 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9220 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9221
9222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9223 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9225 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9226
9227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9228 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9229 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9230 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9231
9232 // exit hover when pointer goes away
9233 processId(mapper, -1);
9234 processSync(mapper);
9235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9236 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9238 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9239}
9240
9241TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009242 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009243 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009244 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009245 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009246
9247 NotifyMotionArgs motionArgs;
9248
9249 // initially hovering because pressure is 0
9250 processId(mapper, 1);
9251 processPosition(mapper, 100, 200);
9252 processPressure(mapper, 0);
9253 processSync(mapper);
9254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9255 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9256 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9257 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9258
9259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9260 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9262 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9263
9264 // move a little
9265 processPosition(mapper, 150, 250);
9266 processSync(mapper);
9267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9268 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9270 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9271
9272 // down when pressure becomes non-zero
9273 processPressure(mapper, RAW_PRESSURE_MAX);
9274 processSync(mapper);
9275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9276 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9278 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9279
9280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9281 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9283 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9284
9285 // up when pressure becomes 0, hover restored
9286 processPressure(mapper, 0);
9287 processSync(mapper);
9288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9289 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9291 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9292
9293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9294 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9295 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9296 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9297
9298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9299 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9301 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9302
9303 // exit hover when pointer goes away
9304 processId(mapper, -1);
9305 processSync(mapper);
9306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9307 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9309 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9310}
9311
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009312/**
9313 * Set the input device port <--> display port associations, and check that the
9314 * events are routed to the display that matches the display port.
9315 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9316 */
9317TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009318 const std::string usb2 = "USB2";
9319 const uint8_t hdmi1 = 0;
9320 const uint8_t hdmi2 = 1;
9321 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009322 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009323
9324 addConfigurationProperty("touch.deviceType", "touchScreen");
9325 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009326 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009327
9328 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9329 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9330
9331 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9332 // for this input device is specified, and the matching viewport is not present,
9333 // the input device should be disabled (at the mapper level).
9334
9335 // Add viewport for display 2 on hdmi2
9336 prepareSecondaryDisplay(type, hdmi2);
9337 // Send a touch event
9338 processPosition(mapper, 100, 100);
9339 processSync(mapper);
9340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9341
9342 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009343 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009344 // Send a touch event again
9345 processPosition(mapper, 100, 100);
9346 processSync(mapper);
9347
9348 NotifyMotionArgs args;
9349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9350 ASSERT_EQ(DISPLAY_ID, args.displayId);
9351}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009352
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009353TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9354 addConfigurationProperty("touch.deviceType", "touchScreen");
9355 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009356 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009357
9358 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9359
Michael Wrighta9cf4192022-12-01 23:46:39 +00009360 prepareDisplay(ui::ROTATION_0);
9361 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009362
9363 // Send a touch event
9364 processPosition(mapper, 100, 100);
9365 processSync(mapper);
9366
9367 NotifyMotionArgs args;
9368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9369 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9370}
9371
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009372TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009373 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009374 std::shared_ptr<FakePointerController> fakePointerController =
9375 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009376 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009377 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009378 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009379
Garfield Tan888a6a42020-01-09 11:39:16 -08009380 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009381 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009382
Michael Wrighta9cf4192022-12-01 23:46:39 +00009383 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009384 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009385 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009386
Josep del Río2d8c79a2023-01-23 19:33:50 +00009387 // Check source is mouse that would obtain the PointerController.
9388 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009389
9390 NotifyMotionArgs motionArgs;
9391 processPosition(mapper, 100, 100);
9392 processSync(mapper);
9393
9394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9395 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9396 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9397}
9398
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009399/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009400 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9401 */
9402TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9403 addConfigurationProperty("touch.deviceType", "touchScreen");
9404 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009405 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009406
Michael Wrighta9cf4192022-12-01 23:46:39 +00009407 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009408 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9409 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9410 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9411 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009412
9413 NotifyMotionArgs args;
9414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9415 ASSERT_EQ(26, args.readTime);
9416
Harry Cutts33476232023-01-30 19:57:29 +00009417 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9418 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9419 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009420
9421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9422 ASSERT_EQ(33, args.readTime);
9423}
9424
9425/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009426 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9427 * events should not be delivered to the listener.
9428 */
9429TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9430 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009431 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009432 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009433 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009434 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009435 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009436 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009437
9438 NotifyMotionArgs motionArgs;
9439 processPosition(mapper, 100, 100);
9440 processSync(mapper);
9441
9442 mFakeListener->assertNotifyMotionWasNotCalled();
9443}
9444
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009445/**
9446 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9447 * the touch mapper can process the events and the events can be delivered to the listener.
9448 */
9449TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9450 addConfigurationProperty("touch.deviceType", "touchScreen");
9451 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009452 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009453 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009454 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009455 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009456 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009457
9458 NotifyMotionArgs motionArgs;
9459 processPosition(mapper, 100, 100);
9460 processSync(mapper);
9461
9462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9463 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9464}
9465
Josh Thielene986aed2023-06-01 14:17:30 +00009466/**
9467 * When the viewport is deactivated (isActive transitions from true to false),
9468 * and touch.enableForInactiveViewport is false, touches prior to the transition
9469 * should be cancelled.
9470 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08009471TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9472 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009473 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009474 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009475 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009476 std::optional<DisplayViewport> optionalDisplayViewport =
9477 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9478 ASSERT_TRUE(optionalDisplayViewport.has_value());
9479 DisplayViewport displayViewport = *optionalDisplayViewport;
9480
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009481 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009482 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009483 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08009484
9485 // Finger down
9486 int32_t x = 100, y = 100;
9487 processPosition(mapper, x, y);
9488 processSync(mapper);
9489
9490 NotifyMotionArgs motionArgs;
9491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9492 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9493
9494 // Deactivate display viewport
9495 displayViewport.isActive = false;
9496 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009497 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009498
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009499 // The ongoing touch should be canceled immediately
9500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9501 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9502
9503 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009504 x += 10, y += 10;
9505 processPosition(mapper, x, y);
9506 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009508
9509 // Reactivate display viewport
9510 displayViewport.isActive = true;
9511 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009512 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009513
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009514 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009515 x += 10, y += 10;
9516 processPosition(mapper, x, y);
9517 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9519 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009520}
9521
Josh Thielene986aed2023-06-01 14:17:30 +00009522/**
9523 * When the viewport is deactivated (isActive transitions from true to false),
9524 * and touch.enableForInactiveViewport is true, touches prior to the transition
9525 * should not be cancelled.
9526 */
9527TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
9528 addConfigurationProperty("touch.deviceType", "touchScreen");
9529 addConfigurationProperty("touch.enableForInactiveViewport", "1");
9530 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
9531 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
9532 std::optional<DisplayViewport> optionalDisplayViewport =
9533 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9534 ASSERT_TRUE(optionalDisplayViewport.has_value());
9535 DisplayViewport displayViewport = *optionalDisplayViewport;
9536
9537 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9538 prepareAxes(POSITION);
9539 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
9540
9541 // Finger down
9542 int32_t x = 100, y = 100;
9543 processPosition(mapper, x, y);
9544 processSync(mapper);
9545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9546 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9547
9548 // Deactivate display viewport
9549 displayViewport.isActive = false;
9550 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9551 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9552
9553 // The ongoing touch should not be canceled
9554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9555
9556 // Finger move is not ignored
9557 x += 10, y += 10;
9558 processPosition(mapper, x, y);
9559 processSync(mapper);
9560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9561 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
9562
9563 // Reactivate display viewport
9564 displayViewport.isActive = true;
9565 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9566 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9567
9568 // Finger move continues and does not start new gesture
9569 x += 10, y += 10;
9570 processPosition(mapper, x, y);
9571 processSync(mapper);
9572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9573 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
9574}
9575
Arthur Hung7c645402019-01-25 17:45:42 +08009576TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9577 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009578 prepareAxes(POSITION | ID | SLOT);
9579 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009580 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009581
9582 // Create the second touch screen device, and enable multi fingers.
9583 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009584 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009585 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009586 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009587 std::shared_ptr<InputDevice> device2 =
9588 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009589 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009590
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009591 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009592 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009593 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009594 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009595 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009596 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009597 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009598 /*flat=*/0, /*fuzz=*/0);
9599 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009600 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9601 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009602
9603 // Setup the second touch screen device.
Arpit Singha8c236b2023-04-25 13:56:05 +00009604 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
9605 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
9606 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009607 std::list<NotifyArgs> unused =
9608 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009609 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009610 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009611
9612 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009613 std::shared_ptr<FakePointerController> fakePointerController =
9614 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009615 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009616
9617 // Setup policy for associated displays and show touches.
9618 const uint8_t hdmi1 = 0;
9619 const uint8_t hdmi2 = 1;
9620 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9621 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9622 mFakePolicy->setShowTouches(true);
9623
9624 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009625 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009626 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009627
9628 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009629 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009630 InputReaderConfiguration::Change::DISPLAY_INFO |
9631 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009632
9633 // Two fingers down at default display.
9634 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9635 processPosition(mapper, x1, y1);
9636 processId(mapper, 1);
9637 processSlot(mapper, 1);
9638 processPosition(mapper, x2, y2);
9639 processId(mapper, 2);
9640 processSync(mapper);
9641
9642 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9643 fakePointerController->getSpots().find(DISPLAY_ID);
9644 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9645 ASSERT_EQ(size_t(2), iter->second.size());
9646
9647 // Two fingers down at second display.
9648 processPosition(mapper2, x1, y1);
9649 processId(mapper2, 1);
9650 processSlot(mapper2, 1);
9651 processPosition(mapper2, x2, y2);
9652 processId(mapper2, 2);
9653 processSync(mapper2);
9654
9655 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9656 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9657 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009658
9659 // Disable the show touches configuration and ensure the spots are cleared.
9660 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009661 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009662 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009663
9664 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009665}
9666
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009667TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009668 prepareAxes(POSITION);
9669 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009670 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009671 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009672
9673 NotifyMotionArgs motionArgs;
9674 // Unrotated video frame
9675 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9676 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009677 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009678 processPosition(mapper, 100, 200);
9679 processSync(mapper);
9680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9681 ASSERT_EQ(frames, motionArgs.videoFrames);
9682
9683 // Subsequent touch events should not have any videoframes
9684 // This is implemented separately in FakeEventHub,
9685 // but that should match the behaviour of TouchVideoDevice.
9686 processPosition(mapper, 200, 200);
9687 processSync(mapper);
9688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9689 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9690}
9691
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009692TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009693 prepareAxes(POSITION);
9694 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009695 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009696 // Unrotated video frame
9697 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9698 NotifyMotionArgs motionArgs;
9699
9700 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009701 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009702 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9703 clearViewports();
9704 prepareDisplay(orientation);
9705 std::vector<TouchVideoFrame> frames{frame};
9706 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9707 processPosition(mapper, 100, 200);
9708 processSync(mapper);
9709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9710 ASSERT_EQ(frames, motionArgs.videoFrames);
9711 }
9712}
9713
9714TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9715 prepareAxes(POSITION);
9716 addConfigurationProperty("touch.deviceType", "touchScreen");
9717 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9718 // orientation-aware are affected by display rotation.
9719 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009720 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009721 // Unrotated video frame
9722 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9723 NotifyMotionArgs motionArgs;
9724
9725 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009726 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009727 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9728 clearViewports();
9729 prepareDisplay(orientation);
9730 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009731 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009732 processPosition(mapper, 100, 200);
9733 processSync(mapper);
9734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009735 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9736 // compared to the display. This is so that when the window transform (which contains the
9737 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9738 // window's coordinate space.
9739 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009740 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009741
9742 // Release finger.
9743 processSync(mapper);
9744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009745 }
9746}
9747
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009748TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009749 prepareAxes(POSITION);
9750 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009751 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009752 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9753 // so mix these.
9754 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9755 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9756 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9757 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9758 NotifyMotionArgs motionArgs;
9759
Michael Wrighta9cf4192022-12-01 23:46:39 +00009760 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009761 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009762 processPosition(mapper, 100, 200);
9763 processSync(mapper);
9764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009765 ASSERT_EQ(frames, motionArgs.videoFrames);
9766}
9767
9768TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9769 prepareAxes(POSITION);
9770 addConfigurationProperty("touch.deviceType", "touchScreen");
9771 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9772 // orientation-aware are affected by display rotation.
9773 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009774 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009775 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9776 // so mix these.
9777 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9778 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9779 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9780 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9781 NotifyMotionArgs motionArgs;
9782
Michael Wrighta9cf4192022-12-01 23:46:39 +00009783 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009784 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9785 processPosition(mapper, 100, 200);
9786 processSync(mapper);
9787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9788 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9789 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9790 // compared to the display. This is so that when the window transform (which contains the
9791 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9792 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009793 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009794 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009795 ASSERT_EQ(frames, motionArgs.videoFrames);
9796}
9797
Arthur Hung9da14732019-09-02 16:16:58 +08009798/**
9799 * If we had defined port associations, but the viewport is not ready, the touch device would be
9800 * expected to be disabled, and it should be enabled after the viewport has found.
9801 */
9802TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009803 constexpr uint8_t hdmi2 = 1;
9804 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009805 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009806
9807 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9808
9809 addConfigurationProperty("touch.deviceType", "touchScreen");
9810 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009811 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009812
9813 ASSERT_EQ(mDevice->isEnabled(), false);
9814
9815 // Add display on hdmi2, the device should be enabled and can receive touch event.
9816 prepareSecondaryDisplay(type, hdmi2);
9817 ASSERT_EQ(mDevice->isEnabled(), true);
9818
9819 // Send a touch event.
9820 processPosition(mapper, 100, 100);
9821 processSync(mapper);
9822
9823 NotifyMotionArgs args;
9824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9825 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9826}
9827
Arthur Hung421eb1c2020-01-16 00:09:42 +08009828TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009829 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009830 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009831 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009832 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009833
9834 NotifyMotionArgs motionArgs;
9835
9836 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9837 // finger down
9838 processId(mapper, 1);
9839 processPosition(mapper, x1, y1);
9840 processSync(mapper);
9841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9842 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009843 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009844
9845 // finger move
9846 processId(mapper, 1);
9847 processPosition(mapper, x2, y2);
9848 processSync(mapper);
9849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009851 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009852
9853 // finger up.
9854 processId(mapper, -1);
9855 processSync(mapper);
9856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9857 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009858 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009859
9860 // new finger down
9861 processId(mapper, 1);
9862 processPosition(mapper, x3, y3);
9863 processSync(mapper);
9864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9865 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009866 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009867}
9868
9869/**
arthurhungcc7f9802020-04-30 17:55:40 +08009870 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9871 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009872 */
arthurhungcc7f9802020-04-30 17:55:40 +08009873TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009874 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009875 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009876 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009877 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009878
9879 NotifyMotionArgs motionArgs;
9880
9881 // default tool type is finger
9882 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009883 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009884 processPosition(mapper, x1, y1);
9885 processSync(mapper);
9886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9887 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009888 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009889
9890 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9891 processToolType(mapper, MT_TOOL_PALM);
9892 processSync(mapper);
9893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9894 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9895
9896 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009897 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009898 processPosition(mapper, x2, y2);
9899 processSync(mapper);
9900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9901
9902 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009903 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009904 processSync(mapper);
9905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9906
9907 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009908 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009909 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009910 processPosition(mapper, x3, y3);
9911 processSync(mapper);
9912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9913 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009914 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009915}
9916
arthurhungbf89a482020-04-17 17:37:55 +08009917/**
arthurhungcc7f9802020-04-30 17:55:40 +08009918 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9919 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009920 */
arthurhungcc7f9802020-04-30 17:55:40 +08009921TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009922 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009923 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009924 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009925 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009926
9927 NotifyMotionArgs motionArgs;
9928
9929 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009930 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9931 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009932 processPosition(mapper, x1, y1);
9933 processSync(mapper);
9934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9935 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009936 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009937
9938 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009939 processSlot(mapper, SECOND_SLOT);
9940 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009941 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009942 processSync(mapper);
9943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009944 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009945 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009946
9947 // If the tool type of the first finger changes to MT_TOOL_PALM,
9948 // we expect to receive ACTION_POINTER_UP with cancel flag.
9949 processSlot(mapper, FIRST_SLOT);
9950 processId(mapper, FIRST_TRACKING_ID);
9951 processToolType(mapper, MT_TOOL_PALM);
9952 processSync(mapper);
9953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009954 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009955 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9956
9957 // The following MOVE events of second finger should be processed.
9958 processSlot(mapper, SECOND_SLOT);
9959 processId(mapper, SECOND_TRACKING_ID);
9960 processPosition(mapper, x2 + 1, y2 + 1);
9961 processSync(mapper);
9962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9963 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009964 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009965
9966 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9967 // it. Second finger receive move.
9968 processSlot(mapper, FIRST_SLOT);
9969 processId(mapper, INVALID_TRACKING_ID);
9970 processSync(mapper);
9971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9972 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009973 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009974
9975 // Second finger keeps moving.
9976 processSlot(mapper, SECOND_SLOT);
9977 processId(mapper, SECOND_TRACKING_ID);
9978 processPosition(mapper, x2 + 2, y2 + 2);
9979 processSync(mapper);
9980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009982 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009983
9984 // Second finger up.
9985 processId(mapper, INVALID_TRACKING_ID);
9986 processSync(mapper);
9987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9988 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9989 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9990}
9991
9992/**
9993 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9994 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9995 */
9996TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9997 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009998 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009999 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010000 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010001
10002 NotifyMotionArgs motionArgs;
10003
10004 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10005 // First finger down.
10006 processId(mapper, FIRST_TRACKING_ID);
10007 processPosition(mapper, x1, y1);
10008 processSync(mapper);
10009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10010 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010011 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010012
10013 // Second finger down.
10014 processSlot(mapper, SECOND_SLOT);
10015 processId(mapper, SECOND_TRACKING_ID);
10016 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +080010017 processSync(mapper);
10018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010019 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010020 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +080010021
arthurhungcc7f9802020-04-30 17:55:40 +080010022 // If the tool type of the first finger changes to MT_TOOL_PALM,
10023 // we expect to receive ACTION_POINTER_UP with cancel flag.
10024 processSlot(mapper, FIRST_SLOT);
10025 processId(mapper, FIRST_TRACKING_ID);
10026 processToolType(mapper, MT_TOOL_PALM);
10027 processSync(mapper);
10028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010029 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010030 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10031
10032 // Second finger keeps moving.
10033 processSlot(mapper, SECOND_SLOT);
10034 processId(mapper, SECOND_TRACKING_ID);
10035 processPosition(mapper, x2 + 1, y2 + 1);
10036 processSync(mapper);
10037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10039
10040 // second finger becomes palm, receive cancel due to only 1 finger is active.
10041 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010042 processToolType(mapper, MT_TOOL_PALM);
10043 processSync(mapper);
10044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10045 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10046
arthurhungcc7f9802020-04-30 17:55:40 +080010047 // third finger down.
10048 processSlot(mapper, THIRD_SLOT);
10049 processId(mapper, THIRD_TRACKING_ID);
10050 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +080010051 processPosition(mapper, x3, y3);
10052 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +080010053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10054 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010055 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010056 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010057
10058 // third finger move
10059 processId(mapper, THIRD_TRACKING_ID);
10060 processPosition(mapper, x3 + 1, y3 + 1);
10061 processSync(mapper);
10062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10063 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10064
10065 // first finger up, third finger receive move.
10066 processSlot(mapper, FIRST_SLOT);
10067 processId(mapper, INVALID_TRACKING_ID);
10068 processSync(mapper);
10069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10070 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010071 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010072
10073 // second finger up, third finger receive move.
10074 processSlot(mapper, SECOND_SLOT);
10075 processId(mapper, INVALID_TRACKING_ID);
10076 processSync(mapper);
10077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10078 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010079 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010080
10081 // third finger up.
10082 processSlot(mapper, THIRD_SLOT);
10083 processId(mapper, INVALID_TRACKING_ID);
10084 processSync(mapper);
10085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10086 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10087 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10088}
10089
10090/**
10091 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10092 * and the active finger could still be allowed to receive the events
10093 */
10094TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10095 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010096 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010097 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010098 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010099
10100 NotifyMotionArgs motionArgs;
10101
10102 // default tool type is finger
10103 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10104 processId(mapper, FIRST_TRACKING_ID);
10105 processPosition(mapper, x1, y1);
10106 processSync(mapper);
10107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10108 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010109 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010110
10111 // Second finger down.
10112 processSlot(mapper, SECOND_SLOT);
10113 processId(mapper, SECOND_TRACKING_ID);
10114 processPosition(mapper, x2, y2);
10115 processSync(mapper);
10116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010117 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010118 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010119
10120 // If the tool type of the second finger changes to MT_TOOL_PALM,
10121 // we expect to receive ACTION_POINTER_UP with cancel flag.
10122 processId(mapper, SECOND_TRACKING_ID);
10123 processToolType(mapper, MT_TOOL_PALM);
10124 processSync(mapper);
10125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010126 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010127 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10128
10129 // The following MOVE event should be processed.
10130 processSlot(mapper, FIRST_SLOT);
10131 processId(mapper, FIRST_TRACKING_ID);
10132 processPosition(mapper, x1 + 1, y1 + 1);
10133 processSync(mapper);
10134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10135 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010136 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010137
10138 // second finger up.
10139 processSlot(mapper, SECOND_SLOT);
10140 processId(mapper, INVALID_TRACKING_ID);
10141 processSync(mapper);
10142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10143 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10144
10145 // first finger keep moving
10146 processSlot(mapper, FIRST_SLOT);
10147 processId(mapper, FIRST_TRACKING_ID);
10148 processPosition(mapper, x1 + 2, y1 + 2);
10149 processSync(mapper);
10150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10152
10153 // first finger up.
10154 processId(mapper, INVALID_TRACKING_ID);
10155 processSync(mapper);
10156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10157 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10158 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010159}
10160
Arthur Hung9ad18942021-06-19 02:04:46 +000010161/**
10162 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10163 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10164 * cause slot be valid again.
10165 */
10166TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10167 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010168 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +000010169 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010170 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +000010171
10172 NotifyMotionArgs motionArgs;
10173
10174 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10175 // First finger down.
10176 processId(mapper, FIRST_TRACKING_ID);
10177 processPosition(mapper, x1, y1);
10178 processPressure(mapper, RAW_PRESSURE_MAX);
10179 processSync(mapper);
10180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10181 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010182 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010183
10184 // First finger move.
10185 processId(mapper, FIRST_TRACKING_ID);
10186 processPosition(mapper, x1 + 1, y1 + 1);
10187 processPressure(mapper, RAW_PRESSURE_MAX);
10188 processSync(mapper);
10189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010191 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010192
10193 // Second finger down.
10194 processSlot(mapper, SECOND_SLOT);
10195 processId(mapper, SECOND_TRACKING_ID);
10196 processPosition(mapper, x2, y2);
10197 processPressure(mapper, RAW_PRESSURE_MAX);
10198 processSync(mapper);
10199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010200 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010201 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010202
10203 // second finger up with some unexpected data.
10204 processSlot(mapper, SECOND_SLOT);
10205 processId(mapper, INVALID_TRACKING_ID);
10206 processPosition(mapper, x2, y2);
10207 processSync(mapper);
10208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010209 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010210 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010211
10212 // first finger up with some unexpected data.
10213 processSlot(mapper, FIRST_SLOT);
10214 processId(mapper, INVALID_TRACKING_ID);
10215 processPosition(mapper, x2, y2);
10216 processPressure(mapper, RAW_PRESSURE_MAX);
10217 processSync(mapper);
10218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10219 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010220 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010221}
10222
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010223TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10224 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010225 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010226 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010227 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010228
10229 // First finger down.
10230 processId(mapper, FIRST_TRACKING_ID);
10231 processPosition(mapper, 100, 200);
10232 processPressure(mapper, RAW_PRESSURE_MAX);
10233 processSync(mapper);
10234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10235 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10236
10237 // Second finger down.
10238 processSlot(mapper, SECOND_SLOT);
10239 processId(mapper, SECOND_TRACKING_ID);
10240 processPosition(mapper, 300, 400);
10241 processPressure(mapper, RAW_PRESSURE_MAX);
10242 processSync(mapper);
10243 ASSERT_NO_FATAL_FAILURE(
10244 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10245
10246 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010247 // preserved. Resetting should cancel the ongoing gesture.
10248 resetMapper(mapper, ARBITRARY_TIME);
10249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10250 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010251
10252 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10253 // the existing touch state to generate a down event.
10254 processPosition(mapper, 301, 302);
10255 processSync(mapper);
10256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10257 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10259 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10260
10261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10262}
10263
10264TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10265 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010266 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010267 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010268 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010269
10270 // First finger touches down and releases.
10271 processId(mapper, FIRST_TRACKING_ID);
10272 processPosition(mapper, 100, 200);
10273 processPressure(mapper, RAW_PRESSURE_MAX);
10274 processSync(mapper);
10275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10276 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10277 processId(mapper, INVALID_TRACKING_ID);
10278 processSync(mapper);
10279 ASSERT_NO_FATAL_FAILURE(
10280 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10281
10282 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10283 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010284 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10286
10287 // Send an empty sync frame. Since there are no pointers, no events are generated.
10288 processSync(mapper);
10289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10290}
10291
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010292TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010293 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010294 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010295 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010296 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010298
10299 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10300 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10301 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10302 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10303 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10304
10305 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010306 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010307 processId(mapper, FIRST_TRACKING_ID);
10308 processToolType(mapper, MT_TOOL_PEN);
10309 processPosition(mapper, 100, 200);
10310 processPressure(mapper, RAW_PRESSURE_MAX);
10311 processSync(mapper);
10312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10313 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10314 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010315 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010316
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010317 // Now that we know the device supports styluses, ensure that the device is re-configured with
10318 // the stylus source.
10319 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10320 {
10321 const auto& devices = mReader->getInputDevices();
10322 auto deviceInfo =
10323 std::find_if(devices.begin(), devices.end(),
10324 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10325 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10326 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10327 }
10328
10329 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10331
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010332 processId(mapper, INVALID_TRACKING_ID);
10333 processSync(mapper);
10334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10335 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10336 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010337 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010338}
10339
Seunghwan Choi356026c2023-02-01 14:37:25 +090010340TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10341 addConfigurationProperty("touch.deviceType", "touchScreen");
10342 prepareDisplay(ui::ROTATION_0);
10343 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10344 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10345 // indicate stylus presence dynamically.
10346 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10347 std::shared_ptr<FakePointerController> fakePointerController =
10348 std::make_shared<FakePointerController>();
10349 mFakePolicy->setPointerController(fakePointerController);
10350 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +000010351 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010352
10353 processId(mapper, FIRST_TRACKING_ID);
10354 processPressure(mapper, RAW_PRESSURE_MIN);
10355 processPosition(mapper, 100, 200);
10356 processToolType(mapper, MT_TOOL_PEN);
10357 processSync(mapper);
10358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10359 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010360 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010361 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10362 ASSERT_TRUE(fakePointerController->isPointerShown());
10363 ASSERT_NO_FATAL_FAILURE(
10364 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10365}
10366
10367TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10368 addConfigurationProperty("touch.deviceType", "touchScreen");
10369 prepareDisplay(ui::ROTATION_0);
10370 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10371 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10372 // indicate stylus presence dynamically.
10373 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10374 std::shared_ptr<FakePointerController> fakePointerController =
10375 std::make_shared<FakePointerController>();
10376 mFakePolicy->setPointerController(fakePointerController);
10377 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010378 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010379
10380 processId(mapper, FIRST_TRACKING_ID);
10381 processPressure(mapper, RAW_PRESSURE_MIN);
10382 processPosition(mapper, 100, 200);
10383 processToolType(mapper, MT_TOOL_PEN);
10384 processSync(mapper);
10385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10386 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010387 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010388 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10389 ASSERT_FALSE(fakePointerController->isPointerShown());
10390}
10391
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010392// --- MultiTouchInputMapperTest_ExternalDevice ---
10393
10394class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10395protected:
Chris Yea52ade12020-08-27 16:49:20 -070010396 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010397};
10398
10399/**
10400 * Expect fallback to internal viewport if device is external and external viewport is not present.
10401 */
10402TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10403 prepareAxes(POSITION);
10404 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010405 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010406 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010407
10408 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10409
10410 NotifyMotionArgs motionArgs;
10411
10412 // Expect the event to be sent to the internal viewport,
10413 // because an external viewport is not present.
10414 processPosition(mapper, 100, 100);
10415 processSync(mapper);
10416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10417 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10418
10419 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010420 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010421 processPosition(mapper, 100, 100);
10422 processSync(mapper);
10423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10424 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10425}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010426
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010427TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10428 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10429 std::shared_ptr<FakePointerController> fakePointerController =
10430 std::make_shared<FakePointerController>();
10431 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10432 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010433
10434 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010435 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010436 prepareAxes(POSITION | ID | SLOT);
10437 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10438 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10439 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010440 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010441 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010442
10443 // captured touchpad should be a touchpad source
10444 NotifyDeviceResetArgs resetArgs;
10445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10446 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10447
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010448 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010449
10450 const InputDeviceInfo::MotionRange* relRangeX =
10451 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10452 ASSERT_NE(relRangeX, nullptr);
10453 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10454 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10455 const InputDeviceInfo::MotionRange* relRangeY =
10456 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10457 ASSERT_NE(relRangeY, nullptr);
10458 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10459 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10460
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010461 // run captured pointer tests - note that this is unscaled, so input listener events should be
10462 // identical to what the hardware sends (accounting for any
10463 // calibration).
10464 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010465 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010466 processId(mapper, 1);
10467 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10468 processKey(mapper, BTN_TOUCH, 1);
10469 processSync(mapper);
10470
10471 // expect coord[0] to contain initial location of touch 0
10472 NotifyMotionArgs args;
10473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10474 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010475 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010476 ASSERT_EQ(0, args.pointerProperties[0].id);
10477 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10478 ASSERT_NO_FATAL_FAILURE(
10479 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10480
10481 // FINGER 1 DOWN
10482 processSlot(mapper, 1);
10483 processId(mapper, 2);
10484 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10485 processSync(mapper);
10486
10487 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010489 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010490 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010491 ASSERT_EQ(0, args.pointerProperties[0].id);
10492 ASSERT_EQ(1, args.pointerProperties[1].id);
10493 ASSERT_NO_FATAL_FAILURE(
10494 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10495 ASSERT_NO_FATAL_FAILURE(
10496 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10497
10498 // FINGER 1 MOVE
10499 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10500 processSync(mapper);
10501
10502 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10503 // from move
10504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10505 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10506 ASSERT_NO_FATAL_FAILURE(
10507 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10508 ASSERT_NO_FATAL_FAILURE(
10509 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10510
10511 // FINGER 0 MOVE
10512 processSlot(mapper, 0);
10513 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10514 processSync(mapper);
10515
10516 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10518 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10519 ASSERT_NO_FATAL_FAILURE(
10520 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10521 ASSERT_NO_FATAL_FAILURE(
10522 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10523
10524 // BUTTON DOWN
10525 processKey(mapper, BTN_LEFT, 1);
10526 processSync(mapper);
10527
10528 // touchinputmapper design sends a move before button press
10529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10530 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10532 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10533
10534 // BUTTON UP
10535 processKey(mapper, BTN_LEFT, 0);
10536 processSync(mapper);
10537
10538 // touchinputmapper design sends a move after button release
10539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10540 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10543
10544 // FINGER 0 UP
10545 processId(mapper, -1);
10546 processSync(mapper);
10547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10548 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10549
10550 // FINGER 1 MOVE
10551 processSlot(mapper, 1);
10552 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10553 processSync(mapper);
10554
10555 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10557 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010558 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010559 ASSERT_EQ(1, args.pointerProperties[0].id);
10560 ASSERT_NO_FATAL_FAILURE(
10561 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10562
10563 // FINGER 1 UP
10564 processId(mapper, -1);
10565 processKey(mapper, BTN_TOUCH, 0);
10566 processSync(mapper);
10567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10568 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10569
Josep del Río2d8c79a2023-01-23 19:33:50 +000010570 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010571 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010572 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010574 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010575}
10576
10577TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10578 std::shared_ptr<FakePointerController> fakePointerController =
10579 std::make_shared<FakePointerController>();
10580 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10581 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010582
10583 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010584 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010585 prepareAxes(POSITION | ID | SLOT);
10586 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10587 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010588 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010589 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010590 // run uncaptured pointer tests - pushes out generic events
10591 // FINGER 0 DOWN
10592 processId(mapper, 3);
10593 processPosition(mapper, 100, 100);
10594 processKey(mapper, BTN_TOUCH, 1);
10595 processSync(mapper);
10596
10597 // start at (100,100), cursor should be at (0,0) * scale
10598 NotifyMotionArgs args;
10599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10600 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10601 ASSERT_NO_FATAL_FAILURE(
10602 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10603
10604 // FINGER 0 MOVE
10605 processPosition(mapper, 200, 200);
10606 processSync(mapper);
10607
10608 // compute scaling to help with touch position checking
10609 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10610 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10611 float scale =
10612 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10613
10614 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10616 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10618 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010619
10620 // BUTTON DOWN
10621 processKey(mapper, BTN_LEFT, 1);
10622 processSync(mapper);
10623
10624 // touchinputmapper design sends a move before button press
10625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10626 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10628 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10629
10630 // BUTTON UP
10631 processKey(mapper, BTN_LEFT, 0);
10632 processSync(mapper);
10633
10634 // touchinputmapper design sends a move after button release
10635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10636 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10638 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010639}
10640
10641TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10642 std::shared_ptr<FakePointerController> fakePointerController =
10643 std::make_shared<FakePointerController>();
10644
Michael Wrighta9cf4192022-12-01 23:46:39 +000010645 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010646 prepareAxes(POSITION | ID | SLOT);
10647 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010648 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010649 mFakePolicy->setPointerCapture(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010650 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010651
Josep del Río2d8c79a2023-01-23 19:33:50 +000010652 // uncaptured touchpad should be a pointer device
10653 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010654
Josep del Río2d8c79a2023-01-23 19:33:50 +000010655 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010656 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010657 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010658 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10659}
10660
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010661// --- BluetoothMultiTouchInputMapperTest ---
10662
10663class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10664protected:
10665 void SetUp() override {
10666 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10667 }
10668};
10669
10670TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10671 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010672 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010673 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010674 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010675
10676 nsecs_t kernelEventTime = ARBITRARY_TIME;
10677 nsecs_t expectedEventTime = ARBITRARY_TIME;
10678 // Touch down.
10679 processId(mapper, FIRST_TRACKING_ID);
10680 processPosition(mapper, 100, 200);
10681 processPressure(mapper, RAW_PRESSURE_MAX);
10682 processSync(mapper, ARBITRARY_TIME);
10683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10684 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10685
10686 // Process several events that come in quick succession, according to their timestamps.
10687 for (int i = 0; i < 3; i++) {
10688 constexpr static nsecs_t delta = ms2ns(1);
10689 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10690 kernelEventTime += delta;
10691 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10692
10693 processPosition(mapper, 101 + i, 201 + i);
10694 processSync(mapper, kernelEventTime);
10695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10696 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10697 WithEventTime(expectedEventTime))));
10698 }
10699
10700 // Release the touch.
10701 processId(mapper, INVALID_TRACKING_ID);
10702 processPressure(mapper, RAW_PRESSURE_MIN);
10703 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10705 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10706 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10707}
10708
10709// --- MultiTouchPointerModeTest ---
10710
HQ Liue6983c72022-04-19 22:14:56 +000010711class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10712protected:
10713 float mPointerMovementScale;
10714 float mPointerXZoomScale;
10715 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10716 addConfigurationProperty("touch.deviceType", "pointer");
10717 std::shared_ptr<FakePointerController> fakePointerController =
10718 std::make_shared<FakePointerController>();
10719 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10720 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010721 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010722
10723 prepareAxes(POSITION);
10724 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10725 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10726 // needs to be disabled, and the pointer gesture needs to be enabled.
10727 mFakePolicy->setPointerCapture(false);
10728 mFakePolicy->setPointerGestureEnabled(true);
10729 mFakePolicy->setPointerController(fakePointerController);
10730
10731 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10732 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10733 mPointerMovementScale =
10734 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10735 mPointerXZoomScale =
10736 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10737 }
10738
10739 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10740 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10741 /*flat*/ 0,
10742 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10743 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10744 /*flat*/ 0,
10745 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10746 }
10747};
10748
10749/**
10750 * Two fingers down on a pointer mode touch pad. The width
10751 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10752 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10753 * be greater than the both value to be freeform gesture, so that after two
10754 * fingers start to move downwards, the gesture should be swipe.
10755 */
10756TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10757 // The min freeform gesture width is 25units/mm x 30mm = 750
10758 // which is greater than fraction of the diagnal length of the touchpad (349).
10759 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010760 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010761 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010762 NotifyMotionArgs motionArgs;
10763
10764 // Two fingers down at once.
10765 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10766 // Pointer's initial position is used the [0,0] coordinate.
10767 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10768
10769 processId(mapper, FIRST_TRACKING_ID);
10770 processPosition(mapper, x1, y1);
10771 processMTSync(mapper);
10772 processId(mapper, SECOND_TRACKING_ID);
10773 processPosition(mapper, x2, y2);
10774 processMTSync(mapper);
10775 processSync(mapper);
10776
10777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010778 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010779 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010780 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010781 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010782 ASSERT_NO_FATAL_FAILURE(
10783 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10784
10785 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10786 // that there should be 1 pointer.
10787 int32_t movingDistance = 200;
10788 y1 += movingDistance;
10789 y2 += movingDistance;
10790
10791 processId(mapper, FIRST_TRACKING_ID);
10792 processPosition(mapper, x1, y1);
10793 processMTSync(mapper);
10794 processId(mapper, SECOND_TRACKING_ID);
10795 processPosition(mapper, x2, y2);
10796 processMTSync(mapper);
10797 processSync(mapper);
10798
10799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010800 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010801 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010802 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010803 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10805 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10806 0, 0, 0, 0));
10807}
10808
10809/**
10810 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10811 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10812 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10813 * value to be freeform gesture, so that after two fingers start to move downwards,
10814 * the gesture should be swipe.
10815 */
10816TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10817 // The min freeform gesture width is 5units/mm x 30mm = 150
10818 // which is greater than fraction of the diagnal length of the touchpad (349).
10819 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010820 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +000010821 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010822 NotifyMotionArgs motionArgs;
10823
10824 // Two fingers down at once.
10825 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10826 // Pointer's initial position is used the [0,0] coordinate.
10827 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10828
10829 processId(mapper, FIRST_TRACKING_ID);
10830 processPosition(mapper, x1, y1);
10831 processMTSync(mapper);
10832 processId(mapper, SECOND_TRACKING_ID);
10833 processPosition(mapper, x2, y2);
10834 processMTSync(mapper);
10835 processSync(mapper);
10836
10837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010838 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010839 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010840 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010841 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010842 ASSERT_NO_FATAL_FAILURE(
10843 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10844
10845 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10846 // and there should be 1 pointer.
10847 int32_t movingDistance = 200;
10848 y1 += movingDistance;
10849 y2 += movingDistance;
10850
10851 processId(mapper, FIRST_TRACKING_ID);
10852 processPosition(mapper, x1, y1);
10853 processMTSync(mapper);
10854 processId(mapper, SECOND_TRACKING_ID);
10855 processPosition(mapper, x2, y2);
10856 processMTSync(mapper);
10857 processSync(mapper);
10858
10859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010860 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010861 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010862 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010863 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010864 // New coordinate is the scaled relative coordinate from the initial coordinate.
10865 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10866 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10867 0, 0, 0, 0));
10868}
10869
10870/**
10871 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10872 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10873 * freeform gestures after two fingers start to move downwards.
10874 */
10875TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010876 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010877 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010878
10879 NotifyMotionArgs motionArgs;
10880
10881 // Two fingers down at once. Wider than the max swipe width.
10882 // The gesture is expected to be PRESS, then transformed to FREEFORM
10883 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10884
10885 processId(mapper, FIRST_TRACKING_ID);
10886 processPosition(mapper, x1, y1);
10887 processMTSync(mapper);
10888 processId(mapper, SECOND_TRACKING_ID);
10889 processPosition(mapper, x2, y2);
10890 processMTSync(mapper);
10891 processSync(mapper);
10892
10893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010894 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010895 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010896 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010897 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010898 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10899 ASSERT_NO_FATAL_FAILURE(
10900 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10901
10902 int32_t movingDistance = 200;
10903
10904 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10905 // then two down events for two pointers.
10906 y1 += movingDistance;
10907 y2 += movingDistance;
10908
10909 processId(mapper, FIRST_TRACKING_ID);
10910 processPosition(mapper, x1, y1);
10911 processMTSync(mapper);
10912 processId(mapper, SECOND_TRACKING_ID);
10913 processPosition(mapper, x2, y2);
10914 processMTSync(mapper);
10915 processSync(mapper);
10916
10917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10918 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010919 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010920 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010922 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010923 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010924 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010926 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010927 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010928 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010929 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010930 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010931 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010932 // Two pointers' scaled relative coordinates from their initial centroid.
10933 // Initial y coordinates are 0 as y1 and y2 have the same value.
10934 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10935 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10936 // When pointers move, the new coordinates equal to the initial coordinates plus
10937 // scaled moving distance.
10938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10939 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10940 0, 0, 0, 0));
10941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10942 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10943 0, 0, 0, 0));
10944
10945 // Move two fingers down again, expect one MOVE motion event.
10946 y1 += movingDistance;
10947 y2 += movingDistance;
10948
10949 processId(mapper, FIRST_TRACKING_ID);
10950 processPosition(mapper, x1, y1);
10951 processMTSync(mapper);
10952 processId(mapper, SECOND_TRACKING_ID);
10953 processPosition(mapper, x2, y2);
10954 processMTSync(mapper);
10955 processSync(mapper);
10956
10957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010958 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010960 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010961 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10963 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10964 0, 0, 0, 0, 0));
10965 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10966 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10967 0, 0, 0, 0, 0));
10968}
10969
Harry Cutts39b7ca22022-10-05 15:55:48 +000010970TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010971 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010972 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010973 NotifyMotionArgs motionArgs;
10974
10975 // Place two fingers down.
10976 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10977
10978 processId(mapper, FIRST_TRACKING_ID);
10979 processPosition(mapper, x1, y1);
10980 processMTSync(mapper);
10981 processId(mapper, SECOND_TRACKING_ID);
10982 processPosition(mapper, x2, y2);
10983 processMTSync(mapper);
10984 processSync(mapper);
10985
10986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010987 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010988 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10989 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10990 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10991 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10992
10993 // Move the two fingers down and to the left.
10994 int32_t movingDistance = 200;
10995 x1 -= movingDistance;
10996 y1 += movingDistance;
10997 x2 -= movingDistance;
10998 y2 += movingDistance;
10999
11000 processId(mapper, FIRST_TRACKING_ID);
11001 processPosition(mapper, x1, y1);
11002 processMTSync(mapper);
11003 processId(mapper, SECOND_TRACKING_ID);
11004 processPosition(mapper, x2, y2);
11005 processMTSync(mapper);
11006 processSync(mapper);
11007
11008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011009 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000011010 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11011 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
11012 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
11013 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
11014}
11015
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011016TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000011017 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011018 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000011019 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
11021
11022 // Start a stylus gesture.
11023 processKey(mapper, BTN_TOOL_PEN, 1);
11024 processId(mapper, FIRST_TRACKING_ID);
11025 processPosition(mapper, 100, 200);
11026 processSync(mapper);
11027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11028 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
11029 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011030 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011031 // TODO(b/257078296): Pointer mode generates extra event.
11032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11033 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11034 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011035 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11037
11038 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
11039 // gesture should be disabled.
11040 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
11041 viewport->isActive = false;
11042 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000011043 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11045 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11046 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011047 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011048 // TODO(b/257078296): Pointer mode generates extra event.
11049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11050 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11051 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011052 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11054}
11055
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011056// --- JoystickInputMapperTest ---
11057
11058class JoystickInputMapperTest : public InputMapperTest {
11059protected:
11060 static const int32_t RAW_X_MIN;
11061 static const int32_t RAW_X_MAX;
11062 static const int32_t RAW_Y_MIN;
11063 static const int32_t RAW_Y_MAX;
11064
11065 void SetUp() override {
11066 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
11067 }
11068 void prepareAxes() {
11069 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
11070 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
11071 }
11072
11073 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
11074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
11075 }
11076
11077 void processSync(JoystickInputMapper& mapper) {
11078 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
11079 }
11080
Michael Wrighta9cf4192022-12-01 23:46:39 +000011081 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011082 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
11083 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
11084 NO_PORT, ViewportType::VIRTUAL);
11085 }
11086};
11087
11088const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
11089const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
11090const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11091const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11092
11093TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11094 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000011095 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011096
11097 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11098
Michael Wrighta9cf4192022-12-01 23:46:39 +000011099 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011100
11101 // Send an axis event
11102 processAxis(mapper, ABS_X, 100);
11103 processSync(mapper);
11104
11105 NotifyMotionArgs args;
11106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11107 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11108
11109 // Send another axis event
11110 processAxis(mapper, ABS_Y, 100);
11111 processSync(mapper);
11112
11113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11114 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11115}
11116
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011117// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011118
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011119class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011120protected:
11121 static const char* DEVICE_NAME;
11122 static const char* DEVICE_LOCATION;
11123 static const int32_t DEVICE_ID;
11124 static const int32_t DEVICE_GENERATION;
11125 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011126 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011127 static const int32_t EVENTHUB_ID;
11128
11129 std::shared_ptr<FakeEventHub> mFakeEventHub;
11130 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011131 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011132 std::unique_ptr<InstrumentedInputReader> mReader;
11133 std::shared_ptr<InputDevice> mDevice;
11134
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011135 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011136 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011137 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011138 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011139 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011140 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011141 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11142 }
11143
11144 void SetUp() override { SetUp(DEVICE_CLASSES); }
11145
11146 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011147 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011148 mFakePolicy.clear();
11149 }
11150
Chris Yee2b1e5c2021-03-10 22:45:12 -080011151 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11152 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011153 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011154 InputDeviceIdentifier identifier;
11155 identifier.name = name;
11156 identifier.location = location;
11157 std::shared_ptr<InputDevice> device =
11158 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11159 identifier);
11160 mReader->pushNextDevice(device);
11161 mFakeEventHub->addDevice(eventHubId, name, classes);
11162 mReader->loopOnce();
11163 return device;
11164 }
11165
11166 template <class T, typename... Args>
11167 T& addControllerAndConfigure(Args... args) {
11168 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11169
11170 return controller;
11171 }
11172};
11173
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011174const char* PeripheralControllerTest::DEVICE_NAME = "device";
11175const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11176const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11177const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11178const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011179const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11180 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011181const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011182
11183// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011184class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011185protected:
11186 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011187 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011188 }
11189};
11190
11191TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011192 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011193
Harry Cuttsa5b71292022-11-28 12:56:17 +000011194 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11195 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11196 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011197}
11198
11199TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011200 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011201
Harry Cuttsa5b71292022-11-28 12:56:17 +000011202 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11203 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11204 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011205}
11206
11207// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011208class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011209protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011210 void SetUp() override {
11211 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11212 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011213};
11214
Chris Ye85758332021-05-16 23:05:17 -070011215TEST_F(LightControllerTest, MonoLight) {
11216 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011217 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011218 .maxBrightness = 255,
11219 .flags = InputLightClass::BRIGHTNESS,
11220 .path = ""};
11221 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011222
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011223 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011224 InputDeviceInfo info;
11225 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011226 std::vector<InputDeviceLightInfo> lights = info.getLights();
11227 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011228 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11229 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11230
11231 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11232 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11233}
11234
11235TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11236 RawLightInfo infoMono = {.id = 1,
11237 .name = "mono_keyboard_backlight",
11238 .maxBrightness = 255,
11239 .flags = InputLightClass::BRIGHTNESS |
11240 InputLightClass::KEYBOARD_BACKLIGHT,
11241 .path = ""};
11242 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11243
11244 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11245 InputDeviceInfo info;
11246 controller.populateDeviceInfo(&info);
11247 std::vector<InputDeviceLightInfo> lights = info.getLights();
11248 ASSERT_EQ(1U, lights.size());
11249 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11250 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011251
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011252 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11253 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011254}
11255
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000011256TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
11257 RawLightInfo infoMono = {.id = 1,
11258 .name = "mono_light",
11259 .maxBrightness = 255,
11260 .flags = InputLightClass::BRIGHTNESS,
11261 .path = ""};
11262 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11263 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11264 "0,100,200");
11265
11266 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11267 std::list<NotifyArgs> unused =
11268 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11269 /*changes=*/{});
11270
11271 InputDeviceInfo info;
11272 controller.populateDeviceInfo(&info);
11273 std::vector<InputDeviceLightInfo> lights = info.getLights();
11274 ASSERT_EQ(1U, lights.size());
11275 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11276}
11277
11278TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
11279 RawLightInfo infoMono = {.id = 1,
11280 .name = "mono_keyboard_backlight",
11281 .maxBrightness = 255,
11282 .flags = InputLightClass::BRIGHTNESS |
11283 InputLightClass::KEYBOARD_BACKLIGHT,
11284 .path = ""};
11285 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11286
11287 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11288 std::list<NotifyArgs> unused =
11289 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11290 /*changes=*/{});
11291
11292 InputDeviceInfo info;
11293 controller.populateDeviceInfo(&info);
11294 std::vector<InputDeviceLightInfo> lights = info.getLights();
11295 ASSERT_EQ(1U, lights.size());
11296 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11297}
11298
11299TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
11300 RawLightInfo infoMono = {.id = 1,
11301 .name = "mono_keyboard_backlight",
11302 .maxBrightness = 255,
11303 .flags = InputLightClass::BRIGHTNESS |
11304 InputLightClass::KEYBOARD_BACKLIGHT,
11305 .path = ""};
11306 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11307 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11308 "0,100,200");
11309
11310 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11311 std::list<NotifyArgs> unused =
11312 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11313 /*changes=*/{});
11314
11315 InputDeviceInfo info;
11316 controller.populateDeviceInfo(&info);
11317 std::vector<InputDeviceLightInfo> lights = info.getLights();
11318 ASSERT_EQ(1U, lights.size());
11319 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
11320 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
11321 ASSERT_EQ(BrightnessLevel(0), *it);
11322 std::advance(it, 1);
11323 ASSERT_EQ(BrightnessLevel(100), *it);
11324 std::advance(it, 1);
11325 ASSERT_EQ(BrightnessLevel(200), *it);
11326}
11327
11328TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
11329 RawLightInfo infoMono = {.id = 1,
11330 .name = "mono_keyboard_backlight",
11331 .maxBrightness = 255,
11332 .flags = InputLightClass::BRIGHTNESS |
11333 InputLightClass::KEYBOARD_BACKLIGHT,
11334 .path = ""};
11335 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11336 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11337 "0,100,200,300,400,500");
11338
11339 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11340 std::list<NotifyArgs> unused =
11341 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11342 /*changes=*/{});
11343
11344 InputDeviceInfo info;
11345 controller.populateDeviceInfo(&info);
11346 std::vector<InputDeviceLightInfo> lights = info.getLights();
11347 ASSERT_EQ(1U, lights.size());
11348 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11349}
11350
Chris Yee2b1e5c2021-03-10 22:45:12 -080011351TEST_F(LightControllerTest, RGBLight) {
11352 RawLightInfo infoRed = {.id = 1,
11353 .name = "red",
11354 .maxBrightness = 255,
11355 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11356 .path = ""};
11357 RawLightInfo infoGreen = {.id = 2,
11358 .name = "green",
11359 .maxBrightness = 255,
11360 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11361 .path = ""};
11362 RawLightInfo infoBlue = {.id = 3,
11363 .name = "blue",
11364 .maxBrightness = 255,
11365 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11366 .path = ""};
11367 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11368 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11369 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11370
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011371 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011372 InputDeviceInfo info;
11373 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011374 std::vector<InputDeviceLightInfo> lights = info.getLights();
11375 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011376 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11377 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11378 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11379
11380 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11381 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11382}
11383
11384TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11385 RawLightInfo infoRed = {.id = 1,
11386 .name = "red_keyboard_backlight",
11387 .maxBrightness = 255,
11388 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11389 InputLightClass::KEYBOARD_BACKLIGHT,
11390 .path = ""};
11391 RawLightInfo infoGreen = {.id = 2,
11392 .name = "green_keyboard_backlight",
11393 .maxBrightness = 255,
11394 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11395 InputLightClass::KEYBOARD_BACKLIGHT,
11396 .path = ""};
11397 RawLightInfo infoBlue = {.id = 3,
11398 .name = "blue_keyboard_backlight",
11399 .maxBrightness = 255,
11400 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11401 InputLightClass::KEYBOARD_BACKLIGHT,
11402 .path = ""};
11403 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11404 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11405 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11406
11407 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11408 InputDeviceInfo info;
11409 controller.populateDeviceInfo(&info);
11410 std::vector<InputDeviceLightInfo> lights = info.getLights();
11411 ASSERT_EQ(1U, lights.size());
11412 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11413 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11414 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11415
11416 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11417 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11418}
11419
11420TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11421 RawLightInfo infoRed = {.id = 1,
11422 .name = "red",
11423 .maxBrightness = 255,
11424 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11425 .path = ""};
11426 RawLightInfo infoGreen = {.id = 2,
11427 .name = "green",
11428 .maxBrightness = 255,
11429 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11430 .path = ""};
11431 RawLightInfo infoBlue = {.id = 3,
11432 .name = "blue",
11433 .maxBrightness = 255,
11434 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11435 .path = ""};
11436 RawLightInfo infoGlobal = {.id = 3,
11437 .name = "global_keyboard_backlight",
11438 .maxBrightness = 255,
11439 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11440 InputLightClass::KEYBOARD_BACKLIGHT,
11441 .path = ""};
11442 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11443 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11444 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11445 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11446
11447 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11448 InputDeviceInfo info;
11449 controller.populateDeviceInfo(&info);
11450 std::vector<InputDeviceLightInfo> lights = info.getLights();
11451 ASSERT_EQ(1U, lights.size());
11452 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11453 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11454 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011455
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011456 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11457 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011458}
11459
11460TEST_F(LightControllerTest, MultiColorRGBLight) {
11461 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011462 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011463 .maxBrightness = 255,
11464 .flags = InputLightClass::BRIGHTNESS |
11465 InputLightClass::MULTI_INTENSITY |
11466 InputLightClass::MULTI_INDEX,
11467 .path = ""};
11468
11469 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11470
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011471 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011472 InputDeviceInfo info;
11473 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011474 std::vector<InputDeviceLightInfo> lights = info.getLights();
11475 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011476 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11477 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11478 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11479
11480 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11481 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11482}
11483
11484TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11485 RawLightInfo infoColor = {.id = 1,
11486 .name = "multi_color_keyboard_backlight",
11487 .maxBrightness = 255,
11488 .flags = InputLightClass::BRIGHTNESS |
11489 InputLightClass::MULTI_INTENSITY |
11490 InputLightClass::MULTI_INDEX |
11491 InputLightClass::KEYBOARD_BACKLIGHT,
11492 .path = ""};
11493
11494 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11495
11496 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11497 InputDeviceInfo info;
11498 controller.populateDeviceInfo(&info);
11499 std::vector<InputDeviceLightInfo> lights = info.getLights();
11500 ASSERT_EQ(1U, lights.size());
11501 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11502 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11503 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011504
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011505 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11506 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011507}
11508
11509TEST_F(LightControllerTest, PlayerIdLight) {
11510 RawLightInfo info1 = {.id = 1,
11511 .name = "player1",
11512 .maxBrightness = 255,
11513 .flags = InputLightClass::BRIGHTNESS,
11514 .path = ""};
11515 RawLightInfo info2 = {.id = 2,
11516 .name = "player2",
11517 .maxBrightness = 255,
11518 .flags = InputLightClass::BRIGHTNESS,
11519 .path = ""};
11520 RawLightInfo info3 = {.id = 3,
11521 .name = "player3",
11522 .maxBrightness = 255,
11523 .flags = InputLightClass::BRIGHTNESS,
11524 .path = ""};
11525 RawLightInfo info4 = {.id = 4,
11526 .name = "player4",
11527 .maxBrightness = 255,
11528 .flags = InputLightClass::BRIGHTNESS,
11529 .path = ""};
11530 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11531 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11532 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11533 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11534
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011535 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011536 InputDeviceInfo info;
11537 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011538 std::vector<InputDeviceLightInfo> lights = info.getLights();
11539 ASSERT_EQ(1U, lights.size());
11540 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011541 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11542 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011543
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011544 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11545 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11546 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011547}
11548
Michael Wrightd02c5b62014-02-10 15:10:22 -080011549} // namespace android