blob: 64ae9e8b141ffbfe121f63651d360deba4937c57 [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>
Prabir Pradhane3b28dd2023-10-06 04:19:29 +000034#include <TestEventMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070035#include <TestInputListener.h>
36#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
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004747/**
4748 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4749 * pointer acceleration or speed processing should not be applied.
4750 */
4751TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4752 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004753 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4754 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004755 mFakePolicy->setVelocityControlParams(testParams);
Arpit Singhe036ad72023-04-27 12:48:15 +00004756 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004757
4758 NotifyDeviceResetArgs resetArgs;
4759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4760 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4761 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4762
4763 NotifyMotionArgs args;
4764
4765 // Move and verify scale is applied.
4766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4770 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4771 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4772 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4773 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4774 ASSERT_GT(relX, 10);
4775 ASSERT_GT(relY, 20);
4776
4777 // Enable Pointer Capture
4778 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004779 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004780 NotifyPointerCaptureChangedArgs captureArgs;
4781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4782 ASSERT_TRUE(captureArgs.request.enable);
4783
4784 // Move and verify scale is not applied.
4785 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4789 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4790 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4791 ASSERT_EQ(10, args.pointerCoords[0].getX());
4792 ASSERT_EQ(20, args.pointerCoords[0].getY());
4793}
4794
Prabir Pradhan208360b2022-06-24 18:37:04 +00004795TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4796 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004797 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan208360b2022-06-24 18:37:04 +00004798
4799 NotifyDeviceResetArgs resetArgs;
4800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4801 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4802 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4803
4804 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004805 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004806
4807 NotifyMotionArgs args;
4808
4809 // Verify that the coordinates are rotated.
4810 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4811 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4812 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4814 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4815 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4816 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4817 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4818
4819 // Enable Pointer Capture.
4820 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004821 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004822 NotifyPointerCaptureChangedArgs captureArgs;
4823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4824 ASSERT_TRUE(captureArgs.request.enable);
4825
4826 // Move and verify rotation is not applied.
4827 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4831 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4832 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4833 ASSERT_EQ(10, args.pointerCoords[0].getX());
4834 ASSERT_EQ(20, args.pointerCoords[0].getY());
4835}
4836
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004837TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004838 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004839
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004840 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004841 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004842
4843 // Set up the secondary display as the display on which the pointer should be shown.
4844 // The InputDevice is not associated with any display.
4845 prepareSecondaryDisplay();
4846 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004847 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004848
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004849 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004850 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004851
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004852 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004853 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4854 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004857 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4858 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4859 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004860 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004861}
4862
4863TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004864 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004865
4866 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004867 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004868
4869 // Set up the secondary display as the display on which the pointer should be shown,
4870 // and associate the InputDevice with the secondary display.
4871 prepareSecondaryDisplay();
4872 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4873 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004874 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004875
4876 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4877 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004878
4879 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4881 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004883 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4884 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4885 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004886 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004887}
4888
4889TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004890 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004891
4892 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004893 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004894 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4895
4896 // Associate the InputDevice with the secondary display.
4897 prepareSecondaryDisplay();
4898 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004899 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004900
4901 // The mapper should not generate any events because it is associated with a display that is
4902 // different from the pointer display.
4903 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4904 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4905 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004907}
4908
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004909// --- BluetoothCursorInputMapperTest ---
4910
4911class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4912protected:
4913 void SetUp() override {
4914 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4915
4916 mFakePointerController = std::make_shared<FakePointerController>();
4917 mFakePolicy->setPointerController(mFakePointerController);
4918 }
4919};
4920
4921TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4922 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004923 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004924
4925 nsecs_t kernelEventTime = ARBITRARY_TIME;
4926 nsecs_t expectedEventTime = ARBITRARY_TIME;
4927 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4928 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4930 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4931 WithEventTime(expectedEventTime))));
4932
4933 // Process several events that come in quick succession, according to their timestamps.
4934 for (int i = 0; i < 3; i++) {
4935 constexpr static nsecs_t delta = ms2ns(1);
4936 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4937 kernelEventTime += delta;
4938 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4939
4940 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4941 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4943 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4944 WithEventTime(expectedEventTime))));
4945 }
4946}
4947
4948TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4949 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004950 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004951
4952 nsecs_t expectedEventTime = ARBITRARY_TIME;
4953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4954 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4956 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4957 WithEventTime(expectedEventTime))));
4958
4959 // Process several events with the same timestamp from the kernel.
4960 // Ensure that we do not generate events too far into the future.
4961 constexpr static int32_t numEvents =
4962 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4963 for (int i = 0; i < numEvents; i++) {
4964 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4965
4966 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4967 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4969 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4970 WithEventTime(expectedEventTime))));
4971 }
4972
4973 // By processing more events with the same timestamp, we should not generate events with a
4974 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4975 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4976 for (int i = 0; i < 3; i++) {
4977 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4978 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4980 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4981 WithEventTime(cappedEventTime))));
4982 }
4983}
4984
4985TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4986 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004987 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004988
4989 nsecs_t kernelEventTime = ARBITRARY_TIME;
4990 nsecs_t expectedEventTime = ARBITRARY_TIME;
4991 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4992 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4994 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4995 WithEventTime(expectedEventTime))));
4996
4997 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4998 // smoothening is not needed, its timestamp is not affected.
4999 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
5000 expectedEventTime = kernelEventTime;
5001
5002 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5003 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5005 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5006 WithEventTime(expectedEventTime))));
5007}
5008
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009// --- TouchInputMapperTest ---
5010
5011class TouchInputMapperTest : public InputMapperTest {
5012protected:
5013 static const int32_t RAW_X_MIN;
5014 static const int32_t RAW_X_MAX;
5015 static const int32_t RAW_Y_MIN;
5016 static const int32_t RAW_Y_MAX;
5017 static const int32_t RAW_TOUCH_MIN;
5018 static const int32_t RAW_TOUCH_MAX;
5019 static const int32_t RAW_TOOL_MIN;
5020 static const int32_t RAW_TOOL_MAX;
5021 static const int32_t RAW_PRESSURE_MIN;
5022 static const int32_t RAW_PRESSURE_MAX;
5023 static const int32_t RAW_ORIENTATION_MIN;
5024 static const int32_t RAW_ORIENTATION_MAX;
5025 static const int32_t RAW_DISTANCE_MIN;
5026 static const int32_t RAW_DISTANCE_MAX;
5027 static const int32_t RAW_TILT_MIN;
5028 static const int32_t RAW_TILT_MAX;
5029 static const int32_t RAW_ID_MIN;
5030 static const int32_t RAW_ID_MAX;
5031 static const int32_t RAW_SLOT_MIN;
5032 static const int32_t RAW_SLOT_MAX;
5033 static const float X_PRECISION;
5034 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005035 static const float X_PRECISION_VIRTUAL;
5036 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005037
5038 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005039 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005040
5041 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5042
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005043 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005044 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005045
Michael Wrightd02c5b62014-02-10 15:10:22 -08005046 enum Axes {
5047 POSITION = 1 << 0,
5048 TOUCH = 1 << 1,
5049 TOOL = 1 << 2,
5050 PRESSURE = 1 << 3,
5051 ORIENTATION = 1 << 4,
5052 MINOR = 1 << 5,
5053 ID = 1 << 6,
5054 DISTANCE = 1 << 7,
5055 TILT = 1 << 8,
5056 SLOT = 1 << 9,
5057 TOOL_TYPE = 1 << 10,
5058 };
5059
Michael Wrighta9cf4192022-12-01 23:46:39 +00005060 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005061 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00005062 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005063 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005064 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005065 int32_t toRawX(float displayX);
5066 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005067 int32_t toRotatedRawX(float displayX);
5068 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005069 float toCookedX(float rawX, float rawY);
5070 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005071 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005072 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005073 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005074 float toDisplayY(int32_t rawY, int32_t displayHeight);
5075
Michael Wrightd02c5b62014-02-10 15:10:22 -08005076};
5077
5078const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5079const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5080const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5081const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5082const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5083const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5084const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5085const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005086const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5087const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005088const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5089const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5090const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5091const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5092const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5093const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5094const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5095const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5096const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5097const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5098const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5099const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005100const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5101 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5102const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5103 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005104const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5105 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005106
5107const float TouchInputMapperTest::GEOMETRIC_SCALE =
5108 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5109 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5110
5111const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5112 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5113 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5114};
5115
Michael Wrighta9cf4192022-12-01 23:46:39 +00005116void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005117 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5118 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005119}
5120
5121void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5122 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00005123 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005124}
5125
Michael Wrighta9cf4192022-12-01 23:46:39 +00005126void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005127 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5128 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5129 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005130}
5131
Michael Wrightd02c5b62014-02-10 15:10:22 -08005132void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005133 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5134 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5135 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5136 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005137}
5138
Jason Gerecke489fda82012-09-07 17:19:40 -07005139void TouchInputMapperTest::prepareLocationCalibration() {
5140 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5141}
5142
Michael Wrightd02c5b62014-02-10 15:10:22 -08005143int32_t TouchInputMapperTest::toRawX(float displayX) {
5144 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5145}
5146
5147int32_t TouchInputMapperTest::toRawY(float displayY) {
5148 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5149}
5150
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005151int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5152 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5153}
5154
5155int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5156 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5157}
5158
Jason Gerecke489fda82012-09-07 17:19:40 -07005159float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5160 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5161 return rawX;
5162}
5163
5164float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5165 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5166 return rawY;
5167}
5168
Michael Wrightd02c5b62014-02-10 15:10:22 -08005169float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005170 return toDisplayX(rawX, DISPLAY_WIDTH);
5171}
5172
5173float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5174 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005175}
5176
5177float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005178 return toDisplayY(rawY, DISPLAY_HEIGHT);
5179}
5180
5181float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5182 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005183}
5184
5185
5186// --- SingleTouchInputMapperTest ---
5187
5188class SingleTouchInputMapperTest : public TouchInputMapperTest {
5189protected:
5190 void prepareButtons();
5191 void prepareAxes(int axes);
5192
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005193 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5194 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5195 void processUp(SingleTouchInputMapper& mappery);
5196 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5197 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5198 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5199 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5200 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5201 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005202};
5203
5204void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005205 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005206}
5207
5208void SingleTouchInputMapperTest::prepareAxes(int axes) {
5209 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005210 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5211 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005212 }
5213 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005214 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5215 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005216 }
5217 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005218 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5219 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005220 }
5221 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005222 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5223 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005224 }
5225 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005226 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5227 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005228 }
5229}
5230
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005231void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005232 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005235}
5236
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005237void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005238 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5239 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005240}
5241
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005242void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005243 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005244}
5245
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005246void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005248}
5249
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005250void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5251 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005252 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005253}
5254
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005255void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005256 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005257}
5258
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005259void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5260 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005261 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5262 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005263}
5264
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005265void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5266 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005268}
5269
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005270void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005272}
5273
Michael Wrightd02c5b62014-02-10 15:10:22 -08005274TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005275 prepareButtons();
5276 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005277 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005278
Josep del Río2d8c79a2023-01-23 19:33:50 +00005279 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005280}
5281
Michael Wrightd02c5b62014-02-10 15:10:22 -08005282TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005283 prepareButtons();
5284 prepareAxes(POSITION);
5285 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00005286 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005287
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005288 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005289}
5290
5291TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005292 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005293 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005294 prepareButtons();
5295 prepareAxes(POSITION);
5296 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005297 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005298
5299 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005300 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005301
5302 // Virtual key is down.
5303 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5304 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5305 processDown(mapper, x, y);
5306 processSync(mapper);
5307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5308
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005309 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005310
5311 // Virtual key is up.
5312 processUp(mapper);
5313 processSync(mapper);
5314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5315
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005316 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005317}
5318
5319TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005320 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005321 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322 prepareButtons();
5323 prepareAxes(POSITION);
5324 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005325 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005326
5327 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005328 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005329
5330 // Virtual key is down.
5331 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5332 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5333 processDown(mapper, x, y);
5334 processSync(mapper);
5335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5336
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005337 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005338
5339 // Virtual key is up.
5340 processUp(mapper);
5341 processSync(mapper);
5342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5343
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005344 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005345}
5346
5347TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005348 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005349 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005350 prepareButtons();
5351 prepareAxes(POSITION);
5352 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005353 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005354
Michael Wrightd02c5b62014-02-10 15:10:22 -08005355 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005356 ASSERT_TRUE(
5357 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005358 ASSERT_TRUE(flags[0]);
5359 ASSERT_FALSE(flags[1]);
5360}
5361
5362TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005363 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005364 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005365 prepareButtons();
5366 prepareAxes(POSITION);
5367 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005368 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369
arthurhungdcef2dc2020-08-11 14:47:50 +08005370 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005371
5372 NotifyKeyArgs args;
5373
5374 // Press virtual key.
5375 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5376 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5377 processDown(mapper, x, y);
5378 processSync(mapper);
5379
5380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5381 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5382 ASSERT_EQ(DEVICE_ID, args.deviceId);
5383 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5384 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5385 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5386 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5387 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5388 ASSERT_EQ(KEY_HOME, args.scanCode);
5389 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5390 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5391
5392 // Release virtual key.
5393 processUp(mapper);
5394 processSync(mapper);
5395
5396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5397 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5398 ASSERT_EQ(DEVICE_ID, args.deviceId);
5399 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5400 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5401 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5402 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5403 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5404 ASSERT_EQ(KEY_HOME, args.scanCode);
5405 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5406 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5407
5408 // Should not have sent any motions.
5409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5410}
5411
5412TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005414 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005415 prepareButtons();
5416 prepareAxes(POSITION);
5417 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005418 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419
arthurhungdcef2dc2020-08-11 14:47:50 +08005420 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005421
5422 NotifyKeyArgs keyArgs;
5423
5424 // Press virtual key.
5425 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5426 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5427 processDown(mapper, x, y);
5428 processSync(mapper);
5429
5430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5431 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5432 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5433 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5434 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5435 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5436 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5437 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5438 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5439 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5440 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5441
5442 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5443 // into the display area.
5444 y -= 100;
5445 processMove(mapper, x, y);
5446 processSync(mapper);
5447
5448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5449 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5450 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5451 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5452 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5453 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5454 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5455 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5456 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5457 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5458 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5459 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5460
5461 NotifyMotionArgs motionArgs;
5462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5463 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5464 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5465 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5466 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5467 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5468 ASSERT_EQ(0, motionArgs.flags);
5469 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5470 ASSERT_EQ(0, motionArgs.buttonState);
5471 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005472 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005473 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005474 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5476 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5477 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5478 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5479 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5480
5481 // Keep moving out of bounds. Should generate a pointer move.
5482 y -= 50;
5483 processMove(mapper, x, y);
5484 processSync(mapper);
5485
5486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5487 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5488 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5489 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5490 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5491 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5492 ASSERT_EQ(0, motionArgs.flags);
5493 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5494 ASSERT_EQ(0, motionArgs.buttonState);
5495 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005496 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005497 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005498 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5500 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5501 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5502 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5503 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5504
5505 // Release out of bounds. Should generate a pointer up.
5506 processUp(mapper);
5507 processSync(mapper);
5508
5509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5510 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5511 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5512 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5513 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5514 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5515 ASSERT_EQ(0, motionArgs.flags);
5516 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5517 ASSERT_EQ(0, motionArgs.buttonState);
5518 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005519 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005520 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005521 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5523 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5524 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5525 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5526 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5527
5528 // Should not have sent any more keys or motions.
5529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5531}
5532
5533TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005534 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005535 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005536 prepareButtons();
5537 prepareAxes(POSITION);
5538 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005539 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005540
arthurhungdcef2dc2020-08-11 14:47:50 +08005541 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005542
5543 NotifyMotionArgs motionArgs;
5544
5545 // Initially go down out of bounds.
5546 int32_t x = -10;
5547 int32_t y = -10;
5548 processDown(mapper, x, y);
5549 processSync(mapper);
5550
5551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5552
5553 // Move into the display area. Should generate a pointer down.
5554 x = 50;
5555 y = 75;
5556 processMove(mapper, x, y);
5557 processSync(mapper);
5558
5559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5560 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5561 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5562 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5563 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5564 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5565 ASSERT_EQ(0, motionArgs.flags);
5566 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5567 ASSERT_EQ(0, motionArgs.buttonState);
5568 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005569 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005570 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005571 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5573 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5574 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5575 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5576 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5577
5578 // Release. Should generate a pointer up.
5579 processUp(mapper);
5580 processSync(mapper);
5581
5582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5583 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5584 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5585 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5586 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5587 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5588 ASSERT_EQ(0, motionArgs.flags);
5589 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5590 ASSERT_EQ(0, motionArgs.buttonState);
5591 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005592 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005593 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005594 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5596 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5597 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5598 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5599 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5600
5601 // Should not have sent any more keys or motions.
5602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5604}
5605
Santos Cordonfa5cf462017-04-05 10:37:00 -07005606TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005607 addConfigurationProperty("touch.deviceType", "touchScreen");
5608 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5609
Michael Wrighta9cf4192022-12-01 23:46:39 +00005610 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005611 prepareButtons();
5612 prepareAxes(POSITION);
5613 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005614 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005615
arthurhungdcef2dc2020-08-11 14:47:50 +08005616 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005617
5618 NotifyMotionArgs motionArgs;
5619
5620 // Down.
5621 int32_t x = 100;
5622 int32_t y = 125;
5623 processDown(mapper, x, y);
5624 processSync(mapper);
5625
5626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5627 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5628 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5629 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5630 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5631 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5632 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5633 ASSERT_EQ(0, motionArgs.flags);
5634 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5635 ASSERT_EQ(0, motionArgs.buttonState);
5636 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005637 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005638 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005639 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5641 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5642 1, 0, 0, 0, 0, 0, 0, 0));
5643 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5644 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5645 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5646
5647 // Move.
5648 x += 50;
5649 y += 75;
5650 processMove(mapper, x, y);
5651 processSync(mapper);
5652
5653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5654 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5655 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5656 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5657 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5658 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5660 ASSERT_EQ(0, motionArgs.flags);
5661 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5662 ASSERT_EQ(0, motionArgs.buttonState);
5663 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005664 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005665 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005666 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005667 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5668 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5669 1, 0, 0, 0, 0, 0, 0, 0));
5670 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5671 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5672 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5673
5674 // Up.
5675 processUp(mapper);
5676 processSync(mapper);
5677
5678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5679 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5680 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5681 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5682 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5683 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5684 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5685 ASSERT_EQ(0, motionArgs.flags);
5686 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5687 ASSERT_EQ(0, motionArgs.buttonState);
5688 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005689 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005690 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005691 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005692 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5693 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5694 1, 0, 0, 0, 0, 0, 0, 0));
5695 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5696 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5697 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5698
5699 // Should not have sent any more keys or motions.
5700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5702}
5703
Michael Wrightd02c5b62014-02-10 15:10:22 -08005704TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005705 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005706 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005707 prepareButtons();
5708 prepareAxes(POSITION);
5709 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005710 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005711
arthurhungdcef2dc2020-08-11 14:47:50 +08005712 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005713
5714 NotifyMotionArgs motionArgs;
5715
5716 // Down.
5717 int32_t x = 100;
5718 int32_t y = 125;
5719 processDown(mapper, x, y);
5720 processSync(mapper);
5721
5722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5723 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5724 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5725 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5726 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5727 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5728 ASSERT_EQ(0, motionArgs.flags);
5729 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5730 ASSERT_EQ(0, motionArgs.buttonState);
5731 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005732 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005733 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005734 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5736 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5737 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5738 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5739 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5740
5741 // Move.
5742 x += 50;
5743 y += 75;
5744 processMove(mapper, x, y);
5745 processSync(mapper);
5746
5747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5748 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5749 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5750 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5751 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5752 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5753 ASSERT_EQ(0, motionArgs.flags);
5754 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5755 ASSERT_EQ(0, motionArgs.buttonState);
5756 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005757 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005758 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005759 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5761 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5762 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5763 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5764 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5765
5766 // Up.
5767 processUp(mapper);
5768 processSync(mapper);
5769
5770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5771 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5772 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5773 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5774 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5775 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5776 ASSERT_EQ(0, motionArgs.flags);
5777 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5778 ASSERT_EQ(0, motionArgs.buttonState);
5779 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005780 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005781 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005782 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5784 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5785 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5786 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5787 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5788
5789 // Should not have sent any more keys or motions.
5790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5792}
5793
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005794TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005795 addConfigurationProperty("touch.deviceType", "touchScreen");
5796 prepareButtons();
5797 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005798 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5799 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005800 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005801
5802 NotifyMotionArgs args;
5803
5804 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005805 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005806 processDown(mapper, toRawX(50), toRawY(75));
5807 processSync(mapper);
5808
5809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5810 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5811 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5812
5813 processUp(mapper);
5814 processSync(mapper);
5815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5816}
5817
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005818TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005819 addConfigurationProperty("touch.deviceType", "touchScreen");
5820 prepareButtons();
5821 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005822 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5823 // orientation-aware are affected by display rotation.
5824 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005825 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005826
5827 NotifyMotionArgs args;
5828
5829 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005830 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005831 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005832 processDown(mapper, toRawX(50), toRawY(75));
5833 processSync(mapper);
5834
5835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5836 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5837 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5838
5839 processUp(mapper);
5840 processSync(mapper);
5841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5842
5843 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005844 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005845 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005846 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005847 processSync(mapper);
5848
5849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5850 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5851 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5852
5853 processUp(mapper);
5854 processSync(mapper);
5855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5856
5857 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005858 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005859 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005860 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5861 processSync(mapper);
5862
5863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5864 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5865 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5866
5867 processUp(mapper);
5868 processSync(mapper);
5869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5870
5871 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005872 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005873 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005874 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005875 processSync(mapper);
5876
5877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5878 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5879 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5880
5881 processUp(mapper);
5882 processSync(mapper);
5883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5884}
5885
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005886TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5887 addConfigurationProperty("touch.deviceType", "touchScreen");
5888 prepareButtons();
5889 prepareAxes(POSITION);
5890 addConfigurationProperty("touch.orientationAware", "1");
5891 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5892 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005893 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005894 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005895 NotifyMotionArgs args;
5896
5897 // Orientation 0.
5898 processDown(mapper, toRawX(50), toRawY(75));
5899 processSync(mapper);
5900
5901 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5902 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5903 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5904
5905 processUp(mapper);
5906 processSync(mapper);
5907 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5908}
5909
5910TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5911 addConfigurationProperty("touch.deviceType", "touchScreen");
5912 prepareButtons();
5913 prepareAxes(POSITION);
5914 addConfigurationProperty("touch.orientationAware", "1");
5915 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5916 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005917 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005918 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005919 NotifyMotionArgs args;
5920
5921 // Orientation 90.
5922 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5923 processSync(mapper);
5924
5925 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5926 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5927 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5928
5929 processUp(mapper);
5930 processSync(mapper);
5931 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5932}
5933
5934TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5935 addConfigurationProperty("touch.deviceType", "touchScreen");
5936 prepareButtons();
5937 prepareAxes(POSITION);
5938 addConfigurationProperty("touch.orientationAware", "1");
5939 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5940 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005941 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005942 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005943 NotifyMotionArgs args;
5944
5945 // Orientation 180.
5946 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5947 processSync(mapper);
5948
5949 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5950 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5951 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5952
5953 processUp(mapper);
5954 processSync(mapper);
5955 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5956}
5957
5958TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5959 addConfigurationProperty("touch.deviceType", "touchScreen");
5960 prepareButtons();
5961 prepareAxes(POSITION);
5962 addConfigurationProperty("touch.orientationAware", "1");
5963 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5964 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005965 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005966 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005967 NotifyMotionArgs args;
5968
5969 // Orientation 270.
5970 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5971 processSync(mapper);
5972
5973 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5974 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5975 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5976
5977 processUp(mapper);
5978 processSync(mapper);
5979 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5980}
5981
5982TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5983 addConfigurationProperty("touch.deviceType", "touchScreen");
5984 prepareButtons();
5985 prepareAxes(POSITION);
5986 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5987 // orientation-aware are affected by display rotation.
5988 addConfigurationProperty("touch.orientationAware", "0");
5989 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00005990 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005991
5992 NotifyMotionArgs args;
5993
5994 // Orientation 90, Rotation 0.
5995 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005996 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005997 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5998 processSync(mapper);
5999
6000 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6001 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6002 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6003
6004 processUp(mapper);
6005 processSync(mapper);
6006 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6007
6008 // Orientation 90, Rotation 90.
6009 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006010 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006011 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006012 processSync(mapper);
6013
6014 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6015 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6016 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6017
6018 processUp(mapper);
6019 processSync(mapper);
6020 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6021
6022 // Orientation 90, Rotation 180.
6023 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006024 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006025 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6026 processSync(mapper);
6027
6028 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6029 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6030 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6031
6032 processUp(mapper);
6033 processSync(mapper);
6034 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6035
6036 // Orientation 90, Rotation 270.
6037 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006038 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006039 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 -07006040 processSync(mapper);
6041
6042 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6043 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6044 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6045
6046 processUp(mapper);
6047 processSync(mapper);
6048 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6049}
6050
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006051TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
6052 addConfigurationProperty("touch.deviceType", "touchScreen");
6053 prepareButtons();
6054 prepareAxes(POSITION);
6055 addConfigurationProperty("touch.orientationAware", "1");
6056 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006057 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006058
6059 // Set a physical frame in the display viewport.
6060 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6061 viewport->physicalLeft = 20;
6062 viewport->physicalTop = 600;
6063 viewport->physicalRight = 30;
6064 viewport->physicalBottom = 610;
6065 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006066 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006067
6068 // Start the touch.
6069 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6070 processSync(mapper);
6071
6072 // Expect all input starting outside the physical frame to be ignored.
6073 const std::array<Point, 6> outsidePoints = {
6074 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6075 for (const auto& p : outsidePoints) {
6076 processMove(mapper, toRawX(p.x), toRawY(p.y));
6077 processSync(mapper);
6078 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6079 }
6080
6081 // Move the touch into the physical frame.
6082 processMove(mapper, toRawX(25), toRawY(605));
6083 processSync(mapper);
6084 NotifyMotionArgs args;
6085 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6086 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6087 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6088 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6089
6090 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
6091 for (const auto& p : outsidePoints) {
6092 processMove(mapper, toRawX(p.x), toRawY(p.y));
6093 processSync(mapper);
6094 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6095 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
6096 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6097 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6098 }
6099
6100 processUp(mapper);
6101 processSync(mapper);
6102 EXPECT_NO_FATAL_FAILURE(
6103 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
6104}
6105
Harry Cutts1db43992023-06-19 17:05:07 +00006106TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
6107 std::shared_ptr<FakePointerController> fakePointerController =
6108 std::make_shared<FakePointerController>();
6109 mFakePolicy->setPointerController(fakePointerController);
6110
6111 addConfigurationProperty("touch.deviceType", "pointer");
6112 prepareAxes(POSITION);
6113 prepareDisplay(ui::ROTATION_0);
6114 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
6115
6116 // Set a physical frame in the display viewport.
6117 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6118 viewport->physicalLeft = 20;
6119 viewport->physicalTop = 600;
6120 viewport->physicalRight = 30;
6121 viewport->physicalBottom = 610;
6122 mFakePolicy->updateViewport(*viewport);
6123 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
6124
6125 // Start the touch.
6126 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6127 processSync(mapper);
6128
6129 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
6130 // produced.
6131 const std::array<Point, 6> outsidePoints = {
6132 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6133 for (const auto& p : outsidePoints) {
6134 processMove(mapper, toRawX(p.x), toRawY(p.y));
6135 processSync(mapper);
6136 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6137 }
6138}
6139
Michael Wrightd02c5b62014-02-10 15:10:22 -08006140TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006141 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006142 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006143 prepareButtons();
6144 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00006145 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006146
6147 // These calculations are based on the input device calibration documentation.
6148 int32_t rawX = 100;
6149 int32_t rawY = 200;
6150 int32_t rawPressure = 10;
6151 int32_t rawToolMajor = 12;
6152 int32_t rawDistance = 2;
6153 int32_t rawTiltX = 30;
6154 int32_t rawTiltY = 110;
6155
6156 float x = toDisplayX(rawX);
6157 float y = toDisplayY(rawY);
6158 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6159 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6160 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6161 float distance = float(rawDistance);
6162
6163 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6164 float tiltScale = M_PI / 180;
6165 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6166 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6167 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6168 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6169
6170 processDown(mapper, rawX, rawY);
6171 processPressure(mapper, rawPressure);
6172 processToolMajor(mapper, rawToolMajor);
6173 processDistance(mapper, rawDistance);
6174 processTilt(mapper, rawTiltX, rawTiltY);
6175 processSync(mapper);
6176
6177 NotifyMotionArgs args;
6178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6180 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6181 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6182}
6183
Jason Gerecke489fda82012-09-07 17:19:40 -07006184TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006185 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006186 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07006187 prepareLocationCalibration();
6188 prepareButtons();
6189 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006190 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006191
6192 int32_t rawX = 100;
6193 int32_t rawY = 200;
6194
6195 float x = toDisplayX(toCookedX(rawX, rawY));
6196 float y = toDisplayY(toCookedY(rawX, rawY));
6197
6198 processDown(mapper, rawX, rawY);
6199 processSync(mapper);
6200
6201 NotifyMotionArgs args;
6202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6203 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6204 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6205}
6206
Michael Wrightd02c5b62014-02-10 15:10:22 -08006207TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006208 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006209 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210 prepareButtons();
6211 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006212 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006213
6214 NotifyMotionArgs motionArgs;
6215 NotifyKeyArgs keyArgs;
6216
6217 processDown(mapper, 100, 200);
6218 processSync(mapper);
6219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6220 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6221 ASSERT_EQ(0, motionArgs.buttonState);
6222
6223 // press BTN_LEFT, release BTN_LEFT
6224 processKey(mapper, BTN_LEFT, 1);
6225 processSync(mapper);
6226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6228 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6229
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6231 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6232 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6233
Michael Wrightd02c5b62014-02-10 15:10:22 -08006234 processKey(mapper, BTN_LEFT, 0);
6235 processSync(mapper);
6236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006237 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006238 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006239
6240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006241 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006242 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006243
6244 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6245 processKey(mapper, BTN_RIGHT, 1);
6246 processKey(mapper, BTN_MIDDLE, 1);
6247 processSync(mapper);
6248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6250 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6251 motionArgs.buttonState);
6252
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6254 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6255 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6256
6257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6258 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6259 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6260 motionArgs.buttonState);
6261
Michael Wrightd02c5b62014-02-10 15:10:22 -08006262 processKey(mapper, BTN_RIGHT, 0);
6263 processSync(mapper);
6264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006265 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006266 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006267
6268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006270 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006271
6272 processKey(mapper, BTN_MIDDLE, 0);
6273 processSync(mapper);
6274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006275 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006276 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006277
6278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006279 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006280 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006281
6282 // press BTN_BACK, release BTN_BACK
6283 processKey(mapper, BTN_BACK, 1);
6284 processSync(mapper);
6285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6286 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6287 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006288
Michael Wrightd02c5b62014-02-10 15:10:22 -08006289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006291 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6292
6293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6295 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006296
6297 processKey(mapper, BTN_BACK, 0);
6298 processSync(mapper);
6299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006301 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006302
6303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006305 ASSERT_EQ(0, motionArgs.buttonState);
6306
Michael Wrightd02c5b62014-02-10 15:10:22 -08006307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6308 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6309 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6310
6311 // press BTN_SIDE, release BTN_SIDE
6312 processKey(mapper, BTN_SIDE, 1);
6313 processSync(mapper);
6314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6315 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6316 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006317
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006320 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6321
6322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6323 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6324 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006325
6326 processKey(mapper, BTN_SIDE, 0);
6327 processSync(mapper);
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006330 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006331
6332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006334 ASSERT_EQ(0, motionArgs.buttonState);
6335
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6337 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6338 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6339
6340 // press BTN_FORWARD, release BTN_FORWARD
6341 processKey(mapper, BTN_FORWARD, 1);
6342 processSync(mapper);
6343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6344 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6345 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006346
Michael Wrightd02c5b62014-02-10 15:10:22 -08006347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006349 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6350
6351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6352 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6353 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006354
6355 processKey(mapper, BTN_FORWARD, 0);
6356 processSync(mapper);
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006358 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006359 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006360
6361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006363 ASSERT_EQ(0, motionArgs.buttonState);
6364
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6366 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6367 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6368
6369 // press BTN_EXTRA, release BTN_EXTRA
6370 processKey(mapper, BTN_EXTRA, 1);
6371 processSync(mapper);
6372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6373 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6374 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006375
Michael Wrightd02c5b62014-02-10 15:10:22 -08006376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006378 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6379
6380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6381 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6382 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006383
6384 processKey(mapper, BTN_EXTRA, 0);
6385 processSync(mapper);
6386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006387 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006388 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006389
6390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006391 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006392 ASSERT_EQ(0, motionArgs.buttonState);
6393
Michael Wrightd02c5b62014-02-10 15:10:22 -08006394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6395 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6396 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6397
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6399
Michael Wrightd02c5b62014-02-10 15:10:22 -08006400 // press BTN_STYLUS, release BTN_STYLUS
6401 processKey(mapper, BTN_STYLUS, 1);
6402 processSync(mapper);
6403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006405 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6406
6407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6408 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6409 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006410
6411 processKey(mapper, BTN_STYLUS, 0);
6412 processSync(mapper);
6413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006414 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006415 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006416
6417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006419 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006420
6421 // press BTN_STYLUS2, release BTN_STYLUS2
6422 processKey(mapper, BTN_STYLUS2, 1);
6423 processSync(mapper);
6424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6425 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006426 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6427
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6429 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6430 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006431
6432 processKey(mapper, BTN_STYLUS2, 0);
6433 processSync(mapper);
6434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006435 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006436 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006437
6438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006440 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006441
6442 // release touch
6443 processUp(mapper);
6444 processSync(mapper);
6445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6446 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6447 ASSERT_EQ(0, motionArgs.buttonState);
6448}
6449
6450TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006451 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006452 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006453 prepareButtons();
6454 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006455 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006456
6457 NotifyMotionArgs motionArgs;
6458
6459 // default tool type is finger
6460 processDown(mapper, 100, 200);
6461 processSync(mapper);
6462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6463 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006464 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006465
6466 // eraser
6467 processKey(mapper, BTN_TOOL_RUBBER, 1);
6468 processSync(mapper);
6469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006471 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006472
6473 // stylus
6474 processKey(mapper, BTN_TOOL_RUBBER, 0);
6475 processKey(mapper, BTN_TOOL_PEN, 1);
6476 processSync(mapper);
6477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6478 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006479 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006480
6481 // brush
6482 processKey(mapper, BTN_TOOL_PEN, 0);
6483 processKey(mapper, BTN_TOOL_BRUSH, 1);
6484 processSync(mapper);
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6486 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006487 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006488
6489 // pencil
6490 processKey(mapper, BTN_TOOL_BRUSH, 0);
6491 processKey(mapper, BTN_TOOL_PENCIL, 1);
6492 processSync(mapper);
6493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6494 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006495 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006496
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006497 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006498 processKey(mapper, BTN_TOOL_PENCIL, 0);
6499 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6500 processSync(mapper);
6501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006503 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006504
6505 // mouse
6506 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6507 processKey(mapper, BTN_TOOL_MOUSE, 1);
6508 processSync(mapper);
6509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6510 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006511 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006512
6513 // lens
6514 processKey(mapper, BTN_TOOL_MOUSE, 0);
6515 processKey(mapper, BTN_TOOL_LENS, 1);
6516 processSync(mapper);
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6518 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006519 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006520
6521 // double-tap
6522 processKey(mapper, BTN_TOOL_LENS, 0);
6523 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6524 processSync(mapper);
6525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6526 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006527 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006528
6529 // triple-tap
6530 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6531 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6532 processSync(mapper);
6533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006535 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006536
6537 // quad-tap
6538 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6539 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6540 processSync(mapper);
6541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006543 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006544
6545 // finger
6546 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6547 processKey(mapper, BTN_TOOL_FINGER, 1);
6548 processSync(mapper);
6549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6550 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006551 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006552
6553 // stylus trumps finger
6554 processKey(mapper, BTN_TOOL_PEN, 1);
6555 processSync(mapper);
6556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6557 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006558 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006559
6560 // eraser trumps stylus
6561 processKey(mapper, BTN_TOOL_RUBBER, 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::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006566
6567 // mouse trumps eraser
6568 processKey(mapper, BTN_TOOL_MOUSE, 1);
6569 processSync(mapper);
6570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006572 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006573
6574 // back to default tool type
6575 processKey(mapper, BTN_TOOL_MOUSE, 0);
6576 processKey(mapper, BTN_TOOL_RUBBER, 0);
6577 processKey(mapper, BTN_TOOL_PEN, 0);
6578 processKey(mapper, BTN_TOOL_FINGER, 0);
6579 processSync(mapper);
6580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6581 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006582 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006583}
6584
6585TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006586 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006587 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006588 prepareButtons();
6589 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006590 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006591 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006592
6593 NotifyMotionArgs motionArgs;
6594
6595 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6596 processKey(mapper, BTN_TOOL_FINGER, 1);
6597 processMove(mapper, 100, 200);
6598 processSync(mapper);
6599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6600 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6602 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6603
6604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6605 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6607 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6608
6609 // move a little
6610 processMove(mapper, 150, 250);
6611 processSync(mapper);
6612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6613 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6615 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6616
6617 // down when BTN_TOUCH is pressed, pressure defaults to 1
6618 processKey(mapper, BTN_TOUCH, 1);
6619 processSync(mapper);
6620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6621 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6623 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6624
6625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6626 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6628 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6629
6630 // up when BTN_TOUCH is released, hover restored
6631 processKey(mapper, BTN_TOUCH, 0);
6632 processSync(mapper);
6633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6634 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6636 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6637
6638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6639 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6641 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6642
6643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6644 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6645 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6646 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6647
6648 // exit hover when pointer goes away
6649 processKey(mapper, BTN_TOOL_FINGER, 0);
6650 processSync(mapper);
6651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6652 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6654 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6655}
6656
6657TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006658 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006659 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006660 prepareButtons();
6661 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006662 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006663
6664 NotifyMotionArgs motionArgs;
6665
6666 // initially hovering because pressure is 0
6667 processDown(mapper, 100, 200);
6668 processPressure(mapper, 0);
6669 processSync(mapper);
6670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6671 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6673 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6674
6675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6676 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6678 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6679
6680 // move a little
6681 processMove(mapper, 150, 250);
6682 processSync(mapper);
6683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6684 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6686 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6687
6688 // down when pressure is non-zero
6689 processPressure(mapper, RAW_PRESSURE_MAX);
6690 processSync(mapper);
6691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6692 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6694 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6695
6696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6697 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6699 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6700
6701 // up when pressure becomes 0, hover restored
6702 processPressure(mapper, 0);
6703 processSync(mapper);
6704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6705 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6706 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6707 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6708
6709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6710 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6712 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6713
6714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6715 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6716 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6717 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6718
6719 // exit hover when pointer goes away
6720 processUp(mapper);
6721 processSync(mapper);
6722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6723 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6725 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6726}
6727
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006728TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6729 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006730 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006731 prepareButtons();
6732 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006733 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006734
6735 // Touch down.
6736 processDown(mapper, 100, 200);
6737 processPressure(mapper, 1);
6738 processSync(mapper);
6739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6740 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6741
6742 // Reset the mapper. This should cancel the ongoing gesture.
6743 resetMapper(mapper, ARBITRARY_TIME);
6744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6745 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6746
6747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6748}
6749
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006750TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6751 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006752 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006753 prepareButtons();
6754 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006755 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006756
6757 // Set the initial state for the touch pointer.
6758 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6759 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6760 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6761 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6762
6763 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006764 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6765 // does not generate any events.
6766 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006767
6768 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6769 // the recreated touch state to generate a down event.
6770 processSync(mapper);
6771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6772 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6773
6774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6775}
6776
lilinnan687e58f2022-07-19 16:00:50 +08006777TEST_F(SingleTouchInputMapperTest,
6778 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6779 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006780 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006781 prepareButtons();
6782 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006783 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006784 NotifyMotionArgs motionArgs;
6785
6786 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006787 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006788 processSync(mapper);
6789
6790 // We should receive a down event
6791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6792 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6793
6794 // Change display id
6795 clearViewports();
6796 prepareSecondaryDisplay(ViewportType::INTERNAL);
6797
6798 // We should receive a cancel event
6799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6800 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6801 // Then receive reset called
6802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6803}
6804
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006805TEST_F(SingleTouchInputMapperTest,
6806 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6807 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006808 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006809 prepareButtons();
6810 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006811 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6813 NotifyMotionArgs motionArgs;
6814
6815 // Start a new gesture.
6816 processDown(mapper, 100, 200);
6817 processSync(mapper);
6818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6819 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6820
6821 // Make the viewport inactive. This will put the device in disabled mode.
6822 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6823 viewport->isActive = false;
6824 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006825 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006826
6827 // We should receive a cancel event for the ongoing gesture.
6828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6829 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6830 // Then we should be notified that the device was reset.
6831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6832
6833 // No events are generated while the viewport is inactive.
6834 processMove(mapper, 101, 201);
6835 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006836 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006837 processSync(mapper);
6838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6839
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006840 // Start a new gesture while the viewport is still inactive.
6841 processDown(mapper, 300, 400);
6842 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6843 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6844 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6845 processSync(mapper);
6846
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006847 // Make the viewport active again. The device should resume processing events.
6848 viewport->isActive = true;
6849 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006850 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006851
6852 // The device is reset because it changes back to direct mode, without generating any events.
6853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6855
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006856 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006857 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6859 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006860
6861 // No more events.
6862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6864}
6865
Prabir Pradhan211ba622022-10-31 21:09:21 +00006866TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6867 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006868 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006869 prepareButtons();
6870 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006871 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6873
6874 // Press a stylus button.
6875 processKey(mapper, BTN_STYLUS, 1);
6876 processSync(mapper);
6877
6878 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6879 processDown(mapper, 100, 200);
6880 processSync(mapper);
6881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6882 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6883 WithCoords(toDisplayX(100), toDisplayY(200)),
6884 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6886 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6887 WithCoords(toDisplayX(100), toDisplayY(200)),
6888 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6889
6890 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6891 // the button has not actually been released, since there will be no pointers through which the
6892 // button state can be reported. The event is generated at the location of the pointer before
6893 // it went up.
6894 processUp(mapper);
6895 processSync(mapper);
6896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6897 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6898 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6900 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6901 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6902}
6903
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006904TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6905 addConfigurationProperty("touch.deviceType", "touchScreen");
6906 prepareDisplay(ui::ROTATION_0);
6907 prepareButtons();
6908 prepareAxes(POSITION);
6909
6910 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6911
Arpit Singha8c236b2023-04-25 13:56:05 +00006912 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6914
6915 // Press a stylus button.
6916 processKey(mapper, BTN_STYLUS, 1);
6917 processSync(mapper);
6918
6919 // Start a touch gesture and ensure that the stylus button is not reported.
6920 processDown(mapper, 100, 200);
6921 processSync(mapper);
6922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6923 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6924
6925 // Release and press the stylus button again.
6926 processKey(mapper, BTN_STYLUS, 0);
6927 processSync(mapper);
6928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6929 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6930 processKey(mapper, BTN_STYLUS, 1);
6931 processSync(mapper);
6932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6933 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6934
6935 // Release the touch gesture.
6936 processUp(mapper);
6937 processSync(mapper);
6938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6939 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6940
6941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6942}
6943
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006944TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6945 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6946 prepareDisplay(ui::ROTATION_0);
6947 prepareButtons();
6948 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006949 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6951
6952 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6953}
6954
Seunghwan Choi356026c2023-02-01 14:37:25 +09006955TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6956 std::shared_ptr<FakePointerController> fakePointerController =
6957 std::make_shared<FakePointerController>();
6958 addConfigurationProperty("touch.deviceType", "touchScreen");
6959 prepareDisplay(ui::ROTATION_0);
6960 prepareButtons();
6961 prepareAxes(POSITION);
6962 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6963 mFakePolicy->setPointerController(fakePointerController);
6964 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00006965 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006966
6967 processKey(mapper, BTN_TOOL_PEN, 1);
6968 processMove(mapper, 100, 200);
6969 processSync(mapper);
6970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6971 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006972 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006973 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6974 ASSERT_TRUE(fakePointerController->isPointerShown());
6975 ASSERT_NO_FATAL_FAILURE(
6976 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6977}
6978
6979TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6980 std::shared_ptr<FakePointerController> fakePointerController =
6981 std::make_shared<FakePointerController>();
6982 addConfigurationProperty("touch.deviceType", "touchScreen");
6983 prepareDisplay(ui::ROTATION_0);
6984 prepareButtons();
6985 prepareAxes(POSITION);
6986 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6987 mFakePolicy->setPointerController(fakePointerController);
6988 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00006989 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006990
6991 processKey(mapper, BTN_TOOL_PEN, 1);
6992 processMove(mapper, 100, 200);
6993 processSync(mapper);
6994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6995 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006996 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006997 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6998 ASSERT_FALSE(fakePointerController->isPointerShown());
6999}
7000
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007001TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
7002 // Initialize the device without setting device source to touch navigation.
7003 addConfigurationProperty("touch.deviceType", "touchScreen");
7004 prepareDisplay(ui::ROTATION_0);
7005 prepareButtons();
7006 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007007 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007008
7009 // Ensure that the device is created as a touchscreen, not touch navigation.
7010 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7011
7012 // Add device type association after the device was created.
7013 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
7014
7015 // Send update to the mapper.
7016 std::list<NotifyArgs> unused2 =
7017 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007018 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007019
7020 // Check whether device type update was successful.
7021 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
7022}
7023
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007024TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
7025 // Initialize the device without setting device source to touch navigation.
7026 addConfigurationProperty("touch.deviceType", "touchScreen");
7027 prepareDisplay(ui::ROTATION_0);
7028 prepareButtons();
7029 prepareAxes(POSITION);
7030 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7031
7032 // Set a physical frame in the display viewport.
7033 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7034 viewport->physicalLeft = 0;
7035 viewport->physicalTop = 0;
7036 viewport->physicalRight = DISPLAY_WIDTH / 2;
7037 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
7038 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007039 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007040
Arpit Singha8c236b2023-04-25 13:56:05 +00007041 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007042
7043 // Hovering inside the physical frame produces events.
7044 processKey(mapper, BTN_TOOL_PEN, 1);
7045 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
7046 processSync(mapper);
7047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7048 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
7049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7050 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
7051
7052 // Leaving the physical frame ends the hovering gesture.
7053 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
7054 processSync(mapper);
7055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7056 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
7057
7058 // Moving outside the physical frame does not produce events.
7059 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
7060 processSync(mapper);
7061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7062
7063 // Re-entering the physical frame produces events.
7064 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
7065 processSync(mapper);
7066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7067 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
7068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7069 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
7070}
7071
Prabir Pradhan5632d622021-09-06 07:57:20 -07007072// --- TouchDisplayProjectionTest ---
7073
7074class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7075public:
7076 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7077 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7078 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00007079 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
7080 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
7081 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007082 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007083 auto rotatedWidth = naturalDisplayWidth;
7084 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007085 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00007086 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007087 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007088 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007089 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007090 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007091 inverseRotationFlags = ui::Transform::ROT_180;
7092 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007093 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007094 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007095 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007096 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007097 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007098 inverseRotationFlags = ui::Transform::ROT_0;
7099 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007100 }
7101
Prabir Pradhana9df3162022-12-05 23:57:27 +00007102 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007103 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7104
7105 std::optional<DisplayViewport> internalViewport =
7106 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7107 DisplayViewport& v = *internalViewport;
7108 v.displayId = DISPLAY_ID;
7109 v.orientation = orientation;
7110
7111 v.logicalLeft = 0;
7112 v.logicalTop = 0;
7113 v.logicalRight = 100;
7114 v.logicalBottom = 100;
7115
7116 v.physicalLeft = rotatedPhysicalDisplay.left;
7117 v.physicalTop = rotatedPhysicalDisplay.top;
7118 v.physicalRight = rotatedPhysicalDisplay.right;
7119 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7120
Prabir Pradhana9df3162022-12-05 23:57:27 +00007121 v.deviceWidth = rotatedWidth;
7122 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007123
7124 v.isActive = true;
7125 v.uniqueId = UNIQUE_ID;
7126 v.type = ViewportType::INTERNAL;
7127 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007128 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007129 }
7130
7131 void assertReceivedMove(const Point& point) {
7132 NotifyMotionArgs motionArgs;
7133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007135 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007136 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7137 1, 0, 0, 0, 0, 0, 0, 0));
7138 }
7139};
7140
7141TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7142 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007143 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007144
7145 prepareButtons();
7146 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007147 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007148
7149 NotifyMotionArgs motionArgs;
7150
7151 // Configure the DisplayViewport such that the logical display maps to a subsection of
7152 // the display panel called the physical display. Here, the physical display is bounded by the
7153 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7154 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7155 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7156 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7157
Michael Wrighta9cf4192022-12-01 23:46:39 +00007158 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007159 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7160
7161 // Touches outside the physical display should be ignored, and should not generate any
7162 // events. Ensure touches at the following points that lie outside of the physical display
7163 // area do not generate any events.
7164 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7165 processDown(mapper, toRawX(point.x), toRawY(point.y));
7166 processSync(mapper);
7167 processUp(mapper);
7168 processSync(mapper);
7169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7170 << "Unexpected event generated for touch outside physical display at point: "
7171 << point.x << ", " << point.y;
7172 }
7173 }
7174}
7175
7176TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7177 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007178 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007179
7180 prepareButtons();
7181 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007182 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007183
7184 NotifyMotionArgs motionArgs;
7185
7186 // Configure the DisplayViewport such that the logical display maps to a subsection of
7187 // the display panel called the physical display. Here, the physical display is bounded by the
7188 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7189 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7190
Michael Wrighta9cf4192022-12-01 23:46:39 +00007191 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007192 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7193
7194 // Touches that start outside the physical display should be ignored until it enters the
7195 // physical display bounds, at which point it should generate a down event. Start a touch at
7196 // the point (5, 100), which is outside the physical display bounds.
7197 static const Point kOutsidePoint{5, 100};
7198 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7199 processSync(mapper);
7200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7201
7202 // Move the touch into the physical display area. This should generate a pointer down.
7203 processMove(mapper, toRawX(11), toRawY(21));
7204 processSync(mapper);
7205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7206 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007207 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007208 ASSERT_NO_FATAL_FAILURE(
7209 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7210
7211 // Move the touch inside the physical display area. This should generate a pointer move.
7212 processMove(mapper, toRawX(69), toRawY(159));
7213 processSync(mapper);
7214 assertReceivedMove({69, 159});
7215
7216 // Move outside the physical display area. Since the pointer is already down, this should
7217 // now continue generating events.
7218 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7219 processSync(mapper);
7220 assertReceivedMove(kOutsidePoint);
7221
7222 // Release. This should generate a pointer up.
7223 processUp(mapper);
7224 processSync(mapper);
7225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7226 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7228 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7229
7230 // Ensure no more events were generated.
7231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7233 }
7234}
7235
Prabir Pradhana9df3162022-12-05 23:57:27 +00007236// --- TouchscreenPrecisionTests ---
7237
7238// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
7239// in various orientations and with different display rotations. We configure the touchscreen to
7240// have a higher resolution than that of the display by an integer scale factor in each axis so that
7241// we can enforce that coordinates match precisely as expected.
7242class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
7243 public ::testing::WithParamInterface<ui::Rotation> {
7244public:
7245 void SetUp() override {
7246 SingleTouchInputMapperTest::SetUp();
7247
7248 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7249 // four times the resolution of the display in the Y axis.
7250 prepareButtons();
7251 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007252 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7253 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007254 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007255 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7256 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007257 }
7258
7259 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7260 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7261 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7262 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7263
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007264 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7265 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7266
7267 static const int32_t PRECISION_RAW_X_FLAT = 16;
7268 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7269
7270 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7271 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7272
Prabir Pradhana9df3162022-12-05 23:57:27 +00007273 static const std::array<Point, 4> kRawCorners;
7274};
7275
7276const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7277 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7278 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7279 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7280 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7281}};
7282
7283// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7284// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7285// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7286TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7287 enum class Orientation {
7288 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7289 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7290 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7291 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7292 ftl_last = ORIENTATION_270,
7293 };
7294 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7295 Orientation::ORIENTATION_270;
7296 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7297 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7298 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7299 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7300 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7301 };
7302
7303 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7304
7305 // Configure the touchscreen as being installed in the one of the four different orientations
7306 // relative to the display.
7307 addConfigurationProperty("touch.deviceType", "touchScreen");
7308 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7309 prepareDisplay(ui::ROTATION_0);
7310
Arpit Singha8c236b2023-04-25 13:56:05 +00007311 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00007312
7313 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7314 // orientations of either 90 or 270) this means the display's natural resolution will be
7315 // flipped.
7316 const bool displayRotated =
7317 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7318 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7319 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7320 const Rect physicalFrame{0, 0, width, height};
7321 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7322
7323 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7324 const float expectedPrecisionX = displayRotated ? 4 : 2;
7325 const float expectedPrecisionY = displayRotated ? 2 : 4;
7326
7327 // Test all four corners.
7328 for (int i = 0; i < 4; i++) {
7329 const auto& raw = kRawCorners[i];
7330 processDown(mapper, raw.x, raw.y);
7331 processSync(mapper);
7332 const auto& expected = expectedPoints[i];
7333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7334 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7335 WithCoords(expected.x, expected.y),
7336 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7337 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7338 << "with touchscreen orientation "
7339 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7340 << expected.x << ", " << expected.y << ").";
7341 processUp(mapper);
7342 processSync(mapper);
7343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7344 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7345 WithCoords(expected.x, expected.y))));
7346 }
7347}
7348
Prabir Pradhan82687402022-12-06 01:32:53 +00007349TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7350 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7351 kMappedCorners = {
7352 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7353 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7354 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7355 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7356 };
7357
7358 const ui::Rotation displayRotation = GetParam();
7359
7360 addConfigurationProperty("touch.deviceType", "touchScreen");
7361 prepareDisplay(displayRotation);
7362
Arpit Singha8c236b2023-04-25 13:56:05 +00007363 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00007364
7365 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7366
7367 // Test all four corners.
7368 for (int i = 0; i < 4; i++) {
7369 const auto& expected = expectedPoints[i];
7370 const auto& raw = kRawCorners[i];
7371 processDown(mapper, raw.x, raw.y);
7372 processSync(mapper);
7373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7374 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7375 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7376 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7377 << "with display rotation " << ui::toCString(displayRotation)
7378 << ", expected point (" << expected.x << ", " << expected.y << ").";
7379 processUp(mapper);
7380 processSync(mapper);
7381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7382 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7383 WithCoords(expected.x, expected.y))));
7384 }
7385}
7386
Prabir Pradhan3e798762022-12-02 21:02:11 +00007387TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7388 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7389 kMappedCorners = {
7390 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7391 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7392 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7393 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7394 };
7395
7396 const ui::Rotation displayRotation = GetParam();
7397
7398 addConfigurationProperty("touch.deviceType", "touchScreen");
7399 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7400
Arpit Singha8c236b2023-04-25 13:56:05 +00007401 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00007402
7403 // Ori 270, so width and height swapped
7404 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7405 prepareDisplay(displayRotation);
7406 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7407
7408 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7409
7410 // Test all four corners.
7411 for (int i = 0; i < 4; i++) {
7412 const auto& expected = expectedPoints[i];
7413 const auto& raw = kRawCorners[i];
7414 processDown(mapper, raw.x, raw.y);
7415 processSync(mapper);
7416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7417 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7418 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7419 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7420 << "with display rotation " << ui::toCString(displayRotation)
7421 << ", expected point (" << expected.x << ", " << expected.y << ").";
7422 processUp(mapper);
7423 processSync(mapper);
7424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7425 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7426 WithCoords(expected.x, expected.y))));
7427 }
7428}
7429
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007430TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7431 const ui::Rotation displayRotation = GetParam();
7432
7433 addConfigurationProperty("touch.deviceType", "touchScreen");
7434 prepareDisplay(displayRotation);
7435
7436 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00007437 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007438
7439 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7440 // MotionRanges use display pixels as their units
7441 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7442 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7443
7444 // The MotionRanges should be oriented in the rotated display's coordinate space
7445 const bool displayRotated =
7446 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7447
7448 constexpr float MAX_X = 479.5;
7449 constexpr float MAX_Y = 799.75;
7450 EXPECT_EQ(xRange->min, 0.f);
7451 EXPECT_EQ(yRange->min, 0.f);
7452 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7453 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7454
7455 EXPECT_EQ(xRange->flat, 8.f);
7456 EXPECT_EQ(yRange->flat, 8.f);
7457
7458 EXPECT_EQ(xRange->fuzz, 2.f);
7459 EXPECT_EQ(yRange->fuzz, 2.f);
7460
7461 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7462 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7463}
7464
Prabir Pradhana9df3162022-12-05 23:57:27 +00007465// Run the precision tests for all rotations.
7466INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7467 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7468 ui::ROTATION_270),
7469 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7470 return ftl::enum_string(testParamInfo.param);
7471 });
7472
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007473// --- ExternalStylusFusionTest ---
7474
7475class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7476public:
7477 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7478 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007479 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007480 prepareButtons();
7481 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007482 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007483
7484 mStylusState.when = ARBITRARY_TIME;
7485 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007486 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007487 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007488 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007489 processExternalStylusState(mapper);
7490 return mapper;
7491 }
7492
7493 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7494 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7495 for (const NotifyArgs& args : generatedArgs) {
7496 mFakeListener->notify(args);
7497 }
7498 // Loop the reader to flush the input listener queue.
7499 mReader->loopOnce();
7500 return generatedArgs;
7501 }
7502
7503protected:
7504 StylusState mStylusState{};
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007505
7506 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7507 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007508 AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007509
7510 // The first pointer is withheld.
7511 processDown(mapper, 100, 200);
7512 processSync(mapper);
7513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7514 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7515 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7516
7517 // The external stylus reports pressure. The withheld finger pointer is released as a
7518 // stylus.
7519 mStylusState.pressure = 1.f;
7520 processExternalStylusState(mapper);
7521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7522 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7523 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7524
7525 // Subsequent pointer events are not withheld.
7526 processMove(mapper, 101, 201);
7527 processSync(mapper);
7528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7529 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7530
7531 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7533 }
7534
7535 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7536 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7537
7538 // Releasing the touch pointer ends the gesture.
7539 processUp(mapper);
7540 processSync(mapper);
7541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007542 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007543 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007544
7545 mStylusState.pressure = 0.f;
7546 processExternalStylusState(mapper);
7547 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7549 }
7550
7551 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007552 // When stylus fusion is not successful, events should be reported with the original source.
7553 // In this case, it is from a touchscreen.
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007554 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007555 AllOf(WithSource(AINPUT_SOURCE_TOUCHSCREEN), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007556
7557 // The first pointer is withheld when an external stylus is connected,
7558 // and a timeout is requested.
7559 processDown(mapper, 100, 200);
7560 processSync(mapper);
7561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7562 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7563 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7564
7565 // If the timeout expires early, it is requested again.
7566 handleTimeout(mapper, ARBITRARY_TIME + 1);
7567 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7568 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7569
7570 // When the timeout expires, the withheld touch is released as a finger pointer.
7571 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7573 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7574
7575 // Subsequent pointer events are not withheld.
7576 processMove(mapper, 101, 201);
7577 processSync(mapper);
7578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7579 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7580 processUp(mapper);
7581 processSync(mapper);
7582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7583 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7584
7585 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7587 }
7588
7589private:
7590 InputDeviceInfo mExternalStylusDeviceInfo{};
7591};
7592
7593TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7594 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007595 ASSERT_EQ(STYLUS_FUSION_SOURCE, mapper.getSources());
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007596}
7597
7598TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7599 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7600 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7601}
7602
7603TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7604 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7605 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7606}
7607
7608// Test a successful stylus fusion gesture where the pressure is reported by the external
7609// before the touch is reported by the touchscreen.
7610TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7611 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007612 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007613
7614 // The external stylus reports pressure first. It is ignored for now.
7615 mStylusState.pressure = 1.f;
7616 processExternalStylusState(mapper);
7617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7618 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7619
7620 // When the touch goes down afterwards, it is reported as a stylus pointer.
7621 processDown(mapper, 100, 200);
7622 processSync(mapper);
7623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7624 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7625 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7626
7627 processMove(mapper, 101, 201);
7628 processSync(mapper);
7629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7630 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7631 processUp(mapper);
7632 processSync(mapper);
7633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7634 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7635
7636 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7638}
7639
7640TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7641 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7642
7643 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7644 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7645
7646 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7647 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7648 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7649 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7650}
7651
7652TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7653 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007654 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007655
7656 mStylusState.pressure = 0.8f;
7657 processExternalStylusState(mapper);
7658 processDown(mapper, 100, 200);
7659 processSync(mapper);
7660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7661 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7662 WithPressure(0.8f))));
7663 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7664
7665 // The external stylus reports a pressure change. We wait for some time for a touch event.
7666 mStylusState.pressure = 0.6f;
7667 processExternalStylusState(mapper);
7668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7669 ASSERT_NO_FATAL_FAILURE(
7670 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7671
7672 // If a touch is reported within the timeout, it reports the updated pressure.
7673 processMove(mapper, 101, 201);
7674 processSync(mapper);
7675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7676 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7677 WithPressure(0.6f))));
7678 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7679
7680 // There is another pressure change.
7681 mStylusState.pressure = 0.5f;
7682 processExternalStylusState(mapper);
7683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7684 ASSERT_NO_FATAL_FAILURE(
7685 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7686
7687 // If a touch is not reported within the timeout, a move event is generated to report
7688 // the new pressure.
7689 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7691 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7692 WithPressure(0.5f))));
7693
7694 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7695 // repeated indefinitely.
7696 mStylusState.pressure = 0.0f;
7697 processExternalStylusState(mapper);
7698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7699 ASSERT_NO_FATAL_FAILURE(
7700 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7701 processMove(mapper, 102, 202);
7702 processSync(mapper);
7703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7704 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7705 WithPressure(0.5f))));
7706 processMove(mapper, 103, 203);
7707 processSync(mapper);
7708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7709 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7710 WithPressure(0.5f))));
7711
7712 processUp(mapper);
7713 processSync(mapper);
7714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007715 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007716 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007717
7718 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7720}
7721
7722TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7723 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007724 auto source = WithSource(STYLUS_FUSION_SOURCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007725
7726 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007727 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007728 processExternalStylusState(mapper);
7729 processDown(mapper, 100, 200);
7730 processSync(mapper);
7731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7732 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007733 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007734 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7735
7736 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007737 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007738 processExternalStylusState(mapper);
7739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7740 ASSERT_NO_FATAL_FAILURE(
7741 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7742
7743 // If a touch is reported within the timeout, it reports the updated pressure.
7744 processMove(mapper, 101, 201);
7745 processSync(mapper);
7746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7747 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007748 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007749 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7750
7751 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007752 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007753 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 not reported within the timeout, a move event is generated to report
7759 // the new tool type.
7760 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7762 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007763 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007764
7765 processUp(mapper);
7766 processSync(mapper);
7767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7768 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007769 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007770
7771 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7773}
7774
7775TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7776 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007777 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007778
7779 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7780
7781 // The external stylus reports a button change. We wait for some time for a touch event.
7782 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7783 processExternalStylusState(mapper);
7784 ASSERT_NO_FATAL_FAILURE(
7785 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7786
7787 // If a touch is reported within the timeout, it reports the updated button state.
7788 processMove(mapper, 101, 201);
7789 processSync(mapper);
7790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7791 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7792 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7794 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7795 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7796 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7797
7798 // The button is now released.
7799 mStylusState.buttons = 0;
7800 processExternalStylusState(mapper);
7801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7802 ASSERT_NO_FATAL_FAILURE(
7803 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7804
7805 // If a touch is not reported within the timeout, a move event is generated to report
7806 // the new button state.
7807 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7809 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7810 WithButtonState(0))));
7811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007812 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7813 WithButtonState(0))));
7814
7815 processUp(mapper);
7816 processSync(mapper);
7817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007818 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7819
7820 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7822}
7823
Michael Wrightd02c5b62014-02-10 15:10:22 -08007824// --- MultiTouchInputMapperTest ---
7825
7826class MultiTouchInputMapperTest : public TouchInputMapperTest {
7827protected:
7828 void prepareAxes(int axes);
7829
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007830 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7831 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7832 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7833 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7834 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7835 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7836 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7837 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7838 void processId(MultiTouchInputMapper& mapper, int32_t id);
7839 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7840 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7841 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007842 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007843 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007844 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7845 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007846};
7847
7848void MultiTouchInputMapperTest::prepareAxes(int axes) {
7849 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007850 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7851 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007852 }
7853 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007854 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7855 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007856 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007857 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7858 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007859 }
7860 }
7861 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007862 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7863 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007864 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007865 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007866 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007867 }
7868 }
7869 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007870 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7871 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007872 }
7873 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007874 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7875 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007876 }
7877 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007878 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7879 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007880 }
7881 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007882 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7883 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007884 }
7885 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007886 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7887 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007888 }
7889 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007890 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007891 }
7892}
7893
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007894void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7895 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007896 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7897 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007898}
7899
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007900void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7901 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007902 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007903}
7904
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007905void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7906 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007907 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007908}
7909
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007910void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007911 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007912}
7913
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007914void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007915 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007916}
7917
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007918void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7919 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007920 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007921}
7922
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007923void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007924 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007925}
7926
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007927void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007928 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007929}
7930
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007931void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007932 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007933}
7934
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007935void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007936 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007937}
7938
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007939void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007940 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007941}
7942
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007943void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7944 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007945 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007946}
7947
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007948void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7949 int32_t value) {
7950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7951 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7952}
7953
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007954void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007955 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007956}
7957
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007958void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7959 nsecs_t readTime) {
7960 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007961}
7962
Michael Wrightd02c5b62014-02-10 15:10:22 -08007963TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007964 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007965 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007966 prepareAxes(POSITION);
7967 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007968 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007969
arthurhungdcef2dc2020-08-11 14:47:50 +08007970 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007971
7972 NotifyMotionArgs motionArgs;
7973
7974 // Two fingers down at once.
7975 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7976 processPosition(mapper, x1, y1);
7977 processMTSync(mapper);
7978 processPosition(mapper, x2, y2);
7979 processMTSync(mapper);
7980 processSync(mapper);
7981
7982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7983 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7984 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7985 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7986 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7987 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7988 ASSERT_EQ(0, motionArgs.flags);
7989 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7990 ASSERT_EQ(0, motionArgs.buttonState);
7991 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007992 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007993 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007994 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7996 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7997 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7998 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7999 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8000
8001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8002 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8003 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8004 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8005 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008006 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008007 ASSERT_EQ(0, motionArgs.flags);
8008 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8009 ASSERT_EQ(0, motionArgs.buttonState);
8010 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008011 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008012 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008013 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008014 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008015 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8017 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8018 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8019 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8020 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8021 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8022 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8023
8024 // Move.
8025 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8026 processPosition(mapper, x1, y1);
8027 processMTSync(mapper);
8028 processPosition(mapper, x2, y2);
8029 processMTSync(mapper);
8030 processSync(mapper);
8031
8032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8033 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8034 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8035 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8036 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8038 ASSERT_EQ(0, motionArgs.flags);
8039 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8040 ASSERT_EQ(0, motionArgs.buttonState);
8041 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008042 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008043 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008044 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008045 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008046 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8048 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8049 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8050 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8051 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8052 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8053 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8054
8055 // First finger up.
8056 x2 += 15; y2 -= 20;
8057 processPosition(mapper, x2, y2);
8058 processMTSync(mapper);
8059 processSync(mapper);
8060
8061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8062 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8063 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8064 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8065 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008066 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008067 ASSERT_EQ(0, motionArgs.flags);
8068 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8069 ASSERT_EQ(0, motionArgs.buttonState);
8070 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008071 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008073 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008074 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008075 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008076 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8077 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8078 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8079 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8080 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8081 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8082 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8083
8084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8085 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8086 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8087 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8088 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8089 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8090 ASSERT_EQ(0, motionArgs.flags);
8091 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8092 ASSERT_EQ(0, motionArgs.buttonState);
8093 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008094 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008095 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008096 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8098 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8099 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8100 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8101 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8102
8103 // Move.
8104 x2 += 20; y2 -= 25;
8105 processPosition(mapper, x2, y2);
8106 processMTSync(mapper);
8107 processSync(mapper);
8108
8109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8110 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8111 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8112 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8113 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8115 ASSERT_EQ(0, motionArgs.flags);
8116 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8117 ASSERT_EQ(0, motionArgs.buttonState);
8118 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008119 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008120 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008121 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008122 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8123 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8124 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8125 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8126 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8127
8128 // New finger down.
8129 int32_t x3 = 700, y3 = 300;
8130 processPosition(mapper, x2, y2);
8131 processMTSync(mapper);
8132 processPosition(mapper, x3, y3);
8133 processMTSync(mapper);
8134 processSync(mapper);
8135
8136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8137 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8138 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8139 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8140 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008141 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008142 ASSERT_EQ(0, motionArgs.flags);
8143 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8144 ASSERT_EQ(0, motionArgs.buttonState);
8145 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008146 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008147 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008148 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008149 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008150 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008151 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8152 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8154 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8155 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8156 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8157 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8158
8159 // Second finger up.
8160 x3 += 30; y3 -= 20;
8161 processPosition(mapper, x3, y3);
8162 processMTSync(mapper);
8163 processSync(mapper);
8164
8165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8166 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8167 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8168 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8169 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008170 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008171 ASSERT_EQ(0, motionArgs.flags);
8172 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8173 ASSERT_EQ(0, motionArgs.buttonState);
8174 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008175 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008176 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008177 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008178 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008179 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8181 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8183 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8184 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8185 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8186 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8187
8188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8189 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8190 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8191 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8192 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8193 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8194 ASSERT_EQ(0, motionArgs.flags);
8195 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8196 ASSERT_EQ(0, motionArgs.buttonState);
8197 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008198 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008199 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008200 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8202 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8203 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8204 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8205 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8206
8207 // Last finger up.
8208 processMTSync(mapper);
8209 processSync(mapper);
8210
8211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8212 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8213 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8214 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8215 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8216 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8217 ASSERT_EQ(0, motionArgs.flags);
8218 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8219 ASSERT_EQ(0, motionArgs.buttonState);
8220 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008221 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008222 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008223 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8225 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8226 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8227 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8228 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8229
8230 // Should not have sent any more keys or motions.
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8233}
8234
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008235TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8236 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008237 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008238
8239 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8240 /*fuzz*/ 0, /*resolution*/ 10);
8241 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8242 /*fuzz*/ 0, /*resolution*/ 11);
8243 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8244 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8245 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8246 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8247 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8248 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8249 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8250 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8251
Arpit Singha8c236b2023-04-25 13:56:05 +00008252 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008253
8254 // X and Y axes
8255 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8256 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8257 // Touch major and minor
8258 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8259 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8260 // Tool major and minor
8261 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8262 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8263}
8264
8265TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8266 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008267 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008268
8269 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8270 /*fuzz*/ 0, /*resolution*/ 10);
8271 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8272 /*fuzz*/ 0, /*resolution*/ 11);
8273
8274 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8275
Arpit Singha8c236b2023-04-25 13:56:05 +00008276 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008277
8278 // Touch major and minor
8279 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8280 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8281 // Tool major and minor
8282 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8283 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8284}
8285
Michael Wrightd02c5b62014-02-10 15:10:22 -08008286TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008287 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008288 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008289 prepareAxes(POSITION | ID);
8290 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008291 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008292
arthurhungdcef2dc2020-08-11 14:47:50 +08008293 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008294
8295 NotifyMotionArgs motionArgs;
8296
8297 // Two fingers down at once.
8298 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8299 processPosition(mapper, x1, y1);
8300 processId(mapper, 1);
8301 processMTSync(mapper);
8302 processPosition(mapper, x2, y2);
8303 processId(mapper, 2);
8304 processMTSync(mapper);
8305 processSync(mapper);
8306
8307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8308 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008309 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008310 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008311 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8313 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8314
8315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008316 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008317 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008318 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008319 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008320 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008321 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8323 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8324 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8325 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8326
8327 // Move.
8328 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8329 processPosition(mapper, x1, y1);
8330 processId(mapper, 1);
8331 processMTSync(mapper);
8332 processPosition(mapper, x2, y2);
8333 processId(mapper, 2);
8334 processMTSync(mapper);
8335 processSync(mapper);
8336
8337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8338 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008339 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008340 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008341 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008342 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008343 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8345 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8347 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8348
8349 // First finger up.
8350 x2 += 15; y2 -= 20;
8351 processPosition(mapper, x2, y2);
8352 processId(mapper, 2);
8353 processMTSync(mapper);
8354 processSync(mapper);
8355
8356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008357 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008358 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008359 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008360 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008361 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008362 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8364 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8366 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8367
8368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8369 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008370 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008371 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008372 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8374 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8375
8376 // Move.
8377 x2 += 20; y2 -= 25;
8378 processPosition(mapper, x2, y2);
8379 processId(mapper, 2);
8380 processMTSync(mapper);
8381 processSync(mapper);
8382
8383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008385 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008386 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008387 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8389 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8390
8391 // New finger down.
8392 int32_t x3 = 700, y3 = 300;
8393 processPosition(mapper, x2, y2);
8394 processId(mapper, 2);
8395 processMTSync(mapper);
8396 processPosition(mapper, x3, y3);
8397 processId(mapper, 3);
8398 processMTSync(mapper);
8399 processSync(mapper);
8400
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008402 ASSERT_EQ(ACTION_POINTER_0_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(x3), toDisplayY(y3), 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 // Second finger up.
8414 x3 += 30; y3 -= 20;
8415 processPosition(mapper, x3, y3);
8416 processId(mapper, 3);
8417 processMTSync(mapper);
8418 processSync(mapper);
8419
8420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008421 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008422 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008423 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008424 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008425 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008426 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8428 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8429 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8430 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8431
8432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8433 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008434 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008435 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008436 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008437 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8438 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8439
8440 // Last finger up.
8441 processMTSync(mapper);
8442 processSync(mapper);
8443
8444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8445 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008446 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008447 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008448 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8450 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8451
8452 // Should not have sent any more keys or motions.
8453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8455}
8456
8457TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008458 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008459 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008460 prepareAxes(POSITION | ID | SLOT);
8461 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008462 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008463
arthurhungdcef2dc2020-08-11 14:47:50 +08008464 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008465
8466 NotifyMotionArgs motionArgs;
8467
8468 // Two fingers down at once.
8469 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8470 processPosition(mapper, x1, y1);
8471 processId(mapper, 1);
8472 processSlot(mapper, 1);
8473 processPosition(mapper, x2, y2);
8474 processId(mapper, 2);
8475 processSync(mapper);
8476
8477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8478 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008479 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008480 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008481 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008482 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8483 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8484
8485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008486 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008487 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008488 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008489 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008490 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008491 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8493 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8495 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8496
8497 // Move.
8498 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8499 processSlot(mapper, 0);
8500 processPosition(mapper, x1, y1);
8501 processSlot(mapper, 1);
8502 processPosition(mapper, x2, y2);
8503 processSync(mapper);
8504
8505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8506 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008507 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008508 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008509 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008510 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008511 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8513 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8515 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8516
8517 // First finger up.
8518 x2 += 15; y2 -= 20;
8519 processSlot(mapper, 0);
8520 processId(mapper, -1);
8521 processSlot(mapper, 1);
8522 processPosition(mapper, x2, y2);
8523 processSync(mapper);
8524
8525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008526 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008527 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008528 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008529 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008530 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008531 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008532 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8533 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8535 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8536
8537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8538 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008539 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008540 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008541 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8543 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8544
8545 // Move.
8546 x2 += 20; y2 -= 25;
8547 processPosition(mapper, x2, y2);
8548 processSync(mapper);
8549
8550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008552 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008553 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008554 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8556 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8557
8558 // New finger down.
8559 int32_t x3 = 700, y3 = 300;
8560 processPosition(mapper, x2, y2);
8561 processSlot(mapper, 0);
8562 processId(mapper, 3);
8563 processPosition(mapper, x3, y3);
8564 processSync(mapper);
8565
8566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008567 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008568 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008569 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008570 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008571 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008572 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8574 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8576 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8577
8578 // Second finger up.
8579 x3 += 30; y3 -= 20;
8580 processSlot(mapper, 1);
8581 processId(mapper, -1);
8582 processSlot(mapper, 0);
8583 processPosition(mapper, x3, y3);
8584 processSync(mapper);
8585
8586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008587 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008588 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008589 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008590 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008591 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008592 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8594 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8596 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8597
8598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008600 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008601 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008602 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8604 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8605
8606 // Last finger up.
8607 processId(mapper, -1);
8608 processSync(mapper);
8609
8610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8611 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008612 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008613 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008614 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8616 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8617
8618 // Should not have sent any more keys or motions.
8619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8621}
8622
8623TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008624 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008625 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008626 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008627 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008628
8629 // These calculations are based on the input device calibration documentation.
8630 int32_t rawX = 100;
8631 int32_t rawY = 200;
8632 int32_t rawTouchMajor = 7;
8633 int32_t rawTouchMinor = 6;
8634 int32_t rawToolMajor = 9;
8635 int32_t rawToolMinor = 8;
8636 int32_t rawPressure = 11;
8637 int32_t rawDistance = 0;
8638 int32_t rawOrientation = 3;
8639 int32_t id = 5;
8640
8641 float x = toDisplayX(rawX);
8642 float y = toDisplayY(rawY);
8643 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8644 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8645 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8646 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8647 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8648 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8649 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8650 float distance = float(rawDistance);
8651
8652 processPosition(mapper, rawX, rawY);
8653 processTouchMajor(mapper, rawTouchMajor);
8654 processTouchMinor(mapper, rawTouchMinor);
8655 processToolMajor(mapper, rawToolMajor);
8656 processToolMinor(mapper, rawToolMinor);
8657 processPressure(mapper, rawPressure);
8658 processOrientation(mapper, rawOrientation);
8659 processDistance(mapper, rawDistance);
8660 processId(mapper, id);
8661 processMTSync(mapper);
8662 processSync(mapper);
8663
8664 NotifyMotionArgs args;
8665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8666 ASSERT_EQ(0, args.pointerProperties[0].id);
8667 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8668 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8669 orientation, distance));
8670}
8671
8672TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008673 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008674 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008675 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8676 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00008677 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008678
8679 // These calculations are based on the input device calibration documentation.
8680 int32_t rawX = 100;
8681 int32_t rawY = 200;
8682 int32_t rawTouchMajor = 140;
8683 int32_t rawTouchMinor = 120;
8684 int32_t rawToolMajor = 180;
8685 int32_t rawToolMinor = 160;
8686
8687 float x = toDisplayX(rawX);
8688 float y = toDisplayY(rawY);
8689 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8690 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8691 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8692 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8693 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8694
8695 processPosition(mapper, rawX, rawY);
8696 processTouchMajor(mapper, rawTouchMajor);
8697 processTouchMinor(mapper, rawTouchMinor);
8698 processToolMajor(mapper, rawToolMajor);
8699 processToolMinor(mapper, rawToolMinor);
8700 processMTSync(mapper);
8701 processSync(mapper);
8702
8703 NotifyMotionArgs args;
8704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8706 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8707}
8708
8709TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
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);
8713 addConfigurationProperty("touch.size.calibration", "diameter");
8714 addConfigurationProperty("touch.size.scale", "10");
8715 addConfigurationProperty("touch.size.bias", "160");
8716 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00008717 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008718
8719 // These calculations are based on the input device calibration documentation.
8720 // Note: We only provide a single common touch/tool value because the device is assumed
8721 // not to emit separate values for each pointer (isSummed = 1).
8722 int32_t rawX = 100;
8723 int32_t rawY = 200;
8724 int32_t rawX2 = 150;
8725 int32_t rawY2 = 250;
8726 int32_t rawTouchMajor = 5;
8727 int32_t rawToolMajor = 8;
8728
8729 float x = toDisplayX(rawX);
8730 float y = toDisplayY(rawY);
8731 float x2 = toDisplayX(rawX2);
8732 float y2 = toDisplayY(rawY2);
8733 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8734 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8735 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8736
8737 processPosition(mapper, rawX, rawY);
8738 processTouchMajor(mapper, rawTouchMajor);
8739 processToolMajor(mapper, rawToolMajor);
8740 processMTSync(mapper);
8741 processPosition(mapper, rawX2, rawY2);
8742 processTouchMajor(mapper, rawTouchMajor);
8743 processToolMajor(mapper, rawToolMajor);
8744 processMTSync(mapper);
8745 processSync(mapper);
8746
8747 NotifyMotionArgs args;
8748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8749 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8750
8751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008752 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008753 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8755 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8756 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8757 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8758}
8759
8760TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008761 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008762 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008763 prepareAxes(POSITION | TOUCH | TOOL);
8764 addConfigurationProperty("touch.size.calibration", "area");
8765 addConfigurationProperty("touch.size.scale", "43");
8766 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00008767 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008768
8769 // These calculations are based on the input device calibration documentation.
8770 int32_t rawX = 100;
8771 int32_t rawY = 200;
8772 int32_t rawTouchMajor = 5;
8773 int32_t rawToolMajor = 8;
8774
8775 float x = toDisplayX(rawX);
8776 float y = toDisplayY(rawY);
8777 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8778 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8779 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8780
8781 processPosition(mapper, rawX, rawY);
8782 processTouchMajor(mapper, rawTouchMajor);
8783 processToolMajor(mapper, rawToolMajor);
8784 processMTSync(mapper);
8785 processSync(mapper);
8786
8787 NotifyMotionArgs args;
8788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8790 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8791}
8792
8793TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008794 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008795 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008796 prepareAxes(POSITION | PRESSURE);
8797 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8798 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00008799 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008800
Michael Wrightaa449c92017-12-13 21:21:43 +00008801 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008802 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008803 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8804 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8805 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8806
Michael Wrightd02c5b62014-02-10 15:10:22 -08008807 // These calculations are based on the input device calibration documentation.
8808 int32_t rawX = 100;
8809 int32_t rawY = 200;
8810 int32_t rawPressure = 60;
8811
8812 float x = toDisplayX(rawX);
8813 float y = toDisplayY(rawY);
8814 float pressure = float(rawPressure) * 0.01f;
8815
8816 processPosition(mapper, rawX, rawY);
8817 processPressure(mapper, rawPressure);
8818 processMTSync(mapper);
8819 processSync(mapper);
8820
8821 NotifyMotionArgs args;
8822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8823 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8824 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8825}
8826
8827TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008828 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008829 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008830 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008831 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008832
8833 NotifyMotionArgs motionArgs;
8834 NotifyKeyArgs keyArgs;
8835
8836 processId(mapper, 1);
8837 processPosition(mapper, 100, 200);
8838 processSync(mapper);
8839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8840 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8841 ASSERT_EQ(0, motionArgs.buttonState);
8842
8843 // press BTN_LEFT, release BTN_LEFT
8844 processKey(mapper, BTN_LEFT, 1);
8845 processSync(mapper);
8846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8847 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8848 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8849
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8851 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8852 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8853
Michael Wrightd02c5b62014-02-10 15:10:22 -08008854 processKey(mapper, BTN_LEFT, 0);
8855 processSync(mapper);
8856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008857 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008858 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008859
8860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008861 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008862 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008863
8864 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8865 processKey(mapper, BTN_RIGHT, 1);
8866 processKey(mapper, BTN_MIDDLE, 1);
8867 processSync(mapper);
8868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8869 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8870 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8871 motionArgs.buttonState);
8872
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8874 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8875 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8876
8877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8878 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8879 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8880 motionArgs.buttonState);
8881
Michael Wrightd02c5b62014-02-10 15:10:22 -08008882 processKey(mapper, BTN_RIGHT, 0);
8883 processSync(mapper);
8884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008885 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008886 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008887
8888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008889 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008890 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008891
8892 processKey(mapper, BTN_MIDDLE, 0);
8893 processSync(mapper);
8894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008895 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008896 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008897
8898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008899 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008900 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008901
8902 // press BTN_BACK, release BTN_BACK
8903 processKey(mapper, BTN_BACK, 1);
8904 processSync(mapper);
8905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8906 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8907 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008908
Michael Wrightd02c5b62014-02-10 15:10:22 -08008909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008910 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008911 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8912
8913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8914 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8915 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008916
8917 processKey(mapper, BTN_BACK, 0);
8918 processSync(mapper);
8919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008920 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008921 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008922
8923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008925 ASSERT_EQ(0, motionArgs.buttonState);
8926
Michael Wrightd02c5b62014-02-10 15:10:22 -08008927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8928 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8929 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8930
8931 // press BTN_SIDE, release BTN_SIDE
8932 processKey(mapper, BTN_SIDE, 1);
8933 processSync(mapper);
8934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8935 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8936 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008937
Michael Wrightd02c5b62014-02-10 15:10:22 -08008938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008939 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008940 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8941
8942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8943 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8944 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008945
8946 processKey(mapper, BTN_SIDE, 0);
8947 processSync(mapper);
8948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008949 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008950 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008951
8952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008953 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008954 ASSERT_EQ(0, motionArgs.buttonState);
8955
Michael Wrightd02c5b62014-02-10 15:10:22 -08008956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8957 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8958 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8959
8960 // press BTN_FORWARD, release BTN_FORWARD
8961 processKey(mapper, BTN_FORWARD, 1);
8962 processSync(mapper);
8963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8964 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8965 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008966
Michael Wrightd02c5b62014-02-10 15:10:22 -08008967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008968 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008969 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8970
8971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8972 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8973 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008974
8975 processKey(mapper, BTN_FORWARD, 0);
8976 processSync(mapper);
8977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008978 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008979 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008980
8981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008982 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008983 ASSERT_EQ(0, motionArgs.buttonState);
8984
Michael Wrightd02c5b62014-02-10 15:10:22 -08008985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8986 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8987 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8988
8989 // press BTN_EXTRA, release BTN_EXTRA
8990 processKey(mapper, BTN_EXTRA, 1);
8991 processSync(mapper);
8992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8993 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8994 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008995
Michael Wrightd02c5b62014-02-10 15:10:22 -08008996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008997 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008998 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8999
9000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9001 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9002 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009003
9004 processKey(mapper, BTN_EXTRA, 0);
9005 processSync(mapper);
9006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009007 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009008 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009009
9010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009011 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009012 ASSERT_EQ(0, motionArgs.buttonState);
9013
Michael Wrightd02c5b62014-02-10 15:10:22 -08009014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9015 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9016 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9017
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9019
Michael Wrightd02c5b62014-02-10 15:10:22 -08009020 // press BTN_STYLUS, release BTN_STYLUS
9021 processKey(mapper, BTN_STYLUS, 1);
9022 processSync(mapper);
9023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009025 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
9026
9027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9028 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9029 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009030
9031 processKey(mapper, BTN_STYLUS, 0);
9032 processSync(mapper);
9033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009034 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009035 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009036
9037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009039 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009040
9041 // press BTN_STYLUS2, release BTN_STYLUS2
9042 processKey(mapper, BTN_STYLUS2, 1);
9043 processSync(mapper);
9044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009046 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
9047
9048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9049 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9050 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009051
9052 processKey(mapper, BTN_STYLUS2, 0);
9053 processSync(mapper);
9054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009055 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009056 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009057
9058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009060 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009061
9062 // release touch
9063 processId(mapper, -1);
9064 processSync(mapper);
9065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9066 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9067 ASSERT_EQ(0, motionArgs.buttonState);
9068}
9069
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009070TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
9071 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009072 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009073 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00009074 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009075
9076 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
9077 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
9078
9079 // Touch down.
9080 processId(mapper, 1);
9081 processPosition(mapper, 100, 200);
9082 processSync(mapper);
9083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9084 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
9085
9086 // Press and release button mapped to the primary stylus button.
9087 processKey(mapper, BTN_A, 1);
9088 processSync(mapper);
9089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9090 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9091 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9093 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9094 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9095
9096 processKey(mapper, BTN_A, 0);
9097 processSync(mapper);
9098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9099 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9101 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9102
9103 // Press and release the HID usage mapped to the secondary stylus button.
9104 processHidUsage(mapper, 0xabcd, 1);
9105 processSync(mapper);
9106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9107 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9108 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9110 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9111 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9112
9113 processHidUsage(mapper, 0xabcd, 0);
9114 processSync(mapper);
9115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9116 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9118 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9119
9120 // Release touch.
9121 processId(mapper, -1);
9122 processSync(mapper);
9123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9124 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
9125}
9126
Michael Wrightd02c5b62014-02-10 15:10:22 -08009127TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009128 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009129 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009130 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009131 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009132
9133 NotifyMotionArgs motionArgs;
9134
9135 // default tool type is finger
9136 processId(mapper, 1);
9137 processPosition(mapper, 100, 200);
9138 processSync(mapper);
9139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9140 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009141 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009142
9143 // eraser
9144 processKey(mapper, BTN_TOOL_RUBBER, 1);
9145 processSync(mapper);
9146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009148 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009149
9150 // stylus
9151 processKey(mapper, BTN_TOOL_RUBBER, 0);
9152 processKey(mapper, BTN_TOOL_PEN, 1);
9153 processSync(mapper);
9154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009156 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009157
9158 // brush
9159 processKey(mapper, BTN_TOOL_PEN, 0);
9160 processKey(mapper, BTN_TOOL_BRUSH, 1);
9161 processSync(mapper);
9162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009164 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009165
9166 // pencil
9167 processKey(mapper, BTN_TOOL_BRUSH, 0);
9168 processKey(mapper, BTN_TOOL_PENCIL, 1);
9169 processSync(mapper);
9170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9171 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009172 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009173
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009174 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009175 processKey(mapper, BTN_TOOL_PENCIL, 0);
9176 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
9177 processSync(mapper);
9178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009180 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009181
9182 // mouse
9183 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9184 processKey(mapper, BTN_TOOL_MOUSE, 1);
9185 processSync(mapper);
9186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9187 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009188 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009189
9190 // lens
9191 processKey(mapper, BTN_TOOL_MOUSE, 0);
9192 processKey(mapper, BTN_TOOL_LENS, 1);
9193 processSync(mapper);
9194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9195 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009196 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009197
9198 // double-tap
9199 processKey(mapper, BTN_TOOL_LENS, 0);
9200 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9201 processSync(mapper);
9202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009204 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009205
9206 // triple-tap
9207 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9208 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9209 processSync(mapper);
9210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9211 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009212 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009213
9214 // quad-tap
9215 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9216 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9217 processSync(mapper);
9218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9219 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009220 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009221
9222 // finger
9223 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9224 processKey(mapper, BTN_TOOL_FINGER, 1);
9225 processSync(mapper);
9226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009228 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009229
9230 // stylus trumps finger
9231 processKey(mapper, BTN_TOOL_PEN, 1);
9232 processSync(mapper);
9233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009235 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009236
9237 // eraser trumps stylus
9238 processKey(mapper, BTN_TOOL_RUBBER, 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::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009243
9244 // mouse trumps eraser
9245 processKey(mapper, BTN_TOOL_MOUSE, 1);
9246 processSync(mapper);
9247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009249 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009250
9251 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9252 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9253 processSync(mapper);
9254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009256 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009257
9258 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9259 processToolType(mapper, MT_TOOL_PEN);
9260 processSync(mapper);
9261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9262 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009263 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009264
9265 // back to default tool type
9266 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9267 processKey(mapper, BTN_TOOL_MOUSE, 0);
9268 processKey(mapper, BTN_TOOL_RUBBER, 0);
9269 processKey(mapper, BTN_TOOL_PEN, 0);
9270 processKey(mapper, BTN_TOOL_FINGER, 0);
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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009275}
9276
9277TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009278 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009279 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009280 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009281 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009282 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009283
9284 NotifyMotionArgs motionArgs;
9285
9286 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9287 processId(mapper, 1);
9288 processPosition(mapper, 100, 200);
9289 processSync(mapper);
9290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9291 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9293 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9294
9295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9296 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9298 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9299
9300 // move a little
9301 processPosition(mapper, 150, 250);
9302 processSync(mapper);
9303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9304 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9306 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9307
9308 // down when BTN_TOUCH is pressed, pressure defaults to 1
9309 processKey(mapper, BTN_TOUCH, 1);
9310 processSync(mapper);
9311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9312 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9314 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9315
9316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9317 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9319 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9320
9321 // up when BTN_TOUCH is released, hover restored
9322 processKey(mapper, BTN_TOUCH, 0);
9323 processSync(mapper);
9324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9325 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9327 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9328
9329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9330 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9332 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9333
9334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9335 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9337 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9338
9339 // exit hover when pointer goes away
9340 processId(mapper, -1);
9341 processSync(mapper);
9342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9343 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9345 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9346}
9347
9348TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009349 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009350 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009351 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009352 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009353
9354 NotifyMotionArgs motionArgs;
9355
9356 // initially hovering because pressure is 0
9357 processId(mapper, 1);
9358 processPosition(mapper, 100, 200);
9359 processPressure(mapper, 0);
9360 processSync(mapper);
9361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9362 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9364 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9365
9366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9367 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9369 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9370
9371 // move a little
9372 processPosition(mapper, 150, 250);
9373 processSync(mapper);
9374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9375 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9377 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9378
9379 // down when pressure becomes non-zero
9380 processPressure(mapper, RAW_PRESSURE_MAX);
9381 processSync(mapper);
9382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9383 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9385 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9386
9387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9388 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9390 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9391
9392 // up when pressure becomes 0, hover restored
9393 processPressure(mapper, 0);
9394 processSync(mapper);
9395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9396 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9398 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9399
9400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9401 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9403 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9404
9405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9406 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9408 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9409
9410 // exit hover when pointer goes away
9411 processId(mapper, -1);
9412 processSync(mapper);
9413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9414 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9416 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9417}
9418
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009419/**
9420 * Set the input device port <--> display port associations, and check that the
9421 * events are routed to the display that matches the display port.
9422 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9423 */
9424TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009425 const std::string usb2 = "USB2";
9426 const uint8_t hdmi1 = 0;
9427 const uint8_t hdmi2 = 1;
9428 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009429 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009430
9431 addConfigurationProperty("touch.deviceType", "touchScreen");
9432 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009433 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009434
9435 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9436 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9437
9438 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9439 // for this input device is specified, and the matching viewport is not present,
9440 // the input device should be disabled (at the mapper level).
9441
9442 // Add viewport for display 2 on hdmi2
9443 prepareSecondaryDisplay(type, hdmi2);
9444 // Send a touch event
9445 processPosition(mapper, 100, 100);
9446 processSync(mapper);
9447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9448
9449 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009450 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009451 // Send a touch event again
9452 processPosition(mapper, 100, 100);
9453 processSync(mapper);
9454
9455 NotifyMotionArgs args;
9456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9457 ASSERT_EQ(DISPLAY_ID, args.displayId);
9458}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009459
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009460TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9461 addConfigurationProperty("touch.deviceType", "touchScreen");
9462 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009463 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009464
9465 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9466
Michael Wrighta9cf4192022-12-01 23:46:39 +00009467 prepareDisplay(ui::ROTATION_0);
9468 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009469
9470 // Send a touch event
9471 processPosition(mapper, 100, 100);
9472 processSync(mapper);
9473
9474 NotifyMotionArgs args;
9475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9476 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9477}
9478
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009479TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009480 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009481 std::shared_ptr<FakePointerController> fakePointerController =
9482 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009483 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009484 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009485 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009486
Garfield Tan888a6a42020-01-09 11:39:16 -08009487 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009488 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009489
Michael Wrighta9cf4192022-12-01 23:46:39 +00009490 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009491 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009492 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009493
Josep del Río2d8c79a2023-01-23 19:33:50 +00009494 // Check source is mouse that would obtain the PointerController.
9495 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009496
9497 NotifyMotionArgs motionArgs;
9498 processPosition(mapper, 100, 100);
9499 processSync(mapper);
9500
9501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9502 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9503 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9504}
9505
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009506/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009507 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9508 */
9509TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9510 addConfigurationProperty("touch.deviceType", "touchScreen");
9511 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009512 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009513
Michael Wrighta9cf4192022-12-01 23:46:39 +00009514 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009515 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9516 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9517 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9518 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009519
9520 NotifyMotionArgs args;
9521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9522 ASSERT_EQ(26, args.readTime);
9523
Harry Cutts33476232023-01-30 19:57:29 +00009524 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9525 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9526 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009527
9528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9529 ASSERT_EQ(33, args.readTime);
9530}
9531
9532/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009533 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9534 * events should not be delivered to the listener.
9535 */
9536TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9537 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009538 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009539 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009540 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009541 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009542 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009543 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009544
9545 NotifyMotionArgs motionArgs;
9546 processPosition(mapper, 100, 100);
9547 processSync(mapper);
9548
9549 mFakeListener->assertNotifyMotionWasNotCalled();
9550}
9551
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009552/**
9553 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9554 * the touch mapper can process the events and the events can be delivered to the listener.
9555 */
9556TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9557 addConfigurationProperty("touch.deviceType", "touchScreen");
9558 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009559 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009560 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009561 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009562 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009563 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009564
9565 NotifyMotionArgs motionArgs;
9566 processPosition(mapper, 100, 100);
9567 processSync(mapper);
9568
9569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9570 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9571}
9572
Josh Thielene986aed2023-06-01 14:17:30 +00009573/**
9574 * When the viewport is deactivated (isActive transitions from true to false),
9575 * and touch.enableForInactiveViewport is false, touches prior to the transition
9576 * should be cancelled.
9577 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08009578TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9579 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009580 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009581 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009582 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009583 std::optional<DisplayViewport> optionalDisplayViewport =
9584 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9585 ASSERT_TRUE(optionalDisplayViewport.has_value());
9586 DisplayViewport displayViewport = *optionalDisplayViewport;
9587
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009588 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009589 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009590 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08009591
9592 // Finger down
9593 int32_t x = 100, y = 100;
9594 processPosition(mapper, x, y);
9595 processSync(mapper);
9596
9597 NotifyMotionArgs motionArgs;
9598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9599 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9600
9601 // Deactivate display viewport
9602 displayViewport.isActive = false;
9603 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009604 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009605
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009606 // The ongoing touch should be canceled immediately
9607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9608 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9609
9610 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009611 x += 10, y += 10;
9612 processPosition(mapper, x, y);
9613 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009615
9616 // Reactivate display viewport
9617 displayViewport.isActive = true;
9618 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009619 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009620
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009621 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009622 x += 10, y += 10;
9623 processPosition(mapper, x, y);
9624 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9626 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009627}
9628
Josh Thielene986aed2023-06-01 14:17:30 +00009629/**
9630 * When the viewport is deactivated (isActive transitions from true to false),
9631 * and touch.enableForInactiveViewport is true, touches prior to the transition
9632 * should not be cancelled.
9633 */
9634TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
9635 addConfigurationProperty("touch.deviceType", "touchScreen");
9636 addConfigurationProperty("touch.enableForInactiveViewport", "1");
9637 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
9638 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
9639 std::optional<DisplayViewport> optionalDisplayViewport =
9640 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9641 ASSERT_TRUE(optionalDisplayViewport.has_value());
9642 DisplayViewport displayViewport = *optionalDisplayViewport;
9643
9644 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9645 prepareAxes(POSITION);
9646 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
9647
9648 // Finger down
9649 int32_t x = 100, y = 100;
9650 processPosition(mapper, x, y);
9651 processSync(mapper);
9652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9653 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9654
9655 // Deactivate display viewport
9656 displayViewport.isActive = false;
9657 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9658 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9659
9660 // The ongoing touch should not be canceled
9661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9662
9663 // Finger move is not ignored
9664 x += 10, y += 10;
9665 processPosition(mapper, x, y);
9666 processSync(mapper);
9667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9668 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
9669
9670 // Reactivate display viewport
9671 displayViewport.isActive = true;
9672 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9673 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9674
9675 // Finger move continues and does not start new gesture
9676 x += 10, y += 10;
9677 processPosition(mapper, x, y);
9678 processSync(mapper);
9679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9680 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
9681}
9682
Arthur Hung7c645402019-01-25 17:45:42 +08009683TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9684 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009685 prepareAxes(POSITION | ID | SLOT);
9686 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009687 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009688
9689 // Create the second touch screen device, and enable multi fingers.
9690 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009691 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009692 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009693 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009694 std::shared_ptr<InputDevice> device2 =
9695 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009696 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009697
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009698 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009699 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009700 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009701 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009702 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009703 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009704 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009705 /*flat=*/0, /*fuzz=*/0);
9706 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009707 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9708 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009709
9710 // Setup the second touch screen device.
Arpit Singha8c236b2023-04-25 13:56:05 +00009711 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
9712 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
9713 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009714 std::list<NotifyArgs> unused =
9715 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009716 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009717 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009718
9719 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009720 std::shared_ptr<FakePointerController> fakePointerController =
9721 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009722 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009723
9724 // Setup policy for associated displays and show touches.
9725 const uint8_t hdmi1 = 0;
9726 const uint8_t hdmi2 = 1;
9727 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9728 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9729 mFakePolicy->setShowTouches(true);
9730
9731 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009732 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009733 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009734
9735 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009736 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009737 InputReaderConfiguration::Change::DISPLAY_INFO |
9738 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009739
9740 // Two fingers down at default display.
9741 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9742 processPosition(mapper, x1, y1);
9743 processId(mapper, 1);
9744 processSlot(mapper, 1);
9745 processPosition(mapper, x2, y2);
9746 processId(mapper, 2);
9747 processSync(mapper);
9748
9749 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9750 fakePointerController->getSpots().find(DISPLAY_ID);
9751 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9752 ASSERT_EQ(size_t(2), iter->second.size());
9753
9754 // Two fingers down at second display.
9755 processPosition(mapper2, x1, y1);
9756 processId(mapper2, 1);
9757 processSlot(mapper2, 1);
9758 processPosition(mapper2, x2, y2);
9759 processId(mapper2, 2);
9760 processSync(mapper2);
9761
9762 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9763 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9764 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009765
9766 // Disable the show touches configuration and ensure the spots are cleared.
9767 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009768 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009769 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009770
9771 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009772}
9773
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009774TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009775 prepareAxes(POSITION);
9776 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009777 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009778 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009779
9780 NotifyMotionArgs motionArgs;
9781 // Unrotated video frame
9782 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9783 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009784 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009785 processPosition(mapper, 100, 200);
9786 processSync(mapper);
9787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9788 ASSERT_EQ(frames, motionArgs.videoFrames);
9789
9790 // Subsequent touch events should not have any videoframes
9791 // This is implemented separately in FakeEventHub,
9792 // but that should match the behaviour of TouchVideoDevice.
9793 processPosition(mapper, 200, 200);
9794 processSync(mapper);
9795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9796 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9797}
9798
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009799TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009800 prepareAxes(POSITION);
9801 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009802 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009803 // Unrotated video frame
9804 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9805 NotifyMotionArgs motionArgs;
9806
9807 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009808 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009809 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9810 clearViewports();
9811 prepareDisplay(orientation);
9812 std::vector<TouchVideoFrame> frames{frame};
9813 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9814 processPosition(mapper, 100, 200);
9815 processSync(mapper);
9816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9817 ASSERT_EQ(frames, motionArgs.videoFrames);
9818 }
9819}
9820
9821TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9822 prepareAxes(POSITION);
9823 addConfigurationProperty("touch.deviceType", "touchScreen");
9824 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9825 // orientation-aware are affected by display rotation.
9826 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009827 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009828 // Unrotated video frame
9829 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9830 NotifyMotionArgs motionArgs;
9831
9832 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009833 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009834 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9835 clearViewports();
9836 prepareDisplay(orientation);
9837 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009838 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009839 processPosition(mapper, 100, 200);
9840 processSync(mapper);
9841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009842 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9843 // compared to the display. This is so that when the window transform (which contains the
9844 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9845 // window's coordinate space.
9846 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009847 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009848
9849 // Release finger.
9850 processSync(mapper);
9851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009852 }
9853}
9854
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009855TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009856 prepareAxes(POSITION);
9857 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009858 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009859 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9860 // so mix these.
9861 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9862 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9863 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9864 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9865 NotifyMotionArgs motionArgs;
9866
Michael Wrighta9cf4192022-12-01 23:46:39 +00009867 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009868 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009869 processPosition(mapper, 100, 200);
9870 processSync(mapper);
9871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009872 ASSERT_EQ(frames, motionArgs.videoFrames);
9873}
9874
9875TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9876 prepareAxes(POSITION);
9877 addConfigurationProperty("touch.deviceType", "touchScreen");
9878 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9879 // orientation-aware are affected by display rotation.
9880 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009881 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009882 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9883 // so mix these.
9884 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9885 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9886 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9887 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9888 NotifyMotionArgs motionArgs;
9889
Michael Wrighta9cf4192022-12-01 23:46:39 +00009890 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009891 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9892 processPosition(mapper, 100, 200);
9893 processSync(mapper);
9894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9895 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9896 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9897 // compared to the display. This is so that when the window transform (which contains the
9898 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9899 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009900 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009901 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009902 ASSERT_EQ(frames, motionArgs.videoFrames);
9903}
9904
Arthur Hung9da14732019-09-02 16:16:58 +08009905/**
9906 * If we had defined port associations, but the viewport is not ready, the touch device would be
9907 * expected to be disabled, and it should be enabled after the viewport has found.
9908 */
9909TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009910 constexpr uint8_t hdmi2 = 1;
9911 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009912 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009913
9914 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9915
9916 addConfigurationProperty("touch.deviceType", "touchScreen");
9917 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009918 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009919
9920 ASSERT_EQ(mDevice->isEnabled(), false);
9921
9922 // Add display on hdmi2, the device should be enabled and can receive touch event.
9923 prepareSecondaryDisplay(type, hdmi2);
9924 ASSERT_EQ(mDevice->isEnabled(), true);
9925
9926 // Send a touch event.
9927 processPosition(mapper, 100, 100);
9928 processSync(mapper);
9929
9930 NotifyMotionArgs args;
9931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9932 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9933}
9934
Arthur Hung421eb1c2020-01-16 00:09:42 +08009935TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009936 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009937 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009938 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009939 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009940
9941 NotifyMotionArgs motionArgs;
9942
9943 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9944 // finger down
9945 processId(mapper, 1);
9946 processPosition(mapper, x1, y1);
9947 processSync(mapper);
9948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9949 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009950 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009951
9952 // finger move
9953 processId(mapper, 1);
9954 processPosition(mapper, x2, y2);
9955 processSync(mapper);
9956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9957 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009958 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009959
9960 // finger up.
9961 processId(mapper, -1);
9962 processSync(mapper);
9963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9964 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009965 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009966
9967 // new finger down
9968 processId(mapper, 1);
9969 processPosition(mapper, x3, y3);
9970 processSync(mapper);
9971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9972 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009973 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009974}
9975
9976/**
arthurhungcc7f9802020-04-30 17:55:40 +08009977 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9978 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009979 */
arthurhungcc7f9802020-04-30 17:55:40 +08009980TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009981 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009982 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009983 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009984 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009985
9986 NotifyMotionArgs motionArgs;
9987
9988 // default tool type is finger
9989 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009990 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009991 processPosition(mapper, x1, y1);
9992 processSync(mapper);
9993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9994 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009995 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009996
9997 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9998 processToolType(mapper, MT_TOOL_PALM);
9999 processSync(mapper);
10000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10001 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10002
10003 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +080010004 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010005 processPosition(mapper, x2, y2);
10006 processSync(mapper);
10007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10008
10009 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +080010010 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010011 processSync(mapper);
10012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10013
10014 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +080010015 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010016 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010017 processPosition(mapper, x3, y3);
10018 processSync(mapper);
10019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10020 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010021 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010022}
10023
arthurhungbf89a482020-04-17 17:37:55 +080010024/**
arthurhungcc7f9802020-04-30 17:55:40 +080010025 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10026 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +080010027 */
arthurhungcc7f9802020-04-30 17:55:40 +080010028TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +080010029 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010030 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +080010031 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010032 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +080010033
10034 NotifyMotionArgs motionArgs;
10035
10036 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +080010037 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10038 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010039 processPosition(mapper, x1, y1);
10040 processSync(mapper);
10041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10042 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010043 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +080010044
10045 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +080010046 processSlot(mapper, SECOND_SLOT);
10047 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010048 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +080010049 processSync(mapper);
10050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010051 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010052 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010053
10054 // If the tool type of the first finger changes to MT_TOOL_PALM,
10055 // we expect to receive ACTION_POINTER_UP with cancel flag.
10056 processSlot(mapper, FIRST_SLOT);
10057 processId(mapper, FIRST_TRACKING_ID);
10058 processToolType(mapper, MT_TOOL_PALM);
10059 processSync(mapper);
10060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010061 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010062 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10063
10064 // The following MOVE events of second finger should be processed.
10065 processSlot(mapper, SECOND_SLOT);
10066 processId(mapper, SECOND_TRACKING_ID);
10067 processPosition(mapper, x2 + 1, y2 + 1);
10068 processSync(mapper);
10069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10070 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010071 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010072
10073 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
10074 // it. Second finger receive move.
10075 processSlot(mapper, FIRST_SLOT);
10076 processId(mapper, INVALID_TRACKING_ID);
10077 processSync(mapper);
10078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10079 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010080 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010081
10082 // Second finger keeps moving.
10083 processSlot(mapper, SECOND_SLOT);
10084 processId(mapper, SECOND_TRACKING_ID);
10085 processPosition(mapper, x2 + 2, y2 + 2);
10086 processSync(mapper);
10087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10088 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010089 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010090
10091 // Second finger up.
10092 processId(mapper, INVALID_TRACKING_ID);
10093 processSync(mapper);
10094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10095 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10096 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10097}
10098
10099/**
10100 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
10101 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
10102 */
10103TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
10104 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010105 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010106 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010107 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010108
10109 NotifyMotionArgs motionArgs;
10110
10111 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10112 // First finger down.
10113 processId(mapper, FIRST_TRACKING_ID);
10114 processPosition(mapper, x1, y1);
10115 processSync(mapper);
10116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10117 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010118 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010119
10120 // Second finger down.
10121 processSlot(mapper, SECOND_SLOT);
10122 processId(mapper, SECOND_TRACKING_ID);
10123 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +080010124 processSync(mapper);
10125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010126 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010127 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +080010128
arthurhungcc7f9802020-04-30 17:55:40 +080010129 // If the tool type of the first finger changes to MT_TOOL_PALM,
10130 // we expect to receive ACTION_POINTER_UP with cancel flag.
10131 processSlot(mapper, FIRST_SLOT);
10132 processId(mapper, FIRST_TRACKING_ID);
10133 processToolType(mapper, MT_TOOL_PALM);
10134 processSync(mapper);
10135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010136 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010137 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10138
10139 // Second finger keeps moving.
10140 processSlot(mapper, SECOND_SLOT);
10141 processId(mapper, SECOND_TRACKING_ID);
10142 processPosition(mapper, x2 + 1, y2 + 1);
10143 processSync(mapper);
10144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10145 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10146
10147 // second finger becomes palm, receive cancel due to only 1 finger is active.
10148 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010149 processToolType(mapper, MT_TOOL_PALM);
10150 processSync(mapper);
10151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10152 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10153
arthurhungcc7f9802020-04-30 17:55:40 +080010154 // third finger down.
10155 processSlot(mapper, THIRD_SLOT);
10156 processId(mapper, THIRD_TRACKING_ID);
10157 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +080010158 processPosition(mapper, x3, y3);
10159 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +080010160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10161 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010162 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010163 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010164
10165 // third finger move
10166 processId(mapper, THIRD_TRACKING_ID);
10167 processPosition(mapper, x3 + 1, y3 + 1);
10168 processSync(mapper);
10169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10170 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10171
10172 // first finger up, third finger receive move.
10173 processSlot(mapper, FIRST_SLOT);
10174 processId(mapper, INVALID_TRACKING_ID);
10175 processSync(mapper);
10176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10177 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010178 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010179
10180 // second finger up, third finger receive move.
10181 processSlot(mapper, SECOND_SLOT);
10182 processId(mapper, INVALID_TRACKING_ID);
10183 processSync(mapper);
10184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10185 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010186 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010187
10188 // third finger up.
10189 processSlot(mapper, THIRD_SLOT);
10190 processId(mapper, INVALID_TRACKING_ID);
10191 processSync(mapper);
10192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10193 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10194 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10195}
10196
10197/**
10198 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10199 * and the active finger could still be allowed to receive the events
10200 */
10201TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10202 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010203 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010204 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010205 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010206
10207 NotifyMotionArgs motionArgs;
10208
10209 // default tool type is finger
10210 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10211 processId(mapper, FIRST_TRACKING_ID);
10212 processPosition(mapper, x1, y1);
10213 processSync(mapper);
10214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10215 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010216 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010217
10218 // Second finger down.
10219 processSlot(mapper, SECOND_SLOT);
10220 processId(mapper, SECOND_TRACKING_ID);
10221 processPosition(mapper, x2, y2);
10222 processSync(mapper);
10223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010224 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010225 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010226
10227 // If the tool type of the second finger changes to MT_TOOL_PALM,
10228 // we expect to receive ACTION_POINTER_UP with cancel flag.
10229 processId(mapper, SECOND_TRACKING_ID);
10230 processToolType(mapper, MT_TOOL_PALM);
10231 processSync(mapper);
10232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010233 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010234 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10235
10236 // The following MOVE event should be processed.
10237 processSlot(mapper, FIRST_SLOT);
10238 processId(mapper, FIRST_TRACKING_ID);
10239 processPosition(mapper, x1 + 1, y1 + 1);
10240 processSync(mapper);
10241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010243 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010244
10245 // second finger up.
10246 processSlot(mapper, SECOND_SLOT);
10247 processId(mapper, INVALID_TRACKING_ID);
10248 processSync(mapper);
10249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10251
10252 // first finger keep moving
10253 processSlot(mapper, FIRST_SLOT);
10254 processId(mapper, FIRST_TRACKING_ID);
10255 processPosition(mapper, x1 + 2, y1 + 2);
10256 processSync(mapper);
10257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10259
10260 // first finger up.
10261 processId(mapper, INVALID_TRACKING_ID);
10262 processSync(mapper);
10263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10264 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10265 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010266}
10267
Arthur Hung9ad18942021-06-19 02:04:46 +000010268/**
10269 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10270 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10271 * cause slot be valid again.
10272 */
10273TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10274 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010275 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +000010276 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010277 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +000010278
10279 NotifyMotionArgs motionArgs;
10280
10281 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10282 // First finger down.
10283 processId(mapper, FIRST_TRACKING_ID);
10284 processPosition(mapper, x1, y1);
10285 processPressure(mapper, RAW_PRESSURE_MAX);
10286 processSync(mapper);
10287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10288 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010289 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010290
10291 // First finger move.
10292 processId(mapper, FIRST_TRACKING_ID);
10293 processPosition(mapper, x1 + 1, y1 + 1);
10294 processPressure(mapper, RAW_PRESSURE_MAX);
10295 processSync(mapper);
10296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010298 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010299
10300 // Second finger down.
10301 processSlot(mapper, SECOND_SLOT);
10302 processId(mapper, SECOND_TRACKING_ID);
10303 processPosition(mapper, x2, y2);
10304 processPressure(mapper, RAW_PRESSURE_MAX);
10305 processSync(mapper);
10306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010307 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010308 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010309
10310 // second finger up with some unexpected data.
10311 processSlot(mapper, SECOND_SLOT);
10312 processId(mapper, INVALID_TRACKING_ID);
10313 processPosition(mapper, x2, y2);
10314 processSync(mapper);
10315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010316 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010317 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010318
10319 // first finger up with some unexpected data.
10320 processSlot(mapper, FIRST_SLOT);
10321 processId(mapper, INVALID_TRACKING_ID);
10322 processPosition(mapper, x2, y2);
10323 processPressure(mapper, RAW_PRESSURE_MAX);
10324 processSync(mapper);
10325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10326 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010327 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010328}
10329
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010330TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10331 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010332 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010333 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010334 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010335
10336 // First finger down.
10337 processId(mapper, FIRST_TRACKING_ID);
10338 processPosition(mapper, 100, 200);
10339 processPressure(mapper, RAW_PRESSURE_MAX);
10340 processSync(mapper);
10341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10342 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10343
10344 // Second finger down.
10345 processSlot(mapper, SECOND_SLOT);
10346 processId(mapper, SECOND_TRACKING_ID);
10347 processPosition(mapper, 300, 400);
10348 processPressure(mapper, RAW_PRESSURE_MAX);
10349 processSync(mapper);
10350 ASSERT_NO_FATAL_FAILURE(
10351 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10352
10353 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010354 // preserved. Resetting should cancel the ongoing gesture.
10355 resetMapper(mapper, ARBITRARY_TIME);
10356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10357 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010358
10359 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10360 // the existing touch state to generate a down event.
10361 processPosition(mapper, 301, 302);
10362 processSync(mapper);
10363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10364 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10366 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10367
10368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10369}
10370
10371TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10372 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010373 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010374 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010375 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010376
10377 // First finger touches down and releases.
10378 processId(mapper, FIRST_TRACKING_ID);
10379 processPosition(mapper, 100, 200);
10380 processPressure(mapper, RAW_PRESSURE_MAX);
10381 processSync(mapper);
10382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10383 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10384 processId(mapper, INVALID_TRACKING_ID);
10385 processSync(mapper);
10386 ASSERT_NO_FATAL_FAILURE(
10387 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10388
10389 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10390 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010391 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10393
10394 // Send an empty sync frame. Since there are no pointers, no events are generated.
10395 processSync(mapper);
10396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10397}
10398
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010399TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010400 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010401 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010402 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010403 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010405
10406 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10407 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10408 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10409 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10410 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10411
10412 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010413 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010414 processId(mapper, FIRST_TRACKING_ID);
10415 processToolType(mapper, MT_TOOL_PEN);
10416 processPosition(mapper, 100, 200);
10417 processPressure(mapper, RAW_PRESSURE_MAX);
10418 processSync(mapper);
10419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10420 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10421 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010422 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010423
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010424 // Now that we know the device supports styluses, ensure that the device is re-configured with
10425 // the stylus source.
10426 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10427 {
10428 const auto& devices = mReader->getInputDevices();
10429 auto deviceInfo =
10430 std::find_if(devices.begin(), devices.end(),
10431 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10432 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10433 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10434 }
10435
10436 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10438
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010439 processId(mapper, INVALID_TRACKING_ID);
10440 processSync(mapper);
10441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10442 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10443 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010444 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010445}
10446
Seunghwan Choi356026c2023-02-01 14:37:25 +090010447TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10448 addConfigurationProperty("touch.deviceType", "touchScreen");
10449 prepareDisplay(ui::ROTATION_0);
10450 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10451 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10452 // indicate stylus presence dynamically.
10453 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10454 std::shared_ptr<FakePointerController> fakePointerController =
10455 std::make_shared<FakePointerController>();
10456 mFakePolicy->setPointerController(fakePointerController);
10457 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +000010458 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010459
10460 processId(mapper, FIRST_TRACKING_ID);
10461 processPressure(mapper, RAW_PRESSURE_MIN);
10462 processPosition(mapper, 100, 200);
10463 processToolType(mapper, MT_TOOL_PEN);
10464 processSync(mapper);
10465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10466 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010467 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010468 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10469 ASSERT_TRUE(fakePointerController->isPointerShown());
10470 ASSERT_NO_FATAL_FAILURE(
10471 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10472}
10473
10474TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10475 addConfigurationProperty("touch.deviceType", "touchScreen");
10476 prepareDisplay(ui::ROTATION_0);
10477 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10478 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10479 // indicate stylus presence dynamically.
10480 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10481 std::shared_ptr<FakePointerController> fakePointerController =
10482 std::make_shared<FakePointerController>();
10483 mFakePolicy->setPointerController(fakePointerController);
10484 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010485 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010486
10487 processId(mapper, FIRST_TRACKING_ID);
10488 processPressure(mapper, RAW_PRESSURE_MIN);
10489 processPosition(mapper, 100, 200);
10490 processToolType(mapper, MT_TOOL_PEN);
10491 processSync(mapper);
10492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10493 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010494 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010495 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10496 ASSERT_FALSE(fakePointerController->isPointerShown());
10497}
10498
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010499// --- MultiTouchInputMapperTest_ExternalDevice ---
10500
10501class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10502protected:
Chris Yea52ade12020-08-27 16:49:20 -070010503 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010504};
10505
10506/**
10507 * Expect fallback to internal viewport if device is external and external viewport is not present.
10508 */
10509TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10510 prepareAxes(POSITION);
10511 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010512 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010513 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010514
10515 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10516
10517 NotifyMotionArgs motionArgs;
10518
10519 // Expect the event to be sent to the internal viewport,
10520 // because an external viewport is not present.
10521 processPosition(mapper, 100, 100);
10522 processSync(mapper);
10523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10524 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10525
10526 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010527 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010528 processPosition(mapper, 100, 100);
10529 processSync(mapper);
10530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10531 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10532}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010533
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010534TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10535 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10536 std::shared_ptr<FakePointerController> fakePointerController =
10537 std::make_shared<FakePointerController>();
10538 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10539 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010540
10541 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010542 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010543 prepareAxes(POSITION | ID | SLOT);
10544 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10545 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10546 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010547 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010548 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010549
10550 // captured touchpad should be a touchpad source
10551 NotifyDeviceResetArgs resetArgs;
10552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10553 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10554
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010555 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010556
10557 const InputDeviceInfo::MotionRange* relRangeX =
10558 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10559 ASSERT_NE(relRangeX, nullptr);
10560 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10561 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10562 const InputDeviceInfo::MotionRange* relRangeY =
10563 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10564 ASSERT_NE(relRangeY, nullptr);
10565 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10566 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10567
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010568 // run captured pointer tests - note that this is unscaled, so input listener events should be
10569 // identical to what the hardware sends (accounting for any
10570 // calibration).
10571 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010572 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010573 processId(mapper, 1);
10574 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10575 processKey(mapper, BTN_TOUCH, 1);
10576 processSync(mapper);
10577
10578 // expect coord[0] to contain initial location of touch 0
10579 NotifyMotionArgs args;
10580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10581 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010582 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010583 ASSERT_EQ(0, args.pointerProperties[0].id);
10584 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10585 ASSERT_NO_FATAL_FAILURE(
10586 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10587
10588 // FINGER 1 DOWN
10589 processSlot(mapper, 1);
10590 processId(mapper, 2);
10591 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10592 processSync(mapper);
10593
10594 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010596 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010597 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010598 ASSERT_EQ(0, args.pointerProperties[0].id);
10599 ASSERT_EQ(1, args.pointerProperties[1].id);
10600 ASSERT_NO_FATAL_FAILURE(
10601 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10602 ASSERT_NO_FATAL_FAILURE(
10603 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10604
10605 // FINGER 1 MOVE
10606 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10607 processSync(mapper);
10608
10609 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10610 // from move
10611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10612 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10613 ASSERT_NO_FATAL_FAILURE(
10614 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10615 ASSERT_NO_FATAL_FAILURE(
10616 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10617
10618 // FINGER 0 MOVE
10619 processSlot(mapper, 0);
10620 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10621 processSync(mapper);
10622
10623 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10625 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10626 ASSERT_NO_FATAL_FAILURE(
10627 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10628 ASSERT_NO_FATAL_FAILURE(
10629 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10630
10631 // BUTTON DOWN
10632 processKey(mapper, BTN_LEFT, 1);
10633 processSync(mapper);
10634
10635 // touchinputmapper design sends a move before button press
10636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10637 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10639 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10640
10641 // BUTTON UP
10642 processKey(mapper, BTN_LEFT, 0);
10643 processSync(mapper);
10644
10645 // touchinputmapper design sends a move after button release
10646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10647 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10649 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10650
10651 // FINGER 0 UP
10652 processId(mapper, -1);
10653 processSync(mapper);
10654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10655 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10656
10657 // FINGER 1 MOVE
10658 processSlot(mapper, 1);
10659 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10660 processSync(mapper);
10661
10662 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10664 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010665 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010666 ASSERT_EQ(1, args.pointerProperties[0].id);
10667 ASSERT_NO_FATAL_FAILURE(
10668 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10669
10670 // FINGER 1 UP
10671 processId(mapper, -1);
10672 processKey(mapper, BTN_TOUCH, 0);
10673 processSync(mapper);
10674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10675 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10676
Josep del Río2d8c79a2023-01-23 19:33:50 +000010677 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010678 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010679 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010681 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010682}
10683
10684TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10685 std::shared_ptr<FakePointerController> fakePointerController =
10686 std::make_shared<FakePointerController>();
10687 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10688 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010689
10690 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010691 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010692 prepareAxes(POSITION | ID | SLOT);
10693 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10694 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010695 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010696 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010697 // run uncaptured pointer tests - pushes out generic events
10698 // FINGER 0 DOWN
10699 processId(mapper, 3);
10700 processPosition(mapper, 100, 100);
10701 processKey(mapper, BTN_TOUCH, 1);
10702 processSync(mapper);
10703
10704 // start at (100,100), cursor should be at (0,0) * scale
10705 NotifyMotionArgs args;
10706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10707 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10708 ASSERT_NO_FATAL_FAILURE(
10709 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10710
10711 // FINGER 0 MOVE
10712 processPosition(mapper, 200, 200);
10713 processSync(mapper);
10714
10715 // compute scaling to help with touch position checking
10716 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10717 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10718 float scale =
10719 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10720
10721 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10723 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10725 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010726
10727 // BUTTON DOWN
10728 processKey(mapper, BTN_LEFT, 1);
10729 processSync(mapper);
10730
10731 // touchinputmapper design sends a move before button press
10732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10733 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10735 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10736
10737 // BUTTON UP
10738 processKey(mapper, BTN_LEFT, 0);
10739 processSync(mapper);
10740
10741 // touchinputmapper design sends a move after button release
10742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10743 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10745 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010746}
10747
10748TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10749 std::shared_ptr<FakePointerController> fakePointerController =
10750 std::make_shared<FakePointerController>();
10751
Michael Wrighta9cf4192022-12-01 23:46:39 +000010752 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010753 prepareAxes(POSITION | ID | SLOT);
10754 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010755 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010756 mFakePolicy->setPointerCapture(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010757 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010758
Josep del Río2d8c79a2023-01-23 19:33:50 +000010759 // uncaptured touchpad should be a pointer device
10760 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010761
Josep del Río2d8c79a2023-01-23 19:33:50 +000010762 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010763 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010764 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010765 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10766}
10767
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010768// --- BluetoothMultiTouchInputMapperTest ---
10769
10770class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10771protected:
10772 void SetUp() override {
10773 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10774 }
10775};
10776
10777TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10778 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010779 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010780 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010781 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010782
10783 nsecs_t kernelEventTime = ARBITRARY_TIME;
10784 nsecs_t expectedEventTime = ARBITRARY_TIME;
10785 // Touch down.
10786 processId(mapper, FIRST_TRACKING_ID);
10787 processPosition(mapper, 100, 200);
10788 processPressure(mapper, RAW_PRESSURE_MAX);
10789 processSync(mapper, ARBITRARY_TIME);
10790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10791 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10792
10793 // Process several events that come in quick succession, according to their timestamps.
10794 for (int i = 0; i < 3; i++) {
10795 constexpr static nsecs_t delta = ms2ns(1);
10796 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10797 kernelEventTime += delta;
10798 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10799
10800 processPosition(mapper, 101 + i, 201 + i);
10801 processSync(mapper, kernelEventTime);
10802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10803 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10804 WithEventTime(expectedEventTime))));
10805 }
10806
10807 // Release the touch.
10808 processId(mapper, INVALID_TRACKING_ID);
10809 processPressure(mapper, RAW_PRESSURE_MIN);
10810 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10812 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10813 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10814}
10815
10816// --- MultiTouchPointerModeTest ---
10817
HQ Liue6983c72022-04-19 22:14:56 +000010818class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10819protected:
10820 float mPointerMovementScale;
10821 float mPointerXZoomScale;
10822 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10823 addConfigurationProperty("touch.deviceType", "pointer");
10824 std::shared_ptr<FakePointerController> fakePointerController =
10825 std::make_shared<FakePointerController>();
10826 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10827 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010828 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010829
10830 prepareAxes(POSITION);
10831 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10832 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10833 // needs to be disabled, and the pointer gesture needs to be enabled.
10834 mFakePolicy->setPointerCapture(false);
10835 mFakePolicy->setPointerGestureEnabled(true);
10836 mFakePolicy->setPointerController(fakePointerController);
10837
10838 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10839 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10840 mPointerMovementScale =
10841 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10842 mPointerXZoomScale =
10843 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10844 }
10845
10846 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10847 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10848 /*flat*/ 0,
10849 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10850 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10851 /*flat*/ 0,
10852 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10853 }
10854};
10855
10856/**
10857 * Two fingers down on a pointer mode touch pad. The width
10858 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10859 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10860 * be greater than the both value to be freeform gesture, so that after two
10861 * fingers start to move downwards, the gesture should be swipe.
10862 */
10863TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10864 // The min freeform gesture width is 25units/mm x 30mm = 750
10865 // which is greater than fraction of the diagnal length of the touchpad (349).
10866 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010867 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010868 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010869 NotifyMotionArgs motionArgs;
10870
10871 // Two fingers down at once.
10872 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10873 // Pointer's initial position is used the [0,0] coordinate.
10874 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10875
10876 processId(mapper, FIRST_TRACKING_ID);
10877 processPosition(mapper, x1, y1);
10878 processMTSync(mapper);
10879 processId(mapper, SECOND_TRACKING_ID);
10880 processPosition(mapper, x2, y2);
10881 processMTSync(mapper);
10882 processSync(mapper);
10883
10884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010885 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010886 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010887 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010888 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010889 ASSERT_NO_FATAL_FAILURE(
10890 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10891
10892 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10893 // that there should be 1 pointer.
10894 int32_t movingDistance = 200;
10895 y1 += movingDistance;
10896 y2 += movingDistance;
10897
10898 processId(mapper, FIRST_TRACKING_ID);
10899 processPosition(mapper, x1, y1);
10900 processMTSync(mapper);
10901 processId(mapper, SECOND_TRACKING_ID);
10902 processPosition(mapper, x2, y2);
10903 processMTSync(mapper);
10904 processSync(mapper);
10905
10906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010907 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010908 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010909 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010910 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10912 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10913 0, 0, 0, 0));
10914}
10915
10916/**
10917 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10918 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10919 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10920 * value to be freeform gesture, so that after two fingers start to move downwards,
10921 * the gesture should be swipe.
10922 */
10923TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10924 // The min freeform gesture width is 5units/mm x 30mm = 150
10925 // which is greater than fraction of the diagnal length of the touchpad (349).
10926 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010927 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +000010928 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010929 NotifyMotionArgs motionArgs;
10930
10931 // Two fingers down at once.
10932 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10933 // Pointer's initial position is used the [0,0] coordinate.
10934 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10935
10936 processId(mapper, FIRST_TRACKING_ID);
10937 processPosition(mapper, x1, y1);
10938 processMTSync(mapper);
10939 processId(mapper, SECOND_TRACKING_ID);
10940 processPosition(mapper, x2, y2);
10941 processMTSync(mapper);
10942 processSync(mapper);
10943
10944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010945 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010946 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010947 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010948 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010949 ASSERT_NO_FATAL_FAILURE(
10950 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10951
10952 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10953 // and there should be 1 pointer.
10954 int32_t movingDistance = 200;
10955 y1 += movingDistance;
10956 y2 += movingDistance;
10957
10958 processId(mapper, FIRST_TRACKING_ID);
10959 processPosition(mapper, x1, y1);
10960 processMTSync(mapper);
10961 processId(mapper, SECOND_TRACKING_ID);
10962 processPosition(mapper, x2, y2);
10963 processMTSync(mapper);
10964 processSync(mapper);
10965
10966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010967 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010968 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010969 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010970 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010971 // New coordinate is the scaled relative coordinate from the initial coordinate.
10972 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10973 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10974 0, 0, 0, 0));
10975}
10976
10977/**
10978 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10979 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10980 * freeform gestures after two fingers start to move downwards.
10981 */
10982TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010983 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010984 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010985
10986 NotifyMotionArgs motionArgs;
10987
10988 // Two fingers down at once. Wider than the max swipe width.
10989 // The gesture is expected to be PRESS, then transformed to FREEFORM
10990 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10991
10992 processId(mapper, FIRST_TRACKING_ID);
10993 processPosition(mapper, x1, y1);
10994 processMTSync(mapper);
10995 processId(mapper, SECOND_TRACKING_ID);
10996 processPosition(mapper, x2, y2);
10997 processMTSync(mapper);
10998 processSync(mapper);
10999
11000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011001 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011002 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011003 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011004 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011005 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
11006 ASSERT_NO_FATAL_FAILURE(
11007 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11008
11009 int32_t movingDistance = 200;
11010
11011 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
11012 // then two down events for two pointers.
11013 y1 += movingDistance;
11014 y2 += movingDistance;
11015
11016 processId(mapper, FIRST_TRACKING_ID);
11017 processPosition(mapper, x1, y1);
11018 processMTSync(mapper);
11019 processId(mapper, SECOND_TRACKING_ID);
11020 processPosition(mapper, x2, y2);
11021 processMTSync(mapper);
11022 processSync(mapper);
11023
11024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11025 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011026 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011027 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
11028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011029 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011030 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011031 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
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);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011035 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011036 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011037 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011038 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011039 // Two pointers' scaled relative coordinates from their initial centroid.
11040 // Initial y coordinates are 0 as y1 and y2 have the same value.
11041 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
11042 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
11043 // When pointers move, the new coordinates equal to the initial coordinates plus
11044 // scaled moving distance.
11045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11046 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11047 0, 0, 0, 0));
11048 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11049 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11050 0, 0, 0, 0));
11051
11052 // Move two fingers down again, expect one MOVE motion event.
11053 y1 += movingDistance;
11054 y2 += movingDistance;
11055
11056 processId(mapper, FIRST_TRACKING_ID);
11057 processPosition(mapper, x1, y1);
11058 processMTSync(mapper);
11059 processId(mapper, SECOND_TRACKING_ID);
11060 processPosition(mapper, x2, y2);
11061 processMTSync(mapper);
11062 processSync(mapper);
11063
11064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011065 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011066 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011067 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011068 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011069 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11070 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11071 0, 0, 0, 0, 0));
11072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11073 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11074 0, 0, 0, 0, 0));
11075}
11076
Harry Cutts39b7ca22022-10-05 15:55:48 +000011077TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000011078 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000011079 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000011080 NotifyMotionArgs motionArgs;
11081
11082 // Place two fingers down.
11083 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
11084
11085 processId(mapper, FIRST_TRACKING_ID);
11086 processPosition(mapper, x1, y1);
11087 processMTSync(mapper);
11088 processId(mapper, SECOND_TRACKING_ID);
11089 processPosition(mapper, x2, y2);
11090 processMTSync(mapper);
11091 processSync(mapper);
11092
11093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011094 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000011095 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11096 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
11097 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
11098 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
11099
11100 // Move the two fingers down and to the left.
11101 int32_t movingDistance = 200;
11102 x1 -= movingDistance;
11103 y1 += movingDistance;
11104 x2 -= movingDistance;
11105 y2 += movingDistance;
11106
11107 processId(mapper, FIRST_TRACKING_ID);
11108 processPosition(mapper, x1, y1);
11109 processMTSync(mapper);
11110 processId(mapper, SECOND_TRACKING_ID);
11111 processPosition(mapper, x2, y2);
11112 processMTSync(mapper);
11113 processSync(mapper);
11114
11115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011116 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000011117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11118 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
11119 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
11120 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
11121}
11122
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011123TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000011124 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011125 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000011126 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
11128
11129 // Start a stylus gesture.
11130 processKey(mapper, BTN_TOOL_PEN, 1);
11131 processId(mapper, FIRST_TRACKING_ID);
11132 processPosition(mapper, 100, 200);
11133 processSync(mapper);
11134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11135 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
11136 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011137 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011138 // TODO(b/257078296): Pointer mode generates extra event.
11139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11140 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11141 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011142 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11144
11145 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
11146 // gesture should be disabled.
11147 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
11148 viewport->isActive = false;
11149 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000011150 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11152 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11153 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011154 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011155 // TODO(b/257078296): Pointer mode generates extra event.
11156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11157 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11158 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011159 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11161}
11162
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011163// --- JoystickInputMapperTest ---
11164
11165class JoystickInputMapperTest : public InputMapperTest {
11166protected:
11167 static const int32_t RAW_X_MIN;
11168 static const int32_t RAW_X_MAX;
11169 static const int32_t RAW_Y_MIN;
11170 static const int32_t RAW_Y_MAX;
11171
11172 void SetUp() override {
11173 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
11174 }
11175 void prepareAxes() {
11176 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
11177 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
11178 }
11179
11180 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
11181 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
11182 }
11183
11184 void processSync(JoystickInputMapper& mapper) {
11185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
11186 }
11187
Michael Wrighta9cf4192022-12-01 23:46:39 +000011188 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011189 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
11190 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
11191 NO_PORT, ViewportType::VIRTUAL);
11192 }
11193};
11194
11195const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
11196const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
11197const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11198const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11199
11200TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11201 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000011202 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011203
11204 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11205
Michael Wrighta9cf4192022-12-01 23:46:39 +000011206 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011207
11208 // Send an axis event
11209 processAxis(mapper, ABS_X, 100);
11210 processSync(mapper);
11211
11212 NotifyMotionArgs args;
11213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11214 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11215
11216 // Send another axis event
11217 processAxis(mapper, ABS_Y, 100);
11218 processSync(mapper);
11219
11220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11221 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11222}
11223
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011224// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011225
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011226class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011227protected:
11228 static const char* DEVICE_NAME;
11229 static const char* DEVICE_LOCATION;
11230 static const int32_t DEVICE_ID;
11231 static const int32_t DEVICE_GENERATION;
11232 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011233 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011234 static const int32_t EVENTHUB_ID;
11235
11236 std::shared_ptr<FakeEventHub> mFakeEventHub;
11237 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011238 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011239 std::unique_ptr<InstrumentedInputReader> mReader;
11240 std::shared_ptr<InputDevice> mDevice;
11241
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011242 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011243 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011244 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011245 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011246 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011247 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011248 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11249 }
11250
11251 void SetUp() override { SetUp(DEVICE_CLASSES); }
11252
11253 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011254 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011255 mFakePolicy.clear();
11256 }
11257
Chris Yee2b1e5c2021-03-10 22:45:12 -080011258 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11259 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011260 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011261 InputDeviceIdentifier identifier;
11262 identifier.name = name;
11263 identifier.location = location;
11264 std::shared_ptr<InputDevice> device =
11265 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11266 identifier);
11267 mReader->pushNextDevice(device);
11268 mFakeEventHub->addDevice(eventHubId, name, classes);
11269 mReader->loopOnce();
11270 return device;
11271 }
11272
11273 template <class T, typename... Args>
11274 T& addControllerAndConfigure(Args... args) {
11275 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11276
11277 return controller;
11278 }
11279};
11280
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011281const char* PeripheralControllerTest::DEVICE_NAME = "device";
11282const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11283const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11284const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11285const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011286const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11287 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011288const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011289
11290// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011291class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011292protected:
11293 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011294 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011295 }
11296};
11297
11298TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011299 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011300
Harry Cuttsa5b71292022-11-28 12:56:17 +000011301 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11302 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11303 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011304}
11305
11306TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011307 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011308
Harry Cuttsa5b71292022-11-28 12:56:17 +000011309 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11310 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11311 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011312}
11313
11314// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011315class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011316protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011317 void SetUp() override {
11318 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11319 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011320};
11321
Chris Ye85758332021-05-16 23:05:17 -070011322TEST_F(LightControllerTest, MonoLight) {
11323 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011324 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011325 .maxBrightness = 255,
11326 .flags = InputLightClass::BRIGHTNESS,
11327 .path = ""};
11328 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011329
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011330 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011331 InputDeviceInfo info;
11332 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011333 std::vector<InputDeviceLightInfo> lights = info.getLights();
11334 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011335 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11336 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11337
11338 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11339 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11340}
11341
11342TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11343 RawLightInfo infoMono = {.id = 1,
11344 .name = "mono_keyboard_backlight",
11345 .maxBrightness = 255,
11346 .flags = InputLightClass::BRIGHTNESS |
11347 InputLightClass::KEYBOARD_BACKLIGHT,
11348 .path = ""};
11349 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11350
11351 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11352 InputDeviceInfo info;
11353 controller.populateDeviceInfo(&info);
11354 std::vector<InputDeviceLightInfo> lights = info.getLights();
11355 ASSERT_EQ(1U, lights.size());
11356 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11357 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011358
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011359 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11360 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011361}
11362
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000011363TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
11364 RawLightInfo infoMono = {.id = 1,
11365 .name = "mono_light",
11366 .maxBrightness = 255,
11367 .flags = InputLightClass::BRIGHTNESS,
11368 .path = ""};
11369 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11370 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11371 "0,100,200");
11372
11373 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11374 std::list<NotifyArgs> unused =
11375 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11376 /*changes=*/{});
11377
11378 InputDeviceInfo info;
11379 controller.populateDeviceInfo(&info);
11380 std::vector<InputDeviceLightInfo> lights = info.getLights();
11381 ASSERT_EQ(1U, lights.size());
11382 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11383}
11384
11385TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
11386 RawLightInfo infoMono = {.id = 1,
11387 .name = "mono_keyboard_backlight",
11388 .maxBrightness = 255,
11389 .flags = InputLightClass::BRIGHTNESS |
11390 InputLightClass::KEYBOARD_BACKLIGHT,
11391 .path = ""};
11392 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11393
11394 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11395 std::list<NotifyArgs> unused =
11396 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11397 /*changes=*/{});
11398
11399 InputDeviceInfo info;
11400 controller.populateDeviceInfo(&info);
11401 std::vector<InputDeviceLightInfo> lights = info.getLights();
11402 ASSERT_EQ(1U, lights.size());
11403 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11404}
11405
11406TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
11407 RawLightInfo infoMono = {.id = 1,
11408 .name = "mono_keyboard_backlight",
11409 .maxBrightness = 255,
11410 .flags = InputLightClass::BRIGHTNESS |
11411 InputLightClass::KEYBOARD_BACKLIGHT,
11412 .path = ""};
11413 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11414 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11415 "0,100,200");
11416
11417 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11418 std::list<NotifyArgs> unused =
11419 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11420 /*changes=*/{});
11421
11422 InputDeviceInfo info;
11423 controller.populateDeviceInfo(&info);
11424 std::vector<InputDeviceLightInfo> lights = info.getLights();
11425 ASSERT_EQ(1U, lights.size());
11426 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
11427 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
11428 ASSERT_EQ(BrightnessLevel(0), *it);
11429 std::advance(it, 1);
11430 ASSERT_EQ(BrightnessLevel(100), *it);
11431 std::advance(it, 1);
11432 ASSERT_EQ(BrightnessLevel(200), *it);
11433}
11434
11435TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
11436 RawLightInfo infoMono = {.id = 1,
11437 .name = "mono_keyboard_backlight",
11438 .maxBrightness = 255,
11439 .flags = InputLightClass::BRIGHTNESS |
11440 InputLightClass::KEYBOARD_BACKLIGHT,
11441 .path = ""};
11442 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11443 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11444 "0,100,200,300,400,500");
11445
11446 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11447 std::list<NotifyArgs> unused =
11448 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11449 /*changes=*/{});
11450
11451 InputDeviceInfo info;
11452 controller.populateDeviceInfo(&info);
11453 std::vector<InputDeviceLightInfo> lights = info.getLights();
11454 ASSERT_EQ(1U, lights.size());
11455 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11456}
11457
Chris Yee2b1e5c2021-03-10 22:45:12 -080011458TEST_F(LightControllerTest, RGBLight) {
11459 RawLightInfo infoRed = {.id = 1,
11460 .name = "red",
11461 .maxBrightness = 255,
11462 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11463 .path = ""};
11464 RawLightInfo infoGreen = {.id = 2,
11465 .name = "green",
11466 .maxBrightness = 255,
11467 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11468 .path = ""};
11469 RawLightInfo infoBlue = {.id = 3,
11470 .name = "blue",
11471 .maxBrightness = 255,
11472 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11473 .path = ""};
11474 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11475 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11476 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11477
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011478 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011479 InputDeviceInfo info;
11480 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011481 std::vector<InputDeviceLightInfo> lights = info.getLights();
11482 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011483 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11484 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11485 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11486
11487 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11488 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11489}
11490
11491TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11492 RawLightInfo infoRed = {.id = 1,
11493 .name = "red_keyboard_backlight",
11494 .maxBrightness = 255,
11495 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11496 InputLightClass::KEYBOARD_BACKLIGHT,
11497 .path = ""};
11498 RawLightInfo infoGreen = {.id = 2,
11499 .name = "green_keyboard_backlight",
11500 .maxBrightness = 255,
11501 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11502 InputLightClass::KEYBOARD_BACKLIGHT,
11503 .path = ""};
11504 RawLightInfo infoBlue = {.id = 3,
11505 .name = "blue_keyboard_backlight",
11506 .maxBrightness = 255,
11507 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11508 InputLightClass::KEYBOARD_BACKLIGHT,
11509 .path = ""};
11510 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11511 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11512 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11513
11514 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11515 InputDeviceInfo info;
11516 controller.populateDeviceInfo(&info);
11517 std::vector<InputDeviceLightInfo> lights = info.getLights();
11518 ASSERT_EQ(1U, lights.size());
11519 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11520 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11521 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11522
11523 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11524 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11525}
11526
11527TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11528 RawLightInfo infoRed = {.id = 1,
11529 .name = "red",
11530 .maxBrightness = 255,
11531 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11532 .path = ""};
11533 RawLightInfo infoGreen = {.id = 2,
11534 .name = "green",
11535 .maxBrightness = 255,
11536 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11537 .path = ""};
11538 RawLightInfo infoBlue = {.id = 3,
11539 .name = "blue",
11540 .maxBrightness = 255,
11541 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11542 .path = ""};
11543 RawLightInfo infoGlobal = {.id = 3,
11544 .name = "global_keyboard_backlight",
11545 .maxBrightness = 255,
11546 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11547 InputLightClass::KEYBOARD_BACKLIGHT,
11548 .path = ""};
11549 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11550 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11551 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11552 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11553
11554 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11555 InputDeviceInfo info;
11556 controller.populateDeviceInfo(&info);
11557 std::vector<InputDeviceLightInfo> lights = info.getLights();
11558 ASSERT_EQ(1U, lights.size());
11559 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11560 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11561 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011562
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011563 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11564 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011565}
11566
11567TEST_F(LightControllerTest, MultiColorRGBLight) {
11568 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011569 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011570 .maxBrightness = 255,
11571 .flags = InputLightClass::BRIGHTNESS |
11572 InputLightClass::MULTI_INTENSITY |
11573 InputLightClass::MULTI_INDEX,
11574 .path = ""};
11575
11576 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11577
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011578 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011579 InputDeviceInfo info;
11580 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011581 std::vector<InputDeviceLightInfo> lights = info.getLights();
11582 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011583 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11584 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11585 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11586
11587 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11588 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11589}
11590
11591TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11592 RawLightInfo infoColor = {.id = 1,
11593 .name = "multi_color_keyboard_backlight",
11594 .maxBrightness = 255,
11595 .flags = InputLightClass::BRIGHTNESS |
11596 InputLightClass::MULTI_INTENSITY |
11597 InputLightClass::MULTI_INDEX |
11598 InputLightClass::KEYBOARD_BACKLIGHT,
11599 .path = ""};
11600
11601 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11602
11603 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11604 InputDeviceInfo info;
11605 controller.populateDeviceInfo(&info);
11606 std::vector<InputDeviceLightInfo> lights = info.getLights();
11607 ASSERT_EQ(1U, lights.size());
11608 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11609 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11610 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011611
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011612 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11613 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011614}
11615
11616TEST_F(LightControllerTest, PlayerIdLight) {
11617 RawLightInfo info1 = {.id = 1,
11618 .name = "player1",
11619 .maxBrightness = 255,
11620 .flags = InputLightClass::BRIGHTNESS,
11621 .path = ""};
11622 RawLightInfo info2 = {.id = 2,
11623 .name = "player2",
11624 .maxBrightness = 255,
11625 .flags = InputLightClass::BRIGHTNESS,
11626 .path = ""};
11627 RawLightInfo info3 = {.id = 3,
11628 .name = "player3",
11629 .maxBrightness = 255,
11630 .flags = InputLightClass::BRIGHTNESS,
11631 .path = ""};
11632 RawLightInfo info4 = {.id = 4,
11633 .name = "player4",
11634 .maxBrightness = 255,
11635 .flags = InputLightClass::BRIGHTNESS,
11636 .path = ""};
11637 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11638 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11639 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11640 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11641
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011642 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011643 InputDeviceInfo info;
11644 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011645 std::vector<InputDeviceLightInfo> lights = info.getLights();
11646 ASSERT_EQ(1U, lights.size());
11647 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011648 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11649 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011650
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011651 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11652 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11653 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011654}
11655
Michael Wrightd02c5b62014-02-10 15:10:22 -080011656} // namespace android