blob: 6539593217ee33de1696e176cd961bab9678d561 [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 Pradhanb08a0e82023-09-14 22:28:32 +000094static constexpr uint32_t STYLUS_FUSION_SOURCE =
95 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
96
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000097// Minimum timestamp separation between subsequent input events from a Bluetooth device.
98static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
99// Maximum smoothing time delta so that we don't generate events too far into the future.
100constexpr static nsecs_t MAX_BLUETOOTH_SMOOTHING_DELTA = ms2ns(32);
101
Michael Wrightd02c5b62014-02-10 15:10:22 -0800102template<typename T>
103static inline T min(T a, T b) {
104 return a < b ? a : b;
105}
106
107static inline float avg(float x, float y) {
108 return (x + y) / 2;
109}
110
Chris Ye3fdbfef2021-01-06 18:45:18 -0800111// Mapping for light color name and the light color
112const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
113 {"green", LightColor::GREEN},
114 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115
Michael Wrighta9cf4192022-12-01 23:46:39 +0000116static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700117 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000118 case ui::ROTATION_90:
119 return ui::ROTATION_270;
120 case ui::ROTATION_270:
121 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700122 default:
123 return orientation;
124 }
125}
126
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800127static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
128 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000129 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800130
131 const InputDeviceInfo::MotionRange* motionRange =
132 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
133 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
134}
135
136static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
137 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000138 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800139
140 const InputDeviceInfo::MotionRange* motionRange =
141 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
142 ASSERT_EQ(nullptr, motionRange);
143}
144
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700145[[maybe_unused]] static void dumpReader(InputReader& reader) {
146 std::string dump;
147 reader.dump(dump);
148 std::istringstream iss(dump);
149 for (std::string line; std::getline(iss, line);) {
150 ALOGE("%s", line.c_str());
151 std::this_thread::sleep_for(std::chrono::milliseconds(1));
152 }
153}
154
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155// --- FakeInputMapper ---
156
157class FakeInputMapper : public InputMapper {
158 uint32_t mSources;
159 int32_t mKeyboardType;
160 int32_t mMetaState;
161 KeyedVector<int32_t, int32_t> mKeyCodeStates;
162 KeyedVector<int32_t, int32_t> mScanCodeStates;
163 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100164 // fake mapping which would normally come from keyCharacterMap
165 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800166 std::vector<int32_t> mSupportedKeyCodes;
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700167 std::list<NotifyArgs> mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700169 std::mutex mLock;
170 std::condition_variable mStateChangedCondition;
171 bool mConfigureWasCalled GUARDED_BY(mLock);
172 bool mResetWasCalled GUARDED_BY(mLock);
173 bool mProcessWasCalled GUARDED_BY(mLock);
174 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175
Arthur Hungc23540e2018-11-29 20:42:11 +0800176 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000178 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
179 uint32_t sources)
180 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800181 mSources(sources),
182 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800184 mConfigureWasCalled(false),
185 mResetWasCalled(false),
186 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800187
Chris Yea52ade12020-08-27 16:49:20 -0700188 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800189
190 void setKeyboardType(int32_t keyboardType) {
191 mKeyboardType = keyboardType;
192 }
193
194 void setMetaState(int32_t metaState) {
195 mMetaState = metaState;
196 }
197
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700198 // Sets the return value for the `process` call.
199 void setProcessResult(std::list<NotifyArgs> notifyArgs) {
200 mProcessResult.clear();
201 for (auto notifyArg : notifyArgs) {
202 mProcessResult.push_back(notifyArg);
203 }
204 }
205
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700207 std::unique_lock<std::mutex> lock(mLock);
208 base::ScopedLockAssertion assumeLocked(mLock);
209 const bool configureCalled =
210 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
211 return mConfigureWasCalled;
212 });
213 if (!configureCalled) {
214 FAIL() << "Expected configure() to have been called.";
215 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216 mConfigureWasCalled = false;
217 }
218
219 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700220 std::unique_lock<std::mutex> lock(mLock);
221 base::ScopedLockAssertion assumeLocked(mLock);
222 const bool resetCalled =
223 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
224 return mResetWasCalled;
225 });
226 if (!resetCalled) {
227 FAIL() << "Expected reset() to have been called.";
228 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800229 mResetWasCalled = false;
230 }
231
Yi Kong9b14ac62018-07-17 13:48:38 -0700232 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700233 std::unique_lock<std::mutex> lock(mLock);
234 base::ScopedLockAssertion assumeLocked(mLock);
235 const bool processCalled =
236 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
237 return mProcessWasCalled;
238 });
239 if (!processCalled) {
240 FAIL() << "Expected process() to have been called.";
241 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800242 if (outLastEvent) {
243 *outLastEvent = mLastEvent;
244 }
245 mProcessWasCalled = false;
246 }
247
248 void setKeyCodeState(int32_t keyCode, int32_t state) {
249 mKeyCodeStates.replaceValueFor(keyCode, state);
250 }
251
252 void setScanCodeState(int32_t scanCode, int32_t state) {
253 mScanCodeStates.replaceValueFor(scanCode, state);
254 }
255
256 void setSwitchState(int32_t switchCode, int32_t state) {
257 mSwitchStates.replaceValueFor(switchCode, state);
258 }
259
260 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800261 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800262 }
263
Philip Junker4af3b3d2021-12-14 10:36:55 +0100264 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
265 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
266 }
267
Michael Wrightd02c5b62014-02-10 15:10:22 -0800268private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100269 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270
Harry Cuttsd02ea102023-03-17 18:21:30 +0000271 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800272 InputMapper::populateDeviceInfo(deviceInfo);
273
274 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000275 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800276 }
277 }
278
Arpit Singhed6c3de2023-04-05 19:24:37 +0000279 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000280 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700281 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800282 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800283
284 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800285 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000286 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000287 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800288 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700289
290 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700291 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800292 }
293
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700294 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700295 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700297 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700298 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800299 }
300
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700301 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700302 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303 mLastEvent = *rawEvent;
304 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700305 mStateChangedCondition.notify_all();
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700306 return mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800307 }
308
Chris Yea52ade12020-08-27 16:49:20 -0700309 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800310 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
311 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
312 }
313
Philip Junker4af3b3d2021-12-14 10:36:55 +0100314 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
315 auto it = mKeyCodeMapping.find(locationKeyCode);
316 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
317 }
318
Chris Yea52ade12020-08-27 16:49:20 -0700319 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800320 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
321 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
322 }
323
Chris Yea52ade12020-08-27 16:49:20 -0700324 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325 ssize_t index = mSwitchStates.indexOfKey(switchCode);
326 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
327 }
328
Chris Yea52ade12020-08-27 16:49:20 -0700329 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700330 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700331 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700332 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800333 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
334 if (keyCodes[i] == mSupportedKeyCodes[j]) {
335 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336 }
337 }
338 }
Chris Yea52ade12020-08-27 16:49:20 -0700339 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800340 return result;
341 }
342
343 virtual int32_t getMetaState() {
344 return mMetaState;
345 }
346
347 virtual void fadePointer() {
348 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800349
350 virtual std::optional<int32_t> getAssociatedDisplay() {
351 if (mViewport) {
352 return std::make_optional(mViewport->displayId);
353 }
354 return std::nullopt;
355 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800356};
357
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700358// --- InputReaderPolicyTest ---
359class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700360protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700361 sp<FakeInputReaderPolicy> mFakePolicy;
362
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700363 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700364 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700365};
366
367/**
368 * Check that empty set of viewports is an acceptable configuration.
369 * Also try to get internal viewport two different ways - by type and by uniqueId.
370 *
371 * There will be confusion if two viewports with empty uniqueId and identical type are present.
372 * Such configuration is not currently allowed.
373 */
374TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700375 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700376
377 // We didn't add any viewports yet, so there shouldn't be any.
378 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100379 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700380 ASSERT_FALSE(internalViewport);
381
382 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000383 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000384 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700385
386 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700387 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700388 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100389 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700390
391 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100392 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700393 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700394 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700395
396 mFakePolicy->clearViewports();
397 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700398 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700399 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100400 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700401 ASSERT_FALSE(internalViewport);
402}
403
404TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
405 const std::string internalUniqueId = "local:0";
406 const std::string externalUniqueId = "local:1";
407 const std::string virtualUniqueId1 = "virtual:2";
408 const std::string virtualUniqueId2 = "virtual:3";
409 constexpr int32_t virtualDisplayId1 = 2;
410 constexpr int32_t virtualDisplayId2 = 3;
411
412 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000413 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000414 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000415 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700416 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000417 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000418 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000419 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700420 // Add an virtual viewport
421 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000422 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000423 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700424 // Add another virtual viewport
425 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000426 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000427 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700428
429 // Check matching by type for internal
430 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100431 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700432 ASSERT_TRUE(internalViewport);
433 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
434
435 // Check matching by type for external
436 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100437 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700438 ASSERT_TRUE(externalViewport);
439 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
440
441 // Check matching by uniqueId for virtual viewport #1
442 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700443 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700444 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100445 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700446 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
447 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
448
449 // Check matching by uniqueId for virtual viewport #2
450 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700451 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700452 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100453 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700454 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
455 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
456}
457
458
459/**
460 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
461 * that lookup works by checking display id.
462 * Check that 2 viewports of each kind is possible, for all existing viewport types.
463 */
464TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
465 const std::string uniqueId1 = "uniqueId1";
466 const std::string uniqueId2 = "uniqueId2";
467 constexpr int32_t displayId1 = 2;
468 constexpr int32_t displayId2 = 3;
469
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100470 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
471 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700472 for (const ViewportType& type : types) {
473 mFakePolicy->clearViewports();
474 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000475 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000476 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700477 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000478 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000479 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700480
481 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700482 std::optional<DisplayViewport> viewport1 =
483 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700484 ASSERT_TRUE(viewport1);
485 ASSERT_EQ(displayId1, viewport1->displayId);
486 ASSERT_EQ(type, viewport1->type);
487
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700488 std::optional<DisplayViewport> viewport2 =
489 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700490 ASSERT_TRUE(viewport2);
491 ASSERT_EQ(displayId2, viewport2->displayId);
492 ASSERT_EQ(type, viewport2->type);
493
494 // When there are multiple viewports of the same kind, and uniqueId is not specified
495 // in the call to getDisplayViewport, then that situation is not supported.
496 // The viewports can be stored in any order, so we cannot rely on the order, since that
497 // is just implementation detail.
498 // However, we can check that it still returns *a* viewport, we just cannot assert
499 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700500 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700501 ASSERT_TRUE(someViewport);
502 }
503}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800504
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700505/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000506 * When we have multiple internal displays make sure we always return the default display when
507 * querying by type.
508 */
509TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
510 const std::string uniqueId1 = "uniqueId1";
511 const std::string uniqueId2 = "uniqueId2";
512 constexpr int32_t nonDefaultDisplayId = 2;
513 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
514 "Test display ID should not be ADISPLAY_ID_DEFAULT");
515
516 // Add the default display first and ensure it gets returned.
517 mFakePolicy->clearViewports();
518 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000519 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000520 ViewportType::INTERNAL);
521 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000522 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000523 ViewportType::INTERNAL);
524
525 std::optional<DisplayViewport> viewport =
526 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
527 ASSERT_TRUE(viewport);
528 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
529 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
530
531 // Add the default display second to make sure order doesn't matter.
532 mFakePolicy->clearViewports();
533 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000534 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000535 ViewportType::INTERNAL);
536 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000537 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000538 ViewportType::INTERNAL);
539
540 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
541 ASSERT_TRUE(viewport);
542 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
543 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
544}
545
546/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700547 * Check getDisplayViewportByPort
548 */
549TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100550 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700551 const std::string uniqueId1 = "uniqueId1";
552 const std::string uniqueId2 = "uniqueId2";
553 constexpr int32_t displayId1 = 1;
554 constexpr int32_t displayId2 = 2;
555 const uint8_t hdmi1 = 0;
556 const uint8_t hdmi2 = 1;
557 const uint8_t hdmi3 = 2;
558
559 mFakePolicy->clearViewports();
560 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000561 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000562 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700563 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000564 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000565 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700566
567 // Check that correct display viewport was returned by comparing the display ports.
568 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
569 ASSERT_TRUE(hdmi1Viewport);
570 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
571 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
572
573 // Check that we can still get the same viewport using the uniqueId
574 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
575 ASSERT_TRUE(hdmi1Viewport);
576 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
577 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
578 ASSERT_EQ(type, hdmi1Viewport->type);
579
580 // Check that we cannot find a port with "HDMI2", because we never added one
581 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
582 ASSERT_FALSE(hdmi2Viewport);
583}
584
Michael Wrightd02c5b62014-02-10 15:10:22 -0800585// --- InputReaderTest ---
586
587class InputReaderTest : public testing::Test {
588protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700589 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800590 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700591 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000592 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593
Chris Yea52ade12020-08-27 16:49:20 -0700594 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700595 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700596 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700597 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800598
Prabir Pradhan28efc192019-11-05 01:10:04 +0000599 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700600 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800601 }
602
Chris Yea52ade12020-08-27 16:49:20 -0700603 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700604 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800605 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606 }
607
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700608 void addDevice(int32_t eventHubId, const std::string& name,
609 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800610 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800611
612 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800613 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800614 }
615 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000616 mReader->loopOnce();
617 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700618 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700620 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800621 }
622
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800623 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700624 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000625 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700626 }
627
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800628 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700629 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000630 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700631 }
632
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800633 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700634 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700635 ftl::Flags<InputDeviceClass> classes,
636 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800637 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800638 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000639 FakeInputMapper& mapper =
640 device->addMapper<FakeInputMapper>(eventHubId,
641 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800642 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800643 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800644 return mapper;
645 }
646};
647
Chris Ye98d3f532020-10-01 21:48:59 -0700648TEST_F(InputReaderTest, PolicyGetInputDevices) {
649 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700650 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700651 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800652
653 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700654 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800656 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100657 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
659 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000660 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661}
662
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000663TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
664 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
665 mFakeEventHub->setSysfsRootPath(1, "xyz");
666
667 // Should also have received a notification describing the new input device.
668 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
669 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
670 ASSERT_EQ(0U, inputDevice.getLights().size());
671
672 RawLightInfo infoMonolight = {.id = 123,
673 .name = "mono_keyboard_backlight",
674 .maxBrightness = 255,
675 .flags = InputLightClass::BRIGHTNESS,
676 .path = ""};
677 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
678 mReader->sysfsNodeChanged("xyz");
679 mReader->loopOnce();
680
681 // Should also have received a notification describing the new recreated input device.
682 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
683 inputDevice = mFakePolicy->getInputDevices()[0];
684 ASSERT_EQ(1U, inputDevice.getLights().size());
685}
686
Chris Yee7310032020-09-22 15:36:28 -0700687TEST_F(InputReaderTest, GetMergedInputDevices) {
688 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
689 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
690 // Add two subdevices to device
691 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
692 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000693 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
694 AINPUT_SOURCE_KEYBOARD);
695 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
696 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700697
698 // Push same device instance for next device to be added, so they'll have same identifier.
699 mReader->pushNextDevice(device);
700 mReader->pushNextDevice(device);
701 ASSERT_NO_FATAL_FAILURE(
702 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
703 ASSERT_NO_FATAL_FAILURE(
704 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
705
706 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000707 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700708}
709
Chris Yee14523a2020-12-19 13:46:00 -0800710TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
711 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
712 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
713 // Add two subdevices to device
714 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
715 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000716 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
717 AINPUT_SOURCE_KEYBOARD);
718 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
719 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800720
721 // Push same device instance for next device to be added, so they'll have same identifier.
722 mReader->pushNextDevice(device);
723 mReader->pushNextDevice(device);
724 // Sensor device is initially disabled
725 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
726 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
727 nullptr));
728 // Device is disabled because the only sub device is a sensor device and disabled initially.
729 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
730 ASSERT_FALSE(device->isEnabled());
731 ASSERT_NO_FATAL_FAILURE(
732 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
733 // The merged device is enabled if any sub device is enabled
734 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
735 ASSERT_TRUE(device->isEnabled());
736}
737
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700738TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800739 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700740 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800741 constexpr int32_t eventHubId = 1;
742 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700743 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000744 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
745 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800746 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800747 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700748
Yi Kong9b14ac62018-07-17 13:48:38 -0700749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700750
751 NotifyDeviceResetArgs resetArgs;
752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700753 ASSERT_EQ(deviceId, resetArgs.deviceId);
754
755 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800756 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000757 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700758
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700760 ASSERT_EQ(deviceId, resetArgs.deviceId);
761 ASSERT_EQ(device->isEnabled(), false);
762
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800763 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000764 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700767 ASSERT_EQ(device->isEnabled(), false);
768
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800769 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000770 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700772 ASSERT_EQ(deviceId, resetArgs.deviceId);
773 ASSERT_EQ(device->isEnabled(), true);
774}
775
Michael Wrightd02c5b62014-02-10 15:10:22 -0800776TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800777 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700778 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800779 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800780 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800781 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800782 AINPUT_SOURCE_KEYBOARD, nullptr);
783 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800784
785 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
786 AINPUT_SOURCE_ANY, AKEYCODE_A))
787 << "Should return unknown when the device id is >= 0 but unknown.";
788
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800789 ASSERT_EQ(AKEY_STATE_UNKNOWN,
790 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
791 << "Should return unknown when the device id is valid but the sources are not "
792 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800793
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800794 ASSERT_EQ(AKEY_STATE_DOWN,
795 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
796 AKEYCODE_A))
797 << "Should return value provided by mapper when device id is valid and the device "
798 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800799
800 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
801 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
802 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
803
804 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
805 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
806 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
807}
808
Philip Junker4af3b3d2021-12-14 10:36:55 +0100809TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
810 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
811 constexpr int32_t eventHubId = 1;
812 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
813 InputDeviceClass::KEYBOARD,
814 AINPUT_SOURCE_KEYBOARD, nullptr);
815 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
816
817 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
818 << "Should return unknown when the device with the specified id is not found.";
819
820 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
821 << "Should return correct mapping when device id is valid and mapping exists.";
822
823 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
824 << "Should return the location key code when device id is valid and there's no "
825 "mapping.";
826}
827
828TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
829 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
830 constexpr int32_t eventHubId = 1;
831 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
832 InputDeviceClass::JOYSTICK,
833 AINPUT_SOURCE_GAMEPAD, nullptr);
834 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
835
836 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
837 << "Should return unknown when the device id is valid but there is no keyboard mapper";
838}
839
Michael Wrightd02c5b62014-02-10 15:10:22 -0800840TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800841 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700842 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800843 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800844 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800845 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800846 AINPUT_SOURCE_KEYBOARD, nullptr);
847 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800848
849 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
850 AINPUT_SOURCE_ANY, KEY_A))
851 << "Should return unknown when the device id is >= 0 but unknown.";
852
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800853 ASSERT_EQ(AKEY_STATE_UNKNOWN,
854 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
855 << "Should return unknown when the device id is valid but the sources are not "
856 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800857
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800858 ASSERT_EQ(AKEY_STATE_DOWN,
859 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
860 KEY_A))
861 << "Should return value provided by mapper when device id is valid and the device "
862 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800863
864 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
865 AINPUT_SOURCE_TRACKBALL, KEY_A))
866 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
867
868 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
869 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
870 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
871}
872
873TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800874 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700875 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800876 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800877 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800878 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800879 AINPUT_SOURCE_KEYBOARD, nullptr);
880 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800881
882 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
883 AINPUT_SOURCE_ANY, SW_LID))
884 << "Should return unknown when the device id is >= 0 but unknown.";
885
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800886 ASSERT_EQ(AKEY_STATE_UNKNOWN,
887 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
888 << "Should return unknown when the device id is valid but the sources are not "
889 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800890
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800891 ASSERT_EQ(AKEY_STATE_DOWN,
892 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
893 SW_LID))
894 << "Should return value provided by mapper when device id is valid and the device "
895 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896
897 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
898 AINPUT_SOURCE_TRACKBALL, SW_LID))
899 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
900
901 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
902 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
903 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
904}
905
906TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800907 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700908 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800909 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800910 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800911 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800912 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100913
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800914 mapper.addSupportedKeyCode(AKEYCODE_A);
915 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700917 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800918 uint8_t flags[4] = { 0, 0, 0, 1 };
919
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700920 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921 << "Should return false when device id is >= 0 but unknown.";
922 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
923
924 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700925 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800926 << "Should return false when device id is valid but the sources are not supported by "
927 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800928 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
929
930 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700931 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800932 keyCodes, flags))
933 << "Should return value provided by mapper when device id is valid and the device "
934 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800935 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
936
937 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700938 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
939 << "Should return false when the device id is < 0 but the sources are not supported by "
940 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800941 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
942
943 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700944 ASSERT_TRUE(
945 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
946 << "Should return value provided by mapper when device id is < 0 and one of the "
947 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800948 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
949}
950
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000951TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800952 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700953 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800954
955 NotifyConfigurationChangedArgs args;
956
957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
958 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
959}
960
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000961TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800962 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700963 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000964 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800965 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000966 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800967 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800968 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800969 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800970
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000971 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000972 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800973 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
974
975 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800976 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000977 ASSERT_EQ(when, event.when);
978 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800979 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800980 ASSERT_EQ(EV_KEY, event.type);
981 ASSERT_EQ(KEY_A, event.code);
982 ASSERT_EQ(1, event.value);
983}
984
Garfield Tan1c7bc862020-01-28 13:24:04 -0800985TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800986 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700987 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800988 constexpr int32_t eventHubId = 1;
989 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800990 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000991 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
992 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800993 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800994 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800995
996 NotifyDeviceResetArgs resetArgs;
997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800998 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800999
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001000 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001001 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001003 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001004 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001005
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001006 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001007 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001009 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001010 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001011
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001012 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001013 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001015 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001016 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001017}
1018
Garfield Tan1c7bc862020-01-28 13:24:04 -08001019TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1020 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001021 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001022 constexpr int32_t eventHubId = 1;
1023 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1024 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001025 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1026 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001027 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001028 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1029
1030 NotifyDeviceResetArgs resetArgs;
1031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1032 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1033}
1034
Arthur Hungc23540e2018-11-29 20:42:11 +08001035TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001036 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001037 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001038 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001039 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001040 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1041 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001042 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1043 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001044 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001045
1046 const uint8_t hdmi1 = 1;
1047
1048 // Associated touch screen with second display.
1049 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1050
1051 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001052 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001053 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001054 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001055 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001056 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001057 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001058 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001059 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001060
1061 // Add the device, and make sure all of the callbacks are triggered.
1062 // The device is added after the input port associations are processed since
1063 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001064 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001067 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001068
Arthur Hung2c9a3342019-07-23 14:18:59 +08001069 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001070 ASSERT_EQ(deviceId, device->getId());
1071 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1072 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001073
1074 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001075 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001076 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001077 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001078}
1079
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001080TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1081 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001082 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001083 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1084 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1085 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001086 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1087 AINPUT_SOURCE_KEYBOARD);
1088 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1089 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001090 mReader->pushNextDevice(device);
1091 mReader->pushNextDevice(device);
1092 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1093 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1094
1095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1096
1097 NotifyDeviceResetArgs resetArgs;
1098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1099 ASSERT_EQ(deviceId, resetArgs.deviceId);
1100 ASSERT_TRUE(device->isEnabled());
1101 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1102 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1103
1104 disableDevice(deviceId);
1105 mReader->loopOnce();
1106
1107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1108 ASSERT_EQ(deviceId, resetArgs.deviceId);
1109 ASSERT_FALSE(device->isEnabled());
1110 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1111 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1112
1113 enableDevice(deviceId);
1114 mReader->loopOnce();
1115
1116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1117 ASSERT_EQ(deviceId, resetArgs.deviceId);
1118 ASSERT_TRUE(device->isEnabled());
1119 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1120 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1121}
1122
1123TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1124 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001125 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001126 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1127 // Add two subdevices to device
1128 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1129 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001130 device->addMapper<FakeInputMapper>(eventHubIds[0],
1131 mFakePolicy->getReaderConfiguration(),
1132 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001133 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001134 device->addMapper<FakeInputMapper>(eventHubIds[1],
1135 mFakePolicy->getReaderConfiguration(),
1136 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001137 mReader->pushNextDevice(device);
1138 mReader->pushNextDevice(device);
1139 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1140 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1141
1142 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1143 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1144
1145 ASSERT_EQ(AKEY_STATE_DOWN,
1146 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1147 ASSERT_EQ(AKEY_STATE_DOWN,
1148 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1149 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1150 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1151}
1152
Prabir Pradhan7e186182020-11-10 13:56:45 -08001153TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1154 NotifyPointerCaptureChangedArgs args;
1155
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001156 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001157 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001158 mReader->loopOnce();
1159 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001160 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1161 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001162
1163 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001164 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001165 mReader->loopOnce();
1166 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001167 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001168
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001169 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001170 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001171 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001172 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001173 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001174}
1175
Chris Ye87143712020-11-10 05:05:58 +00001176class FakeVibratorInputMapper : public FakeInputMapper {
1177public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001178 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1179 const InputReaderConfiguration& readerConfig, uint32_t sources)
1180 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001181
1182 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1183};
1184
1185TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1186 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001187 ftl::Flags<InputDeviceClass> deviceClass =
1188 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001189 constexpr int32_t eventHubId = 1;
1190 const char* DEVICE_LOCATION = "BLUETOOTH";
1191 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1192 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001193 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1194 mFakePolicy->getReaderConfiguration(),
1195 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001196 mReader->pushNextDevice(device);
1197
1198 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1199 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1200
1201 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1202 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1203}
1204
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001205// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001206
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001207class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001208public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001209 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001210
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001211 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001212
Andy Chenf9f1a022022-08-29 20:07:10 -04001213 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1214
Chris Yee2b1e5c2021-03-10 22:45:12 -08001215 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1216
1217 void dump(std::string& dump) override {}
1218
1219 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1220 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001221 }
1222
Chris Yee2b1e5c2021-03-10 22:45:12 -08001223 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1224 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001225 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001226
1227 bool setLightColor(int32_t lightId, int32_t color) override {
1228 getDeviceContext().setLightBrightness(lightId, color >> 24);
1229 return true;
1230 }
1231
1232 std::optional<int32_t> getLightColor(int32_t lightId) override {
1233 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1234 if (!result.has_value()) {
1235 return std::nullopt;
1236 }
1237 return result.value() << 24;
1238 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001239
1240 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1241
1242 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1243
1244private:
1245 InputDeviceContext& mDeviceContext;
1246 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1247 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001248 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001249};
1250
Chris Yee2b1e5c2021-03-10 22:45:12 -08001251TEST_F(InputReaderTest, BatteryGetCapacity) {
1252 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001253 ftl::Flags<InputDeviceClass> deviceClass =
1254 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001255 constexpr int32_t eventHubId = 1;
1256 const char* DEVICE_LOCATION = "BLUETOOTH";
1257 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001258 FakePeripheralController& controller =
1259 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001260 mReader->pushNextDevice(device);
1261
1262 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1263
Harry Cuttsa5b71292022-11-28 12:56:17 +00001264 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1265 FakeEventHub::BATTERY_CAPACITY);
1266 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001267}
1268
1269TEST_F(InputReaderTest, BatteryGetStatus) {
1270 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001271 ftl::Flags<InputDeviceClass> deviceClass =
1272 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001273 constexpr int32_t eventHubId = 1;
1274 const char* DEVICE_LOCATION = "BLUETOOTH";
1275 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001276 FakePeripheralController& controller =
1277 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001278 mReader->pushNextDevice(device);
1279
1280 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1281
Harry Cuttsa5b71292022-11-28 12:56:17 +00001282 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1283 FakeEventHub::BATTERY_STATUS);
1284 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001285}
1286
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001287TEST_F(InputReaderTest, BatteryGetDevicePath) {
1288 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1289 ftl::Flags<InputDeviceClass> deviceClass =
1290 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1291 constexpr int32_t eventHubId = 1;
1292 const char* DEVICE_LOCATION = "BLUETOOTH";
1293 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1294 device->addController<FakePeripheralController>(eventHubId);
1295 mReader->pushNextDevice(device);
1296
1297 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1298
Harry Cuttsa5b71292022-11-28 12:56:17 +00001299 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001300}
1301
Chris Ye3fdbfef2021-01-06 18:45:18 -08001302TEST_F(InputReaderTest, LightGetColor) {
1303 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001304 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001305 constexpr int32_t eventHubId = 1;
1306 const char* DEVICE_LOCATION = "BLUETOOTH";
1307 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001308 FakePeripheralController& controller =
1309 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001310 mReader->pushNextDevice(device);
1311 RawLightInfo info = {.id = 1,
1312 .name = "Mono",
1313 .maxBrightness = 255,
1314 .flags = InputLightClass::BRIGHTNESS,
1315 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001316 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1317 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001318
1319 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001320
Harry Cutts33476232023-01-30 19:57:29 +00001321 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1322 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1323 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1324 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001325}
1326
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001327// --- InputReaderIntegrationTest ---
1328
1329// These tests create and interact with the InputReader only through its interface.
1330// The InputReader is started during SetUp(), which starts its processing in its own
1331// thread. The tests use linux uinput to emulate input devices.
1332// NOTE: Interacting with the physical device while these tests are running may cause
1333// the tests to fail.
1334class InputReaderIntegrationTest : public testing::Test {
1335protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001336 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001337 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001338 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001339
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001340 std::shared_ptr<FakePointerController> mFakePointerController;
1341
Chris Yea52ade12020-08-27 16:49:20 -07001342 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001343#if !defined(__ANDROID__)
1344 GTEST_SKIP();
1345#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001346 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001347 mFakePointerController = std::make_shared<FakePointerController>();
1348 mFakePolicy->setPointerController(mFakePointerController);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001349
Arpit Singh440bf652023-08-09 09:23:43 +00001350 setupInputReader();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001351 }
1352
Chris Yea52ade12020-08-27 16:49:20 -07001353 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001354#if !defined(__ANDROID__)
1355 return;
1356#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001357 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001358 mReader.reset();
1359 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001360 mFakePolicy.clear();
1361 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001362
1363 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1364 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1365 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1366 [&name](const InputDeviceInfo& info) {
1367 return info.getIdentifier().name == name;
1368 });
1369 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1370 }
Arpit Singh440bf652023-08-09 09:23:43 +00001371
1372 void setupInputReader() {
1373 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1374 /*eventDidNotHappenTimeout=*/30ms);
1375
1376 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1377 *mTestListener);
1378 ASSERT_EQ(mReader->start(), OK);
1379
1380 // Since this test is run on a real device, all the input devices connected
1381 // to the test device will show up in mReader. We wait for those input devices to
1382 // show up before beginning the tests.
1383 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1384 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
1385 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1386 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001387};
1388
1389TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1390 // An invalid input device that is only used for this test.
1391 class InvalidUinputDevice : public UinputDevice {
1392 public:
Harry Cutts33476232023-01-30 19:57:29 +00001393 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001394
1395 private:
1396 void configureDevice(int fd, uinput_user_dev* device) override {}
1397 };
1398
1399 const size_t numDevices = mFakePolicy->getInputDevices().size();
1400
1401 // UinputDevice does not set any event or key bits, so InputReader should not
1402 // consider it as a valid device.
1403 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1404 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1405 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1406 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1407
1408 invalidDevice.reset();
1409 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1410 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1411 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1412}
1413
1414TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1415 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1416
1417 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1418 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1419 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1420 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1421
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001422 const auto device = findDeviceByName(keyboard->getName());
1423 ASSERT_TRUE(device.has_value());
1424 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1425 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1426 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001427
1428 keyboard.reset();
1429 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1430 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1431 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1432}
1433
1434TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1435 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1436 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1437
1438 NotifyConfigurationChangedArgs configChangedArgs;
1439 ASSERT_NO_FATAL_FAILURE(
1440 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001441 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001442 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1443
1444 NotifyKeyArgs keyArgs;
1445 keyboard->pressAndReleaseHomeKey();
1446 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1447 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001448 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001449 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001450 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001451 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001452 prevTimestamp = keyArgs.eventTime;
1453
1454 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1455 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001456 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001457 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001458 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001459}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001460
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001461TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1462 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1463 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1464
1465 const auto device = findDeviceByName(stylus->getName());
1466 ASSERT_TRUE(device.has_value());
1467
Prabir Pradhana3621852022-10-14 18:57:23 +00001468 // An external stylus with buttons should also be recognized as a keyboard.
1469 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001470 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1471 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1472
1473 const auto DOWN =
1474 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1475 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1476
1477 stylus->pressAndReleaseKey(BTN_STYLUS);
1478 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1479 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1480 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1481 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1482
1483 stylus->pressAndReleaseKey(BTN_STYLUS2);
1484 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1485 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1486 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1487 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1488
1489 stylus->pressAndReleaseKey(BTN_STYLUS3);
1490 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1491 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1492 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1493 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1494}
1495
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001496TEST_F(InputReaderIntegrationTest, KeyboardWithStylusButtons) {
1497 std::unique_ptr<UinputKeyboard> keyboard =
1498 createUinputDevice<UinputKeyboard>("KeyboardWithStylusButtons", /*productId=*/99,
1499 std::initializer_list<int>{KEY_Q, KEY_W, KEY_E,
1500 KEY_R, KEY_T, KEY_Y,
1501 BTN_STYLUS, BTN_STYLUS2,
1502 BTN_STYLUS3});
1503 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1504
1505 const auto device = findDeviceByName(keyboard->getName());
1506 ASSERT_TRUE(device.has_value());
1507
1508 // An alphabetical keyboard that reports stylus buttons should not be recognized as a stylus.
1509 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1510 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1511 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, device->getKeyboardType());
1512}
1513
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001514TEST_F(InputReaderIntegrationTest, HidUsageKeyboardIsNotAStylus) {
1515 // Create a Uinput keyboard that simulates a keyboard that can report HID usage codes. The
1516 // hid-input driver reports HID usage codes using the value for EV_MSC MSC_SCAN event.
1517 std::unique_ptr<UinputKeyboardWithHidUsage> keyboard =
1518 createUinputDevice<UinputKeyboardWithHidUsage>(
1519 std::initializer_list<int>{KEY_VOLUMEUP, KEY_VOLUMEDOWN});
1520 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1521
1522 const auto device = findDeviceByName(keyboard->getName());
1523 ASSERT_TRUE(device.has_value());
1524
1525 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1526 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1527
1528 // If a device supports reporting HID usage codes, it shouldn't automatically support
1529 // stylus keys.
1530 const std::vector<int> keycodes{AKEYCODE_STYLUS_BUTTON_PRIMARY};
1531 uint8_t outFlags[] = {0};
1532 ASSERT_TRUE(mReader->hasKeys(device->getId(), AINPUT_SOURCE_KEYBOARD, keycodes, outFlags));
1533 ASSERT_EQ(0, outFlags[0]) << "Keyboard should not have stylus button";
1534}
1535
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001536/**
1537 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1538 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1539 * are passed to the listener.
1540 */
1541static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1542TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1543 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1544 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1545 NotifyKeyArgs keyArgs;
1546
1547 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1548 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1549 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1550 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1551
1552 controller->pressAndReleaseKey(BTN_GEAR_UP);
1553 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1554 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1555 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1556}
1557
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001558// --- TouchIntegrationTest ---
1559
Arpit Singh440bf652023-08-09 09:23:43 +00001560class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
Arthur Hungaab25622020-01-16 11:22:11 +08001561protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001562 const std::string UNIQUE_ID = "local:0";
1563
Chris Yea52ade12020-08-27 16:49:20 -07001564 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001565#if !defined(__ANDROID__)
1566 GTEST_SKIP();
1567#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001568 InputReaderIntegrationTest::SetUp();
1569 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001570 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1571 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001572
1573 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1574 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1575 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001576 const auto info = findDeviceByName(mDevice->getName());
1577 ASSERT_TRUE(info);
1578 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001579 }
1580
1581 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001582 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001583 std::optional<uint8_t> physicalPort,
1584 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001585 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001586 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001587 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001588 }
1589
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001590 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1591 NotifyMotionArgs args;
1592 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1593 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001594 ASSERT_EQ(points.size(), args.getPointerCount());
1595 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001596 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1597 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1598 }
1599 }
1600
Arthur Hungaab25622020-01-16 11:22:11 +08001601 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001602 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001603};
1604
Arpit Singh440bf652023-08-09 09:23:43 +00001605enum class TouchIntegrationTestDisplays { DISPLAY_INTERNAL, DISPLAY_INPUT_PORT, DISPLAY_UNIQUE_ID };
1606
1607class TouchIntegrationTest : public BaseTouchIntegrationTest,
1608 public testing::WithParamInterface<TouchIntegrationTestDisplays> {
1609protected:
1610 static constexpr std::optional<uint8_t> DISPLAY_PORT = 0;
1611 const std::string INPUT_PORT = "uinput_touch/input0";
1612
1613 void SetUp() override {
1614#if !defined(__ANDROID__)
1615 GTEST_SKIP();
1616#endif
1617 if (GetParam() == TouchIntegrationTestDisplays::DISPLAY_INTERNAL) {
1618 BaseTouchIntegrationTest::SetUp();
1619 return;
1620 }
1621
1622 // setup policy with a input-port or UniqueId association to the display
1623 bool isInputPortAssociation =
1624 GetParam() == TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT;
1625
1626 mFakePolicy = sp<FakeInputReaderPolicy>::make();
1627 if (isInputPortAssociation) {
1628 mFakePolicy->addInputPortAssociation(INPUT_PORT, DISPLAY_PORT.value());
1629 } else {
1630 mFakePolicy->addInputUniqueIdAssociation(INPUT_PORT, UNIQUE_ID);
1631 }
1632 mFakePointerController = std::make_shared<FakePointerController>();
1633 mFakePolicy->setPointerController(mFakePointerController);
1634
1635 InputReaderIntegrationTest::setupInputReader();
1636
1637 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
1638 INPUT_PORT);
1639 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1640
1641 // Add a display linked to a physical port or UniqueId.
1642 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1643 UNIQUE_ID, isInputPortAssociation ? DISPLAY_PORT : NO_PORT,
1644 ViewportType::INTERNAL);
1645 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1646 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1647 const auto info = findDeviceByName(mDevice->getName());
1648 ASSERT_TRUE(info);
1649 mDeviceInfo = *info;
1650 }
1651};
1652
1653TEST_P(TouchIntegrationTest, MultiTouchDeviceSource) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001654 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1655 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1656 // presses).
1657 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1658 mDeviceInfo.getSources());
1659}
1660
Arpit Singh440bf652023-08-09 09:23:43 +00001661TEST_P(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001662 NotifyMotionArgs args;
1663 const Point centerPoint = mDevice->getCenterPoint();
1664
1665 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001666 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001667 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001668 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001669 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1670 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1671
1672 // ACTION_MOVE
1673 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001674 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001675 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1676 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1677
1678 // ACTION_UP
1679 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001680 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001681 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1682 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1683}
1684
Arpit Singh440bf652023-08-09 09:23:43 +00001685TEST_P(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001686 NotifyMotionArgs args;
1687 const Point centerPoint = mDevice->getCenterPoint();
1688
1689 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001690 mDevice->sendSlot(FIRST_SLOT);
1691 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001692 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001693 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001694 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1695 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1696
1697 // ACTION_POINTER_DOWN (Second slot)
1698 const Point secondPoint = centerPoint + Point(100, 100);
1699 mDevice->sendSlot(SECOND_SLOT);
1700 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001701 mDevice->sendDown(secondPoint);
1702 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001703 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001704 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001705
1706 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001707 mDevice->sendMove(secondPoint + Point(1, 1));
1708 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001709 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1710 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1711
1712 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001713 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001714 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001715 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001716 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001717
1718 // ACTION_UP
1719 mDevice->sendSlot(FIRST_SLOT);
1720 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001721 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001722 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1723 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1724}
1725
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001726/**
1727 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1728 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1729 * data?
1730 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1731 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1732 * for Pointer 0 only is generated after.
1733 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1734 * events, we will not miss any information.
1735 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1736 * event generated afterwards that contains the newest movement of pointer 0.
1737 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1738 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1739 * losing information about non-palm pointers.
1740 */
Arpit Singh440bf652023-08-09 09:23:43 +00001741TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001742 NotifyMotionArgs args;
1743 const Point centerPoint = mDevice->getCenterPoint();
1744
1745 // ACTION_DOWN
1746 mDevice->sendSlot(FIRST_SLOT);
1747 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1748 mDevice->sendDown(centerPoint);
1749 mDevice->sendSync();
1750 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1751
1752 // ACTION_POINTER_DOWN (Second slot)
1753 const Point secondPoint = centerPoint + Point(100, 100);
1754 mDevice->sendSlot(SECOND_SLOT);
1755 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1756 mDevice->sendDown(secondPoint);
1757 mDevice->sendSync();
1758 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1759
1760 // ACTION_MOVE (First slot)
1761 mDevice->sendSlot(FIRST_SLOT);
1762 mDevice->sendMove(centerPoint + Point(5, 5));
1763 // ACTION_POINTER_UP (Second slot)
1764 mDevice->sendSlot(SECOND_SLOT);
1765 mDevice->sendPointerUp();
1766 // Send a single sync for the above 2 pointer updates
1767 mDevice->sendSync();
1768
1769 // First, we should get POINTER_UP for the second pointer
1770 assertReceivedMotion(ACTION_POINTER_1_UP,
1771 {/*first pointer */ centerPoint + Point(5, 5),
1772 /*second pointer*/ secondPoint});
1773
1774 // Next, the MOVE event for the first pointer
1775 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1776}
1777
1778/**
1779 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1780 * move, and then it will go up, all in the same frame.
1781 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1782 * gets sent to the listener.
1783 */
Arpit Singh440bf652023-08-09 09:23:43 +00001784TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001785 NotifyMotionArgs args;
1786 const Point centerPoint = mDevice->getCenterPoint();
1787
1788 // ACTION_DOWN
1789 mDevice->sendSlot(FIRST_SLOT);
1790 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1791 mDevice->sendDown(centerPoint);
1792 mDevice->sendSync();
1793 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1794
1795 // ACTION_POINTER_DOWN (Second slot)
1796 const Point secondPoint = centerPoint + Point(100, 100);
1797 mDevice->sendSlot(SECOND_SLOT);
1798 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1799 mDevice->sendDown(secondPoint);
1800 mDevice->sendSync();
1801 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1802
1803 // ACTION_MOVE (First slot)
1804 mDevice->sendSlot(FIRST_SLOT);
1805 mDevice->sendMove(centerPoint + Point(5, 5));
1806 // ACTION_POINTER_UP (Second slot)
1807 mDevice->sendSlot(SECOND_SLOT);
1808 mDevice->sendMove(secondPoint + Point(6, 6));
1809 mDevice->sendPointerUp();
1810 // Send a single sync for the above 2 pointer updates
1811 mDevice->sendSync();
1812
1813 // First, we should get POINTER_UP for the second pointer
1814 // The movement of the second pointer during the liftoff frame is ignored.
1815 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1816 assertReceivedMotion(ACTION_POINTER_1_UP,
1817 {/*first pointer */ centerPoint + Point(5, 5),
1818 /*second pointer*/ secondPoint});
1819
1820 // Next, the MOVE event for the first pointer
1821 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1822}
1823
Arpit Singh440bf652023-08-09 09:23:43 +00001824TEST_P(TouchIntegrationTest, InputEvent_ProcessPalm) {
Arthur Hungaab25622020-01-16 11:22:11 +08001825 NotifyMotionArgs args;
1826 const Point centerPoint = mDevice->getCenterPoint();
1827
1828 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001829 mDevice->sendSlot(FIRST_SLOT);
1830 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001831 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001832 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001833 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1834 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1835
arthurhungcc7f9802020-04-30 17:55:40 +08001836 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001837 const Point secondPoint = centerPoint + Point(100, 100);
1838 mDevice->sendSlot(SECOND_SLOT);
1839 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1840 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001841 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001842 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001843 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001844
arthurhungcc7f9802020-04-30 17:55:40 +08001845 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001846 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001847 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001848 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1850
arthurhungcc7f9802020-04-30 17:55:40 +08001851 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1852 // a palm event.
1853 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001854 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001855 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001856 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001857 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001858 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001859
arthurhungcc7f9802020-04-30 17:55:40 +08001860 // Send up to second slot, expect first slot send moving.
1861 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001862 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001863 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1864 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001865
arthurhungcc7f9802020-04-30 17:55:40 +08001866 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001867 mDevice->sendSlot(FIRST_SLOT);
1868 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001869 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001870
arthurhungcc7f9802020-04-30 17:55:40 +08001871 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1872 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001873}
1874
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +00001875/**
1876 * Some drivers historically have reported axis values outside of the range specified in the
1877 * evdev axis info. Ensure we don't crash when this happens. For example, a driver may report a
1878 * pressure value greater than the reported maximum, since it unclear what specific meaning the
1879 * maximum value for pressure has (beyond the maximum value that can be produced by a sensor),
1880 * and no units for pressure (resolution) is specified by the evdev documentation.
1881 */
1882TEST_P(TouchIntegrationTest, AcceptsAxisValuesOutsideReportedRange) {
1883 const Point centerPoint = mDevice->getCenterPoint();
1884
1885 // Down with pressure outside the reported range
1886 mDevice->sendSlot(FIRST_SLOT);
1887 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1888 mDevice->sendDown(centerPoint);
1889 mDevice->sendPressure(UinputTouchScreen::RAW_PRESSURE_MAX + 2);
1890 mDevice->sendSync();
1891 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1892 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1893
1894 // Move to a point outside the reported range
1895 mDevice->sendMove(Point(DISPLAY_WIDTH, DISPLAY_HEIGHT) + Point(1, 1));
1896 mDevice->sendSync();
1897 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1898 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1899
1900 // Up
1901 mDevice->sendUp();
1902 mDevice->sendSync();
1903 ASSERT_NO_FATAL_FAILURE(
1904 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1905}
1906
Arpit Singh440bf652023-08-09 09:23:43 +00001907TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00001908 const Point centerPoint = mDevice->getCenterPoint();
1909
1910 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1911 mDevice->sendSlot(FIRST_SLOT);
1912 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1913 mDevice->sendToolType(MT_TOOL_PEN);
1914 mDevice->sendDown(centerPoint);
1915 mDevice->sendSync();
1916 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1917 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001918 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001919
1920 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1921
1922 // Release the stylus touch.
1923 mDevice->sendUp();
1924 mDevice->sendSync();
1925 ASSERT_NO_FATAL_FAILURE(
1926 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1927
1928 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1929
1930 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1931 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1932 mDevice->sendToolType(MT_TOOL_FINGER);
1933 mDevice->sendDown(centerPoint);
1934 mDevice->sendSync();
1935 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1936 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001937 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001938
1939 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1940
1941 mDevice->sendUp();
1942 mDevice->sendSync();
1943 ASSERT_NO_FATAL_FAILURE(
1944 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1945
1946 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1947 // The policy should be notified of the stylus presence.
1948 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1949 mDevice->sendToolType(MT_TOOL_PEN);
1950 mDevice->sendMove(centerPoint);
1951 mDevice->sendSync();
1952 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1953 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001954 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001955
1956 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1957}
1958
Arpit Singh440bf652023-08-09 09:23:43 +00001959TEST_P(TouchIntegrationTest, ExternalStylusConnectedDuringTouchGesture) {
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001960 const Point centerPoint = mDevice->getCenterPoint();
1961
1962 // Down
1963 mDevice->sendSlot(FIRST_SLOT);
1964 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1965 mDevice->sendDown(centerPoint);
1966 mDevice->sendSync();
1967 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1968 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1969
1970 // Move
1971 mDevice->sendMove(centerPoint + Point(1, 1));
1972 mDevice->sendSync();
1973 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1974 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1975
1976 // Connecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
1977 auto externalStylus = createUinputDevice<UinputExternalStylus>();
1978 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1979 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1980 const auto stylusInfo = findDeviceByName(externalStylus->getName());
1981 ASSERT_TRUE(stylusInfo);
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001982
1983 // Move
1984 mDevice->sendMove(centerPoint + Point(2, 2));
1985 mDevice->sendSync();
1986 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1987 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1988
1989 // Disconnecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
1990 externalStylus.reset();
1991 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1992 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1993 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
1994
1995 // Up
1996 mDevice->sendUp();
1997 mDevice->sendSync();
1998 ASSERT_NO_FATAL_FAILURE(
1999 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2000
2001 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2002}
2003
Arpit Singh440bf652023-08-09 09:23:43 +00002004INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
2005 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
2006 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
2007 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
2008
Prabir Pradhan124ea442022-10-28 20:27:44 +00002009// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002010
Prabir Pradhan124ea442022-10-28 20:27:44 +00002011// Verify the behavior of button presses reported by various kinds of styluses, including buttons
2012// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
2013// stylus.
2014template <typename UinputStylusDevice>
Arpit Singh440bf652023-08-09 09:23:43 +00002015class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002016protected:
2017 void SetUp() override {
2018#if !defined(__ANDROID__)
2019 GTEST_SKIP();
2020#endif
Arpit Singh440bf652023-08-09 09:23:43 +00002021 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00002022 mTouchscreen = mDevice.get();
2023 mTouchscreenInfo = mDeviceInfo;
2024
2025 setUpStylusDevice();
2026 }
2027
2028 UinputStylusDevice* mStylus{nullptr};
2029 InputDeviceInfo mStylusInfo{};
2030
2031 UinputTouchScreen* mTouchscreen{nullptr};
2032 InputDeviceInfo mTouchscreenInfo{};
2033
2034private:
2035 // When we are attempting to test stylus button events that are sent from the touchscreen,
2036 // use the same Uinput device for the touchscreen and the stylus.
2037 template <typename T = UinputStylusDevice>
2038 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2039 mStylus = mDevice.get();
2040 mStylusInfo = mDeviceInfo;
2041 }
2042
2043 // When we are attempting to stylus buttons from an external stylus being merged with touches
2044 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2045 template <typename T = UinputStylusDevice>
2046 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2047 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2048 mStylus = mStylusDeviceLifecycleTracker.get();
2049 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2050 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2051 const auto info = findDeviceByName(mStylus->getName());
2052 ASSERT_TRUE(info);
2053 mStylusInfo = *info;
2054 }
2055
2056 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2057
2058 // Hide the base class's device to expose it with a different name for readability.
Arpit Singh440bf652023-08-09 09:23:43 +00002059 using BaseTouchIntegrationTest::mDevice;
2060 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00002061};
2062
2063using StylusButtonIntegrationTestTypes =
2064 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2065TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2066
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002067TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002068 const auto stylusId = TestFixture::mStylusInfo.getId();
2069
2070 TestFixture::mStylus->pressKey(BTN_STYLUS);
2071 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2072 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2073 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2074
2075 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2076 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002077 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002078 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002079}
2080
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002081TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002082 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2083 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2084 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002085
2086 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002087 TestFixture::mStylus->pressKey(BTN_STYLUS);
2088 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002089 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002090 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002091
2092 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002093 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2094 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2095 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2096 TestFixture::mTouchscreen->sendDown(centerPoint);
2097 TestFixture::mTouchscreen->sendSync();
2098 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002099 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002100 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002101 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2102 WithDeviceId(touchscreenId))));
2103 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002104 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002105 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002106 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2107 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002108
Prabir Pradhan124ea442022-10-28 20:27:44 +00002109 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2110 TestFixture::mTouchscreen->sendSync();
2111 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002112 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002113 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002114 WithDeviceId(touchscreenId))));
2115 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002116 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002117 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002118 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002119
2120 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002121 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2122 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002123 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002124 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002125}
2126
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002127TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002128 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2129 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2130 const auto stylusId = TestFixture::mStylusInfo.getId();
2131 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002132 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002133
2134 // Press the stylus button.
2135 TestFixture::mStylus->pressKey(BTN_STYLUS);
2136 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2137 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2138 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2139
2140 // Start hovering with the stylus.
2141 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2142 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2143 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2144 TestFixture::mTouchscreen->sendMove(centerPoint);
2145 TestFixture::mTouchscreen->sendSync();
2146 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2147 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2148 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2149 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2150 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2151 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2152 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2153 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2154 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2155
2156 // Touch down with the stylus.
2157 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2158 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2159 TestFixture::mTouchscreen->sendDown(centerPoint);
2160 TestFixture::mTouchscreen->sendSync();
2161 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2162 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2163 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2164
2165 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2166 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2167 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2168
2169 // Stop touching with the stylus, and start hovering.
2170 TestFixture::mTouchscreen->sendUp();
2171 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2172 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2173 TestFixture::mTouchscreen->sendMove(centerPoint);
2174 TestFixture::mTouchscreen->sendSync();
2175 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2176 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2177 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2178 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2179 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2180 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2181 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2182 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2183 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2184
2185 // Stop hovering.
2186 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2187 TestFixture::mTouchscreen->sendSync();
2188 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2189 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2190 WithButtonState(0))));
2191 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2192 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2193 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2194 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2195
2196 // Release the stylus button.
2197 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2198 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2199 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2200 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2201}
2202
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002203TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002204 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2205 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2206 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002207
2208 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002209 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2210 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2211 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2212 TestFixture::mTouchscreen->sendDown(centerPoint);
2213 TestFixture::mTouchscreen->sendSync();
2214 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002215 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002216 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002217 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002218
2219 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002220 TestFixture::mStylus->pressKey(BTN_STYLUS);
2221 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002222 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002223 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2224 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002225 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002226 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002227 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2228 WithDeviceId(touchscreenId))));
2229 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002230 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002231 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002232 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2233 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002234
Prabir Pradhan124ea442022-10-28 20:27:44 +00002235 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2236 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002237 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002238 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2239 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002240 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002241 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002242 WithDeviceId(touchscreenId))));
2243 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002244 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002245 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002246 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002247
2248 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002249 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2250 TestFixture::mTouchscreen->sendSync();
2251 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002252 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002253 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002254 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002255}
2256
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002257TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002258 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2259 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002260 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002261
2262 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2263 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2264 const auto stylusId = TestFixture::mStylusInfo.getId();
2265
2266 // Start a stylus gesture. By the time this event is processed, the configuration change that
2267 // was requested is guaranteed to be completed.
2268 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2269 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2270 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2271 TestFixture::mTouchscreen->sendDown(centerPoint);
2272 TestFixture::mTouchscreen->sendSync();
2273 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2274 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002275 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002276 WithDeviceId(touchscreenId))));
2277
2278 // Press and release a stylus button. Each change only generates a MOVE motion event.
2279 // Key events are unaffected.
2280 TestFixture::mStylus->pressKey(BTN_STYLUS);
2281 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2282 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2283 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2284 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2285 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002286 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002287 WithDeviceId(touchscreenId))));
2288
2289 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2290 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2291 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2292 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2293 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2294 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002295 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002296 WithDeviceId(touchscreenId))));
2297
2298 // Finish the stylus gesture.
2299 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2300 TestFixture::mTouchscreen->sendSync();
2301 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2302 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002303 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002304 WithDeviceId(touchscreenId))));
2305}
2306
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002307// --- ExternalStylusIntegrationTest ---
2308
2309// Verify the behavior of an external stylus. An external stylus can report pressure or button
2310// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2311// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singh440bf652023-08-09 09:23:43 +00002312using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002313
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002314TEST_F(ExternalStylusIntegrationTest, ExternalStylusConnectionChangesTouchscreenSource) {
2315 // Create an external stylus capable of reporting pressure data that
2316 // should be fused with a touch pointer.
2317 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2318 createUinputDevice<UinputExternalStylusWithPressure>();
2319 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2320 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2321 const auto stylusInfo = findDeviceByName(stylus->getName());
2322 ASSERT_TRUE(stylusInfo);
2323
2324 // Connecting an external stylus changes the source of the touchscreen.
2325 const auto deviceInfo = findDeviceByName(mDevice->getName());
2326 ASSERT_TRUE(deviceInfo);
2327 ASSERT_TRUE(isFromSource(deviceInfo->getSources(), STYLUS_FUSION_SOURCE));
2328}
2329
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002330TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002331 const Point centerPoint = mDevice->getCenterPoint();
2332
2333 // Create an external stylus capable of reporting pressure data that
2334 // should be fused with a touch pointer.
2335 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2336 createUinputDevice<UinputExternalStylusWithPressure>();
2337 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2338 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2339 const auto stylusInfo = findDeviceByName(stylus->getName());
2340 ASSERT_TRUE(stylusInfo);
2341
2342 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2343
2344 const auto touchscreenId = mDeviceInfo.getId();
2345
2346 // Set a pressure value on the stylus. It doesn't generate any events.
2347 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2348 stylus->setPressure(100);
2349 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2350
2351 // Start a finger gesture, and ensure it shows up as stylus gesture
2352 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002353 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002354 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002355 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002356 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002357 mDevice->sendSync();
2358 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002359 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithToolType(ToolType::STYLUS),
2360 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2361 WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002362
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002363 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2364 // event with the updated pressure.
2365 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002366 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002367 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithToolType(ToolType::STYLUS),
2368 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2369 WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002370
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002371 // The external stylus did not generate any events.
2372 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2373 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2374}
2375
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002376TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002377 const Point centerPoint = mDevice->getCenterPoint();
2378
2379 // Create an external stylus capable of reporting pressure data that
2380 // should be fused with a touch pointer.
2381 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2382 createUinputDevice<UinputExternalStylusWithPressure>();
2383 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2384 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2385 const auto stylusInfo = findDeviceByName(stylus->getName());
2386 ASSERT_TRUE(stylusInfo);
2387
2388 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2389
2390 const auto touchscreenId = mDeviceInfo.getId();
2391
2392 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2393 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002394 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2395 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002396 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002397 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002398
2399 // Start a finger gesture. The touch device will withhold generating any touches for
2400 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2401 mDevice->sendSlot(FIRST_SLOT);
2402 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2403 mDevice->sendToolType(MT_TOOL_FINGER);
2404 mDevice->sendDown(centerPoint);
2405 auto waitUntil = std::chrono::system_clock::now() +
2406 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002407 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002408 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002409
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002410 // Since the external stylus did not report a pressure value within the timeout,
2411 // it shows up as a finger pointer.
2412 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2413 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002414 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
2415 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId), WithPressure(1.f))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002416
2417 // Change the pressure on the external stylus. Since the pressure was not present at the start
2418 // of the gesture, it is ignored for now.
2419 stylus->setPressure(200);
2420 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2421
2422 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002423 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2424 mDevice->sendSync();
2425 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2426 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002427 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002428 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002429
2430 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2431 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2432 mDevice->sendToolType(MT_TOOL_FINGER);
2433 mDevice->sendDown(centerPoint);
2434 mDevice->sendSync();
2435 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002436 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithSource(STYLUS_FUSION_SOURCE),
2437 WithToolType(ToolType::STYLUS), WithButtonState(0), WithDeviceId(touchscreenId),
2438 WithPressure(200.f / RAW_PRESSURE_MAX))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002439
2440 // The external stylus did not generate any events.
2441 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2442 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002443}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002444
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002445TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002446 const Point centerPoint = mDevice->getCenterPoint();
2447
2448 // Create an external stylus device that does not support pressure. It should not affect any
2449 // touch pointers.
2450 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2451 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2452 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2453 const auto stylusInfo = findDeviceByName(stylus->getName());
2454 ASSERT_TRUE(stylusInfo);
2455
2456 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2457
2458 const auto touchscreenId = mDeviceInfo.getId();
2459
2460 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2461 // pressure data from the external stylus.
2462 mDevice->sendSlot(FIRST_SLOT);
2463 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2464 mDevice->sendToolType(MT_TOOL_FINGER);
2465 mDevice->sendDown(centerPoint);
2466 auto waitUntil = std::chrono::system_clock::now() +
2467 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2468 mDevice->sendSync();
2469 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002470 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2471 AMOTION_EVENT_ACTION_DOWN),
2472 WithToolType(ToolType::FINGER),
2473 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2474 AINPUT_SOURCE_STYLUS),
2475 WithButtonState(0),
2476 WithDeviceId(touchscreenId),
2477 WithPressure(1.f)),
2478 waitUntil));
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002479
2480 // The external stylus did not generate any events.
2481 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2482 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2483}
2484
Michael Wrightd02c5b62014-02-10 15:10:22 -08002485// --- InputDeviceTest ---
2486class InputDeviceTest : public testing::Test {
2487protected:
2488 static const char* DEVICE_NAME;
2489 static const char* DEVICE_LOCATION;
2490 static const int32_t DEVICE_ID;
2491 static const int32_t DEVICE_GENERATION;
2492 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002493 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002494 static const int32_t EVENTHUB_ID;
2495 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2496
2497 std::shared_ptr<FakeEventHub> mFakeEventHub;
2498 sp<FakeInputReaderPolicy> mFakePolicy;
2499 std::unique_ptr<TestInputListener> mFakeListener;
2500 std::unique_ptr<InstrumentedInputReader> mReader;
2501 std::shared_ptr<InputDevice> mDevice;
2502
2503 void SetUp() override {
2504 mFakeEventHub = std::make_unique<FakeEventHub>();
2505 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2506 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002507 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002508 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002509 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002510 identifier.name = DEVICE_NAME;
2511 identifier.location = DEVICE_LOCATION;
2512 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2513 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2514 identifier);
2515 mReader->pushNextDevice(mDevice);
2516 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002517 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002518 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002519
2520 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002521 mFakeListener.reset();
2522 mFakePolicy.clear();
2523 }
2524};
2525
2526const char* InputDeviceTest::DEVICE_NAME = "device";
2527const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2528const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2529const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002530const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002531const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2532 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002533const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002534const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2535
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002536TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002537 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002538 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2539 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002540}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002541
Michael Wrightd02c5b62014-02-10 15:10:22 -08002542TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2543 ASSERT_EQ(mDevice->isEnabled(), false);
2544}
2545
2546TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2547 // Configuration.
2548 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002549 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002550
2551 // Reset.
2552 unused += mDevice->reset(ARBITRARY_TIME);
2553
2554 NotifyDeviceResetArgs resetArgs;
2555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2556 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2557 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2558
2559 // Metadata.
2560 ASSERT_TRUE(mDevice->isIgnored());
2561 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2562
2563 InputDeviceInfo info = mDevice->getDeviceInfo();
2564 ASSERT_EQ(DEVICE_ID, info.getId());
2565 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2566 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2567 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2568
2569 // State queries.
2570 ASSERT_EQ(0, mDevice->getMetaState());
2571
2572 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2573 << "Ignored device should return unknown key code state.";
2574 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2575 << "Ignored device should return unknown scan code state.";
2576 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2577 << "Ignored device should return unknown switch state.";
2578
2579 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2580 uint8_t flags[2] = { 0, 1 };
2581 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2582 << "Ignored device should never mark any key codes.";
2583 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2584 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2585}
2586
2587TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2588 // Configuration.
2589 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2590
2591 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002592 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2593 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002594 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2595 mapper1.setMetaState(AMETA_ALT_ON);
2596 mapper1.addSupportedKeyCode(AKEYCODE_A);
2597 mapper1.addSupportedKeyCode(AKEYCODE_B);
2598 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2599 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2600 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2601 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2602 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2603
2604 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002605 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2606 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002607 mapper2.setMetaState(AMETA_SHIFT_ON);
2608
2609 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002610 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002611
Harry Cuttsf13161a2023-03-08 14:15:49 +00002612 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2613 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002614 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002615 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002617 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2618 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002619
2620 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002621 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002622 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2623 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002624
2625 NotifyDeviceResetArgs resetArgs;
2626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2627 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2628 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2629
2630 // Metadata.
2631 ASSERT_FALSE(mDevice->isIgnored());
2632 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2633
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002634 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002636 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2638 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2639
2640 // State queries.
2641 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2642 << "Should query mappers and combine meta states.";
2643
2644 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2645 << "Should return unknown key code state when source not supported.";
2646 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2647 << "Should return unknown scan code state when source not supported.";
2648 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2649 << "Should return unknown switch state when source not supported.";
2650
2651 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2652 << "Should query mapper when source is supported.";
2653 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2654 << "Should query mapper when source is supported.";
2655 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2656 << "Should query mapper when source is supported.";
2657
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002658 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002659 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002660 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661 << "Should do nothing when source is unsupported.";
2662 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2663 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2664 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2665 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2666
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002667 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002668 << "Should query mapper when source is supported.";
2669 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2670 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2671 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2672 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2673
2674 // Event handling.
2675 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002676 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002677 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002678
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002679 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2680 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002681}
2682
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -07002683TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2684 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2685 FakeInputMapper& mapper =
2686 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2687 AINPUT_SOURCE_KEYBOARD);
2688 NotifyMotionArgs args1;
2689 NotifySwitchArgs args2;
2690 NotifyKeyArgs args3;
2691 mapper.setProcessResult({args1, args2, args3});
2692
2693 InputReaderConfiguration config;
2694 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2695
2696 RawEvent event;
2697 event.deviceId = EVENTHUB_ID;
2698 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2699
2700 for (auto& arg : notifyArgs) {
2701 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2702 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2703 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2704 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2705 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2706 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2707 }
2708 }
2709}
2710
2711TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2712 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2713 FakeInputMapper& mapper =
2714 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2715 AINPUT_SOURCE_KEYBOARD);
2716 NotifyMotionArgs args;
2717 mapper.setProcessResult({args});
2718
2719 InputReaderConfiguration config;
2720 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2721
2722 RawEvent event;
2723 event.deviceId = EVENTHUB_ID;
2724 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2725
2726 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2727 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2728}
2729
2730TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2731 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2732 FakeInputMapper& mapper =
2733 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2734 AINPUT_SOURCE_KEYBOARD);
2735 NotifyMotionArgs args;
2736 args.policyFlags = POLICY_FLAG_WAKE;
2737 mapper.setProcessResult({args});
2738
2739 InputReaderConfiguration config;
2740 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2741
2742 RawEvent event;
2743 event.deviceId = EVENTHUB_ID;
2744 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2745
2746 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2747 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2748}
2749
Arthur Hung2c9a3342019-07-23 14:18:59 +08002750// A single input device is associated with a specific display. Check that:
2751// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh48189772023-05-30 14:12:49 +00002752// 2. Device is disabled when configure API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002753TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002754 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2755 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002756
2757 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002758 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002759 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2760 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002761
2762 // Device should be enabled by default.
2763 ASSERT_TRUE(mDevice->isEnabled());
2764
2765 // Prepare associated info.
2766 constexpr uint8_t hdmi = 1;
2767 const std::string UNIQUE_ID = "local:1";
2768
2769 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002770 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002771 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002772 // Device should be disabled because it is associated with a specific display via
2773 // input port <-> display port association, but the corresponding display is not found
2774 ASSERT_FALSE(mDevice->isEnabled());
2775
2776 // Prepare displays.
2777 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002778 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002779 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002780 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002781 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002782 ASSERT_TRUE(mDevice->isEnabled());
2783
2784 // Device should be disabled after set disable.
2785 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002786 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002787 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002788 ASSERT_FALSE(mDevice->isEnabled());
2789
2790 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002791 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002792 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002793 ASSERT_FALSE(mDevice->isEnabled());
2794}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002795
Christine Franks1ba71cc2021-04-07 14:37:42 -07002796TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2797 // Device should be enabled by default.
2798 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002799 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2800 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002801 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002802 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2803 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002804 ASSERT_TRUE(mDevice->isEnabled());
2805
2806 // Device should be disabled because it is associated with a specific display, but the
2807 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002808 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002809 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002810 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002811 ASSERT_FALSE(mDevice->isEnabled());
2812
2813 // Device should be enabled when a display is found.
2814 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002815 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002816 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002817 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002818 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002819 ASSERT_TRUE(mDevice->isEnabled());
2820
2821 // Device should be disabled after set disable.
2822 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002823 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002824 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002825 ASSERT_FALSE(mDevice->isEnabled());
2826
2827 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002828 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002829 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002830 ASSERT_FALSE(mDevice->isEnabled());
2831}
2832
Christine Franks2a2293c2022-01-18 11:51:16 -08002833TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2834 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002835 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2836 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002837 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002838 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2839 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002840
Christine Franks2a2293c2022-01-18 11:51:16 -08002841 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2842 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002843 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002844 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002845 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002846 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002847 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2848}
2849
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002850/**
2851 * This test reproduces a crash caused by a dangling reference that remains after device is added
2852 * and removed. The reference is accessed in InputDevice::dump(..);
2853 */
2854TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2855 constexpr int32_t TEST_EVENTHUB_ID = 10;
2856 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2857
Harry Cutts33476232023-01-30 19:57:29 +00002858 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh82f29a12023-06-13 15:05:53 +00002859 auto _ = device.addEventHubDevice(ARBITRARY_TIME, TEST_EVENTHUB_ID,
2860 mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002861 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2862 std::string dumpStr, eventHubDevStr;
2863 device.dump(dumpStr, eventHubDevStr);
2864}
2865
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002866TEST_F(InputDeviceTest, GetBluetoothAddress) {
2867 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2868 ASSERT_TRUE(address);
2869 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2870}
2871
Michael Wrightd02c5b62014-02-10 15:10:22 -08002872// --- SwitchInputMapperTest ---
2873
2874class SwitchInputMapperTest : public InputMapperTest {
2875protected:
2876};
2877
2878TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002879 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002880
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002881 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002882}
2883
2884TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002885 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002886
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002887 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002888 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002889
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002890 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002891 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002892}
2893
2894TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002895 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002896 std::list<NotifyArgs> out;
2897 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2898 ASSERT_TRUE(out.empty());
2899 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2900 ASSERT_TRUE(out.empty());
2901 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2902 ASSERT_TRUE(out.empty());
2903 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002904
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002905 ASSERT_EQ(1u, out.size());
2906 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002907 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002908 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2909 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002910 args.switchMask);
2911 ASSERT_EQ(uint32_t(0), args.policyFlags);
2912}
2913
Chris Ye87143712020-11-10 05:05:58 +00002914// --- VibratorInputMapperTest ---
2915class VibratorInputMapperTest : public InputMapperTest {
2916protected:
2917 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2918};
2919
2920TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002921 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002922
2923 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2924}
2925
2926TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002927 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002928
2929 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2930}
2931
2932TEST_F(VibratorInputMapperTest, Vibrate) {
2933 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002934 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00002935 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002936
2937 VibrationElement pattern(2);
2938 VibrationSequence sequence(2);
2939 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002940 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2941 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002942 sequence.addElement(pattern);
2943 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002944 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2945 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002946 sequence.addElement(pattern);
2947
2948 std::vector<int64_t> timings = {0, 1};
2949 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2950
2951 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002952 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002953 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002954 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002955 // Verify vibrator state listener was notified.
2956 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002957 ASSERT_EQ(1u, out.size());
2958 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2959 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2960 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002961 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002962 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002963 ASSERT_FALSE(mapper.isVibrating());
2964 // Verify vibrator state listener was notified.
2965 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002966 ASSERT_EQ(1u, out.size());
2967 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2968 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2969 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002970}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002971
Chris Yef59a2f42020-10-16 12:55:26 -07002972// --- SensorInputMapperTest ---
2973
2974class SensorInputMapperTest : public InputMapperTest {
2975protected:
2976 static const int32_t ACCEL_RAW_MIN;
2977 static const int32_t ACCEL_RAW_MAX;
2978 static const int32_t ACCEL_RAW_FUZZ;
2979 static const int32_t ACCEL_RAW_FLAT;
2980 static const int32_t ACCEL_RAW_RESOLUTION;
2981
2982 static const int32_t GYRO_RAW_MIN;
2983 static const int32_t GYRO_RAW_MAX;
2984 static const int32_t GYRO_RAW_FUZZ;
2985 static const int32_t GYRO_RAW_FLAT;
2986 static const int32_t GYRO_RAW_RESOLUTION;
2987
2988 static const float GRAVITY_MS2_UNIT;
2989 static const float DEGREE_RADIAN_UNIT;
2990
2991 void prepareAccelAxes();
2992 void prepareGyroAxes();
2993 void setAccelProperties();
2994 void setGyroProperties();
2995 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2996};
2997
2998const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2999const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3000const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3001const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3002const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3003
3004const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3005const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3006const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3007const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3008const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3009
3010const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3011const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3012
3013void SensorInputMapperTest::prepareAccelAxes() {
3014 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3015 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3016 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3017 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3018 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3019 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3020}
3021
3022void SensorInputMapperTest::prepareGyroAxes() {
3023 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3024 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3025 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3026 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3027 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3028 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3029}
3030
3031void SensorInputMapperTest::setAccelProperties() {
3032 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3033 /* sensorDataIndex */ 0);
3034 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3035 /* sensorDataIndex */ 1);
3036 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3037 /* sensorDataIndex */ 2);
3038 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3039 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3040 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3041 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3042 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3043}
3044
3045void SensorInputMapperTest::setGyroProperties() {
3046 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3047 /* sensorDataIndex */ 0);
3048 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3049 /* sensorDataIndex */ 1);
3050 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3051 /* sensorDataIndex */ 2);
3052 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3053 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3054 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3055 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3056 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3057}
3058
3059TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00003060 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003061
3062 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3063}
3064
3065TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3066 setAccelProperties();
3067 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003068 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003069
3070 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3071 std::chrono::microseconds(10000),
3072 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003073 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3075 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3076 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3078 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003079
3080 NotifySensorArgs args;
3081 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3082 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3083 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3084
3085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3086 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3087 ASSERT_EQ(args.deviceId, DEVICE_ID);
3088 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3089 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3090 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3091 ASSERT_EQ(args.values, values);
3092 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3093}
3094
3095TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3096 setGyroProperties();
3097 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003098 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003099
3100 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3101 std::chrono::microseconds(10000),
3102 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003103 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003104 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3105 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003109
3110 NotifySensorArgs args;
3111 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3112 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3113 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3114
3115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3116 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3117 ASSERT_EQ(args.deviceId, DEVICE_ID);
3118 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3119 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3120 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3121 ASSERT_EQ(args.values, values);
3122 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3123}
3124
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125// --- KeyboardInputMapperTest ---
3126
3127class KeyboardInputMapperTest : public InputMapperTest {
3128protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003129 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003130 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003131 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003132
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003133 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003134 int32_t originalKeyCode, int32_t rotatedKeyCode,
3135 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003136};
3137
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003138/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3139 * orientation.
3140 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003141void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003142 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3143 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003144}
3145
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003146void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003147 int32_t originalScanCode, int32_t originalKeyCode,
3148 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003149 NotifyKeyArgs args;
3150
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3153 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3154 ASSERT_EQ(originalScanCode, args.scanCode);
3155 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003156 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003157
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3160 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3161 ASSERT_EQ(originalScanCode, args.scanCode);
3162 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003163 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003164}
3165
Michael Wrightd02c5b62014-02-10 15:10:22 -08003166TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003167 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003168 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003169 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003170
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003171 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003172}
3173
3174TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3175 const int32_t USAGE_A = 0x070004;
3176 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003177 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3178 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003179 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3180 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3181 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003182
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003183 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003184 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003185 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003186 // Initial metastate is AMETA_NONE.
3187 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003188
3189 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003190 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003191 NotifyKeyArgs args;
3192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3193 ASSERT_EQ(DEVICE_ID, args.deviceId);
3194 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3195 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3196 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3197 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3198 ASSERT_EQ(KEY_HOME, args.scanCode);
3199 ASSERT_EQ(AMETA_NONE, args.metaState);
3200 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3201 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3202 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3203
3204 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003205 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3207 ASSERT_EQ(DEVICE_ID, args.deviceId);
3208 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3209 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3210 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3211 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3212 ASSERT_EQ(KEY_HOME, args.scanCode);
3213 ASSERT_EQ(AMETA_NONE, args.metaState);
3214 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3215 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3216 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3217
3218 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003219 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3220 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3222 ASSERT_EQ(DEVICE_ID, args.deviceId);
3223 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3224 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3225 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3226 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3227 ASSERT_EQ(0, args.scanCode);
3228 ASSERT_EQ(AMETA_NONE, args.metaState);
3229 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3230 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3231 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3232
3233 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3235 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3237 ASSERT_EQ(DEVICE_ID, args.deviceId);
3238 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3239 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3240 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3241 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3242 ASSERT_EQ(0, args.scanCode);
3243 ASSERT_EQ(AMETA_NONE, args.metaState);
3244 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3245 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3246 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3247
3248 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003249 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3250 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3252 ASSERT_EQ(DEVICE_ID, args.deviceId);
3253 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3254 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3255 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3256 ASSERT_EQ(0, args.keyCode);
3257 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3258 ASSERT_EQ(AMETA_NONE, args.metaState);
3259 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3260 ASSERT_EQ(0U, args.policyFlags);
3261 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3262
3263 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3265 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3267 ASSERT_EQ(DEVICE_ID, args.deviceId);
3268 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3269 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3270 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3271 ASSERT_EQ(0, args.keyCode);
3272 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3273 ASSERT_EQ(AMETA_NONE, args.metaState);
3274 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3275 ASSERT_EQ(0U, args.policyFlags);
3276 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3277}
3278
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003279TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3280 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3281 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3282 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3283
3284 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003285 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003286 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3287
3288 // Key down by scan code.
3289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3290 NotifyKeyArgs args;
3291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3292 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3293
3294 // Key up by scan code.
3295 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3297 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3298}
3299
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003300/**
3301 * Ensure that the readTime is set to the time when the EV_KEY is received.
3302 */
3303TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3304 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3305
3306 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003307 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003308 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3309 NotifyKeyArgs args;
3310
3311 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003312 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3314 ASSERT_EQ(12, args.readTime);
3315
3316 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003317 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3319 ASSERT_EQ(15, args.readTime);
3320}
3321
Michael Wrightd02c5b62014-02-10 15:10:22 -08003322TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003323 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3324 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003325 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3326 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3327 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003329 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003330 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003331 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003332
Arthur Hung95f68612022-04-07 14:08:22 +08003333 // Initial metastate is AMETA_NONE.
3334 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003335
3336 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003337 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338 NotifyKeyArgs args;
3339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3340 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003341 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003342 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003343
3344 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003345 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3347 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003348 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003349
3350 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003351 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3353 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003354 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355
3356 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003357 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3359 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003360 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003361 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003362}
3363
3364TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003365 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3366 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3367 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3368 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003369
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003370 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003371 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003372 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003373
Michael Wrighta9cf4192022-12-01 23:46:39 +00003374 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003375 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3376 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3377 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3378 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3379 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3380 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3381 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3382 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3383}
3384
3385TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003386 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3387 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3388 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3389 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003390
Michael Wrightd02c5b62014-02-10 15:10:22 -08003391 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003392 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003393 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003394 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003395
Michael Wrighta9cf4192022-12-01 23:46:39 +00003396 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003397 ASSERT_NO_FATAL_FAILURE(
3398 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3399 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3400 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3401 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3402 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3403 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3404 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003405
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003406 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003407 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003408 ASSERT_NO_FATAL_FAILURE(
3409 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3410 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3411 AKEYCODE_DPAD_UP, DISPLAY_ID));
3412 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3413 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3414 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3415 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003416
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003417 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003418 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003419 ASSERT_NO_FATAL_FAILURE(
3420 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3421 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3422 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3423 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3424 AKEYCODE_DPAD_UP, DISPLAY_ID));
3425 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3426 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003427
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003428 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003429 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003430 ASSERT_NO_FATAL_FAILURE(
3431 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3432 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3433 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3434 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3435 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3436 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3437 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003438
3439 // Special case: if orientation changes while key is down, we still emit the same keycode
3440 // in the key up as we did in the key down.
3441 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003442 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003443 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3446 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3447 ASSERT_EQ(KEY_UP, args.scanCode);
3448 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3449
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003450 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003451 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003452 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3454 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3455 ASSERT_EQ(KEY_UP, args.scanCode);
3456 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3457}
3458
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003459TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3460 // If the keyboard is not orientation aware,
3461 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003462 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003463
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003464 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003465 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003466 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003467 NotifyKeyArgs args;
3468
3469 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003470 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003472 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3474 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3475
Michael Wrighta9cf4192022-12-01 23:46:39 +00003476 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3481 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3482}
3483
3484TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3485 // If the keyboard is orientation aware,
3486 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003487 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003488
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003489 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003490 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003491 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003492 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003493 NotifyKeyArgs args;
3494
3495 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3496 // ^--- already checked by the previous test
3497
Michael Wrighta9cf4192022-12-01 23:46:39 +00003498 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003499 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3504 ASSERT_EQ(DISPLAY_ID, args.displayId);
3505
3506 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003507 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003508 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003509 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003512 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3514 ASSERT_EQ(newDisplayId, args.displayId);
3515}
3516
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003518 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003519 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003520 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003522 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003523 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003525 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003526 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527}
3528
Philip Junker4af3b3d2021-12-14 10:36:55 +01003529TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3530 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003531 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003532 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3533
3534 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3535 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3536 << "If a mapping is available, the result is equal to the mapping";
3537
3538 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3539 << "If no mapping is available, the result is the key location";
3540}
3541
Michael Wrightd02c5b62014-02-10 15:10:22 -08003542TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003543 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003544 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003545 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003547 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003548 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003549
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003550 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003551 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003552}
3553
3554TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003555 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003556 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003557 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003558
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003559 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003560
Michael Wrightd02c5b62014-02-10 15:10:22 -08003561 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003562 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003563 ASSERT_TRUE(flags[0]);
3564 ASSERT_FALSE(flags[1]);
3565}
3566
3567TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003568 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3569 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3570 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3571 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3572 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3573 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003574
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003575 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003576 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003577 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003578 // Initial metastate is AMETA_NONE.
3579 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003580
3581 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003582 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3583 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3584 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003585
3586 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3588 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003589 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3590 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3591 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003592 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003593
3594 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3596 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003597 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3598 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3599 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003600 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003601
3602 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003603 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3604 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003605 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3606 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3607 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003608 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609
3610 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003611 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003613 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3614 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3615 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003616 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003617
3618 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003619 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3620 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003621 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3622 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3623 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003624 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003625
3626 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3628 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003629 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3630 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3631 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003632 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003633}
3634
Chris Yea52ade12020-08-27 16:49:20 -07003635TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3636 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3637 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3638 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3639 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3640
3641 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003642 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003643 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3644
Chris Yea52ade12020-08-27 16:49:20 -07003645 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003646 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003647 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3648 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3649 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3650 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3651
3652 NotifyKeyArgs args;
3653 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3656 ASSERT_EQ(AMETA_NONE, args.metaState);
3657 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3658 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3659 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3660
3661 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003662 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3664 ASSERT_EQ(AMETA_NONE, args.metaState);
3665 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3666 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3667 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3668}
3669
Arthur Hung2c9a3342019-07-23 14:18:59 +08003670TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3671 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003672 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3673 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3674 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3675 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003676
3677 // keyboard 2.
3678 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003679 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003680 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003681 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003682 std::shared_ptr<InputDevice> device2 =
3683 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003684 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003685
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003686 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3687 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3688 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3689 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003690
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003691 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003692 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003693 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003694
Arpit Singh67ca6842023-04-26 14:43:16 +00003695 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003696 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003697 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3698 mFakePolicy
3699 ->getReaderConfiguration(),
3700 AINPUT_SOURCE_KEYBOARD,
3701 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003702 std::list<NotifyArgs> unused =
3703 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003704 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003705 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003706
3707 // Prepared displays and associated info.
3708 constexpr uint8_t hdmi1 = 0;
3709 constexpr uint8_t hdmi2 = 1;
3710 const std::string SECONDARY_UNIQUE_ID = "local:1";
3711
3712 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3713 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3714
3715 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003716 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003717 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003718 ASSERT_FALSE(device2->isEnabled());
3719
3720 // Prepare second display.
3721 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003722 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003723 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003724 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003725 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003726 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003727 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003728 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003729
3730 // Device should be enabled after the associated display is found.
3731 ASSERT_TRUE(mDevice->isEnabled());
3732 ASSERT_TRUE(device2->isEnabled());
3733
3734 // Test pad key events
3735 ASSERT_NO_FATAL_FAILURE(
3736 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3737 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3738 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3739 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3740 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3741 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3742 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3743
3744 ASSERT_NO_FATAL_FAILURE(
3745 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3746 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3747 AKEYCODE_DPAD_RIGHT, newDisplayId));
3748 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3749 AKEYCODE_DPAD_DOWN, newDisplayId));
3750 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3751 AKEYCODE_DPAD_LEFT, newDisplayId));
3752}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003753
arthurhungc903df12020-08-11 15:08:42 +08003754TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3755 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3756 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3757 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3758 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3759 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3760 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3761
3762 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003763 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003764 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003765 // Initial metastate is AMETA_NONE.
3766 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003767
3768 // Initialization should have turned all of the lights off.
3769 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3770 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3771 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3772
3773 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003776 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3777 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3778
3779 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003782 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3783 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3784
3785 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003788 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3789 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3790
3791 mFakeEventHub->removeDevice(EVENTHUB_ID);
3792 mReader->loopOnce();
3793
3794 // keyboard 2 should default toggle keys.
3795 const std::string USB2 = "USB2";
3796 const std::string DEVICE_NAME2 = "KEYBOARD2";
3797 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3798 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3799 std::shared_ptr<InputDevice> device2 =
3800 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003801 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003802 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3803 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3804 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3805 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3806 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3807 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3808
Arpit Singh67ca6842023-04-26 14:43:16 +00003809 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003810 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003811 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3812 mFakePolicy
3813 ->getReaderConfiguration(),
3814 AINPUT_SOURCE_KEYBOARD,
3815 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003816 std::list<NotifyArgs> unused =
3817 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003818 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003819 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003820
3821 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3822 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3823 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003824 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3825 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003826}
3827
Arthur Hungcb40a002021-08-03 14:31:01 +00003828TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3829 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3830 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3831 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3832
3833 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh67ca6842023-04-26 14:43:16 +00003834 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003835 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3836 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003837 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003838 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003839 // Initial metastate is AMETA_NONE.
3840 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003841
3842 mReader->toggleCapsLockState(DEVICE_ID);
3843 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3844}
3845
Arthur Hungfb3cc112022-04-13 07:39:50 +00003846TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3847 // keyboard 1.
3848 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3849 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3850 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3851 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3852 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3853 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3854
3855 KeyboardInputMapper& mapper1 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003856 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003857 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3858
3859 // keyboard 2.
3860 const std::string USB2 = "USB2";
3861 const std::string DEVICE_NAME2 = "KEYBOARD2";
3862 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3863 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3864 std::shared_ptr<InputDevice> device2 =
3865 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3866 ftl::Flags<InputDeviceClass>(0));
3867 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3868 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3869 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3870 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3871 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3872 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3873
Arpit Singh67ca6842023-04-26 14:43:16 +00003874 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003875 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003876 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3877 mFakePolicy
3878 ->getReaderConfiguration(),
3879 AINPUT_SOURCE_KEYBOARD,
3880 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003881 std::list<NotifyArgs> unused =
3882 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003883 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003884 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003885
Arthur Hung95f68612022-04-07 14:08:22 +08003886 // Initial metastate is AMETA_NONE.
3887 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3888 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3889
3890 // Toggle num lock on and off.
3891 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3892 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003893 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3894 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3895 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3896
3897 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3898 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3899 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3900 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3901 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3902
3903 // Toggle caps lock on and off.
3904 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3905 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3906 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3907 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3908 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3909
3910 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3911 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3912 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3913 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3914 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3915
3916 // Toggle scroll lock on and off.
3917 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3918 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3919 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3920 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3921 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3922
3923 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3924 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3925 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3926 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3927 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3928}
3929
Arthur Hung2141d542022-08-23 07:45:21 +00003930TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3931 const int32_t USAGE_A = 0x070004;
3932 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3933 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3934
3935 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003936 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00003937 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3938 // Key down by scan code.
3939 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3940 NotifyKeyArgs args;
3941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3942 ASSERT_EQ(DEVICE_ID, args.deviceId);
3943 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3944 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3945 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3946 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3947 ASSERT_EQ(KEY_HOME, args.scanCode);
3948 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3949
3950 // Disable device, it should synthesize cancellation events for down events.
3951 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003952 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00003953
3954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3955 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3956 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3957 ASSERT_EQ(KEY_HOME, args.scanCode);
3958 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3959}
3960
Zixuan Qufecb6062022-11-12 04:44:31 +00003961TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh67ca6842023-04-26 14:43:16 +00003962 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3963 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00003964 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003965 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3966 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00003967
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003968 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00003969 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3970
3971 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003972 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00003973
3974 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3975 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3976 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3977 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3978 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003979 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
3980
3981 // Call change layout association with the same values: Generation shouldn't change
3982 generation = mReader->getContext()->getGeneration();
3983 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3984 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3985 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
3986 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00003987}
3988
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003989TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3990 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3991 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3992
3993 // Configuration
Arpit Singh67ca6842023-04-26 14:43:16 +00003994 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003995 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3996 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003997 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003998
3999 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
4000 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
4001}
4002
Justin Chung71ddb432023-03-27 04:29:07 +00004003TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
4004 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
4005 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004006 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Justin Chung71ddb432023-03-27 04:29:07 +00004007 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4008 NotifyKeyArgs args;
4009
4010 // Key down
4011 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
4012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4013 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
4014}
4015
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004016// --- KeyboardInputMapperTest_ExternalDevice ---
4017
4018class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4019protected:
Chris Yea52ade12020-08-27 16:49:20 -07004020 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004021};
4022
4023TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004024 // For external devices, keys will trigger wake on key down. Media keys should also trigger
4025 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07004026
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004027 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4028 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4029 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4030 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004031
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004032 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004033 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004034 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004035
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004037 NotifyKeyArgs args;
4038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4039 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4040
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004041 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4043 ASSERT_EQ(uint32_t(0), args.policyFlags);
4044
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004047 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07004048
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004049 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4051 ASSERT_EQ(uint32_t(0), args.policyFlags);
4052
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4055 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4056
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004057 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4059 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4060}
4061
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004062TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004063 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004064
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004065 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4066 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4067 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004068
Powei Fengd041c5d2019-05-03 17:11:33 -07004069 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004070 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004071 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004072 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004073
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004075 NotifyKeyArgs args;
4076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4077 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4078
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004079 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4081 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4082
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4085 ASSERT_EQ(uint32_t(0), args.policyFlags);
4086
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004087 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4089 ASSERT_EQ(uint32_t(0), args.policyFlags);
4090
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004091 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4093 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4094
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004095 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4097 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4098}
4099
Michael Wrightd02c5b62014-02-10 15:10:22 -08004100// --- CursorInputMapperTest ---
4101
4102class CursorInputMapperTest : public InputMapperTest {
4103protected:
4104 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4105
Michael Wright17db18e2020-06-26 20:51:44 +01004106 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107
Chris Yea52ade12020-08-27 16:49:20 -07004108 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109 InputMapperTest::SetUp();
4110
Michael Wright17db18e2020-06-26 20:51:44 +01004111 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004112 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004113 }
4114
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004115 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4116 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004117
Michael Wrighta9cf4192022-12-01 23:46:39 +00004118 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004119 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4120 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4121 }
4122
4123 void prepareSecondaryDisplay() {
4124 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004125 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004126 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004127 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004128
4129 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4130 float pressure) {
4131 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4132 0.0f, 0.0f, 0.0f, EPSILON));
4133 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004134};
4135
4136const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4137
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004138void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4139 int32_t originalY, int32_t rotatedX,
4140 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141 NotifyMotionArgs args;
4142
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004148 ASSERT_NO_FATAL_FAILURE(
4149 assertCursorPointerCoords(args.pointerCoords[0],
4150 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4151 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152}
4153
4154TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004156 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004158 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004159}
4160
4161TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
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
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004165 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004166}
4167
4168TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004169 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004170 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004171
4172 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004173 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004174
4175 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004176 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4177 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4179 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4180
4181 // When the bounds are set, then there should be a valid motion range.
4182 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4183
4184 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004185 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004186
4187 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4188 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4189 1, 800 - 1, 0.0f, 0.0f));
4190 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4191 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4192 2, 480 - 1, 0.0f, 0.0f));
4193 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4194 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4195 0.0f, 1.0f, 0.0f, 0.0f));
4196}
4197
4198TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004200 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201
4202 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004203 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004204
4205 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4206 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4207 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4208 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4209 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4210 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4211 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4212 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4213 0.0f, 1.0f, 0.0f, 0.0f));
4214}
4215
4216TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004218 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219
arthurhungdcef2dc2020-08-11 14:47:50 +08004220 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004221
4222 NotifyMotionArgs args;
4223
4224 // Button press.
4225 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4227 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4229 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4230 ASSERT_EQ(DEVICE_ID, args.deviceId);
4231 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4232 ASSERT_EQ(uint32_t(0), args.policyFlags);
4233 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4234 ASSERT_EQ(0, args.flags);
4235 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4236 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4237 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004238 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004240 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004241 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4243 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4244 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4245
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4247 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4248 ASSERT_EQ(DEVICE_ID, args.deviceId);
4249 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4250 ASSERT_EQ(uint32_t(0), args.policyFlags);
4251 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4252 ASSERT_EQ(0, args.flags);
4253 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4254 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4255 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004256 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004257 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004258 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004259 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004260 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4261 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4262 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4263
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004265 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4266 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4268 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4269 ASSERT_EQ(DEVICE_ID, args.deviceId);
4270 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4271 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004272 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4273 ASSERT_EQ(0, args.flags);
4274 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4275 ASSERT_EQ(0, args.buttonState);
4276 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004277 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004278 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004279 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004280 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004281 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4282 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4283 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4284
4285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4286 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4287 ASSERT_EQ(DEVICE_ID, args.deviceId);
4288 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4289 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4291 ASSERT_EQ(0, args.flags);
4292 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4293 ASSERT_EQ(0, args.buttonState);
4294 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004295 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004297 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004298 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4300 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4301 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4302}
4303
4304TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004305 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004306 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307
4308 NotifyMotionArgs args;
4309
4310 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4312 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004315 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4316 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4317 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318
4319 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004320 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4321 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4323 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004324 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4325 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004326}
4327
4328TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004329 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004330 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331
4332 NotifyMotionArgs args;
4333
4334 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4338 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004339 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4342 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004343 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004344
Michael Wrightd02c5b62014-02-10 15:10:22 -08004345 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004346 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 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(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004349 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004350 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004351
4352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004353 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004354 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004355}
4356
4357TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004359 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360
4361 NotifyMotionArgs args;
4362
4363 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004364 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4365 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4366 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4369 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004370 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4371 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4372 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004373
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4375 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004376 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4377 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4378 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004379
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4382 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004386 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4387 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4388 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389
4390 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004391 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4392 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004394 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004395 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004396
4397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004399 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400}
4401
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004402TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004403 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004405 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4406 // need to be rotated.
4407 addConfigurationProperty("cursor.orientationAware", "1");
Arpit Singhe036ad72023-04-27 12:48:15 +00004408 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409
Michael Wrighta9cf4192022-12-01 23:46:39 +00004410 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4412 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4413 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4414 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4415 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4416 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4417 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4418 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4419}
4420
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004421TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004422 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004424 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4425 // orientation-aware are affected by display rotation.
Arpit Singhe036ad72023-04-27 12:48:15 +00004426 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004428 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004429 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4431 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4432 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4433 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4434 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4435 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4436 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4437 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4438
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004439 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004440 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004441 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4442 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4443 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4444 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4445 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4446 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4447 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4448 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004449
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004450 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004451 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4453 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4454 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4455 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4456 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4457 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4458 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4459 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4460
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004461 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004462 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004463 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4464 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4465 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4466 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4467 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4468 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4469 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4470 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471}
4472
4473TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004475 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004476
4477 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4478 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479
4480 NotifyMotionArgs motionArgs;
4481 NotifyKeyArgs keyArgs;
4482
4483 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004484 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4485 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4487 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4488 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004489 ASSERT_NO_FATAL_FAILURE(
4490 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4493 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4494 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004495 ASSERT_NO_FATAL_FAILURE(
4496 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004497
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004498 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4499 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
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_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004502 ASSERT_EQ(0, 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004505
4506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004507 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508 ASSERT_EQ(0, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004511
4512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004513 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004514 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004515 ASSERT_NO_FATAL_FAILURE(
4516 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004517
4518 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4521 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4523 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4524 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004525 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004526 ASSERT_NO_FATAL_FAILURE(
4527 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004528
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4530 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4531 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004532 ASSERT_NO_FATAL_FAILURE(
4533 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004534
4535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4536 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4537 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004538 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004539 ASSERT_NO_FATAL_FAILURE(
4540 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004541
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004545 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004546 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004547 ASSERT_NO_FATAL_FAILURE(
4548 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004549
4550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004552 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004553 ASSERT_NO_FATAL_FAILURE(
4554 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004556 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004559 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4560 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004561 ASSERT_NO_FATAL_FAILURE(
4562 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004563 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4564 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004565
4566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004567 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004568 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004569 ASSERT_NO_FATAL_FAILURE(
4570 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004571
Michael Wrightd02c5b62014-02-10 15:10:22 -08004572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4573 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004575 ASSERT_NO_FATAL_FAILURE(
4576 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004577
4578 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004579 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4580 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4582 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4583 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004584
Michael Wrightd02c5b62014-02-10 15:10:22 -08004585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004586 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004587 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004588 ASSERT_NO_FATAL_FAILURE(
4589 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004590
4591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4592 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4593 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004594 ASSERT_NO_FATAL_FAILURE(
4595 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004596
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004597 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4598 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004600 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004601 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004602 ASSERT_NO_FATAL_FAILURE(
4603 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004604
4605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004606 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004607 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004608
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004609 ASSERT_NO_FATAL_FAILURE(
4610 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4612 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4613 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4614
4615 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004616 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4617 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4619 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4620 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004621
Michael Wrightd02c5b62014-02-10 15:10:22 -08004622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004623 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004624 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004625 ASSERT_NO_FATAL_FAILURE(
4626 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004627
4628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4629 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4630 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004631 ASSERT_NO_FATAL_FAILURE(
4632 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004633
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004637 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004639 ASSERT_NO_FATAL_FAILURE(
4640 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004641
4642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4643 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4644 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004645 ASSERT_NO_FATAL_FAILURE(
4646 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004647
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4649 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4650 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4651
4652 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004653 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4656 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4657 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004658
Michael Wrightd02c5b62014-02-10 15:10:22 -08004659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004660 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004661 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004662 ASSERT_NO_FATAL_FAILURE(
4663 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004664
4665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4666 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4667 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004668 ASSERT_NO_FATAL_FAILURE(
4669 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004670
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004671 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4672 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004674 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004675 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004676 ASSERT_NO_FATAL_FAILURE(
4677 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004678
4679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4680 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4681 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004682 ASSERT_NO_FATAL_FAILURE(
4683 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004684
Michael Wrightd02c5b62014-02-10 15:10:22 -08004685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4686 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4687 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4688
4689 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4693 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4694 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004695
Michael Wrightd02c5b62014-02-10 15:10:22 -08004696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004697 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004699 ASSERT_NO_FATAL_FAILURE(
4700 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004701
4702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4703 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4704 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004705 ASSERT_NO_FATAL_FAILURE(
4706 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004707
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004708 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4709 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004711 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004712 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004713 ASSERT_NO_FATAL_FAILURE(
4714 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004715
4716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4717 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4718 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004719 ASSERT_NO_FATAL_FAILURE(
4720 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004721
Michael Wrightd02c5b62014-02-10 15:10:22 -08004722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4723 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4724 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4725}
4726
4727TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004728 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004729 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004730
4731 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4732 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004733
4734 NotifyMotionArgs args;
4735
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004736 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4737 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004740 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4741 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4743 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 +00004744 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004745}
4746
4747TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004748 addConfigurationProperty("cursor.mode", "pointer");
4749 mFakePolicy->setPointerCapture(true);
Arpit Singhe036ad72023-04-27 12:48:15 +00004750 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004751
4752 NotifyDeviceResetArgs resetArgs;
4753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4754 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4755 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4756
4757 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4758 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004759
4760 NotifyMotionArgs args;
4761
4762 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004763 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4764 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4765 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4767 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4768 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4770 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 +00004771 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004772
4773 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4777 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4778 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4780 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4782 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4783 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4785 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4786
4787 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004788 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4789 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4791 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4792 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4794 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4796 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4797 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4799 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4800
4801 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4804 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4806 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4808 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4809 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 +00004810 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004811
4812 // Disable pointer capture and check that the device generation got bumped
4813 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004814 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004815 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004816 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004817 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004818
4819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004820 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4821
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004822 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4826 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004827 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4828 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4829 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 +00004830 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831}
4832
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004833/**
4834 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4835 * pointer acceleration or speed processing should not be applied.
4836 */
4837TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4838 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004839 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4840 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004841 mFakePolicy->setVelocityControlParams(testParams);
Arpit Singhe036ad72023-04-27 12:48:15 +00004842 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004843
4844 NotifyDeviceResetArgs resetArgs;
4845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4846 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4847 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4848
4849 NotifyMotionArgs args;
4850
4851 // Move and verify scale is applied.
4852 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4853 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4854 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4856 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4857 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4858 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4859 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4860 ASSERT_GT(relX, 10);
4861 ASSERT_GT(relY, 20);
4862
4863 // Enable Pointer Capture
4864 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004865 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004866 NotifyPointerCaptureChangedArgs captureArgs;
4867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4868 ASSERT_TRUE(captureArgs.request.enable);
4869
4870 // Move and verify scale is not applied.
4871 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4872 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4873 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4875 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4876 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4877 ASSERT_EQ(10, args.pointerCoords[0].getX());
4878 ASSERT_EQ(20, args.pointerCoords[0].getY());
4879}
4880
Prabir Pradhan208360b2022-06-24 18:37:04 +00004881TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4882 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004883 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan208360b2022-06-24 18:37:04 +00004884
4885 NotifyDeviceResetArgs resetArgs;
4886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4887 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4888 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4889
4890 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004891 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004892
4893 NotifyMotionArgs args;
4894
4895 // Verify that the coordinates are rotated.
4896 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4897 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4898 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4900 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4901 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4902 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4903 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4904
4905 // Enable Pointer Capture.
4906 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004907 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004908 NotifyPointerCaptureChangedArgs captureArgs;
4909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4910 ASSERT_TRUE(captureArgs.request.enable);
4911
4912 // Move and verify rotation is not applied.
4913 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4914 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4915 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4917 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4918 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4919 ASSERT_EQ(10, args.pointerCoords[0].getX());
4920 ASSERT_EQ(20, args.pointerCoords[0].getY());
4921}
4922
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004923TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004924 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004925
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004926 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004927 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004928
4929 // Set up the secondary display as the display on which the pointer should be shown.
4930 // The InputDevice is not associated with any display.
4931 prepareSecondaryDisplay();
4932 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004933 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004934
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004935 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004936 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004937
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004938 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004939 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4940 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4941 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004943 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4944 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4945 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004946 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004947}
4948
4949TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004950 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004951
4952 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004953 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004954
4955 // Set up the secondary display as the display on which the pointer should be shown,
4956 // and associate the InputDevice with the secondary display.
4957 prepareSecondaryDisplay();
4958 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4959 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004960 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004961
4962 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4963 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004964
4965 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4966 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4967 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004969 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4970 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4971 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004972 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004973}
4974
4975TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004976 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004977
4978 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004979 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004980 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4981
4982 // Associate the InputDevice with the secondary display.
4983 prepareSecondaryDisplay();
4984 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004985 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004986
4987 // The mapper should not generate any events because it is associated with a display that is
4988 // different from the pointer display.
4989 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4990 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004993}
4994
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004995// --- BluetoothCursorInputMapperTest ---
4996
4997class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4998protected:
4999 void SetUp() override {
5000 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
5001
5002 mFakePointerController = std::make_shared<FakePointerController>();
5003 mFakePolicy->setPointerController(mFakePointerController);
5004 }
5005};
5006
5007TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
5008 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00005009 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005010
5011 nsecs_t kernelEventTime = ARBITRARY_TIME;
5012 nsecs_t expectedEventTime = ARBITRARY_TIME;
5013 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5014 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5016 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5017 WithEventTime(expectedEventTime))));
5018
5019 // Process several events that come in quick succession, according to their timestamps.
5020 for (int i = 0; i < 3; i++) {
5021 constexpr static nsecs_t delta = ms2ns(1);
5022 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
5023 kernelEventTime += delta;
5024 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5025
5026 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5027 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5029 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5030 WithEventTime(expectedEventTime))));
5031 }
5032}
5033
5034TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
5035 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00005036 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005037
5038 nsecs_t expectedEventTime = ARBITRARY_TIME;
5039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5040 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5042 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5043 WithEventTime(expectedEventTime))));
5044
5045 // Process several events with the same timestamp from the kernel.
5046 // Ensure that we do not generate events too far into the future.
5047 constexpr static int32_t numEvents =
5048 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
5049 for (int i = 0; i < numEvents; i++) {
5050 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5051
5052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5055 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5056 WithEventTime(expectedEventTime))));
5057 }
5058
5059 // By processing more events with the same timestamp, we should not generate events with a
5060 // timestamp that is more than the specified max time delta from the timestamp at its injection.
5061 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
5062 for (int i = 0; i < 3; i++) {
5063 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5064 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5066 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5067 WithEventTime(cappedEventTime))));
5068 }
5069}
5070
5071TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
5072 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00005073 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005074
5075 nsecs_t kernelEventTime = ARBITRARY_TIME;
5076 nsecs_t expectedEventTime = ARBITRARY_TIME;
5077 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5078 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5080 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5081 WithEventTime(expectedEventTime))));
5082
5083 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
5084 // smoothening is not needed, its timestamp is not affected.
5085 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
5086 expectedEventTime = kernelEventTime;
5087
5088 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5089 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5091 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5092 WithEventTime(expectedEventTime))));
5093}
5094
Michael Wrightd02c5b62014-02-10 15:10:22 -08005095// --- TouchInputMapperTest ---
5096
5097class TouchInputMapperTest : public InputMapperTest {
5098protected:
5099 static const int32_t RAW_X_MIN;
5100 static const int32_t RAW_X_MAX;
5101 static const int32_t RAW_Y_MIN;
5102 static const int32_t RAW_Y_MAX;
5103 static const int32_t RAW_TOUCH_MIN;
5104 static const int32_t RAW_TOUCH_MAX;
5105 static const int32_t RAW_TOOL_MIN;
5106 static const int32_t RAW_TOOL_MAX;
5107 static const int32_t RAW_PRESSURE_MIN;
5108 static const int32_t RAW_PRESSURE_MAX;
5109 static const int32_t RAW_ORIENTATION_MIN;
5110 static const int32_t RAW_ORIENTATION_MAX;
5111 static const int32_t RAW_DISTANCE_MIN;
5112 static const int32_t RAW_DISTANCE_MAX;
5113 static const int32_t RAW_TILT_MIN;
5114 static const int32_t RAW_TILT_MAX;
5115 static const int32_t RAW_ID_MIN;
5116 static const int32_t RAW_ID_MAX;
5117 static const int32_t RAW_SLOT_MIN;
5118 static const int32_t RAW_SLOT_MAX;
5119 static const float X_PRECISION;
5120 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005121 static const float X_PRECISION_VIRTUAL;
5122 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123
5124 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005125 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005126
5127 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5128
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005129 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005130 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005131
Michael Wrightd02c5b62014-02-10 15:10:22 -08005132 enum Axes {
5133 POSITION = 1 << 0,
5134 TOUCH = 1 << 1,
5135 TOOL = 1 << 2,
5136 PRESSURE = 1 << 3,
5137 ORIENTATION = 1 << 4,
5138 MINOR = 1 << 5,
5139 ID = 1 << 6,
5140 DISTANCE = 1 << 7,
5141 TILT = 1 << 8,
5142 SLOT = 1 << 9,
5143 TOOL_TYPE = 1 << 10,
5144 };
5145
Michael Wrighta9cf4192022-12-01 23:46:39 +00005146 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005147 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00005148 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005149 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005150 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005151 int32_t toRawX(float displayX);
5152 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005153 int32_t toRotatedRawX(float displayX);
5154 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005155 float toCookedX(float rawX, float rawY);
5156 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005157 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005158 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005159 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005160 float toDisplayY(int32_t rawY, int32_t displayHeight);
5161
Michael Wrightd02c5b62014-02-10 15:10:22 -08005162};
5163
5164const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5165const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5166const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5167const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5168const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5169const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5170const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5171const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005172const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5173const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005174const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5175const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5176const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5177const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5178const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5179const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5180const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5181const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5182const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5183const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5184const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5185const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005186const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5187 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5188const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5189 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005190const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5191 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005192
5193const float TouchInputMapperTest::GEOMETRIC_SCALE =
5194 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5195 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5196
5197const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5198 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5199 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5200};
5201
Michael Wrighta9cf4192022-12-01 23:46:39 +00005202void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005203 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5204 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005205}
5206
5207void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5208 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00005209 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005210}
5211
Michael Wrighta9cf4192022-12-01 23:46:39 +00005212void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005213 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5214 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5215 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005216}
5217
Michael Wrightd02c5b62014-02-10 15:10:22 -08005218void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005219 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5220 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5221 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5222 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005223}
5224
Jason Gerecke489fda82012-09-07 17:19:40 -07005225void TouchInputMapperTest::prepareLocationCalibration() {
5226 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5227}
5228
Michael Wrightd02c5b62014-02-10 15:10:22 -08005229int32_t TouchInputMapperTest::toRawX(float displayX) {
5230 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5231}
5232
5233int32_t TouchInputMapperTest::toRawY(float displayY) {
5234 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5235}
5236
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005237int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5238 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5239}
5240
5241int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5242 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5243}
5244
Jason Gerecke489fda82012-09-07 17:19:40 -07005245float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5246 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5247 return rawX;
5248}
5249
5250float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5251 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5252 return rawY;
5253}
5254
Michael Wrightd02c5b62014-02-10 15:10:22 -08005255float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005256 return toDisplayX(rawX, DISPLAY_WIDTH);
5257}
5258
5259float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5260 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005261}
5262
5263float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005264 return toDisplayY(rawY, DISPLAY_HEIGHT);
5265}
5266
5267float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5268 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005269}
5270
5271
5272// --- SingleTouchInputMapperTest ---
5273
5274class SingleTouchInputMapperTest : public TouchInputMapperTest {
5275protected:
5276 void prepareButtons();
5277 void prepareAxes(int axes);
5278
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005279 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5280 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5281 void processUp(SingleTouchInputMapper& mappery);
5282 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5283 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5284 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5285 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5286 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5287 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005288};
5289
5290void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005291 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005292}
5293
5294void SingleTouchInputMapperTest::prepareAxes(int axes) {
5295 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005296 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5297 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005298 }
5299 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005300 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5301 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005302 }
5303 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005304 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5305 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005306 }
5307 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005308 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5309 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005310 }
5311 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005312 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5313 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314 }
5315}
5316
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005317void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5319 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5320 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005321}
5322
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005323void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005324 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005326}
5327
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005328void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005329 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330}
5331
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005332void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005334}
5335
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005336void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5337 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005338 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005339}
5340
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005341void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005343}
5344
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005345void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5346 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5348 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005349}
5350
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005351void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5352 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005353 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005354}
5355
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005356void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005358}
5359
Michael Wrightd02c5b62014-02-10 15:10:22 -08005360TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005361 prepareButtons();
5362 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005363 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005364
Josep del Río2d8c79a2023-01-23 19:33:50 +00005365 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005366}
5367
Michael Wrightd02c5b62014-02-10 15:10:22 -08005368TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369 prepareButtons();
5370 prepareAxes(POSITION);
5371 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00005372 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005373
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005374 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005375}
5376
5377TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005378 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005379 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005380 prepareButtons();
5381 prepareAxes(POSITION);
5382 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005383 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005384
5385 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005386 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005387
5388 // Virtual key is down.
5389 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5390 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5391 processDown(mapper, x, y);
5392 processSync(mapper);
5393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5394
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005395 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005396
5397 // Virtual key is up.
5398 processUp(mapper);
5399 processSync(mapper);
5400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5401
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005402 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403}
5404
5405TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005406 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005407 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005408 prepareButtons();
5409 prepareAxes(POSITION);
5410 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005411 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005412
5413 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005414 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005415
5416 // Virtual key is down.
5417 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5418 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5419 processDown(mapper, x, y);
5420 processSync(mapper);
5421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5422
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005423 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005424
5425 // Virtual key is up.
5426 processUp(mapper);
5427 processSync(mapper);
5428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5429
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005430 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005431}
5432
5433TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005434 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005435 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005436 prepareButtons();
5437 prepareAxes(POSITION);
5438 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005439 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005440
Michael Wrightd02c5b62014-02-10 15:10:22 -08005441 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005442 ASSERT_TRUE(
5443 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005444 ASSERT_TRUE(flags[0]);
5445 ASSERT_FALSE(flags[1]);
5446}
5447
5448TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005449 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005450 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005451 prepareButtons();
5452 prepareAxes(POSITION);
5453 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005454 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005455
arthurhungdcef2dc2020-08-11 14:47:50 +08005456 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005457
5458 NotifyKeyArgs args;
5459
5460 // Press virtual key.
5461 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5462 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5463 processDown(mapper, x, y);
5464 processSync(mapper);
5465
5466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5467 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5468 ASSERT_EQ(DEVICE_ID, args.deviceId);
5469 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5470 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5471 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5472 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5473 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5474 ASSERT_EQ(KEY_HOME, args.scanCode);
5475 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5476 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5477
5478 // Release virtual key.
5479 processUp(mapper);
5480 processSync(mapper);
5481
5482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5483 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5484 ASSERT_EQ(DEVICE_ID, args.deviceId);
5485 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5486 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5487 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5488 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5489 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5490 ASSERT_EQ(KEY_HOME, args.scanCode);
5491 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5492 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5493
5494 // Should not have sent any motions.
5495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5496}
5497
5498TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005499 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005500 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005501 prepareButtons();
5502 prepareAxes(POSITION);
5503 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005504 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005505
arthurhungdcef2dc2020-08-11 14:47:50 +08005506 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005507
5508 NotifyKeyArgs keyArgs;
5509
5510 // Press virtual key.
5511 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5512 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5513 processDown(mapper, x, y);
5514 processSync(mapper);
5515
5516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5517 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5518 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5519 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5520 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5521 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5522 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5523 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5524 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5525 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5526 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5527
5528 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5529 // into the display area.
5530 y -= 100;
5531 processMove(mapper, x, y);
5532 processSync(mapper);
5533
5534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5535 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5536 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5537 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5538 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5539 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5540 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5541 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5542 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5543 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5544 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5545 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5546
5547 NotifyMotionArgs motionArgs;
5548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5549 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5550 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5551 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5552 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5553 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5554 ASSERT_EQ(0, motionArgs.flags);
5555 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5556 ASSERT_EQ(0, motionArgs.buttonState);
5557 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005558 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005559 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005560 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5562 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5563 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5564 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5565 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5566
5567 // Keep moving out of bounds. Should generate a pointer move.
5568 y -= 50;
5569 processMove(mapper, x, y);
5570 processSync(mapper);
5571
5572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5573 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5574 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5575 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5576 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5577 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5578 ASSERT_EQ(0, motionArgs.flags);
5579 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5580 ASSERT_EQ(0, motionArgs.buttonState);
5581 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005582 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005583 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005584 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5586 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5587 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5588 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5589 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5590
5591 // Release out of bounds. Should generate a pointer up.
5592 processUp(mapper);
5593 processSync(mapper);
5594
5595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5596 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5597 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5598 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5599 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5600 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5601 ASSERT_EQ(0, motionArgs.flags);
5602 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5603 ASSERT_EQ(0, motionArgs.buttonState);
5604 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005605 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005606 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005607 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5609 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5610 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5611 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5612 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5613
5614 // Should not have sent any more keys or motions.
5615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5617}
5618
5619TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005621 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005622 prepareButtons();
5623 prepareAxes(POSITION);
5624 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005625 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005626
arthurhungdcef2dc2020-08-11 14:47:50 +08005627 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005628
5629 NotifyMotionArgs motionArgs;
5630
5631 // Initially go down out of bounds.
5632 int32_t x = -10;
5633 int32_t y = -10;
5634 processDown(mapper, x, y);
5635 processSync(mapper);
5636
5637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5638
5639 // Move into the display area. Should generate a pointer down.
5640 x = 50;
5641 y = 75;
5642 processMove(mapper, x, y);
5643 processSync(mapper);
5644
5645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5646 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5647 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5648 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5649 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5650 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5651 ASSERT_EQ(0, motionArgs.flags);
5652 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5653 ASSERT_EQ(0, motionArgs.buttonState);
5654 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005655 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005656 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005657 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5659 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5660 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5661 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5662 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5663
5664 // Release. Should generate a pointer up.
5665 processUp(mapper);
5666 processSync(mapper);
5667
5668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5669 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5670 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5671 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5672 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5673 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5674 ASSERT_EQ(0, motionArgs.flags);
5675 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5676 ASSERT_EQ(0, motionArgs.buttonState);
5677 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005678 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005679 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005680 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5682 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5683 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5684 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5685 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5686
5687 // Should not have sent any more keys or motions.
5688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5690}
5691
Santos Cordonfa5cf462017-04-05 10:37:00 -07005692TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005693 addConfigurationProperty("touch.deviceType", "touchScreen");
5694 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5695
Michael Wrighta9cf4192022-12-01 23:46:39 +00005696 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005697 prepareButtons();
5698 prepareAxes(POSITION);
5699 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005700 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005701
arthurhungdcef2dc2020-08-11 14:47:50 +08005702 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005703
5704 NotifyMotionArgs motionArgs;
5705
5706 // Down.
5707 int32_t x = 100;
5708 int32_t y = 125;
5709 processDown(mapper, x, y);
5710 processSync(mapper);
5711
5712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5713 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5714 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5715 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5716 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5717 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5718 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5719 ASSERT_EQ(0, motionArgs.flags);
5720 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5721 ASSERT_EQ(0, motionArgs.buttonState);
5722 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005723 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005724 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005725 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5727 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5728 1, 0, 0, 0, 0, 0, 0, 0));
5729 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5730 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5731 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5732
5733 // Move.
5734 x += 50;
5735 y += 75;
5736 processMove(mapper, x, y);
5737 processSync(mapper);
5738
5739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5740 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5741 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5742 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5743 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5744 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5745 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5746 ASSERT_EQ(0, motionArgs.flags);
5747 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5748 ASSERT_EQ(0, motionArgs.buttonState);
5749 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005750 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005751 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005752 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5754 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5755 1, 0, 0, 0, 0, 0, 0, 0));
5756 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5757 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5758 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5759
5760 // Up.
5761 processUp(mapper);
5762 processSync(mapper);
5763
5764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5765 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5766 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5767 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5768 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5769 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5770 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5771 ASSERT_EQ(0, motionArgs.flags);
5772 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5773 ASSERT_EQ(0, motionArgs.buttonState);
5774 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005775 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005776 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005777 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5779 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5780 1, 0, 0, 0, 0, 0, 0, 0));
5781 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5782 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5783 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5784
5785 // Should not have sent any more keys or motions.
5786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5788}
5789
Michael Wrightd02c5b62014-02-10 15:10:22 -08005790TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005791 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005792 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005793 prepareButtons();
5794 prepareAxes(POSITION);
5795 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005796 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005797
arthurhungdcef2dc2020-08-11 14:47:50 +08005798 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005799
5800 NotifyMotionArgs motionArgs;
5801
5802 // Down.
5803 int32_t x = 100;
5804 int32_t y = 125;
5805 processDown(mapper, x, y);
5806 processSync(mapper);
5807
5808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5809 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5810 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5811 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5812 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5813 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5814 ASSERT_EQ(0, motionArgs.flags);
5815 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5816 ASSERT_EQ(0, motionArgs.buttonState);
5817 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005818 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005819 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005820 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5822 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5823 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5824 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5825 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5826
5827 // Move.
5828 x += 50;
5829 y += 75;
5830 processMove(mapper, x, y);
5831 processSync(mapper);
5832
5833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5834 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5835 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5836 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5837 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5838 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5839 ASSERT_EQ(0, motionArgs.flags);
5840 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5841 ASSERT_EQ(0, motionArgs.buttonState);
5842 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005843 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005844 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005845 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5847 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5848 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5849 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5850 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5851
5852 // Up.
5853 processUp(mapper);
5854 processSync(mapper);
5855
5856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5857 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5858 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5859 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5860 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5861 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5862 ASSERT_EQ(0, motionArgs.flags);
5863 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5864 ASSERT_EQ(0, motionArgs.buttonState);
5865 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005866 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005867 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005868 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5870 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5871 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5872 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5873 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5874
5875 // Should not have sent any more keys or motions.
5876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5878}
5879
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005880TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005881 addConfigurationProperty("touch.deviceType", "touchScreen");
5882 prepareButtons();
5883 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005884 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5885 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005886 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005887
5888 NotifyMotionArgs args;
5889
5890 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005891 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005892 processDown(mapper, toRawX(50), toRawY(75));
5893 processSync(mapper);
5894
5895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5896 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5897 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5898
5899 processUp(mapper);
5900 processSync(mapper);
5901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5902}
5903
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005904TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005905 addConfigurationProperty("touch.deviceType", "touchScreen");
5906 prepareButtons();
5907 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005908 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5909 // orientation-aware are affected by display rotation.
5910 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005911 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005912
5913 NotifyMotionArgs args;
5914
5915 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005916 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005917 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005918 processDown(mapper, toRawX(50), toRawY(75));
5919 processSync(mapper);
5920
5921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5922 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5923 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5924
5925 processUp(mapper);
5926 processSync(mapper);
5927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5928
5929 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005930 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005931 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005932 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005933 processSync(mapper);
5934
5935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5936 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5937 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5938
5939 processUp(mapper);
5940 processSync(mapper);
5941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5942
5943 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005944 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005945 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005946 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5947 processSync(mapper);
5948
5949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5950 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5951 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5952
5953 processUp(mapper);
5954 processSync(mapper);
5955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5956
5957 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005958 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005959 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005960 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005961 processSync(mapper);
5962
5963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5964 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5965 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5966
5967 processUp(mapper);
5968 processSync(mapper);
5969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5970}
5971
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005972TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5973 addConfigurationProperty("touch.deviceType", "touchScreen");
5974 prepareButtons();
5975 prepareAxes(POSITION);
5976 addConfigurationProperty("touch.orientationAware", "1");
5977 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5978 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005979 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005980 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005981 NotifyMotionArgs args;
5982
5983 // Orientation 0.
5984 processDown(mapper, toRawX(50), toRawY(75));
5985 processSync(mapper);
5986
5987 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5988 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5989 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5990
5991 processUp(mapper);
5992 processSync(mapper);
5993 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5994}
5995
5996TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5997 addConfigurationProperty("touch.deviceType", "touchScreen");
5998 prepareButtons();
5999 prepareAxes(POSITION);
6000 addConfigurationProperty("touch.orientationAware", "1");
6001 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6002 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006003 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006004 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006005 NotifyMotionArgs args;
6006
6007 // Orientation 90.
6008 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6009 processSync(mapper);
6010
6011 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6012 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6013 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6014
6015 processUp(mapper);
6016 processSync(mapper);
6017 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6018}
6019
6020TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6021 addConfigurationProperty("touch.deviceType", "touchScreen");
6022 prepareButtons();
6023 prepareAxes(POSITION);
6024 addConfigurationProperty("touch.orientationAware", "1");
6025 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6026 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006027 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006028 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006029 NotifyMotionArgs args;
6030
6031 // Orientation 180.
6032 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6033 processSync(mapper);
6034
6035 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6036 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6037 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6038
6039 processUp(mapper);
6040 processSync(mapper);
6041 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6042}
6043
6044TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6045 addConfigurationProperty("touch.deviceType", "touchScreen");
6046 prepareButtons();
6047 prepareAxes(POSITION);
6048 addConfigurationProperty("touch.orientationAware", "1");
6049 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6050 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006051 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006052 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006053 NotifyMotionArgs args;
6054
6055 // Orientation 270.
6056 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6057 processSync(mapper);
6058
6059 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6060 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6061 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6062
6063 processUp(mapper);
6064 processSync(mapper);
6065 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6066}
6067
6068TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6069 addConfigurationProperty("touch.deviceType", "touchScreen");
6070 prepareButtons();
6071 prepareAxes(POSITION);
6072 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6073 // orientation-aware are affected by display rotation.
6074 addConfigurationProperty("touch.orientationAware", "0");
6075 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00006076 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006077
6078 NotifyMotionArgs args;
6079
6080 // Orientation 90, Rotation 0.
6081 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006082 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006083 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6084 processSync(mapper);
6085
6086 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6087 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6088 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6089
6090 processUp(mapper);
6091 processSync(mapper);
6092 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6093
6094 // Orientation 90, Rotation 90.
6095 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006096 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006097 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006098 processSync(mapper);
6099
6100 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6101 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6102 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6103
6104 processUp(mapper);
6105 processSync(mapper);
6106 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6107
6108 // Orientation 90, Rotation 180.
6109 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006110 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006111 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6112 processSync(mapper);
6113
6114 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6115 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6116 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6117
6118 processUp(mapper);
6119 processSync(mapper);
6120 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6121
6122 // Orientation 90, Rotation 270.
6123 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006124 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006125 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 -07006126 processSync(mapper);
6127
6128 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6129 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6130 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6131
6132 processUp(mapper);
6133 processSync(mapper);
6134 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6135}
6136
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006137TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
6138 addConfigurationProperty("touch.deviceType", "touchScreen");
6139 prepareButtons();
6140 prepareAxes(POSITION);
6141 addConfigurationProperty("touch.orientationAware", "1");
6142 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006143 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006144
6145 // Set a physical frame in the display viewport.
6146 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6147 viewport->physicalLeft = 20;
6148 viewport->physicalTop = 600;
6149 viewport->physicalRight = 30;
6150 viewport->physicalBottom = 610;
6151 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006152 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006153
6154 // Start the touch.
6155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6156 processSync(mapper);
6157
6158 // Expect all input starting outside the physical frame to be ignored.
6159 const std::array<Point, 6> outsidePoints = {
6160 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6161 for (const auto& p : outsidePoints) {
6162 processMove(mapper, toRawX(p.x), toRawY(p.y));
6163 processSync(mapper);
6164 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6165 }
6166
6167 // Move the touch into the physical frame.
6168 processMove(mapper, toRawX(25), toRawY(605));
6169 processSync(mapper);
6170 NotifyMotionArgs args;
6171 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6172 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6173 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6174 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6175
6176 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
6177 for (const auto& p : outsidePoints) {
6178 processMove(mapper, toRawX(p.x), toRawY(p.y));
6179 processSync(mapper);
6180 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6181 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
6182 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6183 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6184 }
6185
6186 processUp(mapper);
6187 processSync(mapper);
6188 EXPECT_NO_FATAL_FAILURE(
6189 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
6190}
6191
Harry Cutts1db43992023-06-19 17:05:07 +00006192TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
6193 std::shared_ptr<FakePointerController> fakePointerController =
6194 std::make_shared<FakePointerController>();
6195 mFakePolicy->setPointerController(fakePointerController);
6196
6197 addConfigurationProperty("touch.deviceType", "pointer");
6198 prepareAxes(POSITION);
6199 prepareDisplay(ui::ROTATION_0);
6200 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
6201
6202 // Set a physical frame in the display viewport.
6203 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6204 viewport->physicalLeft = 20;
6205 viewport->physicalTop = 600;
6206 viewport->physicalRight = 30;
6207 viewport->physicalBottom = 610;
6208 mFakePolicy->updateViewport(*viewport);
6209 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
6210
6211 // Start the touch.
6212 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6213 processSync(mapper);
6214
6215 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
6216 // produced.
6217 const std::array<Point, 6> outsidePoints = {
6218 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6219 for (const auto& p : outsidePoints) {
6220 processMove(mapper, toRawX(p.x), toRawY(p.y));
6221 processSync(mapper);
6222 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6223 }
6224}
6225
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006227 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006228 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006229 prepareButtons();
6230 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00006231 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006232
6233 // These calculations are based on the input device calibration documentation.
6234 int32_t rawX = 100;
6235 int32_t rawY = 200;
6236 int32_t rawPressure = 10;
6237 int32_t rawToolMajor = 12;
6238 int32_t rawDistance = 2;
6239 int32_t rawTiltX = 30;
6240 int32_t rawTiltY = 110;
6241
6242 float x = toDisplayX(rawX);
6243 float y = toDisplayY(rawY);
6244 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6245 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6246 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6247 float distance = float(rawDistance);
6248
6249 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6250 float tiltScale = M_PI / 180;
6251 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6252 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6253 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6254 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6255
6256 processDown(mapper, rawX, rawY);
6257 processPressure(mapper, rawPressure);
6258 processToolMajor(mapper, rawToolMajor);
6259 processDistance(mapper, rawDistance);
6260 processTilt(mapper, rawTiltX, rawTiltY);
6261 processSync(mapper);
6262
6263 NotifyMotionArgs args;
6264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6265 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6266 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6267 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6268}
6269
Jason Gerecke489fda82012-09-07 17:19:40 -07006270TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006271 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006272 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07006273 prepareLocationCalibration();
6274 prepareButtons();
6275 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006276 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006277
6278 int32_t rawX = 100;
6279 int32_t rawY = 200;
6280
6281 float x = toDisplayX(toCookedX(rawX, rawY));
6282 float y = toDisplayY(toCookedY(rawX, rawY));
6283
6284 processDown(mapper, rawX, rawY);
6285 processSync(mapper);
6286
6287 NotifyMotionArgs args;
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6290 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6291}
6292
Michael Wrightd02c5b62014-02-10 15:10:22 -08006293TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006294 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006295 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006296 prepareButtons();
6297 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006298 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006299
6300 NotifyMotionArgs motionArgs;
6301 NotifyKeyArgs keyArgs;
6302
6303 processDown(mapper, 100, 200);
6304 processSync(mapper);
6305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6306 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6307 ASSERT_EQ(0, motionArgs.buttonState);
6308
6309 // press BTN_LEFT, release BTN_LEFT
6310 processKey(mapper, BTN_LEFT, 1);
6311 processSync(mapper);
6312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6314 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6315
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6317 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6318 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6319
Michael Wrightd02c5b62014-02-10 15:10:22 -08006320 processKey(mapper, BTN_LEFT, 0);
6321 processSync(mapper);
6322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006323 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006324 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006325
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006328 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006329
6330 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6331 processKey(mapper, BTN_RIGHT, 1);
6332 processKey(mapper, BTN_MIDDLE, 1);
6333 processSync(mapper);
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6336 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6337 motionArgs.buttonState);
6338
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6340 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6341 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6342
6343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6344 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6345 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6346 motionArgs.buttonState);
6347
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348 processKey(mapper, BTN_RIGHT, 0);
6349 processSync(mapper);
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006351 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006352 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006353
6354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006355 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006356 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006357
6358 processKey(mapper, BTN_MIDDLE, 0);
6359 processSync(mapper);
6360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006361 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006362 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006363
6364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006366 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006367
6368 // press BTN_BACK, release BTN_BACK
6369 processKey(mapper, BTN_BACK, 1);
6370 processSync(mapper);
6371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6372 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6373 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006374
Michael Wrightd02c5b62014-02-10 15:10:22 -08006375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006376 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006377 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6378
6379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6380 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6381 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006382
6383 processKey(mapper, BTN_BACK, 0);
6384 processSync(mapper);
6385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006386 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006387 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006388
6389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006391 ASSERT_EQ(0, motionArgs.buttonState);
6392
Michael Wrightd02c5b62014-02-10 15:10:22 -08006393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6394 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6395 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6396
6397 // press BTN_SIDE, release BTN_SIDE
6398 processKey(mapper, BTN_SIDE, 1);
6399 processSync(mapper);
6400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6401 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6402 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006403
Michael Wrightd02c5b62014-02-10 15:10:22 -08006404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006405 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006406 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6407
6408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6409 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6410 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006411
6412 processKey(mapper, BTN_SIDE, 0);
6413 processSync(mapper);
6414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006415 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006416 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006417
6418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006420 ASSERT_EQ(0, motionArgs.buttonState);
6421
Michael Wrightd02c5b62014-02-10 15:10:22 -08006422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6423 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6424 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6425
6426 // press BTN_FORWARD, release BTN_FORWARD
6427 processKey(mapper, BTN_FORWARD, 1);
6428 processSync(mapper);
6429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6430 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6431 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006432
Michael Wrightd02c5b62014-02-10 15:10:22 -08006433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006434 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006435 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6436
6437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6438 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6439 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006440
6441 processKey(mapper, BTN_FORWARD, 0);
6442 processSync(mapper);
6443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006444 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006445 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006446
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006449 ASSERT_EQ(0, motionArgs.buttonState);
6450
Michael Wrightd02c5b62014-02-10 15:10:22 -08006451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6452 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6453 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6454
6455 // press BTN_EXTRA, release BTN_EXTRA
6456 processKey(mapper, BTN_EXTRA, 1);
6457 processSync(mapper);
6458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6459 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6460 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006461
Michael Wrightd02c5b62014-02-10 15:10:22 -08006462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006464 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6465
6466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6467 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6468 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006469
6470 processKey(mapper, BTN_EXTRA, 0);
6471 processSync(mapper);
6472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006473 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006474 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006475
6476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006477 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006478 ASSERT_EQ(0, motionArgs.buttonState);
6479
Michael Wrightd02c5b62014-02-10 15:10:22 -08006480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6481 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6482 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6483
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6485
Michael Wrightd02c5b62014-02-10 15:10:22 -08006486 // press BTN_STYLUS, release BTN_STYLUS
6487 processKey(mapper, BTN_STYLUS, 1);
6488 processSync(mapper);
6489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6490 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006491 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6492
6493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6494 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6495 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006496
6497 processKey(mapper, BTN_STYLUS, 0);
6498 processSync(mapper);
6499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006500 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006501 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006502
6503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006504 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006505 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006506
6507 // press BTN_STYLUS2, release BTN_STYLUS2
6508 processKey(mapper, BTN_STYLUS2, 1);
6509 processSync(mapper);
6510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6511 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006512 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6513
6514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6515 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6516 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006517
6518 processKey(mapper, BTN_STYLUS2, 0);
6519 processSync(mapper);
6520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006521 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006522 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006523
6524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006525 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006526 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006527
6528 // release touch
6529 processUp(mapper);
6530 processSync(mapper);
6531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6532 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6533 ASSERT_EQ(0, motionArgs.buttonState);
6534}
6535
6536TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006537 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006538 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006539 prepareButtons();
6540 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006541 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006542
6543 NotifyMotionArgs motionArgs;
6544
6545 // default tool type is finger
6546 processDown(mapper, 100, 200);
6547 processSync(mapper);
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6549 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006550 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006551
6552 // eraser
6553 processKey(mapper, BTN_TOOL_RUBBER, 1);
6554 processSync(mapper);
6555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6556 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006557 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006558
6559 // stylus
6560 processKey(mapper, BTN_TOOL_RUBBER, 0);
6561 processKey(mapper, BTN_TOOL_PEN, 1);
6562 processSync(mapper);
6563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6564 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006565 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006566
6567 // brush
6568 processKey(mapper, BTN_TOOL_PEN, 0);
6569 processKey(mapper, BTN_TOOL_BRUSH, 1);
6570 processSync(mapper);
6571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6572 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006573 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006574
6575 // pencil
6576 processKey(mapper, BTN_TOOL_BRUSH, 0);
6577 processKey(mapper, BTN_TOOL_PENCIL, 1);
6578 processSync(mapper);
6579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006581 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006582
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006583 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006584 processKey(mapper, BTN_TOOL_PENCIL, 0);
6585 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6586 processSync(mapper);
6587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006589 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006590
6591 // mouse
6592 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6593 processKey(mapper, BTN_TOOL_MOUSE, 1);
6594 processSync(mapper);
6595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6596 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006597 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006598
6599 // lens
6600 processKey(mapper, BTN_TOOL_MOUSE, 0);
6601 processKey(mapper, BTN_TOOL_LENS, 1);
6602 processSync(mapper);
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006605 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006606
6607 // double-tap
6608 processKey(mapper, BTN_TOOL_LENS, 0);
6609 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6610 processSync(mapper);
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6612 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006613 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006614
6615 // triple-tap
6616 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6617 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6618 processSync(mapper);
6619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6620 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006621 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006622
6623 // quad-tap
6624 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6625 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6626 processSync(mapper);
6627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6628 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006629 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006630
6631 // finger
6632 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6633 processKey(mapper, BTN_TOOL_FINGER, 1);
6634 processSync(mapper);
6635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6636 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006637 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006638
6639 // stylus trumps finger
6640 processKey(mapper, BTN_TOOL_PEN, 1);
6641 processSync(mapper);
6642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6643 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006644 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006645
6646 // eraser trumps stylus
6647 processKey(mapper, BTN_TOOL_RUBBER, 1);
6648 processSync(mapper);
6649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006651 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006652
6653 // mouse trumps eraser
6654 processKey(mapper, BTN_TOOL_MOUSE, 1);
6655 processSync(mapper);
6656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006658 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006659
6660 // back to default tool type
6661 processKey(mapper, BTN_TOOL_MOUSE, 0);
6662 processKey(mapper, BTN_TOOL_RUBBER, 0);
6663 processKey(mapper, BTN_TOOL_PEN, 0);
6664 processKey(mapper, BTN_TOOL_FINGER, 0);
6665 processSync(mapper);
6666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6667 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006668 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006669}
6670
6671TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006672 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006673 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006674 prepareButtons();
6675 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006676 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006677 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006678
6679 NotifyMotionArgs motionArgs;
6680
6681 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6682 processKey(mapper, BTN_TOOL_FINGER, 1);
6683 processMove(mapper, 100, 200);
6684 processSync(mapper);
6685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6686 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6688 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6689
6690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6691 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6692 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6693 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6694
6695 // move a little
6696 processMove(mapper, 150, 250);
6697 processSync(mapper);
6698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6699 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6701 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6702
6703 // down when BTN_TOUCH is pressed, pressure defaults to 1
6704 processKey(mapper, BTN_TOUCH, 1);
6705 processSync(mapper);
6706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6707 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6708 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6709 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6710
6711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6712 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6714 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6715
6716 // up when BTN_TOUCH is released, hover restored
6717 processKey(mapper, BTN_TOUCH, 0);
6718 processSync(mapper);
6719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6720 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6722 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6723
6724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6725 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6727 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6728
6729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6730 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6732 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6733
6734 // exit hover when pointer goes away
6735 processKey(mapper, BTN_TOOL_FINGER, 0);
6736 processSync(mapper);
6737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6738 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6740 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6741}
6742
6743TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006744 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006745 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006746 prepareButtons();
6747 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006748 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006749
6750 NotifyMotionArgs motionArgs;
6751
6752 // initially hovering because pressure is 0
6753 processDown(mapper, 100, 200);
6754 processPressure(mapper, 0);
6755 processSync(mapper);
6756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6757 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6759 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6760
6761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6762 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6764 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6765
6766 // move a little
6767 processMove(mapper, 150, 250);
6768 processSync(mapper);
6769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6770 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6772 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6773
6774 // down when pressure is non-zero
6775 processPressure(mapper, RAW_PRESSURE_MAX);
6776 processSync(mapper);
6777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6778 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6780 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6781
6782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6783 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6785 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6786
6787 // up when pressure becomes 0, hover restored
6788 processPressure(mapper, 0);
6789 processSync(mapper);
6790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6791 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6792 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6793 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6794
6795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6796 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6798 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6799
6800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6801 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6803 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6804
6805 // exit hover when pointer goes away
6806 processUp(mapper);
6807 processSync(mapper);
6808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6809 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6811 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6812}
6813
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006814TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6815 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006816 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006817 prepareButtons();
6818 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006819 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006820
6821 // Touch down.
6822 processDown(mapper, 100, 200);
6823 processPressure(mapper, 1);
6824 processSync(mapper);
6825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6826 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6827
6828 // Reset the mapper. This should cancel the ongoing gesture.
6829 resetMapper(mapper, ARBITRARY_TIME);
6830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6831 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6832
6833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6834}
6835
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006836TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6837 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006838 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006839 prepareButtons();
6840 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006841 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006842
6843 // Set the initial state for the touch pointer.
6844 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6845 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6846 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6847 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6848
6849 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006850 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6851 // does not generate any events.
6852 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006853
6854 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6855 // the recreated touch state to generate a down event.
6856 processSync(mapper);
6857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6858 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6859
6860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6861}
6862
lilinnan687e58f2022-07-19 16:00:50 +08006863TEST_F(SingleTouchInputMapperTest,
6864 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6865 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006866 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006867 prepareButtons();
6868 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006869 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006870 NotifyMotionArgs motionArgs;
6871
6872 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006873 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006874 processSync(mapper);
6875
6876 // We should receive a down event
6877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6878 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6879
6880 // Change display id
6881 clearViewports();
6882 prepareSecondaryDisplay(ViewportType::INTERNAL);
6883
6884 // We should receive a cancel event
6885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6886 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6887 // Then receive reset called
6888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6889}
6890
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006891TEST_F(SingleTouchInputMapperTest,
6892 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6893 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006894 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006895 prepareButtons();
6896 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006897 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6899 NotifyMotionArgs motionArgs;
6900
6901 // Start a new gesture.
6902 processDown(mapper, 100, 200);
6903 processSync(mapper);
6904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6905 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6906
6907 // Make the viewport inactive. This will put the device in disabled mode.
6908 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6909 viewport->isActive = false;
6910 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006911 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006912
6913 // We should receive a cancel event for the ongoing gesture.
6914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6915 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6916 // Then we should be notified that the device was reset.
6917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6918
6919 // No events are generated while the viewport is inactive.
6920 processMove(mapper, 101, 201);
6921 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006922 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006923 processSync(mapper);
6924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6925
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006926 // Start a new gesture while the viewport is still inactive.
6927 processDown(mapper, 300, 400);
6928 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6929 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6930 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6931 processSync(mapper);
6932
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006933 // Make the viewport active again. The device should resume processing events.
6934 viewport->isActive = true;
6935 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006936 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006937
6938 // The device is reset because it changes back to direct mode, without generating any events.
6939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6941
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006942 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006943 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6945 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006946
6947 // No more events.
6948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6950}
6951
Prabir Pradhan211ba622022-10-31 21:09:21 +00006952TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6953 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006954 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006955 prepareButtons();
6956 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006957 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6959
6960 // Press a stylus button.
6961 processKey(mapper, BTN_STYLUS, 1);
6962 processSync(mapper);
6963
6964 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6965 processDown(mapper, 100, 200);
6966 processSync(mapper);
6967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6968 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6969 WithCoords(toDisplayX(100), toDisplayY(200)),
6970 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6972 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6973 WithCoords(toDisplayX(100), toDisplayY(200)),
6974 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6975
6976 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6977 // the button has not actually been released, since there will be no pointers through which the
6978 // button state can be reported. The event is generated at the location of the pointer before
6979 // it went up.
6980 processUp(mapper);
6981 processSync(mapper);
6982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6983 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6984 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6986 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6987 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6988}
6989
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006990TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6991 addConfigurationProperty("touch.deviceType", "touchScreen");
6992 prepareDisplay(ui::ROTATION_0);
6993 prepareButtons();
6994 prepareAxes(POSITION);
6995
6996 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6997
Arpit Singha8c236b2023-04-25 13:56:05 +00006998 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7000
7001 // Press a stylus button.
7002 processKey(mapper, BTN_STYLUS, 1);
7003 processSync(mapper);
7004
7005 // Start a touch gesture and ensure that the stylus button is not reported.
7006 processDown(mapper, 100, 200);
7007 processSync(mapper);
7008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7009 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
7010
7011 // Release and press the stylus button again.
7012 processKey(mapper, BTN_STYLUS, 0);
7013 processSync(mapper);
7014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7015 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
7016 processKey(mapper, BTN_STYLUS, 1);
7017 processSync(mapper);
7018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7019 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
7020
7021 // Release the touch gesture.
7022 processUp(mapper);
7023 processSync(mapper);
7024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7025 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7026
7027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7028}
7029
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00007030TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
7031 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
7032 prepareDisplay(ui::ROTATION_0);
7033 prepareButtons();
7034 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007035 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00007036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7037
7038 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
7039}
7040
Seunghwan Choi356026c2023-02-01 14:37:25 +09007041TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
7042 std::shared_ptr<FakePointerController> fakePointerController =
7043 std::make_shared<FakePointerController>();
7044 addConfigurationProperty("touch.deviceType", "touchScreen");
7045 prepareDisplay(ui::ROTATION_0);
7046 prepareButtons();
7047 prepareAxes(POSITION);
7048 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7049 mFakePolicy->setPointerController(fakePointerController);
7050 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00007051 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09007052
7053 processKey(mapper, BTN_TOOL_PEN, 1);
7054 processMove(mapper, 100, 200);
7055 processSync(mapper);
7056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7057 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007058 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09007059 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
7060 ASSERT_TRUE(fakePointerController->isPointerShown());
7061 ASSERT_NO_FATAL_FAILURE(
7062 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
7063}
7064
7065TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
7066 std::shared_ptr<FakePointerController> fakePointerController =
7067 std::make_shared<FakePointerController>();
7068 addConfigurationProperty("touch.deviceType", "touchScreen");
7069 prepareDisplay(ui::ROTATION_0);
7070 prepareButtons();
7071 prepareAxes(POSITION);
7072 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7073 mFakePolicy->setPointerController(fakePointerController);
7074 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00007075 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09007076
7077 processKey(mapper, BTN_TOOL_PEN, 1);
7078 processMove(mapper, 100, 200);
7079 processSync(mapper);
7080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7081 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007082 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09007083 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
7084 ASSERT_FALSE(fakePointerController->isPointerShown());
7085}
7086
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007087TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
7088 // Initialize the device without setting device source to touch navigation.
7089 addConfigurationProperty("touch.deviceType", "touchScreen");
7090 prepareDisplay(ui::ROTATION_0);
7091 prepareButtons();
7092 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007093 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007094
7095 // Ensure that the device is created as a touchscreen, not touch navigation.
7096 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7097
7098 // Add device type association after the device was created.
7099 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
7100
7101 // Send update to the mapper.
7102 std::list<NotifyArgs> unused2 =
7103 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007104 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007105
7106 // Check whether device type update was successful.
7107 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
7108}
7109
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007110TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
7111 // Initialize the device without setting device source to touch navigation.
7112 addConfigurationProperty("touch.deviceType", "touchScreen");
7113 prepareDisplay(ui::ROTATION_0);
7114 prepareButtons();
7115 prepareAxes(POSITION);
7116 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7117
7118 // Set a physical frame in the display viewport.
7119 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7120 viewport->physicalLeft = 0;
7121 viewport->physicalTop = 0;
7122 viewport->physicalRight = DISPLAY_WIDTH / 2;
7123 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
7124 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007125 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007126
Arpit Singha8c236b2023-04-25 13:56:05 +00007127 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007128
7129 // Hovering inside the physical frame produces events.
7130 processKey(mapper, BTN_TOOL_PEN, 1);
7131 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
7132 processSync(mapper);
7133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7134 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
7135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7136 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
7137
7138 // Leaving the physical frame ends the hovering gesture.
7139 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
7140 processSync(mapper);
7141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7142 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
7143
7144 // Moving outside the physical frame does not produce events.
7145 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
7146 processSync(mapper);
7147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7148
7149 // Re-entering the physical frame produces events.
7150 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
7151 processSync(mapper);
7152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7153 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
7154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7155 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
7156}
7157
Prabir Pradhan5632d622021-09-06 07:57:20 -07007158// --- TouchDisplayProjectionTest ---
7159
7160class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7161public:
7162 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7163 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7164 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00007165 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
7166 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
7167 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007168 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007169 auto rotatedWidth = naturalDisplayWidth;
7170 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007171 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00007172 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007173 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007174 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007175 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007176 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007177 inverseRotationFlags = ui::Transform::ROT_180;
7178 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007179 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007180 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007181 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007182 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007183 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007184 inverseRotationFlags = ui::Transform::ROT_0;
7185 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007186 }
7187
Prabir Pradhana9df3162022-12-05 23:57:27 +00007188 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007189 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7190
7191 std::optional<DisplayViewport> internalViewport =
7192 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7193 DisplayViewport& v = *internalViewport;
7194 v.displayId = DISPLAY_ID;
7195 v.orientation = orientation;
7196
7197 v.logicalLeft = 0;
7198 v.logicalTop = 0;
7199 v.logicalRight = 100;
7200 v.logicalBottom = 100;
7201
7202 v.physicalLeft = rotatedPhysicalDisplay.left;
7203 v.physicalTop = rotatedPhysicalDisplay.top;
7204 v.physicalRight = rotatedPhysicalDisplay.right;
7205 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7206
Prabir Pradhana9df3162022-12-05 23:57:27 +00007207 v.deviceWidth = rotatedWidth;
7208 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007209
7210 v.isActive = true;
7211 v.uniqueId = UNIQUE_ID;
7212 v.type = ViewportType::INTERNAL;
7213 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007214 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007215 }
7216
7217 void assertReceivedMove(const Point& point) {
7218 NotifyMotionArgs motionArgs;
7219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007221 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007222 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7223 1, 0, 0, 0, 0, 0, 0, 0));
7224 }
7225};
7226
7227TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7228 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007229 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007230
7231 prepareButtons();
7232 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007233 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007234
7235 NotifyMotionArgs motionArgs;
7236
7237 // Configure the DisplayViewport such that the logical display maps to a subsection of
7238 // the display panel called the physical display. Here, the physical display is bounded by the
7239 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7240 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7241 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7242 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7243
Michael Wrighta9cf4192022-12-01 23:46:39 +00007244 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007245 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7246
7247 // Touches outside the physical display should be ignored, and should not generate any
7248 // events. Ensure touches at the following points that lie outside of the physical display
7249 // area do not generate any events.
7250 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7251 processDown(mapper, toRawX(point.x), toRawY(point.y));
7252 processSync(mapper);
7253 processUp(mapper);
7254 processSync(mapper);
7255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7256 << "Unexpected event generated for touch outside physical display at point: "
7257 << point.x << ", " << point.y;
7258 }
7259 }
7260}
7261
7262TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7263 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007264 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007265
7266 prepareButtons();
7267 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007268 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007269
7270 NotifyMotionArgs motionArgs;
7271
7272 // Configure the DisplayViewport such that the logical display maps to a subsection of
7273 // the display panel called the physical display. Here, the physical display is bounded by the
7274 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7275 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7276
Michael Wrighta9cf4192022-12-01 23:46:39 +00007277 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007278 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7279
7280 // Touches that start outside the physical display should be ignored until it enters the
7281 // physical display bounds, at which point it should generate a down event. Start a touch at
7282 // the point (5, 100), which is outside the physical display bounds.
7283 static const Point kOutsidePoint{5, 100};
7284 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7285 processSync(mapper);
7286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7287
7288 // Move the touch into the physical display area. This should generate a pointer down.
7289 processMove(mapper, toRawX(11), toRawY(21));
7290 processSync(mapper);
7291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7292 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007293 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007294 ASSERT_NO_FATAL_FAILURE(
7295 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7296
7297 // Move the touch inside the physical display area. This should generate a pointer move.
7298 processMove(mapper, toRawX(69), toRawY(159));
7299 processSync(mapper);
7300 assertReceivedMove({69, 159});
7301
7302 // Move outside the physical display area. Since the pointer is already down, this should
7303 // now continue generating events.
7304 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7305 processSync(mapper);
7306 assertReceivedMove(kOutsidePoint);
7307
7308 // Release. This should generate a pointer up.
7309 processUp(mapper);
7310 processSync(mapper);
7311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7312 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7314 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7315
7316 // Ensure no more events were generated.
7317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7319 }
7320}
7321
Prabir Pradhana9df3162022-12-05 23:57:27 +00007322// --- TouchscreenPrecisionTests ---
7323
7324// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
7325// in various orientations and with different display rotations. We configure the touchscreen to
7326// have a higher resolution than that of the display by an integer scale factor in each axis so that
7327// we can enforce that coordinates match precisely as expected.
7328class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
7329 public ::testing::WithParamInterface<ui::Rotation> {
7330public:
7331 void SetUp() override {
7332 SingleTouchInputMapperTest::SetUp();
7333
7334 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7335 // four times the resolution of the display in the Y axis.
7336 prepareButtons();
7337 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007338 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7339 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007340 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007341 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7342 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007343 }
7344
7345 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7346 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7347 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7348 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7349
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007350 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7351 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7352
7353 static const int32_t PRECISION_RAW_X_FLAT = 16;
7354 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7355
7356 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7357 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7358
Prabir Pradhana9df3162022-12-05 23:57:27 +00007359 static const std::array<Point, 4> kRawCorners;
7360};
7361
7362const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7363 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7364 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7365 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7366 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7367}};
7368
7369// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7370// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7371// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7372TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7373 enum class Orientation {
7374 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7375 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7376 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7377 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7378 ftl_last = ORIENTATION_270,
7379 };
7380 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7381 Orientation::ORIENTATION_270;
7382 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7383 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7384 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7385 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7386 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7387 };
7388
7389 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7390
7391 // Configure the touchscreen as being installed in the one of the four different orientations
7392 // relative to the display.
7393 addConfigurationProperty("touch.deviceType", "touchScreen");
7394 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7395 prepareDisplay(ui::ROTATION_0);
7396
Arpit Singha8c236b2023-04-25 13:56:05 +00007397 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00007398
7399 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7400 // orientations of either 90 or 270) this means the display's natural resolution will be
7401 // flipped.
7402 const bool displayRotated =
7403 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7404 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7405 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7406 const Rect physicalFrame{0, 0, width, height};
7407 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7408
7409 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7410 const float expectedPrecisionX = displayRotated ? 4 : 2;
7411 const float expectedPrecisionY = displayRotated ? 2 : 4;
7412
7413 // Test all four corners.
7414 for (int i = 0; i < 4; i++) {
7415 const auto& raw = kRawCorners[i];
7416 processDown(mapper, raw.x, raw.y);
7417 processSync(mapper);
7418 const auto& expected = expectedPoints[i];
7419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7420 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7421 WithCoords(expected.x, expected.y),
7422 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7423 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7424 << "with touchscreen orientation "
7425 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7426 << expected.x << ", " << expected.y << ").";
7427 processUp(mapper);
7428 processSync(mapper);
7429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7430 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7431 WithCoords(expected.x, expected.y))));
7432 }
7433}
7434
Prabir Pradhan82687402022-12-06 01:32:53 +00007435TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7436 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7437 kMappedCorners = {
7438 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7439 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7440 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7441 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7442 };
7443
7444 const ui::Rotation displayRotation = GetParam();
7445
7446 addConfigurationProperty("touch.deviceType", "touchScreen");
7447 prepareDisplay(displayRotation);
7448
Arpit Singha8c236b2023-04-25 13:56:05 +00007449 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00007450
7451 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7452
7453 // Test all four corners.
7454 for (int i = 0; i < 4; i++) {
7455 const auto& expected = expectedPoints[i];
7456 const auto& raw = kRawCorners[i];
7457 processDown(mapper, raw.x, raw.y);
7458 processSync(mapper);
7459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7460 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7461 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7462 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7463 << "with display rotation " << ui::toCString(displayRotation)
7464 << ", expected point (" << expected.x << ", " << expected.y << ").";
7465 processUp(mapper);
7466 processSync(mapper);
7467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7468 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7469 WithCoords(expected.x, expected.y))));
7470 }
7471}
7472
Prabir Pradhan3e798762022-12-02 21:02:11 +00007473TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7474 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7475 kMappedCorners = {
7476 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7477 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7478 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7479 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7480 };
7481
7482 const ui::Rotation displayRotation = GetParam();
7483
7484 addConfigurationProperty("touch.deviceType", "touchScreen");
7485 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7486
Arpit Singha8c236b2023-04-25 13:56:05 +00007487 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00007488
7489 // Ori 270, so width and height swapped
7490 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7491 prepareDisplay(displayRotation);
7492 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7493
7494 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7495
7496 // Test all four corners.
7497 for (int i = 0; i < 4; i++) {
7498 const auto& expected = expectedPoints[i];
7499 const auto& raw = kRawCorners[i];
7500 processDown(mapper, raw.x, raw.y);
7501 processSync(mapper);
7502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7503 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7504 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7505 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7506 << "with display rotation " << ui::toCString(displayRotation)
7507 << ", expected point (" << expected.x << ", " << expected.y << ").";
7508 processUp(mapper);
7509 processSync(mapper);
7510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7511 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7512 WithCoords(expected.x, expected.y))));
7513 }
7514}
7515
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007516TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7517 const ui::Rotation displayRotation = GetParam();
7518
7519 addConfigurationProperty("touch.deviceType", "touchScreen");
7520 prepareDisplay(displayRotation);
7521
7522 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00007523 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007524
7525 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7526 // MotionRanges use display pixels as their units
7527 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7528 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7529
7530 // The MotionRanges should be oriented in the rotated display's coordinate space
7531 const bool displayRotated =
7532 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7533
7534 constexpr float MAX_X = 479.5;
7535 constexpr float MAX_Y = 799.75;
7536 EXPECT_EQ(xRange->min, 0.f);
7537 EXPECT_EQ(yRange->min, 0.f);
7538 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7539 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7540
7541 EXPECT_EQ(xRange->flat, 8.f);
7542 EXPECT_EQ(yRange->flat, 8.f);
7543
7544 EXPECT_EQ(xRange->fuzz, 2.f);
7545 EXPECT_EQ(yRange->fuzz, 2.f);
7546
7547 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7548 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7549}
7550
Prabir Pradhana9df3162022-12-05 23:57:27 +00007551// Run the precision tests for all rotations.
7552INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7553 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7554 ui::ROTATION_270),
7555 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7556 return ftl::enum_string(testParamInfo.param);
7557 });
7558
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007559// --- ExternalStylusFusionTest ---
7560
7561class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7562public:
7563 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7564 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007565 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007566 prepareButtons();
7567 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007568 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007569
7570 mStylusState.when = ARBITRARY_TIME;
7571 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007572 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007573 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007574 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007575 processExternalStylusState(mapper);
7576 return mapper;
7577 }
7578
7579 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7580 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7581 for (const NotifyArgs& args : generatedArgs) {
7582 mFakeListener->notify(args);
7583 }
7584 // Loop the reader to flush the input listener queue.
7585 mReader->loopOnce();
7586 return generatedArgs;
7587 }
7588
7589protected:
7590 StylusState mStylusState{};
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007591
7592 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7593 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007594 AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007595
7596 // The first pointer is withheld.
7597 processDown(mapper, 100, 200);
7598 processSync(mapper);
7599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7600 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7601 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7602
7603 // The external stylus reports pressure. The withheld finger pointer is released as a
7604 // stylus.
7605 mStylusState.pressure = 1.f;
7606 processExternalStylusState(mapper);
7607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7608 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7609 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7610
7611 // Subsequent pointer events are not withheld.
7612 processMove(mapper, 101, 201);
7613 processSync(mapper);
7614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7615 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7616
7617 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7619 }
7620
7621 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7622 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7623
7624 // Releasing the touch pointer ends the gesture.
7625 processUp(mapper);
7626 processSync(mapper);
7627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007628 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007629 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007630
7631 mStylusState.pressure = 0.f;
7632 processExternalStylusState(mapper);
7633 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7635 }
7636
7637 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007638 // When stylus fusion is not successful, events should be reported with the original source.
7639 // In this case, it is from a touchscreen.
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007640 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007641 AllOf(WithSource(AINPUT_SOURCE_TOUCHSCREEN), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007642
7643 // The first pointer is withheld when an external stylus is connected,
7644 // and a timeout is requested.
7645 processDown(mapper, 100, 200);
7646 processSync(mapper);
7647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7648 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7649 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7650
7651 // If the timeout expires early, it is requested again.
7652 handleTimeout(mapper, ARBITRARY_TIME + 1);
7653 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7654 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7655
7656 // When the timeout expires, the withheld touch is released as a finger pointer.
7657 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7659 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7660
7661 // Subsequent pointer events are not withheld.
7662 processMove(mapper, 101, 201);
7663 processSync(mapper);
7664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7665 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7666 processUp(mapper);
7667 processSync(mapper);
7668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7669 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7670
7671 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7673 }
7674
7675private:
7676 InputDeviceInfo mExternalStylusDeviceInfo{};
7677};
7678
7679TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7680 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007681 ASSERT_EQ(STYLUS_FUSION_SOURCE, mapper.getSources());
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007682}
7683
7684TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7685 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7686 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7687}
7688
7689TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7690 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7691 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7692}
7693
7694// Test a successful stylus fusion gesture where the pressure is reported by the external
7695// before the touch is reported by the touchscreen.
7696TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7697 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007698 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007699
7700 // The external stylus reports pressure first. It is ignored for now.
7701 mStylusState.pressure = 1.f;
7702 processExternalStylusState(mapper);
7703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7704 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7705
7706 // When the touch goes down afterwards, it is reported as a stylus pointer.
7707 processDown(mapper, 100, 200);
7708 processSync(mapper);
7709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7710 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7711 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7712
7713 processMove(mapper, 101, 201);
7714 processSync(mapper);
7715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7716 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7717 processUp(mapper);
7718 processSync(mapper);
7719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7720 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7721
7722 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7724}
7725
7726TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7727 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7728
7729 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7730 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7731
7732 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7733 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7734 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7735 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7736}
7737
7738TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7739 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007740 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007741
7742 mStylusState.pressure = 0.8f;
7743 processExternalStylusState(mapper);
7744 processDown(mapper, 100, 200);
7745 processSync(mapper);
7746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7747 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7748 WithPressure(0.8f))));
7749 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7750
7751 // The external stylus reports a pressure change. We wait for some time for a touch event.
7752 mStylusState.pressure = 0.6f;
7753 processExternalStylusState(mapper);
7754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7755 ASSERT_NO_FATAL_FAILURE(
7756 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7757
7758 // If a touch is reported within the timeout, it reports the updated pressure.
7759 processMove(mapper, 101, 201);
7760 processSync(mapper);
7761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7762 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7763 WithPressure(0.6f))));
7764 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7765
7766 // There is another pressure change.
7767 mStylusState.pressure = 0.5f;
7768 processExternalStylusState(mapper);
7769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7770 ASSERT_NO_FATAL_FAILURE(
7771 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7772
7773 // If a touch is not reported within the timeout, a move event is generated to report
7774 // the new pressure.
7775 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7777 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7778 WithPressure(0.5f))));
7779
7780 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7781 // repeated indefinitely.
7782 mStylusState.pressure = 0.0f;
7783 processExternalStylusState(mapper);
7784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7785 ASSERT_NO_FATAL_FAILURE(
7786 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7787 processMove(mapper, 102, 202);
7788 processSync(mapper);
7789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7790 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7791 WithPressure(0.5f))));
7792 processMove(mapper, 103, 203);
7793 processSync(mapper);
7794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7795 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7796 WithPressure(0.5f))));
7797
7798 processUp(mapper);
7799 processSync(mapper);
7800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007801 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007802 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007803
7804 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7806}
7807
7808TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7809 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007810 auto source = WithSource(STYLUS_FUSION_SOURCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007811
7812 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007813 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007814 processExternalStylusState(mapper);
7815 processDown(mapper, 100, 200);
7816 processSync(mapper);
7817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7818 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007819 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007820 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7821
7822 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007823 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007824 processExternalStylusState(mapper);
7825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7826 ASSERT_NO_FATAL_FAILURE(
7827 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7828
7829 // If a touch is reported within the timeout, it reports the updated pressure.
7830 processMove(mapper, 101, 201);
7831 processSync(mapper);
7832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7833 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007834 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007835 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7836
7837 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007838 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007839 processExternalStylusState(mapper);
7840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7841 ASSERT_NO_FATAL_FAILURE(
7842 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7843
7844 // If a touch is not reported within the timeout, a move event is generated to report
7845 // the new tool type.
7846 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7848 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007849 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007850
7851 processUp(mapper);
7852 processSync(mapper);
7853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7854 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007855 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007856
7857 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7859}
7860
7861TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7862 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007863 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007864
7865 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7866
7867 // The external stylus reports a button change. We wait for some time for a touch event.
7868 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7869 processExternalStylusState(mapper);
7870 ASSERT_NO_FATAL_FAILURE(
7871 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7872
7873 // If a touch is reported within the timeout, it reports the updated button state.
7874 processMove(mapper, 101, 201);
7875 processSync(mapper);
7876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7877 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7878 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7880 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7881 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7882 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7883
7884 // The button is now released.
7885 mStylusState.buttons = 0;
7886 processExternalStylusState(mapper);
7887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7888 ASSERT_NO_FATAL_FAILURE(
7889 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7890
7891 // If a touch is not reported within the timeout, a move event is generated to report
7892 // the new button state.
7893 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7895 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7896 WithButtonState(0))));
7897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007898 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7899 WithButtonState(0))));
7900
7901 processUp(mapper);
7902 processSync(mapper);
7903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007904 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7905
7906 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7908}
7909
Michael Wrightd02c5b62014-02-10 15:10:22 -08007910// --- MultiTouchInputMapperTest ---
7911
7912class MultiTouchInputMapperTest : public TouchInputMapperTest {
7913protected:
7914 void prepareAxes(int axes);
7915
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007916 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7917 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7918 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7919 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7920 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7921 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7922 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7923 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7924 void processId(MultiTouchInputMapper& mapper, int32_t id);
7925 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7926 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7927 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007928 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007929 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007930 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7931 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007932};
7933
7934void MultiTouchInputMapperTest::prepareAxes(int axes) {
7935 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007936 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7937 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007938 }
7939 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007940 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7941 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007942 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007943 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7944 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007945 }
7946 }
7947 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007948 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7949 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007950 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007951 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007952 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007953 }
7954 }
7955 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007956 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7957 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007958 }
7959 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007960 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7961 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007962 }
7963 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007964 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7965 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007966 }
7967 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007968 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7969 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007970 }
7971 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007972 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7973 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007974 }
7975 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007976 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007977 }
7978}
7979
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007980void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7981 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007982 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7983 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007984}
7985
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007986void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7987 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007988 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007989}
7990
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007991void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7992 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007994}
7995
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007996void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007997 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007998}
7999
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008000void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008002}
8003
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008004void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
8005 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008006 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008007}
8008
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008009void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008010 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008011}
8012
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008013void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008015}
8016
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008017void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008019}
8020
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008021void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008023}
8024
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008025void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008026 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008027}
8028
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008029void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
8030 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008032}
8033
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008034void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
8035 int32_t value) {
8036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
8037 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
8038}
8039
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008040void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008041 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008042}
8043
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00008044void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
8045 nsecs_t readTime) {
8046 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008047}
8048
Michael Wrightd02c5b62014-02-10 15:10:22 -08008049TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008050 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008051 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008052 prepareAxes(POSITION);
8053 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008054 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008055
arthurhungdcef2dc2020-08-11 14:47:50 +08008056 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008057
8058 NotifyMotionArgs motionArgs;
8059
8060 // Two fingers down at once.
8061 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8062 processPosition(mapper, x1, y1);
8063 processMTSync(mapper);
8064 processPosition(mapper, x2, y2);
8065 processMTSync(mapper);
8066 processSync(mapper);
8067
8068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8069 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8070 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8071 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8072 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8073 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8074 ASSERT_EQ(0, motionArgs.flags);
8075 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8076 ASSERT_EQ(0, motionArgs.buttonState);
8077 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008078 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008079 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008080 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008081 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8082 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8083 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8084 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8085 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8086
8087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8088 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8089 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8090 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8091 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008092 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008093 ASSERT_EQ(0, motionArgs.flags);
8094 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8095 ASSERT_EQ(0, motionArgs.buttonState);
8096 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008097 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008098 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008099 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008100 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008101 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008102 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8103 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8104 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8105 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8106 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8107 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8108 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8109
8110 // Move.
8111 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8112 processPosition(mapper, x1, y1);
8113 processMTSync(mapper);
8114 processPosition(mapper, x2, y2);
8115 processMTSync(mapper);
8116 processSync(mapper);
8117
8118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8119 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8120 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8121 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8122 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8124 ASSERT_EQ(0, motionArgs.flags);
8125 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8126 ASSERT_EQ(0, motionArgs.buttonState);
8127 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008128 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008129 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008130 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008131 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008132 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8134 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8135 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8136 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8137 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8138 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8139 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8140
8141 // First finger up.
8142 x2 += 15; y2 -= 20;
8143 processPosition(mapper, x2, y2);
8144 processMTSync(mapper);
8145 processSync(mapper);
8146
8147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8148 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8149 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8150 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8151 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008152 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008153 ASSERT_EQ(0, motionArgs.flags);
8154 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8155 ASSERT_EQ(0, motionArgs.buttonState);
8156 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008157 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008158 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008159 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008160 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008161 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008162 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8163 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8164 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8165 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8166 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8167 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8168 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8169
8170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8171 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8172 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8173 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8174 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8175 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8176 ASSERT_EQ(0, motionArgs.flags);
8177 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8178 ASSERT_EQ(0, motionArgs.buttonState);
8179 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008180 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008181 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008182 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008183 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8184 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8185 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8186 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8187 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8188
8189 // Move.
8190 x2 += 20; y2 -= 25;
8191 processPosition(mapper, x2, y2);
8192 processMTSync(mapper);
8193 processSync(mapper);
8194
8195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8196 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8197 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8198 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8199 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8201 ASSERT_EQ(0, motionArgs.flags);
8202 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8203 ASSERT_EQ(0, motionArgs.buttonState);
8204 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008205 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008206 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008207 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8209 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8210 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8211 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8212 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8213
8214 // New finger down.
8215 int32_t x3 = 700, y3 = 300;
8216 processPosition(mapper, x2, y2);
8217 processMTSync(mapper);
8218 processPosition(mapper, x3, y3);
8219 processMTSync(mapper);
8220 processSync(mapper);
8221
8222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8223 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8224 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8225 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8226 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008227 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008228 ASSERT_EQ(0, motionArgs.flags);
8229 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8230 ASSERT_EQ(0, motionArgs.buttonState);
8231 ASSERT_EQ(0, motionArgs.edgeFlags);
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(x3), toDisplayY(y3), 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 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8242 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8243 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8244
8245 // Second finger up.
8246 x3 += 30; y3 -= 20;
8247 processPosition(mapper, x3, y3);
8248 processMTSync(mapper);
8249 processSync(mapper);
8250
8251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8252 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8253 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8254 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8255 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008256 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008257 ASSERT_EQ(0, motionArgs.flags);
8258 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8259 ASSERT_EQ(0, motionArgs.buttonState);
8260 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008261 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008262 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008263 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008264 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008265 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008266 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8267 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8269 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8270 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8271 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8272 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8273
8274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8275 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8276 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8277 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8278 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8279 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8280 ASSERT_EQ(0, motionArgs.flags);
8281 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8282 ASSERT_EQ(0, motionArgs.buttonState);
8283 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008284 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008285 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008286 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8288 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8289 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8290 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8291 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8292
8293 // Last finger up.
8294 processMTSync(mapper);
8295 processSync(mapper);
8296
8297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8298 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8299 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8300 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8301 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8302 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8303 ASSERT_EQ(0, motionArgs.flags);
8304 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8305 ASSERT_EQ(0, motionArgs.buttonState);
8306 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008307 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008308 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008309 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008310 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8311 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8312 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8313 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8314 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8315
8316 // Should not have sent any more keys or motions.
8317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8319}
8320
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008321TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8322 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008323 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008324
8325 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8326 /*fuzz*/ 0, /*resolution*/ 10);
8327 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8328 /*fuzz*/ 0, /*resolution*/ 11);
8329 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8330 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8331 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8332 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8333 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8334 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8335 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8336 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8337
Arpit Singha8c236b2023-04-25 13:56:05 +00008338 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008339
8340 // X and Y axes
8341 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8342 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8343 // Touch major and minor
8344 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8345 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8346 // Tool major and minor
8347 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8348 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8349}
8350
8351TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8352 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008353 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008354
8355 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8356 /*fuzz*/ 0, /*resolution*/ 10);
8357 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8358 /*fuzz*/ 0, /*resolution*/ 11);
8359
8360 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8361
Arpit Singha8c236b2023-04-25 13:56:05 +00008362 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008363
8364 // Touch major and minor
8365 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8366 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8367 // Tool major and minor
8368 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8369 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8370}
8371
Michael Wrightd02c5b62014-02-10 15:10:22 -08008372TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008373 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008374 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008375 prepareAxes(POSITION | ID);
8376 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008377 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008378
arthurhungdcef2dc2020-08-11 14:47:50 +08008379 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008380
8381 NotifyMotionArgs motionArgs;
8382
8383 // Two fingers down at once.
8384 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8385 processPosition(mapper, x1, y1);
8386 processId(mapper, 1);
8387 processMTSync(mapper);
8388 processPosition(mapper, x2, y2);
8389 processId(mapper, 2);
8390 processMTSync(mapper);
8391 processSync(mapper);
8392
8393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8394 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008395 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008396 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008397 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8399 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8400
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008402 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008403 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008404 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008405 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008406 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008407 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008408 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8409 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8411 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8412
8413 // Move.
8414 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8415 processPosition(mapper, x1, y1);
8416 processId(mapper, 1);
8417 processMTSync(mapper);
8418 processPosition(mapper, x2, y2);
8419 processId(mapper, 2);
8420 processMTSync(mapper);
8421 processSync(mapper);
8422
8423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8424 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008425 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008426 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008427 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008428 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008429 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8431 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8433 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8434
8435 // First finger up.
8436 x2 += 15; y2 -= 20;
8437 processPosition(mapper, x2, y2);
8438 processId(mapper, 2);
8439 processMTSync(mapper);
8440 processSync(mapper);
8441
8442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008443 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008444 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008445 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008446 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008447 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008448 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8450 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8452 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8453
8454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8455 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008456 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008457 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008458 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008459 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8460 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8461
8462 // Move.
8463 x2 += 20; y2 -= 25;
8464 processPosition(mapper, x2, y2);
8465 processId(mapper, 2);
8466 processMTSync(mapper);
8467 processSync(mapper);
8468
8469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008471 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008472 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008473 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8475 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8476
8477 // New finger down.
8478 int32_t x3 = 700, y3 = 300;
8479 processPosition(mapper, x2, y2);
8480 processId(mapper, 2);
8481 processMTSync(mapper);
8482 processPosition(mapper, x3, y3);
8483 processId(mapper, 3);
8484 processMTSync(mapper);
8485 processSync(mapper);
8486
8487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008488 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008489 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008490 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008491 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008492 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008493 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8495 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8497 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8498
8499 // Second finger up.
8500 x3 += 30; y3 -= 20;
8501 processPosition(mapper, x3, y3);
8502 processId(mapper, 3);
8503 processMTSync(mapper);
8504 processSync(mapper);
8505
8506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008507 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008508 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008509 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008510 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008511 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008512 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8514 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8516 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8517
8518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8519 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008520 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008521 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008522 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8524 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8525
8526 // Last finger up.
8527 processMTSync(mapper);
8528 processSync(mapper);
8529
8530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8531 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008532 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008533 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008534 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8536 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8537
8538 // Should not have sent any more keys or motions.
8539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8541}
8542
8543TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008544 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008545 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008546 prepareAxes(POSITION | ID | SLOT);
8547 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008548 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008549
arthurhungdcef2dc2020-08-11 14:47:50 +08008550 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008551
8552 NotifyMotionArgs motionArgs;
8553
8554 // Two fingers down at once.
8555 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8556 processPosition(mapper, x1, y1);
8557 processId(mapper, 1);
8558 processSlot(mapper, 1);
8559 processPosition(mapper, x2, y2);
8560 processId(mapper, 2);
8561 processSync(mapper);
8562
8563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8564 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008565 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008566 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008567 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8569 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8570
8571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008572 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008573 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008574 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008575 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008576 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008577 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8579 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8581 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8582
8583 // Move.
8584 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8585 processSlot(mapper, 0);
8586 processPosition(mapper, x1, y1);
8587 processSlot(mapper, 1);
8588 processPosition(mapper, x2, y2);
8589 processSync(mapper);
8590
8591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8592 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008593 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008594 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008595 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008596 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008597 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8599 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8601 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8602
8603 // First finger up.
8604 x2 += 15; y2 -= 20;
8605 processSlot(mapper, 0);
8606 processId(mapper, -1);
8607 processSlot(mapper, 1);
8608 processPosition(mapper, x2, y2);
8609 processSync(mapper);
8610
8611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008612 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008613 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008614 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008615 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008616 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008617 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8619 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8621 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8622
8623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008625 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008626 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008627 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8629 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8630
8631 // Move.
8632 x2 += 20; y2 -= 25;
8633 processPosition(mapper, x2, y2);
8634 processSync(mapper);
8635
8636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8637 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008638 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008639 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008640 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8642 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8643
8644 // New finger down.
8645 int32_t x3 = 700, y3 = 300;
8646 processPosition(mapper, x2, y2);
8647 processSlot(mapper, 0);
8648 processId(mapper, 3);
8649 processPosition(mapper, x3, y3);
8650 processSync(mapper);
8651
8652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008653 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008654 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008655 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008656 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008657 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008658 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8660 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8662 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8663
8664 // Second finger up.
8665 x3 += 30; y3 -= 20;
8666 processSlot(mapper, 1);
8667 processId(mapper, -1);
8668 processSlot(mapper, 0);
8669 processPosition(mapper, x3, y3);
8670 processSync(mapper);
8671
8672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008673 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008674 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008675 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008676 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008677 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008678 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008679 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8680 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8682 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8683
8684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8685 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008686 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008687 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008688 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008689 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8690 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8691
8692 // Last finger up.
8693 processId(mapper, -1);
8694 processSync(mapper);
8695
8696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8697 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008698 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008699 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008700 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8702 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8703
8704 // Should not have sent any more keys or motions.
8705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8707}
8708
8709TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008710 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008711 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008712 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008713 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008714
8715 // These calculations are based on the input device calibration documentation.
8716 int32_t rawX = 100;
8717 int32_t rawY = 200;
8718 int32_t rawTouchMajor = 7;
8719 int32_t rawTouchMinor = 6;
8720 int32_t rawToolMajor = 9;
8721 int32_t rawToolMinor = 8;
8722 int32_t rawPressure = 11;
8723 int32_t rawDistance = 0;
8724 int32_t rawOrientation = 3;
8725 int32_t id = 5;
8726
8727 float x = toDisplayX(rawX);
8728 float y = toDisplayY(rawY);
8729 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8730 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8731 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8732 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8733 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8734 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8735 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8736 float distance = float(rawDistance);
8737
8738 processPosition(mapper, rawX, rawY);
8739 processTouchMajor(mapper, rawTouchMajor);
8740 processTouchMinor(mapper, rawTouchMinor);
8741 processToolMajor(mapper, rawToolMajor);
8742 processToolMinor(mapper, rawToolMinor);
8743 processPressure(mapper, rawPressure);
8744 processOrientation(mapper, rawOrientation);
8745 processDistance(mapper, rawDistance);
8746 processId(mapper, id);
8747 processMTSync(mapper);
8748 processSync(mapper);
8749
8750 NotifyMotionArgs args;
8751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8752 ASSERT_EQ(0, args.pointerProperties[0].id);
8753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8754 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8755 orientation, distance));
8756}
8757
8758TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008759 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008760 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008761 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8762 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00008763 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008764
8765 // These calculations are based on the input device calibration documentation.
8766 int32_t rawX = 100;
8767 int32_t rawY = 200;
8768 int32_t rawTouchMajor = 140;
8769 int32_t rawTouchMinor = 120;
8770 int32_t rawToolMajor = 180;
8771 int32_t rawToolMinor = 160;
8772
8773 float x = toDisplayX(rawX);
8774 float y = toDisplayY(rawY);
8775 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8776 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8777 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8778 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8779 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8780
8781 processPosition(mapper, rawX, rawY);
8782 processTouchMajor(mapper, rawTouchMajor);
8783 processTouchMinor(mapper, rawTouchMinor);
8784 processToolMajor(mapper, rawToolMajor);
8785 processToolMinor(mapper, rawToolMinor);
8786 processMTSync(mapper);
8787 processSync(mapper);
8788
8789 NotifyMotionArgs args;
8790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8792 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8793}
8794
8795TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008796 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008797 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008798 prepareAxes(POSITION | TOUCH | TOOL);
8799 addConfigurationProperty("touch.size.calibration", "diameter");
8800 addConfigurationProperty("touch.size.scale", "10");
8801 addConfigurationProperty("touch.size.bias", "160");
8802 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00008803 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008804
8805 // These calculations are based on the input device calibration documentation.
8806 // Note: We only provide a single common touch/tool value because the device is assumed
8807 // not to emit separate values for each pointer (isSummed = 1).
8808 int32_t rawX = 100;
8809 int32_t rawY = 200;
8810 int32_t rawX2 = 150;
8811 int32_t rawY2 = 250;
8812 int32_t rawTouchMajor = 5;
8813 int32_t rawToolMajor = 8;
8814
8815 float x = toDisplayX(rawX);
8816 float y = toDisplayY(rawY);
8817 float x2 = toDisplayX(rawX2);
8818 float y2 = toDisplayY(rawY2);
8819 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8820 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8821 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8822
8823 processPosition(mapper, rawX, rawY);
8824 processTouchMajor(mapper, rawTouchMajor);
8825 processToolMajor(mapper, rawToolMajor);
8826 processMTSync(mapper);
8827 processPosition(mapper, rawX2, rawY2);
8828 processTouchMajor(mapper, rawTouchMajor);
8829 processToolMajor(mapper, rawToolMajor);
8830 processMTSync(mapper);
8831 processSync(mapper);
8832
8833 NotifyMotionArgs args;
8834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8835 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8836
8837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008838 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008839 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008840 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8841 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8843 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8844}
8845
8846TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008847 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008848 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008849 prepareAxes(POSITION | TOUCH | TOOL);
8850 addConfigurationProperty("touch.size.calibration", "area");
8851 addConfigurationProperty("touch.size.scale", "43");
8852 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00008853 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008854
8855 // These calculations are based on the input device calibration documentation.
8856 int32_t rawX = 100;
8857 int32_t rawY = 200;
8858 int32_t rawTouchMajor = 5;
8859 int32_t rawToolMajor = 8;
8860
8861 float x = toDisplayX(rawX);
8862 float y = toDisplayY(rawY);
8863 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8864 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8865 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8866
8867 processPosition(mapper, rawX, rawY);
8868 processTouchMajor(mapper, rawTouchMajor);
8869 processToolMajor(mapper, rawToolMajor);
8870 processMTSync(mapper);
8871 processSync(mapper);
8872
8873 NotifyMotionArgs args;
8874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8876 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8877}
8878
8879TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008880 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008881 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008882 prepareAxes(POSITION | PRESSURE);
8883 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8884 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00008885 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008886
Michael Wrightaa449c92017-12-13 21:21:43 +00008887 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008888 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008889 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8890 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8891 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8892
Michael Wrightd02c5b62014-02-10 15:10:22 -08008893 // These calculations are based on the input device calibration documentation.
8894 int32_t rawX = 100;
8895 int32_t rawY = 200;
8896 int32_t rawPressure = 60;
8897
8898 float x = toDisplayX(rawX);
8899 float y = toDisplayY(rawY);
8900 float pressure = float(rawPressure) * 0.01f;
8901
8902 processPosition(mapper, rawX, rawY);
8903 processPressure(mapper, rawPressure);
8904 processMTSync(mapper);
8905 processSync(mapper);
8906
8907 NotifyMotionArgs args;
8908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8909 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8910 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8911}
8912
8913TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008914 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008915 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008916 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008917 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008918
8919 NotifyMotionArgs motionArgs;
8920 NotifyKeyArgs keyArgs;
8921
8922 processId(mapper, 1);
8923 processPosition(mapper, 100, 200);
8924 processSync(mapper);
8925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8926 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8927 ASSERT_EQ(0, motionArgs.buttonState);
8928
8929 // press BTN_LEFT, release BTN_LEFT
8930 processKey(mapper, BTN_LEFT, 1);
8931 processSync(mapper);
8932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8933 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8934 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8935
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8937 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8938 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8939
Michael Wrightd02c5b62014-02-10 15:10:22 -08008940 processKey(mapper, BTN_LEFT, 0);
8941 processSync(mapper);
8942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008943 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008944 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008945
8946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008947 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008948 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008949
8950 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8951 processKey(mapper, BTN_RIGHT, 1);
8952 processKey(mapper, BTN_MIDDLE, 1);
8953 processSync(mapper);
8954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8955 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8956 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8957 motionArgs.buttonState);
8958
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8960 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8961 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8962
8963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8964 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8965 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8966 motionArgs.buttonState);
8967
Michael Wrightd02c5b62014-02-10 15:10:22 -08008968 processKey(mapper, BTN_RIGHT, 0);
8969 processSync(mapper);
8970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008971 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008972 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008973
8974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008975 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008976 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008977
8978 processKey(mapper, BTN_MIDDLE, 0);
8979 processSync(mapper);
8980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008981 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008982 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008983
8984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008985 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008986 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008987
8988 // press BTN_BACK, release BTN_BACK
8989 processKey(mapper, BTN_BACK, 1);
8990 processSync(mapper);
8991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8992 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8993 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008994
Michael Wrightd02c5b62014-02-10 15:10:22 -08008995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008997 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8998
8999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9000 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9001 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009002
9003 processKey(mapper, BTN_BACK, 0);
9004 processSync(mapper);
9005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009006 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009007 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009008
9009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009010 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009011 ASSERT_EQ(0, motionArgs.buttonState);
9012
Michael Wrightd02c5b62014-02-10 15:10:22 -08009013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9014 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9015 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9016
9017 // press BTN_SIDE, release BTN_SIDE
9018 processKey(mapper, BTN_SIDE, 1);
9019 processSync(mapper);
9020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9021 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9022 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009023
Michael Wrightd02c5b62014-02-10 15:10:22 -08009024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009025 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009026 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
9027
9028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9029 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9030 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009031
9032 processKey(mapper, BTN_SIDE, 0);
9033 processSync(mapper);
9034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009035 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009036 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009037
9038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009039 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009040 ASSERT_EQ(0, motionArgs.buttonState);
9041
Michael Wrightd02c5b62014-02-10 15:10:22 -08009042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9043 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9044 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9045
9046 // press BTN_FORWARD, release BTN_FORWARD
9047 processKey(mapper, BTN_FORWARD, 1);
9048 processSync(mapper);
9049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9050 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9051 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009052
Michael Wrightd02c5b62014-02-10 15:10:22 -08009053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009054 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009055 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9056
9057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9058 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9059 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009060
9061 processKey(mapper, BTN_FORWARD, 0);
9062 processSync(mapper);
9063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009064 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009065 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009066
9067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009069 ASSERT_EQ(0, motionArgs.buttonState);
9070
Michael Wrightd02c5b62014-02-10 15:10:22 -08009071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9072 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9073 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9074
9075 // press BTN_EXTRA, release BTN_EXTRA
9076 processKey(mapper, BTN_EXTRA, 1);
9077 processSync(mapper);
9078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9079 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9080 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009081
Michael Wrightd02c5b62014-02-10 15:10:22 -08009082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009084 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9085
9086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9087 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9088 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009089
9090 processKey(mapper, BTN_EXTRA, 0);
9091 processSync(mapper);
9092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009093 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009094 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009095
9096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009097 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009098 ASSERT_EQ(0, motionArgs.buttonState);
9099
Michael Wrightd02c5b62014-02-10 15:10:22 -08009100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9101 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9102 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9103
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9105
Michael Wrightd02c5b62014-02-10 15:10:22 -08009106 // press BTN_STYLUS, release BTN_STYLUS
9107 processKey(mapper, BTN_STYLUS, 1);
9108 processSync(mapper);
9109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009111 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
9112
9113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9114 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9115 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009116
9117 processKey(mapper, BTN_STYLUS, 0);
9118 processSync(mapper);
9119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009120 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009121 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009122
9123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009125 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009126
9127 // press BTN_STYLUS2, release BTN_STYLUS2
9128 processKey(mapper, BTN_STYLUS2, 1);
9129 processSync(mapper);
9130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009132 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
9133
9134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9135 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9136 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009137
9138 processKey(mapper, BTN_STYLUS2, 0);
9139 processSync(mapper);
9140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009141 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009142 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009143
9144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009145 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009146 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009147
9148 // release touch
9149 processId(mapper, -1);
9150 processSync(mapper);
9151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9152 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9153 ASSERT_EQ(0, motionArgs.buttonState);
9154}
9155
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009156TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
9157 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009158 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009159 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00009160 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009161
9162 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
9163 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
9164
9165 // Touch down.
9166 processId(mapper, 1);
9167 processPosition(mapper, 100, 200);
9168 processSync(mapper);
9169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9170 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
9171
9172 // Press and release button mapped to the primary stylus button.
9173 processKey(mapper, BTN_A, 1);
9174 processSync(mapper);
9175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9176 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9177 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9179 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9180 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9181
9182 processKey(mapper, BTN_A, 0);
9183 processSync(mapper);
9184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9185 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9187 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9188
9189 // Press and release the HID usage mapped to the secondary stylus button.
9190 processHidUsage(mapper, 0xabcd, 1);
9191 processSync(mapper);
9192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9193 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9194 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9196 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9197 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9198
9199 processHidUsage(mapper, 0xabcd, 0);
9200 processSync(mapper);
9201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9202 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9204 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9205
9206 // Release touch.
9207 processId(mapper, -1);
9208 processSync(mapper);
9209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9210 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
9211}
9212
Michael Wrightd02c5b62014-02-10 15:10:22 -08009213TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009214 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009215 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009216 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009217 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009218
9219 NotifyMotionArgs motionArgs;
9220
9221 // default tool type is finger
9222 processId(mapper, 1);
9223 processPosition(mapper, 100, 200);
9224 processSync(mapper);
9225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9226 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009227 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009228
9229 // eraser
9230 processKey(mapper, BTN_TOOL_RUBBER, 1);
9231 processSync(mapper);
9232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9233 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009234 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009235
9236 // stylus
9237 processKey(mapper, BTN_TOOL_RUBBER, 0);
9238 processKey(mapper, BTN_TOOL_PEN, 1);
9239 processSync(mapper);
9240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9241 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009242 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009243
9244 // brush
9245 processKey(mapper, BTN_TOOL_PEN, 0);
9246 processKey(mapper, BTN_TOOL_BRUSH, 1);
9247 processSync(mapper);
9248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009250 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009251
9252 // pencil
9253 processKey(mapper, BTN_TOOL_BRUSH, 0);
9254 processKey(mapper, BTN_TOOL_PENCIL, 1);
9255 processSync(mapper);
9256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009258 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009259
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009260 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009261 processKey(mapper, BTN_TOOL_PENCIL, 0);
9262 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
9263 processSync(mapper);
9264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9265 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009266 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009267
9268 // mouse
9269 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9270 processKey(mapper, BTN_TOOL_MOUSE, 1);
9271 processSync(mapper);
9272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009274 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009275
9276 // lens
9277 processKey(mapper, BTN_TOOL_MOUSE, 0);
9278 processKey(mapper, BTN_TOOL_LENS, 1);
9279 processSync(mapper);
9280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9281 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009282 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009283
9284 // double-tap
9285 processKey(mapper, BTN_TOOL_LENS, 0);
9286 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9287 processSync(mapper);
9288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009290 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009291
9292 // triple-tap
9293 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9294 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9295 processSync(mapper);
9296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009298 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009299
9300 // quad-tap
9301 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9302 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9303 processSync(mapper);
9304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009306 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009307
9308 // finger
9309 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9310 processKey(mapper, BTN_TOOL_FINGER, 1);
9311 processSync(mapper);
9312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009314 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009315
9316 // stylus trumps finger
9317 processKey(mapper, BTN_TOOL_PEN, 1);
9318 processSync(mapper);
9319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009321 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009322
9323 // eraser trumps stylus
9324 processKey(mapper, BTN_TOOL_RUBBER, 1);
9325 processSync(mapper);
9326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009328 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009329
9330 // mouse trumps eraser
9331 processKey(mapper, BTN_TOOL_MOUSE, 1);
9332 processSync(mapper);
9333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009335 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009336
9337 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9338 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9339 processSync(mapper);
9340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009342 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009343
9344 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9345 processToolType(mapper, MT_TOOL_PEN);
9346 processSync(mapper);
9347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9348 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009349 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009350
9351 // back to default tool type
9352 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9353 processKey(mapper, BTN_TOOL_MOUSE, 0);
9354 processKey(mapper, BTN_TOOL_RUBBER, 0);
9355 processKey(mapper, BTN_TOOL_PEN, 0);
9356 processKey(mapper, BTN_TOOL_FINGER, 0);
9357 processSync(mapper);
9358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009360 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009361}
9362
9363TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009364 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009365 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009366 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009367 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009368 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009369
9370 NotifyMotionArgs motionArgs;
9371
9372 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9373 processId(mapper, 1);
9374 processPosition(mapper, 100, 200);
9375 processSync(mapper);
9376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9377 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9378 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9379 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9380
9381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9382 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9384 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9385
9386 // move a little
9387 processPosition(mapper, 150, 250);
9388 processSync(mapper);
9389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9390 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9392 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9393
9394 // down when BTN_TOUCH is pressed, pressure defaults to 1
9395 processKey(mapper, BTN_TOUCH, 1);
9396 processSync(mapper);
9397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9398 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9400 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9401
9402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9403 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9405 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9406
9407 // up when BTN_TOUCH is released, hover restored
9408 processKey(mapper, BTN_TOUCH, 0);
9409 processSync(mapper);
9410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9411 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9413 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9414
9415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9416 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9417 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9418 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9419
9420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9421 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9423 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9424
9425 // exit hover when pointer goes away
9426 processId(mapper, -1);
9427 processSync(mapper);
9428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9429 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9431 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9432}
9433
9434TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009435 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009436 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009437 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009438 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009439
9440 NotifyMotionArgs motionArgs;
9441
9442 // initially hovering because pressure is 0
9443 processId(mapper, 1);
9444 processPosition(mapper, 100, 200);
9445 processPressure(mapper, 0);
9446 processSync(mapper);
9447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9448 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9450 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9451
9452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9453 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9455 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9456
9457 // move a little
9458 processPosition(mapper, 150, 250);
9459 processSync(mapper);
9460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9461 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9463 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9464
9465 // down when pressure becomes non-zero
9466 processPressure(mapper, RAW_PRESSURE_MAX);
9467 processSync(mapper);
9468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9469 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9471 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9472
9473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9474 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9476 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9477
9478 // up when pressure becomes 0, hover restored
9479 processPressure(mapper, 0);
9480 processSync(mapper);
9481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9482 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9484 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9485
9486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9487 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9488 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9489 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9490
9491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9492 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9494 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9495
9496 // exit hover when pointer goes away
9497 processId(mapper, -1);
9498 processSync(mapper);
9499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9500 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9501 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9502 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9503}
9504
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009505/**
9506 * Set the input device port <--> display port associations, and check that the
9507 * events are routed to the display that matches the display port.
9508 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9509 */
9510TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009511 const std::string usb2 = "USB2";
9512 const uint8_t hdmi1 = 0;
9513 const uint8_t hdmi2 = 1;
9514 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009515 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009516
9517 addConfigurationProperty("touch.deviceType", "touchScreen");
9518 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009519 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009520
9521 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9522 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9523
9524 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9525 // for this input device is specified, and the matching viewport is not present,
9526 // the input device should be disabled (at the mapper level).
9527
9528 // Add viewport for display 2 on hdmi2
9529 prepareSecondaryDisplay(type, hdmi2);
9530 // Send a touch event
9531 processPosition(mapper, 100, 100);
9532 processSync(mapper);
9533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9534
9535 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009536 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009537 // Send a touch event again
9538 processPosition(mapper, 100, 100);
9539 processSync(mapper);
9540
9541 NotifyMotionArgs args;
9542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9543 ASSERT_EQ(DISPLAY_ID, args.displayId);
9544}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009545
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009546TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9547 addConfigurationProperty("touch.deviceType", "touchScreen");
9548 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009549 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009550
9551 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9552
Michael Wrighta9cf4192022-12-01 23:46:39 +00009553 prepareDisplay(ui::ROTATION_0);
9554 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009555
9556 // Send a touch event
9557 processPosition(mapper, 100, 100);
9558 processSync(mapper);
9559
9560 NotifyMotionArgs args;
9561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9562 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9563}
9564
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009565TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009566 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009567 std::shared_ptr<FakePointerController> fakePointerController =
9568 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009569 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009570 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009571 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009572
Garfield Tan888a6a42020-01-09 11:39:16 -08009573 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009574 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009575
Michael Wrighta9cf4192022-12-01 23:46:39 +00009576 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009577 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009578 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009579
Josep del Río2d8c79a2023-01-23 19:33:50 +00009580 // Check source is mouse that would obtain the PointerController.
9581 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009582
9583 NotifyMotionArgs motionArgs;
9584 processPosition(mapper, 100, 100);
9585 processSync(mapper);
9586
9587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9588 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9589 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9590}
9591
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009592/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009593 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9594 */
9595TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9596 addConfigurationProperty("touch.deviceType", "touchScreen");
9597 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009598 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009599
Michael Wrighta9cf4192022-12-01 23:46:39 +00009600 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009601 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9602 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9603 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9604 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009605
9606 NotifyMotionArgs args;
9607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9608 ASSERT_EQ(26, args.readTime);
9609
Harry Cutts33476232023-01-30 19:57:29 +00009610 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9611 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9612 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009613
9614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9615 ASSERT_EQ(33, args.readTime);
9616}
9617
9618/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009619 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9620 * events should not be delivered to the listener.
9621 */
9622TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9623 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009624 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009625 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009626 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009627 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009628 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009629 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009630
9631 NotifyMotionArgs motionArgs;
9632 processPosition(mapper, 100, 100);
9633 processSync(mapper);
9634
9635 mFakeListener->assertNotifyMotionWasNotCalled();
9636}
9637
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009638/**
9639 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9640 * the touch mapper can process the events and the events can be delivered to the listener.
9641 */
9642TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9643 addConfigurationProperty("touch.deviceType", "touchScreen");
9644 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009645 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009646 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009647 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009648 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009649 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009650
9651 NotifyMotionArgs motionArgs;
9652 processPosition(mapper, 100, 100);
9653 processSync(mapper);
9654
9655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9656 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9657}
9658
Josh Thielene986aed2023-06-01 14:17:30 +00009659/**
9660 * When the viewport is deactivated (isActive transitions from true to false),
9661 * and touch.enableForInactiveViewport is false, touches prior to the transition
9662 * should be cancelled.
9663 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08009664TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9665 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009666 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009667 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009668 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009669 std::optional<DisplayViewport> optionalDisplayViewport =
9670 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9671 ASSERT_TRUE(optionalDisplayViewport.has_value());
9672 DisplayViewport displayViewport = *optionalDisplayViewport;
9673
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009674 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009675 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009676 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08009677
9678 // Finger down
9679 int32_t x = 100, y = 100;
9680 processPosition(mapper, x, y);
9681 processSync(mapper);
9682
9683 NotifyMotionArgs motionArgs;
9684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9685 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9686
9687 // Deactivate display viewport
9688 displayViewport.isActive = false;
9689 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009690 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009691
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009692 // The ongoing touch should be canceled immediately
9693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9694 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9695
9696 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009697 x += 10, y += 10;
9698 processPosition(mapper, x, y);
9699 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009701
9702 // Reactivate display viewport
9703 displayViewport.isActive = true;
9704 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009705 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009706
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009707 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009708 x += 10, y += 10;
9709 processPosition(mapper, x, y);
9710 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9712 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009713}
9714
Josh Thielene986aed2023-06-01 14:17:30 +00009715/**
9716 * When the viewport is deactivated (isActive transitions from true to false),
9717 * and touch.enableForInactiveViewport is true, touches prior to the transition
9718 * should not be cancelled.
9719 */
9720TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
9721 addConfigurationProperty("touch.deviceType", "touchScreen");
9722 addConfigurationProperty("touch.enableForInactiveViewport", "1");
9723 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
9724 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
9725 std::optional<DisplayViewport> optionalDisplayViewport =
9726 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9727 ASSERT_TRUE(optionalDisplayViewport.has_value());
9728 DisplayViewport displayViewport = *optionalDisplayViewport;
9729
9730 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9731 prepareAxes(POSITION);
9732 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
9733
9734 // Finger down
9735 int32_t x = 100, y = 100;
9736 processPosition(mapper, x, y);
9737 processSync(mapper);
9738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9739 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9740
9741 // Deactivate display viewport
9742 displayViewport.isActive = false;
9743 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9744 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9745
9746 // The ongoing touch should not be canceled
9747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9748
9749 // Finger move is not ignored
9750 x += 10, y += 10;
9751 processPosition(mapper, x, y);
9752 processSync(mapper);
9753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9754 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
9755
9756 // Reactivate display viewport
9757 displayViewport.isActive = true;
9758 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9759 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9760
9761 // Finger move continues and does not start new gesture
9762 x += 10, y += 10;
9763 processPosition(mapper, x, y);
9764 processSync(mapper);
9765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9766 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
9767}
9768
Arthur Hung7c645402019-01-25 17:45:42 +08009769TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9770 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009771 prepareAxes(POSITION | ID | SLOT);
9772 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009773 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009774
9775 // Create the second touch screen device, and enable multi fingers.
9776 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009777 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009778 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009779 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009780 std::shared_ptr<InputDevice> device2 =
9781 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009782 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009783
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009784 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009785 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009786 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009787 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009788 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009789 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009790 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009791 /*flat=*/0, /*fuzz=*/0);
9792 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009793 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9794 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009795
9796 // Setup the second touch screen device.
Arpit Singha8c236b2023-04-25 13:56:05 +00009797 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
9798 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
9799 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009800 std::list<NotifyArgs> unused =
9801 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009802 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009803 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009804
9805 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009806 std::shared_ptr<FakePointerController> fakePointerController =
9807 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009808 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009809
9810 // Setup policy for associated displays and show touches.
9811 const uint8_t hdmi1 = 0;
9812 const uint8_t hdmi2 = 1;
9813 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9814 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9815 mFakePolicy->setShowTouches(true);
9816
9817 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009818 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009819 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009820
9821 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009822 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009823 InputReaderConfiguration::Change::DISPLAY_INFO |
9824 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009825
9826 // Two fingers down at default display.
9827 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9828 processPosition(mapper, x1, y1);
9829 processId(mapper, 1);
9830 processSlot(mapper, 1);
9831 processPosition(mapper, x2, y2);
9832 processId(mapper, 2);
9833 processSync(mapper);
9834
9835 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9836 fakePointerController->getSpots().find(DISPLAY_ID);
9837 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9838 ASSERT_EQ(size_t(2), iter->second.size());
9839
9840 // Two fingers down at second display.
9841 processPosition(mapper2, x1, y1);
9842 processId(mapper2, 1);
9843 processSlot(mapper2, 1);
9844 processPosition(mapper2, x2, y2);
9845 processId(mapper2, 2);
9846 processSync(mapper2);
9847
9848 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9849 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9850 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009851
9852 // Disable the show touches configuration and ensure the spots are cleared.
9853 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009854 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009855 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009856
9857 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009858}
9859
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009860TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009861 prepareAxes(POSITION);
9862 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009863 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009864 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009865
9866 NotifyMotionArgs motionArgs;
9867 // Unrotated video frame
9868 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9869 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009870 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009871 processPosition(mapper, 100, 200);
9872 processSync(mapper);
9873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9874 ASSERT_EQ(frames, motionArgs.videoFrames);
9875
9876 // Subsequent touch events should not have any videoframes
9877 // This is implemented separately in FakeEventHub,
9878 // but that should match the behaviour of TouchVideoDevice.
9879 processPosition(mapper, 200, 200);
9880 processSync(mapper);
9881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9882 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9883}
9884
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009885TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009886 prepareAxes(POSITION);
9887 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009888 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009889 // Unrotated video frame
9890 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9891 NotifyMotionArgs motionArgs;
9892
9893 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009894 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009895 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9896 clearViewports();
9897 prepareDisplay(orientation);
9898 std::vector<TouchVideoFrame> frames{frame};
9899 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9900 processPosition(mapper, 100, 200);
9901 processSync(mapper);
9902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9903 ASSERT_EQ(frames, motionArgs.videoFrames);
9904 }
9905}
9906
9907TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9908 prepareAxes(POSITION);
9909 addConfigurationProperty("touch.deviceType", "touchScreen");
9910 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9911 // orientation-aware are affected by display rotation.
9912 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009913 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009914 // Unrotated video frame
9915 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9916 NotifyMotionArgs motionArgs;
9917
9918 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009919 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009920 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9921 clearViewports();
9922 prepareDisplay(orientation);
9923 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009924 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009925 processPosition(mapper, 100, 200);
9926 processSync(mapper);
9927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009928 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9929 // compared to the display. This is so that when the window transform (which contains the
9930 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9931 // window's coordinate space.
9932 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009933 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009934
9935 // Release finger.
9936 processSync(mapper);
9937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009938 }
9939}
9940
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009941TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009942 prepareAxes(POSITION);
9943 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009944 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009945 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9946 // so mix these.
9947 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9948 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9949 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9950 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9951 NotifyMotionArgs motionArgs;
9952
Michael Wrighta9cf4192022-12-01 23:46:39 +00009953 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009954 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009955 processPosition(mapper, 100, 200);
9956 processSync(mapper);
9957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009958 ASSERT_EQ(frames, motionArgs.videoFrames);
9959}
9960
9961TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9962 prepareAxes(POSITION);
9963 addConfigurationProperty("touch.deviceType", "touchScreen");
9964 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9965 // orientation-aware are affected by display rotation.
9966 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009967 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009968 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9969 // so mix these.
9970 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9971 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9972 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9973 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9974 NotifyMotionArgs motionArgs;
9975
Michael Wrighta9cf4192022-12-01 23:46:39 +00009976 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009977 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9978 processPosition(mapper, 100, 200);
9979 processSync(mapper);
9980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9981 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9982 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9983 // compared to the display. This is so that when the window transform (which contains the
9984 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9985 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009986 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009987 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009988 ASSERT_EQ(frames, motionArgs.videoFrames);
9989}
9990
Arthur Hung9da14732019-09-02 16:16:58 +08009991/**
9992 * If we had defined port associations, but the viewport is not ready, the touch device would be
9993 * expected to be disabled, and it should be enabled after the viewport has found.
9994 */
9995TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009996 constexpr uint8_t hdmi2 = 1;
9997 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009998 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009999
10000 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
10001
10002 addConfigurationProperty("touch.deviceType", "touchScreen");
10003 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010004 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +080010005
10006 ASSERT_EQ(mDevice->isEnabled(), false);
10007
10008 // Add display on hdmi2, the device should be enabled and can receive touch event.
10009 prepareSecondaryDisplay(type, hdmi2);
10010 ASSERT_EQ(mDevice->isEnabled(), true);
10011
10012 // Send a touch event.
10013 processPosition(mapper, 100, 100);
10014 processSync(mapper);
10015
10016 NotifyMotionArgs args;
10017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10018 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
10019}
10020
Arthur Hung421eb1c2020-01-16 00:09:42 +080010021TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010022 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010023 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010024 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010025 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010026
10027 NotifyMotionArgs motionArgs;
10028
10029 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10030 // finger down
10031 processId(mapper, 1);
10032 processPosition(mapper, x1, y1);
10033 processSync(mapper);
10034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10035 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010036 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010037
10038 // finger move
10039 processId(mapper, 1);
10040 processPosition(mapper, x2, y2);
10041 processSync(mapper);
10042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010044 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010045
10046 // finger up.
10047 processId(mapper, -1);
10048 processSync(mapper);
10049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10050 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010051 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010052
10053 // new finger down
10054 processId(mapper, 1);
10055 processPosition(mapper, x3, y3);
10056 processSync(mapper);
10057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10058 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010059 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010060}
10061
10062/**
arthurhungcc7f9802020-04-30 17:55:40 +080010063 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
10064 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +080010065 */
arthurhungcc7f9802020-04-30 17:55:40 +080010066TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010067 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010068 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010069 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010070 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010071
10072 NotifyMotionArgs motionArgs;
10073
10074 // default tool type is finger
10075 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +080010076 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010077 processPosition(mapper, x1, y1);
10078 processSync(mapper);
10079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10080 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010081 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010082
10083 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
10084 processToolType(mapper, MT_TOOL_PALM);
10085 processSync(mapper);
10086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10087 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10088
10089 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +080010090 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010091 processPosition(mapper, x2, y2);
10092 processSync(mapper);
10093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10094
10095 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +080010096 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010097 processSync(mapper);
10098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10099
10100 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +080010101 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010102 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010103 processPosition(mapper, x3, y3);
10104 processSync(mapper);
10105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10106 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010107 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010108}
10109
arthurhungbf89a482020-04-17 17:37:55 +080010110/**
arthurhungcc7f9802020-04-30 17:55:40 +080010111 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10112 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +080010113 */
arthurhungcc7f9802020-04-30 17:55:40 +080010114TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +080010115 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010116 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +080010117 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010118 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +080010119
10120 NotifyMotionArgs motionArgs;
10121
10122 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +080010123 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10124 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010125 processPosition(mapper, x1, y1);
10126 processSync(mapper);
10127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10128 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010129 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +080010130
10131 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +080010132 processSlot(mapper, SECOND_SLOT);
10133 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010134 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +080010135 processSync(mapper);
10136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010137 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010138 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010139
10140 // If the tool type of the first finger changes to MT_TOOL_PALM,
10141 // we expect to receive ACTION_POINTER_UP with cancel flag.
10142 processSlot(mapper, FIRST_SLOT);
10143 processId(mapper, FIRST_TRACKING_ID);
10144 processToolType(mapper, MT_TOOL_PALM);
10145 processSync(mapper);
10146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010147 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010148 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10149
10150 // The following MOVE events of second finger should be processed.
10151 processSlot(mapper, SECOND_SLOT);
10152 processId(mapper, SECOND_TRACKING_ID);
10153 processPosition(mapper, x2 + 1, y2 + 1);
10154 processSync(mapper);
10155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010157 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010158
10159 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
10160 // it. Second finger receive move.
10161 processSlot(mapper, FIRST_SLOT);
10162 processId(mapper, INVALID_TRACKING_ID);
10163 processSync(mapper);
10164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010166 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010167
10168 // Second finger keeps moving.
10169 processSlot(mapper, SECOND_SLOT);
10170 processId(mapper, SECOND_TRACKING_ID);
10171 processPosition(mapper, x2 + 2, y2 + 2);
10172 processSync(mapper);
10173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010175 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010176
10177 // Second finger up.
10178 processId(mapper, INVALID_TRACKING_ID);
10179 processSync(mapper);
10180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10181 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10182 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10183}
10184
10185/**
10186 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
10187 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
10188 */
10189TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
10190 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010191 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010192 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010193 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010194
10195 NotifyMotionArgs motionArgs;
10196
10197 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10198 // First finger down.
10199 processId(mapper, FIRST_TRACKING_ID);
10200 processPosition(mapper, x1, y1);
10201 processSync(mapper);
10202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10203 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010204 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010205
10206 // Second finger down.
10207 processSlot(mapper, SECOND_SLOT);
10208 processId(mapper, SECOND_TRACKING_ID);
10209 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +080010210 processSync(mapper);
10211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010212 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010213 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +080010214
arthurhungcc7f9802020-04-30 17:55:40 +080010215 // If the tool type of the first finger changes to MT_TOOL_PALM,
10216 // we expect to receive ACTION_POINTER_UP with cancel flag.
10217 processSlot(mapper, FIRST_SLOT);
10218 processId(mapper, FIRST_TRACKING_ID);
10219 processToolType(mapper, MT_TOOL_PALM);
10220 processSync(mapper);
10221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010222 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010223 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10224
10225 // Second finger keeps moving.
10226 processSlot(mapper, SECOND_SLOT);
10227 processId(mapper, SECOND_TRACKING_ID);
10228 processPosition(mapper, x2 + 1, y2 + 1);
10229 processSync(mapper);
10230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10231 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10232
10233 // second finger becomes palm, receive cancel due to only 1 finger is active.
10234 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010235 processToolType(mapper, MT_TOOL_PALM);
10236 processSync(mapper);
10237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10238 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10239
arthurhungcc7f9802020-04-30 17:55:40 +080010240 // third finger down.
10241 processSlot(mapper, THIRD_SLOT);
10242 processId(mapper, THIRD_TRACKING_ID);
10243 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +080010244 processPosition(mapper, x3, y3);
10245 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +080010246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10247 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010248 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010249 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010250
10251 // third finger move
10252 processId(mapper, THIRD_TRACKING_ID);
10253 processPosition(mapper, x3 + 1, y3 + 1);
10254 processSync(mapper);
10255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10256 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10257
10258 // first finger up, third finger receive move.
10259 processSlot(mapper, FIRST_SLOT);
10260 processId(mapper, INVALID_TRACKING_ID);
10261 processSync(mapper);
10262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10263 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010264 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010265
10266 // second finger up, third finger receive move.
10267 processSlot(mapper, SECOND_SLOT);
10268 processId(mapper, INVALID_TRACKING_ID);
10269 processSync(mapper);
10270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010272 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010273
10274 // third finger up.
10275 processSlot(mapper, THIRD_SLOT);
10276 processId(mapper, INVALID_TRACKING_ID);
10277 processSync(mapper);
10278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10279 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10280 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10281}
10282
10283/**
10284 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10285 * and the active finger could still be allowed to receive the events
10286 */
10287TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10288 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010289 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010290 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010291 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010292
10293 NotifyMotionArgs motionArgs;
10294
10295 // default tool type is finger
10296 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10297 processId(mapper, FIRST_TRACKING_ID);
10298 processPosition(mapper, x1, y1);
10299 processSync(mapper);
10300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10301 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010302 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010303
10304 // Second finger down.
10305 processSlot(mapper, SECOND_SLOT);
10306 processId(mapper, SECOND_TRACKING_ID);
10307 processPosition(mapper, x2, y2);
10308 processSync(mapper);
10309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010310 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010311 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010312
10313 // If the tool type of the second finger changes to MT_TOOL_PALM,
10314 // we expect to receive ACTION_POINTER_UP with cancel flag.
10315 processId(mapper, SECOND_TRACKING_ID);
10316 processToolType(mapper, MT_TOOL_PALM);
10317 processSync(mapper);
10318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010319 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010320 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10321
10322 // The following MOVE event should be processed.
10323 processSlot(mapper, FIRST_SLOT);
10324 processId(mapper, FIRST_TRACKING_ID);
10325 processPosition(mapper, x1 + 1, y1 + 1);
10326 processSync(mapper);
10327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010329 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010330
10331 // second finger up.
10332 processSlot(mapper, SECOND_SLOT);
10333 processId(mapper, INVALID_TRACKING_ID);
10334 processSync(mapper);
10335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10336 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10337
10338 // first finger keep moving
10339 processSlot(mapper, FIRST_SLOT);
10340 processId(mapper, FIRST_TRACKING_ID);
10341 processPosition(mapper, x1 + 2, y1 + 2);
10342 processSync(mapper);
10343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10344 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10345
10346 // first finger up.
10347 processId(mapper, INVALID_TRACKING_ID);
10348 processSync(mapper);
10349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10350 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10351 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010352}
10353
Arthur Hung9ad18942021-06-19 02:04:46 +000010354/**
10355 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10356 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10357 * cause slot be valid again.
10358 */
10359TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10360 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010361 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +000010362 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010363 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +000010364
10365 NotifyMotionArgs motionArgs;
10366
10367 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10368 // First finger down.
10369 processId(mapper, FIRST_TRACKING_ID);
10370 processPosition(mapper, x1, y1);
10371 processPressure(mapper, RAW_PRESSURE_MAX);
10372 processSync(mapper);
10373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10374 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010375 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010376
10377 // First finger move.
10378 processId(mapper, FIRST_TRACKING_ID);
10379 processPosition(mapper, x1 + 1, y1 + 1);
10380 processPressure(mapper, RAW_PRESSURE_MAX);
10381 processSync(mapper);
10382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010384 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010385
10386 // Second finger down.
10387 processSlot(mapper, SECOND_SLOT);
10388 processId(mapper, SECOND_TRACKING_ID);
10389 processPosition(mapper, x2, y2);
10390 processPressure(mapper, RAW_PRESSURE_MAX);
10391 processSync(mapper);
10392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010393 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010394 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010395
10396 // second finger up with some unexpected data.
10397 processSlot(mapper, SECOND_SLOT);
10398 processId(mapper, INVALID_TRACKING_ID);
10399 processPosition(mapper, x2, y2);
10400 processSync(mapper);
10401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010402 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010403 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010404
10405 // first finger up with some unexpected data.
10406 processSlot(mapper, FIRST_SLOT);
10407 processId(mapper, INVALID_TRACKING_ID);
10408 processPosition(mapper, x2, y2);
10409 processPressure(mapper, RAW_PRESSURE_MAX);
10410 processSync(mapper);
10411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10412 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010413 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010414}
10415
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010416TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10417 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010418 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010419 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010420 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010421
10422 // First finger down.
10423 processId(mapper, FIRST_TRACKING_ID);
10424 processPosition(mapper, 100, 200);
10425 processPressure(mapper, RAW_PRESSURE_MAX);
10426 processSync(mapper);
10427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10428 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10429
10430 // Second finger down.
10431 processSlot(mapper, SECOND_SLOT);
10432 processId(mapper, SECOND_TRACKING_ID);
10433 processPosition(mapper, 300, 400);
10434 processPressure(mapper, RAW_PRESSURE_MAX);
10435 processSync(mapper);
10436 ASSERT_NO_FATAL_FAILURE(
10437 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10438
10439 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010440 // preserved. Resetting should cancel the ongoing gesture.
10441 resetMapper(mapper, ARBITRARY_TIME);
10442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10443 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010444
10445 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10446 // the existing touch state to generate a down event.
10447 processPosition(mapper, 301, 302);
10448 processSync(mapper);
10449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10450 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10452 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10453
10454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10455}
10456
10457TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10458 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010459 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010460 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010461 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010462
10463 // First finger touches down and releases.
10464 processId(mapper, FIRST_TRACKING_ID);
10465 processPosition(mapper, 100, 200);
10466 processPressure(mapper, RAW_PRESSURE_MAX);
10467 processSync(mapper);
10468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10469 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10470 processId(mapper, INVALID_TRACKING_ID);
10471 processSync(mapper);
10472 ASSERT_NO_FATAL_FAILURE(
10473 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10474
10475 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10476 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010477 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10479
10480 // Send an empty sync frame. Since there are no pointers, no events are generated.
10481 processSync(mapper);
10482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10483}
10484
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010485TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010486 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010487 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010488 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010489 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010491
10492 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10493 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10494 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10495 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10496 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10497
10498 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010499 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010500 processId(mapper, FIRST_TRACKING_ID);
10501 processToolType(mapper, MT_TOOL_PEN);
10502 processPosition(mapper, 100, 200);
10503 processPressure(mapper, RAW_PRESSURE_MAX);
10504 processSync(mapper);
10505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10506 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10507 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010508 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010509
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010510 // Now that we know the device supports styluses, ensure that the device is re-configured with
10511 // the stylus source.
10512 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10513 {
10514 const auto& devices = mReader->getInputDevices();
10515 auto deviceInfo =
10516 std::find_if(devices.begin(), devices.end(),
10517 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10518 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10519 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10520 }
10521
10522 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10524
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010525 processId(mapper, INVALID_TRACKING_ID);
10526 processSync(mapper);
10527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10528 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10529 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010530 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010531}
10532
Seunghwan Choi356026c2023-02-01 14:37:25 +090010533TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10534 addConfigurationProperty("touch.deviceType", "touchScreen");
10535 prepareDisplay(ui::ROTATION_0);
10536 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10537 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10538 // indicate stylus presence dynamically.
10539 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10540 std::shared_ptr<FakePointerController> fakePointerController =
10541 std::make_shared<FakePointerController>();
10542 mFakePolicy->setPointerController(fakePointerController);
10543 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +000010544 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010545
10546 processId(mapper, FIRST_TRACKING_ID);
10547 processPressure(mapper, RAW_PRESSURE_MIN);
10548 processPosition(mapper, 100, 200);
10549 processToolType(mapper, MT_TOOL_PEN);
10550 processSync(mapper);
10551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10552 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010553 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010554 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10555 ASSERT_TRUE(fakePointerController->isPointerShown());
10556 ASSERT_NO_FATAL_FAILURE(
10557 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10558}
10559
10560TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10561 addConfigurationProperty("touch.deviceType", "touchScreen");
10562 prepareDisplay(ui::ROTATION_0);
10563 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10564 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10565 // indicate stylus presence dynamically.
10566 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10567 std::shared_ptr<FakePointerController> fakePointerController =
10568 std::make_shared<FakePointerController>();
10569 mFakePolicy->setPointerController(fakePointerController);
10570 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010571 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010572
10573 processId(mapper, FIRST_TRACKING_ID);
10574 processPressure(mapper, RAW_PRESSURE_MIN);
10575 processPosition(mapper, 100, 200);
10576 processToolType(mapper, MT_TOOL_PEN);
10577 processSync(mapper);
10578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10579 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010580 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010581 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10582 ASSERT_FALSE(fakePointerController->isPointerShown());
10583}
10584
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010585// --- MultiTouchInputMapperTest_ExternalDevice ---
10586
10587class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10588protected:
Chris Yea52ade12020-08-27 16:49:20 -070010589 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010590};
10591
10592/**
10593 * Expect fallback to internal viewport if device is external and external viewport is not present.
10594 */
10595TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10596 prepareAxes(POSITION);
10597 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010598 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010599 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010600
10601 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10602
10603 NotifyMotionArgs motionArgs;
10604
10605 // Expect the event to be sent to the internal viewport,
10606 // because an external viewport is not present.
10607 processPosition(mapper, 100, 100);
10608 processSync(mapper);
10609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10610 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10611
10612 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010613 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010614 processPosition(mapper, 100, 100);
10615 processSync(mapper);
10616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10617 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10618}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010619
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010620TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10621 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10622 std::shared_ptr<FakePointerController> fakePointerController =
10623 std::make_shared<FakePointerController>();
10624 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10625 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010626
10627 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010628 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010629 prepareAxes(POSITION | ID | SLOT);
10630 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10631 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10632 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010633 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010634 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010635
10636 // captured touchpad should be a touchpad source
10637 NotifyDeviceResetArgs resetArgs;
10638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10639 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10640
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010641 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010642
10643 const InputDeviceInfo::MotionRange* relRangeX =
10644 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10645 ASSERT_NE(relRangeX, nullptr);
10646 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10647 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10648 const InputDeviceInfo::MotionRange* relRangeY =
10649 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10650 ASSERT_NE(relRangeY, nullptr);
10651 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10652 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10653
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010654 // run captured pointer tests - note that this is unscaled, so input listener events should be
10655 // identical to what the hardware sends (accounting for any
10656 // calibration).
10657 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010658 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010659 processId(mapper, 1);
10660 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10661 processKey(mapper, BTN_TOUCH, 1);
10662 processSync(mapper);
10663
10664 // expect coord[0] to contain initial location of touch 0
10665 NotifyMotionArgs args;
10666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10667 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010668 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010669 ASSERT_EQ(0, args.pointerProperties[0].id);
10670 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10671 ASSERT_NO_FATAL_FAILURE(
10672 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10673
10674 // FINGER 1 DOWN
10675 processSlot(mapper, 1);
10676 processId(mapper, 2);
10677 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10678 processSync(mapper);
10679
10680 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010682 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010683 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010684 ASSERT_EQ(0, args.pointerProperties[0].id);
10685 ASSERT_EQ(1, args.pointerProperties[1].id);
10686 ASSERT_NO_FATAL_FAILURE(
10687 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10688 ASSERT_NO_FATAL_FAILURE(
10689 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10690
10691 // FINGER 1 MOVE
10692 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10693 processSync(mapper);
10694
10695 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10696 // from move
10697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10698 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10699 ASSERT_NO_FATAL_FAILURE(
10700 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10701 ASSERT_NO_FATAL_FAILURE(
10702 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10703
10704 // FINGER 0 MOVE
10705 processSlot(mapper, 0);
10706 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10707 processSync(mapper);
10708
10709 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10711 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10712 ASSERT_NO_FATAL_FAILURE(
10713 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10714 ASSERT_NO_FATAL_FAILURE(
10715 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10716
10717 // BUTTON DOWN
10718 processKey(mapper, BTN_LEFT, 1);
10719 processSync(mapper);
10720
10721 // touchinputmapper design sends a move before button press
10722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10723 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10725 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10726
10727 // BUTTON UP
10728 processKey(mapper, BTN_LEFT, 0);
10729 processSync(mapper);
10730
10731 // touchinputmapper design sends a move after button release
10732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10733 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10736
10737 // FINGER 0 UP
10738 processId(mapper, -1);
10739 processSync(mapper);
10740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10741 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10742
10743 // FINGER 1 MOVE
10744 processSlot(mapper, 1);
10745 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10746 processSync(mapper);
10747
10748 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10750 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010751 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010752 ASSERT_EQ(1, args.pointerProperties[0].id);
10753 ASSERT_NO_FATAL_FAILURE(
10754 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10755
10756 // FINGER 1 UP
10757 processId(mapper, -1);
10758 processKey(mapper, BTN_TOUCH, 0);
10759 processSync(mapper);
10760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10761 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10762
Josep del Río2d8c79a2023-01-23 19:33:50 +000010763 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010764 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010765 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010767 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010768}
10769
10770TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10771 std::shared_ptr<FakePointerController> fakePointerController =
10772 std::make_shared<FakePointerController>();
10773 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10774 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010775
10776 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010777 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010778 prepareAxes(POSITION | ID | SLOT);
10779 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10780 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010781 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010782 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010783 // run uncaptured pointer tests - pushes out generic events
10784 // FINGER 0 DOWN
10785 processId(mapper, 3);
10786 processPosition(mapper, 100, 100);
10787 processKey(mapper, BTN_TOUCH, 1);
10788 processSync(mapper);
10789
10790 // start at (100,100), cursor should be at (0,0) * scale
10791 NotifyMotionArgs args;
10792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10793 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10794 ASSERT_NO_FATAL_FAILURE(
10795 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10796
10797 // FINGER 0 MOVE
10798 processPosition(mapper, 200, 200);
10799 processSync(mapper);
10800
10801 // compute scaling to help with touch position checking
10802 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10803 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10804 float scale =
10805 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10806
10807 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10809 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10811 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010812
10813 // BUTTON DOWN
10814 processKey(mapper, BTN_LEFT, 1);
10815 processSync(mapper);
10816
10817 // touchinputmapper design sends a move before button press
10818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10819 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10821 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10822
10823 // BUTTON UP
10824 processKey(mapper, BTN_LEFT, 0);
10825 processSync(mapper);
10826
10827 // touchinputmapper design sends a move after button release
10828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10829 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10831 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010832}
10833
10834TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10835 std::shared_ptr<FakePointerController> fakePointerController =
10836 std::make_shared<FakePointerController>();
10837
Michael Wrighta9cf4192022-12-01 23:46:39 +000010838 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010839 prepareAxes(POSITION | ID | SLOT);
10840 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010841 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010842 mFakePolicy->setPointerCapture(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010843 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010844
Josep del Río2d8c79a2023-01-23 19:33:50 +000010845 // uncaptured touchpad should be a pointer device
10846 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010847
Josep del Río2d8c79a2023-01-23 19:33:50 +000010848 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010849 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010850 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010851 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10852}
10853
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010854// --- BluetoothMultiTouchInputMapperTest ---
10855
10856class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10857protected:
10858 void SetUp() override {
10859 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10860 }
10861};
10862
10863TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10864 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010865 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010866 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010867 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010868
10869 nsecs_t kernelEventTime = ARBITRARY_TIME;
10870 nsecs_t expectedEventTime = ARBITRARY_TIME;
10871 // Touch down.
10872 processId(mapper, FIRST_TRACKING_ID);
10873 processPosition(mapper, 100, 200);
10874 processPressure(mapper, RAW_PRESSURE_MAX);
10875 processSync(mapper, ARBITRARY_TIME);
10876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10877 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10878
10879 // Process several events that come in quick succession, according to their timestamps.
10880 for (int i = 0; i < 3; i++) {
10881 constexpr static nsecs_t delta = ms2ns(1);
10882 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10883 kernelEventTime += delta;
10884 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10885
10886 processPosition(mapper, 101 + i, 201 + i);
10887 processSync(mapper, kernelEventTime);
10888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10889 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10890 WithEventTime(expectedEventTime))));
10891 }
10892
10893 // Release the touch.
10894 processId(mapper, INVALID_TRACKING_ID);
10895 processPressure(mapper, RAW_PRESSURE_MIN);
10896 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10898 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10899 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10900}
10901
10902// --- MultiTouchPointerModeTest ---
10903
HQ Liue6983c72022-04-19 22:14:56 +000010904class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10905protected:
10906 float mPointerMovementScale;
10907 float mPointerXZoomScale;
10908 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10909 addConfigurationProperty("touch.deviceType", "pointer");
10910 std::shared_ptr<FakePointerController> fakePointerController =
10911 std::make_shared<FakePointerController>();
10912 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10913 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010914 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010915
10916 prepareAxes(POSITION);
10917 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10918 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10919 // needs to be disabled, and the pointer gesture needs to be enabled.
10920 mFakePolicy->setPointerCapture(false);
10921 mFakePolicy->setPointerGestureEnabled(true);
10922 mFakePolicy->setPointerController(fakePointerController);
10923
10924 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10925 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10926 mPointerMovementScale =
10927 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10928 mPointerXZoomScale =
10929 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10930 }
10931
10932 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10933 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10934 /*flat*/ 0,
10935 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10936 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10937 /*flat*/ 0,
10938 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10939 }
10940};
10941
10942/**
10943 * Two fingers down on a pointer mode touch pad. The width
10944 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10945 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10946 * be greater than the both value to be freeform gesture, so that after two
10947 * fingers start to move downwards, the gesture should be swipe.
10948 */
10949TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10950 // The min freeform gesture width is 25units/mm x 30mm = 750
10951 // which is greater than fraction of the diagnal length of the touchpad (349).
10952 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010953 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010954 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010955 NotifyMotionArgs motionArgs;
10956
10957 // Two fingers down at once.
10958 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10959 // Pointer's initial position is used the [0,0] coordinate.
10960 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10961
10962 processId(mapper, FIRST_TRACKING_ID);
10963 processPosition(mapper, x1, y1);
10964 processMTSync(mapper);
10965 processId(mapper, SECOND_TRACKING_ID);
10966 processPosition(mapper, x2, y2);
10967 processMTSync(mapper);
10968 processSync(mapper);
10969
10970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010971 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010972 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010973 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010974 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010975 ASSERT_NO_FATAL_FAILURE(
10976 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10977
10978 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10979 // that there should be 1 pointer.
10980 int32_t movingDistance = 200;
10981 y1 += movingDistance;
10982 y2 += movingDistance;
10983
10984 processId(mapper, FIRST_TRACKING_ID);
10985 processPosition(mapper, x1, y1);
10986 processMTSync(mapper);
10987 processId(mapper, SECOND_TRACKING_ID);
10988 processPosition(mapper, x2, y2);
10989 processMTSync(mapper);
10990 processSync(mapper);
10991
10992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010993 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010994 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010995 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010996 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010997 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10998 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10999 0, 0, 0, 0));
11000}
11001
11002/**
11003 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
11004 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
11005 * the touch pack diagnal length. Two fingers' distance must be greater than the both
11006 * value to be freeform gesture, so that after two fingers start to move downwards,
11007 * the gesture should be swipe.
11008 */
11009TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
11010 // The min freeform gesture width is 5units/mm x 30mm = 150
11011 // which is greater than fraction of the diagnal length of the touchpad (349).
11012 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000011013 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +000011014 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000011015 NotifyMotionArgs motionArgs;
11016
11017 // Two fingers down at once.
11018 // The two fingers are 250 units apart, expects the current gesture to be PRESS
11019 // Pointer's initial position is used the [0,0] coordinate.
11020 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
11021
11022 processId(mapper, FIRST_TRACKING_ID);
11023 processPosition(mapper, x1, y1);
11024 processMTSync(mapper);
11025 processId(mapper, SECOND_TRACKING_ID);
11026 processPosition(mapper, x2, y2);
11027 processMTSync(mapper);
11028 processSync(mapper);
11029
11030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011031 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011032 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011033 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011034 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011035 ASSERT_NO_FATAL_FAILURE(
11036 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11037
11038 // It should be recognized as a SWIPE gesture when two fingers start to move down,
11039 // and there should be 1 pointer.
11040 int32_t movingDistance = 200;
11041 y1 += movingDistance;
11042 y2 += movingDistance;
11043
11044 processId(mapper, FIRST_TRACKING_ID);
11045 processPosition(mapper, x1, y1);
11046 processMTSync(mapper);
11047 processId(mapper, SECOND_TRACKING_ID);
11048 processPosition(mapper, x2, y2);
11049 processMTSync(mapper);
11050 processSync(mapper);
11051
11052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011053 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011054 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011055 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011056 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011057 // New coordinate is the scaled relative coordinate from the initial coordinate.
11058 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
11059 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11060 0, 0, 0, 0));
11061}
11062
11063/**
11064 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
11065 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
11066 * freeform gestures after two fingers start to move downwards.
11067 */
11068TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000011069 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000011070 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000011071
11072 NotifyMotionArgs motionArgs;
11073
11074 // Two fingers down at once. Wider than the max swipe width.
11075 // The gesture is expected to be PRESS, then transformed to FREEFORM
11076 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
11077
11078 processId(mapper, FIRST_TRACKING_ID);
11079 processPosition(mapper, x1, y1);
11080 processMTSync(mapper);
11081 processId(mapper, SECOND_TRACKING_ID);
11082 processPosition(mapper, x2, y2);
11083 processMTSync(mapper);
11084 processSync(mapper);
11085
11086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011087 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011088 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011089 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011090 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011091 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
11092 ASSERT_NO_FATAL_FAILURE(
11093 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11094
11095 int32_t movingDistance = 200;
11096
11097 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
11098 // then two down events for two pointers.
11099 y1 += movingDistance;
11100 y2 += movingDistance;
11101
11102 processId(mapper, FIRST_TRACKING_ID);
11103 processPosition(mapper, x1, y1);
11104 processMTSync(mapper);
11105 processId(mapper, SECOND_TRACKING_ID);
11106 processPosition(mapper, x2, y2);
11107 processMTSync(mapper);
11108 processSync(mapper);
11109
11110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11111 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011112 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011113 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
11114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011115 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011116 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011117 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011119 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011120 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011121 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011122 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011123 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011124 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011125 // Two pointers' scaled relative coordinates from their initial centroid.
11126 // Initial y coordinates are 0 as y1 and y2 have the same value.
11127 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
11128 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
11129 // When pointers move, the new coordinates equal to the initial coordinates plus
11130 // scaled moving distance.
11131 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11132 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11133 0, 0, 0, 0));
11134 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11135 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11136 0, 0, 0, 0));
11137
11138 // Move two fingers down again, expect one MOVE motion event.
11139 y1 += movingDistance;
11140 y2 += movingDistance;
11141
11142 processId(mapper, FIRST_TRACKING_ID);
11143 processPosition(mapper, x1, y1);
11144 processMTSync(mapper);
11145 processId(mapper, SECOND_TRACKING_ID);
11146 processPosition(mapper, x2, y2);
11147 processMTSync(mapper);
11148 processSync(mapper);
11149
11150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011151 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011153 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011154 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011155 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11156 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11157 0, 0, 0, 0, 0));
11158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11159 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11160 0, 0, 0, 0, 0));
11161}
11162
Harry Cutts39b7ca22022-10-05 15:55:48 +000011163TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000011164 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000011165 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000011166 NotifyMotionArgs motionArgs;
11167
11168 // Place two fingers down.
11169 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
11170
11171 processId(mapper, FIRST_TRACKING_ID);
11172 processPosition(mapper, x1, y1);
11173 processMTSync(mapper);
11174 processId(mapper, SECOND_TRACKING_ID);
11175 processPosition(mapper, x2, y2);
11176 processMTSync(mapper);
11177 processSync(mapper);
11178
11179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011180 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000011181 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11182 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
11183 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
11184 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
11185
11186 // Move the two fingers down and to the left.
11187 int32_t movingDistance = 200;
11188 x1 -= movingDistance;
11189 y1 += movingDistance;
11190 x2 -= movingDistance;
11191 y2 += movingDistance;
11192
11193 processId(mapper, FIRST_TRACKING_ID);
11194 processPosition(mapper, x1, y1);
11195 processMTSync(mapper);
11196 processId(mapper, SECOND_TRACKING_ID);
11197 processPosition(mapper, x2, y2);
11198 processMTSync(mapper);
11199 processSync(mapper);
11200
11201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011202 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000011203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11204 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
11205 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
11206 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
11207}
11208
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011209TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000011210 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011211 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000011212 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
11214
11215 // Start a stylus gesture.
11216 processKey(mapper, BTN_TOOL_PEN, 1);
11217 processId(mapper, FIRST_TRACKING_ID);
11218 processPosition(mapper, 100, 200);
11219 processSync(mapper);
11220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11221 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
11222 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011223 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011224 // TODO(b/257078296): Pointer mode generates extra event.
11225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11226 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11227 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011228 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11230
11231 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
11232 // gesture should be disabled.
11233 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
11234 viewport->isActive = false;
11235 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000011236 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11238 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11239 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011240 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011241 // TODO(b/257078296): Pointer mode generates extra event.
11242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11243 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11244 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011245 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11247}
11248
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011249// --- JoystickInputMapperTest ---
11250
11251class JoystickInputMapperTest : public InputMapperTest {
11252protected:
11253 static const int32_t RAW_X_MIN;
11254 static const int32_t RAW_X_MAX;
11255 static const int32_t RAW_Y_MIN;
11256 static const int32_t RAW_Y_MAX;
11257
11258 void SetUp() override {
11259 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
11260 }
11261 void prepareAxes() {
11262 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
11263 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
11264 }
11265
11266 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
11267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
11268 }
11269
11270 void processSync(JoystickInputMapper& mapper) {
11271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
11272 }
11273
Michael Wrighta9cf4192022-12-01 23:46:39 +000011274 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011275 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
11276 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
11277 NO_PORT, ViewportType::VIRTUAL);
11278 }
11279};
11280
11281const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
11282const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
11283const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11284const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11285
11286TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11287 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000011288 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011289
11290 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11291
Michael Wrighta9cf4192022-12-01 23:46:39 +000011292 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011293
11294 // Send an axis event
11295 processAxis(mapper, ABS_X, 100);
11296 processSync(mapper);
11297
11298 NotifyMotionArgs args;
11299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11300 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11301
11302 // Send another axis event
11303 processAxis(mapper, ABS_Y, 100);
11304 processSync(mapper);
11305
11306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11307 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11308}
11309
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011310// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011311
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011312class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011313protected:
11314 static const char* DEVICE_NAME;
11315 static const char* DEVICE_LOCATION;
11316 static const int32_t DEVICE_ID;
11317 static const int32_t DEVICE_GENERATION;
11318 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011319 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011320 static const int32_t EVENTHUB_ID;
11321
11322 std::shared_ptr<FakeEventHub> mFakeEventHub;
11323 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011324 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011325 std::unique_ptr<InstrumentedInputReader> mReader;
11326 std::shared_ptr<InputDevice> mDevice;
11327
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011328 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011329 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011330 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011331 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011332 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011333 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011334 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11335 }
11336
11337 void SetUp() override { SetUp(DEVICE_CLASSES); }
11338
11339 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011340 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011341 mFakePolicy.clear();
11342 }
11343
Chris Yee2b1e5c2021-03-10 22:45:12 -080011344 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11345 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011346 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011347 InputDeviceIdentifier identifier;
11348 identifier.name = name;
11349 identifier.location = location;
11350 std::shared_ptr<InputDevice> device =
11351 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11352 identifier);
11353 mReader->pushNextDevice(device);
11354 mFakeEventHub->addDevice(eventHubId, name, classes);
11355 mReader->loopOnce();
11356 return device;
11357 }
11358
11359 template <class T, typename... Args>
11360 T& addControllerAndConfigure(Args... args) {
11361 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11362
11363 return controller;
11364 }
11365};
11366
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011367const char* PeripheralControllerTest::DEVICE_NAME = "device";
11368const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11369const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11370const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11371const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011372const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11373 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011374const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011375
11376// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011377class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011378protected:
11379 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011380 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011381 }
11382};
11383
11384TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011385 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011386
Harry Cuttsa5b71292022-11-28 12:56:17 +000011387 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11388 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11389 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011390}
11391
11392TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011393 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011394
Harry Cuttsa5b71292022-11-28 12:56:17 +000011395 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11396 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11397 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011398}
11399
11400// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011401class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011402protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011403 void SetUp() override {
11404 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11405 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011406};
11407
Chris Ye85758332021-05-16 23:05:17 -070011408TEST_F(LightControllerTest, MonoLight) {
11409 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011410 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011411 .maxBrightness = 255,
11412 .flags = InputLightClass::BRIGHTNESS,
11413 .path = ""};
11414 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011415
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011416 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011417 InputDeviceInfo info;
11418 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011419 std::vector<InputDeviceLightInfo> lights = info.getLights();
11420 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011421 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11422 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11423
11424 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11425 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11426}
11427
11428TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11429 RawLightInfo infoMono = {.id = 1,
11430 .name = "mono_keyboard_backlight",
11431 .maxBrightness = 255,
11432 .flags = InputLightClass::BRIGHTNESS |
11433 InputLightClass::KEYBOARD_BACKLIGHT,
11434 .path = ""};
11435 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11436
11437 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11438 InputDeviceInfo info;
11439 controller.populateDeviceInfo(&info);
11440 std::vector<InputDeviceLightInfo> lights = info.getLights();
11441 ASSERT_EQ(1U, lights.size());
11442 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11443 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011444
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011445 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11446 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011447}
11448
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000011449TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
11450 RawLightInfo infoMono = {.id = 1,
11451 .name = "mono_light",
11452 .maxBrightness = 255,
11453 .flags = InputLightClass::BRIGHTNESS,
11454 .path = ""};
11455 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11456 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11457 "0,100,200");
11458
11459 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11460 std::list<NotifyArgs> unused =
11461 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11462 /*changes=*/{});
11463
11464 InputDeviceInfo info;
11465 controller.populateDeviceInfo(&info);
11466 std::vector<InputDeviceLightInfo> lights = info.getLights();
11467 ASSERT_EQ(1U, lights.size());
11468 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11469}
11470
11471TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
11472 RawLightInfo infoMono = {.id = 1,
11473 .name = "mono_keyboard_backlight",
11474 .maxBrightness = 255,
11475 .flags = InputLightClass::BRIGHTNESS |
11476 InputLightClass::KEYBOARD_BACKLIGHT,
11477 .path = ""};
11478 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11479
11480 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11481 std::list<NotifyArgs> unused =
11482 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11483 /*changes=*/{});
11484
11485 InputDeviceInfo info;
11486 controller.populateDeviceInfo(&info);
11487 std::vector<InputDeviceLightInfo> lights = info.getLights();
11488 ASSERT_EQ(1U, lights.size());
11489 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11490}
11491
11492TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
11493 RawLightInfo infoMono = {.id = 1,
11494 .name = "mono_keyboard_backlight",
11495 .maxBrightness = 255,
11496 .flags = InputLightClass::BRIGHTNESS |
11497 InputLightClass::KEYBOARD_BACKLIGHT,
11498 .path = ""};
11499 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11500 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11501 "0,100,200");
11502
11503 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11504 std::list<NotifyArgs> unused =
11505 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11506 /*changes=*/{});
11507
11508 InputDeviceInfo info;
11509 controller.populateDeviceInfo(&info);
11510 std::vector<InputDeviceLightInfo> lights = info.getLights();
11511 ASSERT_EQ(1U, lights.size());
11512 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
11513 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
11514 ASSERT_EQ(BrightnessLevel(0), *it);
11515 std::advance(it, 1);
11516 ASSERT_EQ(BrightnessLevel(100), *it);
11517 std::advance(it, 1);
11518 ASSERT_EQ(BrightnessLevel(200), *it);
11519}
11520
11521TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
11522 RawLightInfo infoMono = {.id = 1,
11523 .name = "mono_keyboard_backlight",
11524 .maxBrightness = 255,
11525 .flags = InputLightClass::BRIGHTNESS |
11526 InputLightClass::KEYBOARD_BACKLIGHT,
11527 .path = ""};
11528 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11529 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11530 "0,100,200,300,400,500");
11531
11532 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11533 std::list<NotifyArgs> unused =
11534 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11535 /*changes=*/{});
11536
11537 InputDeviceInfo info;
11538 controller.populateDeviceInfo(&info);
11539 std::vector<InputDeviceLightInfo> lights = info.getLights();
11540 ASSERT_EQ(1U, lights.size());
11541 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11542}
11543
Chris Yee2b1e5c2021-03-10 22:45:12 -080011544TEST_F(LightControllerTest, RGBLight) {
11545 RawLightInfo infoRed = {.id = 1,
11546 .name = "red",
11547 .maxBrightness = 255,
11548 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11549 .path = ""};
11550 RawLightInfo infoGreen = {.id = 2,
11551 .name = "green",
11552 .maxBrightness = 255,
11553 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11554 .path = ""};
11555 RawLightInfo infoBlue = {.id = 3,
11556 .name = "blue",
11557 .maxBrightness = 255,
11558 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11559 .path = ""};
11560 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11561 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11562 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11563
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011564 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011565 InputDeviceInfo info;
11566 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011567 std::vector<InputDeviceLightInfo> lights = info.getLights();
11568 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011569 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11570 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11571 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11572
11573 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11574 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11575}
11576
11577TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11578 RawLightInfo infoRed = {.id = 1,
11579 .name = "red_keyboard_backlight",
11580 .maxBrightness = 255,
11581 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11582 InputLightClass::KEYBOARD_BACKLIGHT,
11583 .path = ""};
11584 RawLightInfo infoGreen = {.id = 2,
11585 .name = "green_keyboard_backlight",
11586 .maxBrightness = 255,
11587 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11588 InputLightClass::KEYBOARD_BACKLIGHT,
11589 .path = ""};
11590 RawLightInfo infoBlue = {.id = 3,
11591 .name = "blue_keyboard_backlight",
11592 .maxBrightness = 255,
11593 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11594 InputLightClass::KEYBOARD_BACKLIGHT,
11595 .path = ""};
11596 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11597 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11598 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11599
11600 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11601 InputDeviceInfo info;
11602 controller.populateDeviceInfo(&info);
11603 std::vector<InputDeviceLightInfo> lights = info.getLights();
11604 ASSERT_EQ(1U, lights.size());
11605 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11606 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11607 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11608
11609 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11610 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11611}
11612
11613TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11614 RawLightInfo infoRed = {.id = 1,
11615 .name = "red",
11616 .maxBrightness = 255,
11617 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11618 .path = ""};
11619 RawLightInfo infoGreen = {.id = 2,
11620 .name = "green",
11621 .maxBrightness = 255,
11622 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11623 .path = ""};
11624 RawLightInfo infoBlue = {.id = 3,
11625 .name = "blue",
11626 .maxBrightness = 255,
11627 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11628 .path = ""};
11629 RawLightInfo infoGlobal = {.id = 3,
11630 .name = "global_keyboard_backlight",
11631 .maxBrightness = 255,
11632 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11633 InputLightClass::KEYBOARD_BACKLIGHT,
11634 .path = ""};
11635 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11636 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11637 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11638 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11639
11640 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11641 InputDeviceInfo info;
11642 controller.populateDeviceInfo(&info);
11643 std::vector<InputDeviceLightInfo> lights = info.getLights();
11644 ASSERT_EQ(1U, lights.size());
11645 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11646 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11647 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011648
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011649 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11650 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011651}
11652
11653TEST_F(LightControllerTest, MultiColorRGBLight) {
11654 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011655 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011656 .maxBrightness = 255,
11657 .flags = InputLightClass::BRIGHTNESS |
11658 InputLightClass::MULTI_INTENSITY |
11659 InputLightClass::MULTI_INDEX,
11660 .path = ""};
11661
11662 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11663
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011664 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011665 InputDeviceInfo info;
11666 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011667 std::vector<InputDeviceLightInfo> lights = info.getLights();
11668 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011669 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11670 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11671 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11672
11673 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11674 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11675}
11676
11677TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11678 RawLightInfo infoColor = {.id = 1,
11679 .name = "multi_color_keyboard_backlight",
11680 .maxBrightness = 255,
11681 .flags = InputLightClass::BRIGHTNESS |
11682 InputLightClass::MULTI_INTENSITY |
11683 InputLightClass::MULTI_INDEX |
11684 InputLightClass::KEYBOARD_BACKLIGHT,
11685 .path = ""};
11686
11687 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11688
11689 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11690 InputDeviceInfo info;
11691 controller.populateDeviceInfo(&info);
11692 std::vector<InputDeviceLightInfo> lights = info.getLights();
11693 ASSERT_EQ(1U, lights.size());
11694 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11695 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11696 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011697
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011698 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11699 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011700}
11701
11702TEST_F(LightControllerTest, PlayerIdLight) {
11703 RawLightInfo info1 = {.id = 1,
11704 .name = "player1",
11705 .maxBrightness = 255,
11706 .flags = InputLightClass::BRIGHTNESS,
11707 .path = ""};
11708 RawLightInfo info2 = {.id = 2,
11709 .name = "player2",
11710 .maxBrightness = 255,
11711 .flags = InputLightClass::BRIGHTNESS,
11712 .path = ""};
11713 RawLightInfo info3 = {.id = 3,
11714 .name = "player3",
11715 .maxBrightness = 255,
11716 .flags = InputLightClass::BRIGHTNESS,
11717 .path = ""};
11718 RawLightInfo info4 = {.id = 4,
11719 .name = "player4",
11720 .maxBrightness = 255,
11721 .flags = InputLightClass::BRIGHTNESS,
11722 .path = ""};
11723 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11724 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11725 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11726 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11727
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011728 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011729 InputDeviceInfo info;
11730 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011731 std::vector<InputDeviceLightInfo> lights = info.getLights();
11732 ASSERT_EQ(1U, lights.size());
11733 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011734 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11735 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011736
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011737 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11738 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11739 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011740}
11741
Michael Wrightd02c5b62014-02-10 15:10:22 -080011742} // namespace android