blob: 15140969f439746233f54bf2a0124803c0529321 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
Harry Cuttsf13161a2023-03-08 14:15:49 +000019#include <optional>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070020
Prabir Pradhan2770d242019-09-02 18:07:11 -070021#include <CursorInputMapper.h>
22#include <InputDevice.h>
23#include <InputMapper.h>
24#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080025#include <InputReaderBase.h>
26#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000027#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <KeyboardInputMapper.h>
29#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070030#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070031#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070032#include <SingleTouchInputMapper.h>
33#include <SwitchInputMapper.h>
34#include <TestInputListener.h>
Prabir Pradhan739dca42022-09-09 20:12:01 +000035#include <TestInputListenerMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070036#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080037#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000038#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070039#include <android-base/thread_annotations.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000040#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050042#include <gui/constants.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000043#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080044
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070045#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000046#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000047#include "FakeInputReaderPolicy.h"
Harry Cuttsb57f1702022-11-28 15:34:22 +000048#include "FakePointerController.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000049#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000050#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000051#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000052#include "input/DisplayViewport.h"
53#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010054
Michael Wrightd02c5b62014-02-10 15:10:22 -080055namespace android {
56
Dominik Laskowski2f01d772022-03-23 16:01:29 -070057using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000058using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070059using std::chrono_literals::operator""ms;
60
Michael Wrightd02c5b62014-02-10 15:10:22 -080061// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080062static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000063static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080064static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000065static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080066static constexpr int32_t DISPLAY_WIDTH = 480;
67static constexpr int32_t DISPLAY_HEIGHT = 800;
68static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
69static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
70static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070071static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070072static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080073
arthurhungcc7f9802020-04-30 17:55:40 +080074static constexpr int32_t FIRST_SLOT = 0;
75static constexpr int32_t SECOND_SLOT = 1;
76static constexpr int32_t THIRD_SLOT = 2;
77static constexpr int32_t INVALID_TRACKING_ID = -1;
78static constexpr int32_t FIRST_TRACKING_ID = 0;
79static constexpr int32_t SECOND_TRACKING_ID = 1;
80static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080081static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
82static constexpr int32_t LIGHT_COLOR = 0x7F448866;
83static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080084
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080085static constexpr int32_t ACTION_POINTER_0_DOWN =
86 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
87static constexpr int32_t ACTION_POINTER_0_UP =
88 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
89static constexpr int32_t ACTION_POINTER_1_DOWN =
90 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
91static constexpr int32_t ACTION_POINTER_1_UP =
92 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
93
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000094// Minimum timestamp separation between subsequent input events from a Bluetooth device.
95static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
96// Maximum smoothing time delta so that we don't generate events too far into the future.
97constexpr static nsecs_t MAX_BLUETOOTH_SMOOTHING_DELTA = ms2ns(32);
98
Michael Wrightd02c5b62014-02-10 15:10:22 -080099template<typename T>
100static inline T min(T a, T b) {
101 return a < b ? a : b;
102}
103
104static inline float avg(float x, float y) {
105 return (x + y) / 2;
106}
107
Chris Ye3fdbfef2021-01-06 18:45:18 -0800108// Mapping for light color name and the light color
109const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
110 {"green", LightColor::GREEN},
111 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800112
Michael Wrighta9cf4192022-12-01 23:46:39 +0000113static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700114 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000115 case ui::ROTATION_90:
116 return ui::ROTATION_270;
117 case ui::ROTATION_270:
118 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700119 default:
120 return orientation;
121 }
122}
123
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800124static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
125 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000126 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800127
128 const InputDeviceInfo::MotionRange* motionRange =
129 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
130 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
131}
132
133static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
134 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000135 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800136
137 const InputDeviceInfo::MotionRange* motionRange =
138 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
139 ASSERT_EQ(nullptr, motionRange);
140}
141
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700142[[maybe_unused]] static void dumpReader(InputReader& reader) {
143 std::string dump;
144 reader.dump(dump);
145 std::istringstream iss(dump);
146 for (std::string line; std::getline(iss, line);) {
147 ALOGE("%s", line.c_str());
148 std::this_thread::sleep_for(std::chrono::milliseconds(1));
149 }
150}
151
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152// --- FakeInputMapper ---
153
154class FakeInputMapper : public InputMapper {
155 uint32_t mSources;
156 int32_t mKeyboardType;
157 int32_t mMetaState;
158 KeyedVector<int32_t, int32_t> mKeyCodeStates;
159 KeyedVector<int32_t, int32_t> mScanCodeStates;
160 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100161 // fake mapping which would normally come from keyCharacterMap
162 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800163 std::vector<int32_t> mSupportedKeyCodes;
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700164 std::list<NotifyArgs> mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700166 std::mutex mLock;
167 std::condition_variable mStateChangedCondition;
168 bool mConfigureWasCalled GUARDED_BY(mLock);
169 bool mResetWasCalled GUARDED_BY(mLock);
170 bool mProcessWasCalled GUARDED_BY(mLock);
171 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172
Arthur Hungc23540e2018-11-29 20:42:11 +0800173 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800174public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000175 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
176 uint32_t sources)
177 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800178 mSources(sources),
179 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800181 mConfigureWasCalled(false),
182 mResetWasCalled(false),
183 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184
Chris Yea52ade12020-08-27 16:49:20 -0700185 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186
187 void setKeyboardType(int32_t keyboardType) {
188 mKeyboardType = keyboardType;
189 }
190
191 void setMetaState(int32_t metaState) {
192 mMetaState = metaState;
193 }
194
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700195 // Sets the return value for the `process` call.
196 void setProcessResult(std::list<NotifyArgs> notifyArgs) {
197 mProcessResult.clear();
198 for (auto notifyArg : notifyArgs) {
199 mProcessResult.push_back(notifyArg);
200 }
201 }
202
Michael Wrightd02c5b62014-02-10 15:10:22 -0800203 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700204 std::unique_lock<std::mutex> lock(mLock);
205 base::ScopedLockAssertion assumeLocked(mLock);
206 const bool configureCalled =
207 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
208 return mConfigureWasCalled;
209 });
210 if (!configureCalled) {
211 FAIL() << "Expected configure() to have been called.";
212 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800213 mConfigureWasCalled = false;
214 }
215
216 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700217 std::unique_lock<std::mutex> lock(mLock);
218 base::ScopedLockAssertion assumeLocked(mLock);
219 const bool resetCalled =
220 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
221 return mResetWasCalled;
222 });
223 if (!resetCalled) {
224 FAIL() << "Expected reset() to have been called.";
225 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800226 mResetWasCalled = false;
227 }
228
Yi Kong9b14ac62018-07-17 13:48:38 -0700229 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700230 std::unique_lock<std::mutex> lock(mLock);
231 base::ScopedLockAssertion assumeLocked(mLock);
232 const bool processCalled =
233 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
234 return mProcessWasCalled;
235 });
236 if (!processCalled) {
237 FAIL() << "Expected process() to have been called.";
238 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800239 if (outLastEvent) {
240 *outLastEvent = mLastEvent;
241 }
242 mProcessWasCalled = false;
243 }
244
245 void setKeyCodeState(int32_t keyCode, int32_t state) {
246 mKeyCodeStates.replaceValueFor(keyCode, state);
247 }
248
249 void setScanCodeState(int32_t scanCode, int32_t state) {
250 mScanCodeStates.replaceValueFor(scanCode, state);
251 }
252
253 void setSwitchState(int32_t switchCode, int32_t state) {
254 mSwitchStates.replaceValueFor(switchCode, state);
255 }
256
257 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800258 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 }
260
Philip Junker4af3b3d2021-12-14 10:36:55 +0100261 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
262 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
263 }
264
Michael Wrightd02c5b62014-02-10 15:10:22 -0800265private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100266 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800267
Harry Cuttsd02ea102023-03-17 18:21:30 +0000268 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269 InputMapper::populateDeviceInfo(deviceInfo);
270
271 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000272 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800273 }
274 }
275
Arpit Singhed6c3de2023-04-05 19:24:37 +0000276 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000277 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700278 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800280
281 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800282 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000283 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000284 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800285 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700286
287 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700288 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800289 }
290
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700291 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700292 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800293 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700294 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700295 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296 }
297
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700298 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700299 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800300 mLastEvent = *rawEvent;
301 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700302 mStateChangedCondition.notify_all();
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700303 return mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800304 }
305
Chris Yea52ade12020-08-27 16:49:20 -0700306 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800307 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
308 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
309 }
310
Philip Junker4af3b3d2021-12-14 10:36:55 +0100311 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
312 auto it = mKeyCodeMapping.find(locationKeyCode);
313 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
314 }
315
Chris Yea52ade12020-08-27 16:49:20 -0700316 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800317 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
318 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
319 }
320
Chris Yea52ade12020-08-27 16:49:20 -0700321 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800322 ssize_t index = mSwitchStates.indexOfKey(switchCode);
323 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
324 }
325
Chris Yea52ade12020-08-27 16:49:20 -0700326 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700327 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700328 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700329 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800330 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
331 if (keyCodes[i] == mSupportedKeyCodes[j]) {
332 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800333 }
334 }
335 }
Chris Yea52ade12020-08-27 16:49:20 -0700336 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337 return result;
338 }
339
340 virtual int32_t getMetaState() {
341 return mMetaState;
342 }
343
344 virtual void fadePointer() {
345 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800346
347 virtual std::optional<int32_t> getAssociatedDisplay() {
348 if (mViewport) {
349 return std::make_optional(mViewport->displayId);
350 }
351 return std::nullopt;
352 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353};
354
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700355// --- InputReaderPolicyTest ---
356class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700357protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700358 sp<FakeInputReaderPolicy> mFakePolicy;
359
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700360 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700361 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700362};
363
364/**
365 * Check that empty set of viewports is an acceptable configuration.
366 * Also try to get internal viewport two different ways - by type and by uniqueId.
367 *
368 * There will be confusion if two viewports with empty uniqueId and identical type are present.
369 * Such configuration is not currently allowed.
370 */
371TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700372 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700373
374 // We didn't add any viewports yet, so there shouldn't be any.
375 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100376 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700377 ASSERT_FALSE(internalViewport);
378
379 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000380 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000381 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700382
383 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700384 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700385 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100386 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700387
388 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100389 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700390 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700391 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700392
393 mFakePolicy->clearViewports();
394 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700395 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700396 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100397 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700398 ASSERT_FALSE(internalViewport);
399}
400
401TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
402 const std::string internalUniqueId = "local:0";
403 const std::string externalUniqueId = "local:1";
404 const std::string virtualUniqueId1 = "virtual:2";
405 const std::string virtualUniqueId2 = "virtual:3";
406 constexpr int32_t virtualDisplayId1 = 2;
407 constexpr int32_t virtualDisplayId2 = 3;
408
409 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000410 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000411 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000412 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700413 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000414 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000415 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000416 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700417 // Add an virtual viewport
418 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000419 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000420 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700421 // Add another virtual viewport
422 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000423 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000424 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700425
426 // Check matching by type for internal
427 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100428 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700429 ASSERT_TRUE(internalViewport);
430 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
431
432 // Check matching by type for external
433 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100434 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700435 ASSERT_TRUE(externalViewport);
436 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
437
438 // Check matching by uniqueId for virtual viewport #1
439 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700440 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700441 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100442 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700443 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
444 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
445
446 // Check matching by uniqueId for virtual viewport #2
447 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700448 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700449 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100450 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700451 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
452 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
453}
454
455
456/**
457 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
458 * that lookup works by checking display id.
459 * Check that 2 viewports of each kind is possible, for all existing viewport types.
460 */
461TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
462 const std::string uniqueId1 = "uniqueId1";
463 const std::string uniqueId2 = "uniqueId2";
464 constexpr int32_t displayId1 = 2;
465 constexpr int32_t displayId2 = 3;
466
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100467 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
468 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700469 for (const ViewportType& type : types) {
470 mFakePolicy->clearViewports();
471 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000472 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000473 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700474 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000475 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000476 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700477
478 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700479 std::optional<DisplayViewport> viewport1 =
480 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700481 ASSERT_TRUE(viewport1);
482 ASSERT_EQ(displayId1, viewport1->displayId);
483 ASSERT_EQ(type, viewport1->type);
484
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700485 std::optional<DisplayViewport> viewport2 =
486 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700487 ASSERT_TRUE(viewport2);
488 ASSERT_EQ(displayId2, viewport2->displayId);
489 ASSERT_EQ(type, viewport2->type);
490
491 // When there are multiple viewports of the same kind, and uniqueId is not specified
492 // in the call to getDisplayViewport, then that situation is not supported.
493 // The viewports can be stored in any order, so we cannot rely on the order, since that
494 // is just implementation detail.
495 // However, we can check that it still returns *a* viewport, we just cannot assert
496 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700497 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700498 ASSERT_TRUE(someViewport);
499 }
500}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800501
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700502/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000503 * When we have multiple internal displays make sure we always return the default display when
504 * querying by type.
505 */
506TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
507 const std::string uniqueId1 = "uniqueId1";
508 const std::string uniqueId2 = "uniqueId2";
509 constexpr int32_t nonDefaultDisplayId = 2;
510 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
511 "Test display ID should not be ADISPLAY_ID_DEFAULT");
512
513 // Add the default display first and ensure it gets returned.
514 mFakePolicy->clearViewports();
515 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000516 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000517 ViewportType::INTERNAL);
518 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000519 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000520 ViewportType::INTERNAL);
521
522 std::optional<DisplayViewport> viewport =
523 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
524 ASSERT_TRUE(viewport);
525 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
526 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
527
528 // Add the default display second to make sure order doesn't matter.
529 mFakePolicy->clearViewports();
530 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000531 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000532 ViewportType::INTERNAL);
533 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000534 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000535 ViewportType::INTERNAL);
536
537 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
538 ASSERT_TRUE(viewport);
539 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
540 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
541}
542
543/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700544 * Check getDisplayViewportByPort
545 */
546TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100547 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700548 const std::string uniqueId1 = "uniqueId1";
549 const std::string uniqueId2 = "uniqueId2";
550 constexpr int32_t displayId1 = 1;
551 constexpr int32_t displayId2 = 2;
552 const uint8_t hdmi1 = 0;
553 const uint8_t hdmi2 = 1;
554 const uint8_t hdmi3 = 2;
555
556 mFakePolicy->clearViewports();
557 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000558 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000559 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700560 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000561 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000562 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700563
564 // Check that correct display viewport was returned by comparing the display ports.
565 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
566 ASSERT_TRUE(hdmi1Viewport);
567 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
568 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
569
570 // Check that we can still get the same viewport using the uniqueId
571 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
572 ASSERT_TRUE(hdmi1Viewport);
573 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
574 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
575 ASSERT_EQ(type, hdmi1Viewport->type);
576
577 // Check that we cannot find a port with "HDMI2", because we never added one
578 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
579 ASSERT_FALSE(hdmi2Viewport);
580}
581
Michael Wrightd02c5b62014-02-10 15:10:22 -0800582// --- InputReaderTest ---
583
584class InputReaderTest : public testing::Test {
585protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700586 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800587 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700588 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000589 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800590
Chris Yea52ade12020-08-27 16:49:20 -0700591 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700592 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700593 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700594 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800595
Prabir Pradhan28efc192019-11-05 01:10:04 +0000596 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700597 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800598 }
599
Chris Yea52ade12020-08-27 16:49:20 -0700600 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700601 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800603 }
604
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700605 void addDevice(int32_t eventHubId, const std::string& name,
606 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800607 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800608
609 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800610 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800611 }
612 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000613 mReader->loopOnce();
614 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700615 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700617 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 }
619
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800620 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700621 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000622 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700623 }
624
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800625 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700626 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000627 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700628 }
629
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800630 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700631 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700632 ftl::Flags<InputDeviceClass> classes,
633 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800634 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800635 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000636 FakeInputMapper& mapper =
637 device->addMapper<FakeInputMapper>(eventHubId,
638 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800639 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800640 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800641 return mapper;
642 }
643};
644
Chris Ye98d3f532020-10-01 21:48:59 -0700645TEST_F(InputReaderTest, PolicyGetInputDevices) {
646 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700647 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700648 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800649
650 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700651 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800652 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800653 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100654 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
656 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000657 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658}
659
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000660TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
661 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
662 mFakeEventHub->setSysfsRootPath(1, "xyz");
663
664 // Should also have received a notification describing the new input device.
665 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
666 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
667 ASSERT_EQ(0U, inputDevice.getLights().size());
668
669 RawLightInfo infoMonolight = {.id = 123,
670 .name = "mono_keyboard_backlight",
671 .maxBrightness = 255,
672 .flags = InputLightClass::BRIGHTNESS,
673 .path = ""};
674 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
675 mReader->sysfsNodeChanged("xyz");
676 mReader->loopOnce();
677
678 // Should also have received a notification describing the new recreated input device.
679 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
680 inputDevice = mFakePolicy->getInputDevices()[0];
681 ASSERT_EQ(1U, inputDevice.getLights().size());
682}
683
Chris Yee7310032020-09-22 15:36:28 -0700684TEST_F(InputReaderTest, GetMergedInputDevices) {
685 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
686 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
687 // Add two subdevices to device
688 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
689 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000690 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
691 AINPUT_SOURCE_KEYBOARD);
692 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
693 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700694
695 // Push same device instance for next device to be added, so they'll have same identifier.
696 mReader->pushNextDevice(device);
697 mReader->pushNextDevice(device);
698 ASSERT_NO_FATAL_FAILURE(
699 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
700 ASSERT_NO_FATAL_FAILURE(
701 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
702
703 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000704 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700705}
706
Chris Yee14523a2020-12-19 13:46:00 -0800707TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
708 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
709 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
710 // Add two subdevices to device
711 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
712 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000713 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
714 AINPUT_SOURCE_KEYBOARD);
715 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
716 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800717
718 // Push same device instance for next device to be added, so they'll have same identifier.
719 mReader->pushNextDevice(device);
720 mReader->pushNextDevice(device);
721 // Sensor device is initially disabled
722 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
723 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
724 nullptr));
725 // Device is disabled because the only sub device is a sensor device and disabled initially.
726 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
727 ASSERT_FALSE(device->isEnabled());
728 ASSERT_NO_FATAL_FAILURE(
729 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
730 // The merged device is enabled if any sub device is enabled
731 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
732 ASSERT_TRUE(device->isEnabled());
733}
734
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700735TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800736 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700737 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800738 constexpr int32_t eventHubId = 1;
739 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700740 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000741 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
742 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800743 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800744 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700745
Yi Kong9b14ac62018-07-17 13:48:38 -0700746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700747
748 NotifyDeviceResetArgs resetArgs;
749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700750 ASSERT_EQ(deviceId, resetArgs.deviceId);
751
752 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800753 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000754 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700755
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700757 ASSERT_EQ(deviceId, resetArgs.deviceId);
758 ASSERT_EQ(device->isEnabled(), false);
759
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800760 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000761 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700764 ASSERT_EQ(device->isEnabled(), false);
765
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800766 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000767 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700769 ASSERT_EQ(deviceId, resetArgs.deviceId);
770 ASSERT_EQ(device->isEnabled(), true);
771}
772
Michael Wrightd02c5b62014-02-10 15:10:22 -0800773TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800774 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700775 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800776 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800777 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800778 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800779 AINPUT_SOURCE_KEYBOARD, nullptr);
780 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800781
782 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
783 AINPUT_SOURCE_ANY, AKEYCODE_A))
784 << "Should return unknown when the device id is >= 0 but unknown.";
785
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800786 ASSERT_EQ(AKEY_STATE_UNKNOWN,
787 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
788 << "Should return unknown when the device id is valid but the sources are not "
789 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800790
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800791 ASSERT_EQ(AKEY_STATE_DOWN,
792 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
793 AKEYCODE_A))
794 << "Should return value provided by mapper when device id is valid and the device "
795 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800796
797 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
798 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
799 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
800
801 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
802 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
803 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
804}
805
Philip Junker4af3b3d2021-12-14 10:36:55 +0100806TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
807 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
808 constexpr int32_t eventHubId = 1;
809 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
810 InputDeviceClass::KEYBOARD,
811 AINPUT_SOURCE_KEYBOARD, nullptr);
812 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
813
814 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
815 << "Should return unknown when the device with the specified id is not found.";
816
817 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
818 << "Should return correct mapping when device id is valid and mapping exists.";
819
820 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
821 << "Should return the location key code when device id is valid and there's no "
822 "mapping.";
823}
824
825TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
826 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
827 constexpr int32_t eventHubId = 1;
828 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
829 InputDeviceClass::JOYSTICK,
830 AINPUT_SOURCE_GAMEPAD, nullptr);
831 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
832
833 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
834 << "Should return unknown when the device id is valid but there is no keyboard mapper";
835}
836
Michael Wrightd02c5b62014-02-10 15:10:22 -0800837TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800838 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700839 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800840 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800841 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800842 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800843 AINPUT_SOURCE_KEYBOARD, nullptr);
844 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845
846 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
847 AINPUT_SOURCE_ANY, KEY_A))
848 << "Should return unknown when the device id is >= 0 but unknown.";
849
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800850 ASSERT_EQ(AKEY_STATE_UNKNOWN,
851 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
852 << "Should return unknown when the device id is valid but the sources are not "
853 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800854
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800855 ASSERT_EQ(AKEY_STATE_DOWN,
856 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
857 KEY_A))
858 << "Should return value provided by mapper when device id is valid and the device "
859 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860
861 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
862 AINPUT_SOURCE_TRACKBALL, KEY_A))
863 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
864
865 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
866 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
867 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
868}
869
870TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800871 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700872 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800873 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800874 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800875 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800876 AINPUT_SOURCE_KEYBOARD, nullptr);
877 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800878
879 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
880 AINPUT_SOURCE_ANY, SW_LID))
881 << "Should return unknown when the device id is >= 0 but unknown.";
882
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800883 ASSERT_EQ(AKEY_STATE_UNKNOWN,
884 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
885 << "Should return unknown when the device id is valid but the sources are not "
886 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800887
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800888 ASSERT_EQ(AKEY_STATE_DOWN,
889 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
890 SW_LID))
891 << "Should return value provided by mapper when device id is valid and the device "
892 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800893
894 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
895 AINPUT_SOURCE_TRACKBALL, SW_LID))
896 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
897
898 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
899 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
900 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
901}
902
903TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800904 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700905 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800906 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800907 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800908 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800909 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100910
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800911 mapper.addSupportedKeyCode(AKEYCODE_A);
912 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800913
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700914 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800915 uint8_t flags[4] = { 0, 0, 0, 1 };
916
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700917 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800918 << "Should return false when device id is >= 0 but unknown.";
919 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
920
921 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700922 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800923 << "Should return false when device id is valid but the sources are not supported by "
924 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800925 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
926
927 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700928 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800929 keyCodes, flags))
930 << "Should return value provided by mapper when device id is valid and the device "
931 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800932 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
933
934 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700935 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
936 << "Should return false when the device id is < 0 but the sources are not supported by "
937 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800938 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
939
940 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700941 ASSERT_TRUE(
942 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
943 << "Should return value provided by mapper when device id is < 0 and one of the "
944 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
946}
947
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000948TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800949 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700950 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800951
952 NotifyConfigurationChangedArgs args;
953
954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
955 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
956}
957
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000958TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800959 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700960 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000961 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800962 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000963 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800964 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800965 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800966 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800967
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000968 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000969 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800970 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
971
972 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800973 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000974 ASSERT_EQ(when, event.when);
975 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800976 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800977 ASSERT_EQ(EV_KEY, event.type);
978 ASSERT_EQ(KEY_A, event.code);
979 ASSERT_EQ(1, event.value);
980}
981
Garfield Tan1c7bc862020-01-28 13:24:04 -0800982TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800983 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700984 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800985 constexpr int32_t eventHubId = 1;
986 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800987 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000988 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
989 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800990 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800991 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800992
993 NotifyDeviceResetArgs resetArgs;
994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800995 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800996
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800997 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000998 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001000 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001001 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001002
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001003 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001004 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001006 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001007 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001008
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001009 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001010 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001012 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001013 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001014}
1015
Garfield Tan1c7bc862020-01-28 13:24:04 -08001016TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1017 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001018 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001019 constexpr int32_t eventHubId = 1;
1020 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1021 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001022 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1023 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001024 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001025 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1026
1027 NotifyDeviceResetArgs resetArgs;
1028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1029 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1030}
1031
Arthur Hungc23540e2018-11-29 20:42:11 +08001032TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001033 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001034 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001035 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001036 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001037 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1038 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001039 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1040 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001041 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001042
1043 const uint8_t hdmi1 = 1;
1044
1045 // Associated touch screen with second display.
1046 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1047
1048 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001049 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001050 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001051 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001052 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001053 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001054 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001055 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001056 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001057
1058 // Add the device, and make sure all of the callbacks are triggered.
1059 // The device is added after the input port associations are processed since
1060 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001061 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001064 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001065
Arthur Hung2c9a3342019-07-23 14:18:59 +08001066 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001067 ASSERT_EQ(deviceId, device->getId());
1068 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1069 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001070
1071 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001072 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001073 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001074 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001075}
1076
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001077TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1078 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001079 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001080 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1081 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1082 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001083 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1084 AINPUT_SOURCE_KEYBOARD);
1085 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1086 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001087 mReader->pushNextDevice(device);
1088 mReader->pushNextDevice(device);
1089 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1090 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1091
1092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1093
1094 NotifyDeviceResetArgs resetArgs;
1095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1096 ASSERT_EQ(deviceId, resetArgs.deviceId);
1097 ASSERT_TRUE(device->isEnabled());
1098 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1099 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1100
1101 disableDevice(deviceId);
1102 mReader->loopOnce();
1103
1104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1105 ASSERT_EQ(deviceId, resetArgs.deviceId);
1106 ASSERT_FALSE(device->isEnabled());
1107 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1108 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1109
1110 enableDevice(deviceId);
1111 mReader->loopOnce();
1112
1113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1114 ASSERT_EQ(deviceId, resetArgs.deviceId);
1115 ASSERT_TRUE(device->isEnabled());
1116 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1117 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1118}
1119
1120TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1121 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001122 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001123 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1124 // Add two subdevices to device
1125 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1126 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001127 device->addMapper<FakeInputMapper>(eventHubIds[0],
1128 mFakePolicy->getReaderConfiguration(),
1129 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001130 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001131 device->addMapper<FakeInputMapper>(eventHubIds[1],
1132 mFakePolicy->getReaderConfiguration(),
1133 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001134 mReader->pushNextDevice(device);
1135 mReader->pushNextDevice(device);
1136 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1137 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1138
1139 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1140 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1141
1142 ASSERT_EQ(AKEY_STATE_DOWN,
1143 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1144 ASSERT_EQ(AKEY_STATE_DOWN,
1145 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1146 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1147 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1148}
1149
Prabir Pradhan7e186182020-11-10 13:56:45 -08001150TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1151 NotifyPointerCaptureChangedArgs args;
1152
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001153 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001154 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001155 mReader->loopOnce();
1156 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001157 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1158 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001159
1160 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001161 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001162 mReader->loopOnce();
1163 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001164 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001165
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001166 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001167 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001168 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001169 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001170 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001171}
1172
Chris Ye87143712020-11-10 05:05:58 +00001173class FakeVibratorInputMapper : public FakeInputMapper {
1174public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001175 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1176 const InputReaderConfiguration& readerConfig, uint32_t sources)
1177 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001178
1179 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1180};
1181
1182TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1183 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001184 ftl::Flags<InputDeviceClass> deviceClass =
1185 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001186 constexpr int32_t eventHubId = 1;
1187 const char* DEVICE_LOCATION = "BLUETOOTH";
1188 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1189 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001190 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1191 mFakePolicy->getReaderConfiguration(),
1192 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001193 mReader->pushNextDevice(device);
1194
1195 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1196 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1197
1198 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1199 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1200}
1201
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001202// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001203
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001204class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001205public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001206 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001207
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001208 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001209
Andy Chenf9f1a022022-08-29 20:07:10 -04001210 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1211
Chris Yee2b1e5c2021-03-10 22:45:12 -08001212 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1213
1214 void dump(std::string& dump) override {}
1215
1216 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1217 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001218 }
1219
Chris Yee2b1e5c2021-03-10 22:45:12 -08001220 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1221 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001222 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001223
1224 bool setLightColor(int32_t lightId, int32_t color) override {
1225 getDeviceContext().setLightBrightness(lightId, color >> 24);
1226 return true;
1227 }
1228
1229 std::optional<int32_t> getLightColor(int32_t lightId) override {
1230 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1231 if (!result.has_value()) {
1232 return std::nullopt;
1233 }
1234 return result.value() << 24;
1235 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001236
1237 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1238
1239 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1240
1241private:
1242 InputDeviceContext& mDeviceContext;
1243 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1244 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001245 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001246};
1247
Chris Yee2b1e5c2021-03-10 22:45:12 -08001248TEST_F(InputReaderTest, BatteryGetCapacity) {
1249 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001250 ftl::Flags<InputDeviceClass> deviceClass =
1251 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001252 constexpr int32_t eventHubId = 1;
1253 const char* DEVICE_LOCATION = "BLUETOOTH";
1254 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001255 FakePeripheralController& controller =
1256 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001257 mReader->pushNextDevice(device);
1258
1259 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1260
Harry Cuttsa5b71292022-11-28 12:56:17 +00001261 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1262 FakeEventHub::BATTERY_CAPACITY);
1263 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001264}
1265
1266TEST_F(InputReaderTest, BatteryGetStatus) {
1267 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001268 ftl::Flags<InputDeviceClass> deviceClass =
1269 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001270 constexpr int32_t eventHubId = 1;
1271 const char* DEVICE_LOCATION = "BLUETOOTH";
1272 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001273 FakePeripheralController& controller =
1274 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001275 mReader->pushNextDevice(device);
1276
1277 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1278
Harry Cuttsa5b71292022-11-28 12:56:17 +00001279 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1280 FakeEventHub::BATTERY_STATUS);
1281 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001282}
1283
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001284TEST_F(InputReaderTest, BatteryGetDevicePath) {
1285 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1286 ftl::Flags<InputDeviceClass> deviceClass =
1287 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1288 constexpr int32_t eventHubId = 1;
1289 const char* DEVICE_LOCATION = "BLUETOOTH";
1290 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1291 device->addController<FakePeripheralController>(eventHubId);
1292 mReader->pushNextDevice(device);
1293
1294 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1295
Harry Cuttsa5b71292022-11-28 12:56:17 +00001296 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001297}
1298
Chris Ye3fdbfef2021-01-06 18:45:18 -08001299TEST_F(InputReaderTest, LightGetColor) {
1300 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001301 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001302 constexpr int32_t eventHubId = 1;
1303 const char* DEVICE_LOCATION = "BLUETOOTH";
1304 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001305 FakePeripheralController& controller =
1306 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001307 mReader->pushNextDevice(device);
1308 RawLightInfo info = {.id = 1,
1309 .name = "Mono",
1310 .maxBrightness = 255,
1311 .flags = InputLightClass::BRIGHTNESS,
1312 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001313 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1314 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001315
1316 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001317
Harry Cutts33476232023-01-30 19:57:29 +00001318 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1319 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1320 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1321 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001322}
1323
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001324// --- InputReaderIntegrationTest ---
1325
1326// These tests create and interact with the InputReader only through its interface.
1327// The InputReader is started during SetUp(), which starts its processing in its own
1328// thread. The tests use linux uinput to emulate input devices.
1329// NOTE: Interacting with the physical device while these tests are running may cause
1330// the tests to fail.
1331class InputReaderIntegrationTest : public testing::Test {
1332protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001333 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001334 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001335 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001336
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001337 std::shared_ptr<FakePointerController> mFakePointerController;
1338
Chris Yea52ade12020-08-27 16:49:20 -07001339 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001340#if !defined(__ANDROID__)
1341 GTEST_SKIP();
1342#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001343 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001344 mFakePointerController = std::make_shared<FakePointerController>();
1345 mFakePolicy->setPointerController(mFakePointerController);
Harry Cutts33476232023-01-30 19:57:29 +00001346 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1347 /*eventDidNotHappenTimeout=*/30ms);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001348
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001349 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1350 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001351 ASSERT_EQ(mReader->start(), OK);
1352
1353 // Since this test is run on a real device, all the input devices connected
1354 // to the test device will show up in mReader. We wait for those input devices to
1355 // show up before beginning the tests.
1356 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +00001357 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001358 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001359 mTestListener->clearNotifyDeviceResetCalls();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001360 }
1361
Chris Yea52ade12020-08-27 16:49:20 -07001362 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001363#if !defined(__ANDROID__)
1364 return;
1365#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001366 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001367 mReader.reset();
1368 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001369 mFakePolicy.clear();
1370 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001371
1372 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1373 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1374 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1375 [&name](const InputDeviceInfo& info) {
1376 return info.getIdentifier().name == name;
1377 });
1378 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1379 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001380};
1381
1382TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1383 // An invalid input device that is only used for this test.
1384 class InvalidUinputDevice : public UinputDevice {
1385 public:
Harry Cutts33476232023-01-30 19:57:29 +00001386 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001387
1388 private:
1389 void configureDevice(int fd, uinput_user_dev* device) override {}
1390 };
1391
1392 const size_t numDevices = mFakePolicy->getInputDevices().size();
1393
1394 // UinputDevice does not set any event or key bits, so InputReader should not
1395 // consider it as a valid device.
1396 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1397 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1398 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1399 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1400
1401 invalidDevice.reset();
1402 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1403 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1404 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1405}
1406
1407TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1408 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1409
1410 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1411 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1412 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1413 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1414
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001415 const auto device = findDeviceByName(keyboard->getName());
1416 ASSERT_TRUE(device.has_value());
1417 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1418 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1419 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001420
1421 keyboard.reset();
1422 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1423 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1424 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1425}
1426
1427TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1428 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1429 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1430
1431 NotifyConfigurationChangedArgs configChangedArgs;
1432 ASSERT_NO_FATAL_FAILURE(
1433 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001434 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001435 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1436
1437 NotifyKeyArgs keyArgs;
1438 keyboard->pressAndReleaseHomeKey();
1439 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1440 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001441 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001442 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001443 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001444 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001445 prevTimestamp = keyArgs.eventTime;
1446
1447 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1448 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001449 ASSERT_NE(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}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001453
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001454TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1455 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1456 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1457
1458 const auto device = findDeviceByName(stylus->getName());
1459 ASSERT_TRUE(device.has_value());
1460
Prabir Pradhana3621852022-10-14 18:57:23 +00001461 // An external stylus with buttons should also be recognized as a keyboard.
1462 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001463 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1464 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1465
1466 const auto DOWN =
1467 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1468 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1469
1470 stylus->pressAndReleaseKey(BTN_STYLUS);
1471 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1472 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1473 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1474 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1475
1476 stylus->pressAndReleaseKey(BTN_STYLUS2);
1477 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1478 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1479 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1480 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1481
1482 stylus->pressAndReleaseKey(BTN_STYLUS3);
1483 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1484 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1485 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1486 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1487}
1488
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001489/**
1490 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1491 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1492 * are passed to the listener.
1493 */
1494static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1495TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1496 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1497 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1498 NotifyKeyArgs keyArgs;
1499
1500 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1501 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1502 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1503 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1504
1505 controller->pressAndReleaseKey(BTN_GEAR_UP);
1506 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1507 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1508 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1509}
1510
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001511// --- TouchIntegrationTest ---
1512
Arthur Hungaab25622020-01-16 11:22:11 +08001513class TouchIntegrationTest : public InputReaderIntegrationTest {
1514protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001515 const std::string UNIQUE_ID = "local:0";
1516
Chris Yea52ade12020-08-27 16:49:20 -07001517 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001518#if !defined(__ANDROID__)
1519 GTEST_SKIP();
1520#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001521 InputReaderIntegrationTest::SetUp();
1522 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001523 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1524 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001525
1526 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1527 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1528 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001529 const auto info = findDeviceByName(mDevice->getName());
1530 ASSERT_TRUE(info);
1531 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001532 }
1533
1534 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001535 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001536 std::optional<uint8_t> physicalPort,
1537 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001538 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001539 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001540 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001541 }
1542
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001543 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1544 NotifyMotionArgs args;
1545 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1546 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001547 ASSERT_EQ(points.size(), args.getPointerCount());
1548 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001549 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1550 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1551 }
1552 }
1553
Arthur Hungaab25622020-01-16 11:22:11 +08001554 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001555 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001556};
1557
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001558TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
1559 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1560 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1561 // presses).
1562 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1563 mDeviceInfo.getSources());
1564}
1565
Arthur Hungaab25622020-01-16 11:22:11 +08001566TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1567 NotifyMotionArgs args;
1568 const Point centerPoint = mDevice->getCenterPoint();
1569
1570 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001571 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001572 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001573 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001574 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1575 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1576
1577 // ACTION_MOVE
1578 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001579 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001580 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1581 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1582
1583 // ACTION_UP
1584 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001585 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001586 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1587 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1588}
1589
1590TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1591 NotifyMotionArgs args;
1592 const Point centerPoint = mDevice->getCenterPoint();
1593
1594 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001595 mDevice->sendSlot(FIRST_SLOT);
1596 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001597 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001598 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001599 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1600 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1601
1602 // ACTION_POINTER_DOWN (Second slot)
1603 const Point secondPoint = centerPoint + Point(100, 100);
1604 mDevice->sendSlot(SECOND_SLOT);
1605 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001606 mDevice->sendDown(secondPoint);
1607 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001608 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001609 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001610
1611 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001612 mDevice->sendMove(secondPoint + Point(1, 1));
1613 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001614 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1615 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1616
1617 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001618 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001619 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001620 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001621 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001622
1623 // ACTION_UP
1624 mDevice->sendSlot(FIRST_SLOT);
1625 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001626 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001627 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1628 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1629}
1630
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001631/**
1632 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1633 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1634 * data?
1635 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1636 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1637 * for Pointer 0 only is generated after.
1638 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1639 * events, we will not miss any information.
1640 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1641 * event generated afterwards that contains the newest movement of pointer 0.
1642 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1643 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1644 * losing information about non-palm pointers.
1645 */
1646TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1647 NotifyMotionArgs args;
1648 const Point centerPoint = mDevice->getCenterPoint();
1649
1650 // ACTION_DOWN
1651 mDevice->sendSlot(FIRST_SLOT);
1652 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1653 mDevice->sendDown(centerPoint);
1654 mDevice->sendSync();
1655 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1656
1657 // ACTION_POINTER_DOWN (Second slot)
1658 const Point secondPoint = centerPoint + Point(100, 100);
1659 mDevice->sendSlot(SECOND_SLOT);
1660 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1661 mDevice->sendDown(secondPoint);
1662 mDevice->sendSync();
1663 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1664
1665 // ACTION_MOVE (First slot)
1666 mDevice->sendSlot(FIRST_SLOT);
1667 mDevice->sendMove(centerPoint + Point(5, 5));
1668 // ACTION_POINTER_UP (Second slot)
1669 mDevice->sendSlot(SECOND_SLOT);
1670 mDevice->sendPointerUp();
1671 // Send a single sync for the above 2 pointer updates
1672 mDevice->sendSync();
1673
1674 // First, we should get POINTER_UP for the second pointer
1675 assertReceivedMotion(ACTION_POINTER_1_UP,
1676 {/*first pointer */ centerPoint + Point(5, 5),
1677 /*second pointer*/ secondPoint});
1678
1679 // Next, the MOVE event for the first pointer
1680 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1681}
1682
1683/**
1684 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1685 * move, and then it will go up, all in the same frame.
1686 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1687 * gets sent to the listener.
1688 */
1689TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1690 NotifyMotionArgs args;
1691 const Point centerPoint = mDevice->getCenterPoint();
1692
1693 // ACTION_DOWN
1694 mDevice->sendSlot(FIRST_SLOT);
1695 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1696 mDevice->sendDown(centerPoint);
1697 mDevice->sendSync();
1698 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1699
1700 // ACTION_POINTER_DOWN (Second slot)
1701 const Point secondPoint = centerPoint + Point(100, 100);
1702 mDevice->sendSlot(SECOND_SLOT);
1703 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1704 mDevice->sendDown(secondPoint);
1705 mDevice->sendSync();
1706 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1707
1708 // ACTION_MOVE (First slot)
1709 mDevice->sendSlot(FIRST_SLOT);
1710 mDevice->sendMove(centerPoint + Point(5, 5));
1711 // ACTION_POINTER_UP (Second slot)
1712 mDevice->sendSlot(SECOND_SLOT);
1713 mDevice->sendMove(secondPoint + Point(6, 6));
1714 mDevice->sendPointerUp();
1715 // Send a single sync for the above 2 pointer updates
1716 mDevice->sendSync();
1717
1718 // First, we should get POINTER_UP for the second pointer
1719 // The movement of the second pointer during the liftoff frame is ignored.
1720 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1721 assertReceivedMotion(ACTION_POINTER_1_UP,
1722 {/*first pointer */ centerPoint + Point(5, 5),
1723 /*second pointer*/ secondPoint});
1724
1725 // Next, the MOVE event for the first pointer
1726 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1727}
1728
Arthur Hungaab25622020-01-16 11:22:11 +08001729TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1730 NotifyMotionArgs args;
1731 const Point centerPoint = mDevice->getCenterPoint();
1732
1733 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001734 mDevice->sendSlot(FIRST_SLOT);
1735 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001736 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001737 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001738 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1739 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1740
arthurhungcc7f9802020-04-30 17:55:40 +08001741 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001742 const Point secondPoint = centerPoint + Point(100, 100);
1743 mDevice->sendSlot(SECOND_SLOT);
1744 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1745 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001746 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001747 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001748 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001749
arthurhungcc7f9802020-04-30 17:55:40 +08001750 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001751 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001752 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001753 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1754 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1755
arthurhungcc7f9802020-04-30 17:55:40 +08001756 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1757 // a palm event.
1758 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001759 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001760 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001761 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001762 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001763 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001764
arthurhungcc7f9802020-04-30 17:55:40 +08001765 // Send up to second slot, expect first slot send moving.
1766 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001767 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001768 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1769 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001770
arthurhungcc7f9802020-04-30 17:55:40 +08001771 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001772 mDevice->sendSlot(FIRST_SLOT);
1773 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001774 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001775
arthurhungcc7f9802020-04-30 17:55:40 +08001776 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1777 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001778}
1779
Prabir Pradhanda20b172022-09-26 17:01:18 +00001780TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
1781 const Point centerPoint = mDevice->getCenterPoint();
1782
1783 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1784 mDevice->sendSlot(FIRST_SLOT);
1785 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1786 mDevice->sendToolType(MT_TOOL_PEN);
1787 mDevice->sendDown(centerPoint);
1788 mDevice->sendSync();
1789 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1790 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001791 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001792
1793 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1794
1795 // Release the stylus touch.
1796 mDevice->sendUp();
1797 mDevice->sendSync();
1798 ASSERT_NO_FATAL_FAILURE(
1799 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1800
1801 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1802
1803 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1804 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1805 mDevice->sendToolType(MT_TOOL_FINGER);
1806 mDevice->sendDown(centerPoint);
1807 mDevice->sendSync();
1808 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1809 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001810 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001811
1812 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1813
1814 mDevice->sendUp();
1815 mDevice->sendSync();
1816 ASSERT_NO_FATAL_FAILURE(
1817 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1818
1819 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1820 // The policy should be notified of the stylus presence.
1821 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1822 mDevice->sendToolType(MT_TOOL_PEN);
1823 mDevice->sendMove(centerPoint);
1824 mDevice->sendSync();
1825 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1826 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001827 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001828
1829 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1830}
1831
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001832TEST_F(TouchIntegrationTest, ExternalStylusConnectedDuringTouchGesture) {
1833 ASSERT_NO_FATAL_FAILURE(
1834 mTestListener->assertNotifyDeviceResetWasCalled(WithDeviceId(mDeviceInfo.getId())));
1835 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyDeviceResetWasNotCalled());
1836 const Point centerPoint = mDevice->getCenterPoint();
1837
1838 // Down
1839 mDevice->sendSlot(FIRST_SLOT);
1840 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1841 mDevice->sendDown(centerPoint);
1842 mDevice->sendSync();
1843 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1844 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1845
1846 // Move
1847 mDevice->sendMove(centerPoint + Point(1, 1));
1848 mDevice->sendSync();
1849 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1850 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1851
1852 // Connecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
1853 auto externalStylus = createUinputDevice<UinputExternalStylus>();
1854 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1855 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1856 const auto stylusInfo = findDeviceByName(externalStylus->getName());
1857 ASSERT_TRUE(stylusInfo);
1858 ASSERT_NO_FATAL_FAILURE(
1859 mTestListener->assertNotifyDeviceResetWasCalled(WithDeviceId(stylusInfo->getId())));
1860 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyDeviceResetWasNotCalled());
1861
1862 // Move
1863 mDevice->sendMove(centerPoint + Point(2, 2));
1864 mDevice->sendSync();
1865 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1866 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1867
1868 // Disconnecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
1869 externalStylus.reset();
1870 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1871 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1872 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
1873
1874 // Up
1875 mDevice->sendUp();
1876 mDevice->sendSync();
1877 ASSERT_NO_FATAL_FAILURE(
1878 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1879
1880 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
1881}
1882
Prabir Pradhan124ea442022-10-28 20:27:44 +00001883// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001884
Prabir Pradhan124ea442022-10-28 20:27:44 +00001885// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1886// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1887// stylus.
1888template <typename UinputStylusDevice>
1889class StylusButtonIntegrationTest : public TouchIntegrationTest {
1890protected:
1891 void SetUp() override {
1892#if !defined(__ANDROID__)
1893 GTEST_SKIP();
1894#endif
1895 TouchIntegrationTest::SetUp();
1896 mTouchscreen = mDevice.get();
1897 mTouchscreenInfo = mDeviceInfo;
1898
1899 setUpStylusDevice();
1900 }
1901
1902 UinputStylusDevice* mStylus{nullptr};
1903 InputDeviceInfo mStylusInfo{};
1904
1905 UinputTouchScreen* mTouchscreen{nullptr};
1906 InputDeviceInfo mTouchscreenInfo{};
1907
1908private:
1909 // When we are attempting to test stylus button events that are sent from the touchscreen,
1910 // use the same Uinput device for the touchscreen and the stylus.
1911 template <typename T = UinputStylusDevice>
1912 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1913 mStylus = mDevice.get();
1914 mStylusInfo = mDeviceInfo;
1915 }
1916
1917 // When we are attempting to stylus buttons from an external stylus being merged with touches
1918 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1919 template <typename T = UinputStylusDevice>
1920 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1921 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1922 mStylus = mStylusDeviceLifecycleTracker.get();
1923 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1924 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1925 const auto info = findDeviceByName(mStylus->getName());
1926 ASSERT_TRUE(info);
1927 mStylusInfo = *info;
1928 }
1929
1930 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1931
1932 // Hide the base class's device to expose it with a different name for readability.
1933 using TouchIntegrationTest::mDevice;
1934 using TouchIntegrationTest::mDeviceInfo;
1935};
1936
1937using StylusButtonIntegrationTestTypes =
1938 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1939TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1940
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00001941TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001942 const auto stylusId = TestFixture::mStylusInfo.getId();
1943
1944 TestFixture::mStylus->pressKey(BTN_STYLUS);
1945 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1946 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1947 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1948
1949 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1950 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001951 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001952 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001953}
1954
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00001955TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001956 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1957 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1958 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001959
1960 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001961 TestFixture::mStylus->pressKey(BTN_STYLUS);
1962 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001963 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001964 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001965
1966 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001967 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1968 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1969 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1970 TestFixture::mTouchscreen->sendDown(centerPoint);
1971 TestFixture::mTouchscreen->sendSync();
1972 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001973 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001974 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001975 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1976 WithDeviceId(touchscreenId))));
1977 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001978 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001979 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001980 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1981 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001982
Prabir Pradhan124ea442022-10-28 20:27:44 +00001983 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1984 TestFixture::mTouchscreen->sendSync();
1985 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001986 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001987 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001988 WithDeviceId(touchscreenId))));
1989 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001990 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001991 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001992 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001993
1994 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001995 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1996 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001997 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001998 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001999}
2000
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002001TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002002 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2003 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2004 const auto stylusId = TestFixture::mStylusInfo.getId();
2005 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002006 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002007
2008 // Press the stylus button.
2009 TestFixture::mStylus->pressKey(BTN_STYLUS);
2010 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2011 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2012 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2013
2014 // Start hovering with the stylus.
2015 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2016 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2017 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2018 TestFixture::mTouchscreen->sendMove(centerPoint);
2019 TestFixture::mTouchscreen->sendSync();
2020 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2021 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2022 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2023 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2024 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2025 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2026 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2027 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2028 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2029
2030 // Touch down with the stylus.
2031 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2032 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2033 TestFixture::mTouchscreen->sendDown(centerPoint);
2034 TestFixture::mTouchscreen->sendSync();
2035 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2036 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2037 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2038
2039 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2040 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2041 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2042
2043 // Stop touching with the stylus, and start hovering.
2044 TestFixture::mTouchscreen->sendUp();
2045 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2046 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2047 TestFixture::mTouchscreen->sendMove(centerPoint);
2048 TestFixture::mTouchscreen->sendSync();
2049 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2050 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2051 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2052 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2053 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2054 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2055 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2056 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2057 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2058
2059 // Stop hovering.
2060 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2061 TestFixture::mTouchscreen->sendSync();
2062 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2063 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2064 WithButtonState(0))));
2065 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2066 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2067 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2068 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2069
2070 // Release the stylus button.
2071 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2072 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2073 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2074 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2075}
2076
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002077TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002078 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2079 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2080 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002081
2082 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002083 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2084 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2085 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2086 TestFixture::mTouchscreen->sendDown(centerPoint);
2087 TestFixture::mTouchscreen->sendSync();
2088 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002089 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002090 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002091 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002092
2093 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002094 TestFixture::mStylus->pressKey(BTN_STYLUS);
2095 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002096 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002097 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2098 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002099 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
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::mStylus->releaseKey(BTN_STYLUS);
2110 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002111 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002112 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2113 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002114 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002115 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002116 WithDeviceId(touchscreenId))));
2117 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002118 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002119 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002120 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002121
2122 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002123 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2124 TestFixture::mTouchscreen->sendSync();
2125 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002126 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002127 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002128 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002129}
2130
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002131TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002132 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2133 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002134 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002135
2136 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2137 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2138 const auto stylusId = TestFixture::mStylusInfo.getId();
2139
2140 // Start a stylus gesture. By the time this event is processed, the configuration change that
2141 // was requested is guaranteed to be completed.
2142 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2143 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2144 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2145 TestFixture::mTouchscreen->sendDown(centerPoint);
2146 TestFixture::mTouchscreen->sendSync();
2147 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2148 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002149 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002150 WithDeviceId(touchscreenId))));
2151
2152 // Press and release a stylus button. Each change only generates a MOVE motion event.
2153 // Key events are unaffected.
2154 TestFixture::mStylus->pressKey(BTN_STYLUS);
2155 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2156 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2157 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2158 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2159 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002160 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002161 WithDeviceId(touchscreenId))));
2162
2163 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2164 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2165 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2166 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2167 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2168 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002169 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002170 WithDeviceId(touchscreenId))));
2171
2172 // Finish the stylus gesture.
2173 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2174 TestFixture::mTouchscreen->sendSync();
2175 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2176 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002177 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002178 WithDeviceId(touchscreenId))));
2179}
2180
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002181// --- ExternalStylusIntegrationTest ---
2182
2183// Verify the behavior of an external stylus. An external stylus can report pressure or button
2184// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2185// ongoing stylus gesture that is being emitted by the touchscreen.
2186using ExternalStylusIntegrationTest = TouchIntegrationTest;
2187
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002188TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002189 const Point centerPoint = mDevice->getCenterPoint();
2190
2191 // Create an external stylus capable of reporting pressure data that
2192 // should be fused with a touch pointer.
2193 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2194 createUinputDevice<UinputExternalStylusWithPressure>();
2195 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2196 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2197 const auto stylusInfo = findDeviceByName(stylus->getName());
2198 ASSERT_TRUE(stylusInfo);
2199
2200 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2201
2202 const auto touchscreenId = mDeviceInfo.getId();
2203
2204 // Set a pressure value on the stylus. It doesn't generate any events.
2205 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2206 stylus->setPressure(100);
2207 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2208
2209 // Start a finger gesture, and ensure it shows up as stylus gesture
2210 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002211 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002212 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002213 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002214 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002215 mDevice->sendSync();
2216 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2217 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002218 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002219 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002220
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002221 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2222 // event with the updated pressure.
2223 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002224 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2225 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002226 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002227 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002228
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002229 // The external stylus did not generate any events.
2230 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2231 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2232}
2233
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002234TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002235 const Point centerPoint = mDevice->getCenterPoint();
2236
2237 // Create an external stylus capable of reporting pressure data that
2238 // should be fused with a touch pointer.
2239 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2240 createUinputDevice<UinputExternalStylusWithPressure>();
2241 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2242 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2243 const auto stylusInfo = findDeviceByName(stylus->getName());
2244 ASSERT_TRUE(stylusInfo);
2245
2246 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2247
2248 const auto touchscreenId = mDeviceInfo.getId();
2249
2250 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2251 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002252 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2253 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002254 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002255 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002256
2257 // Start a finger gesture. The touch device will withhold generating any touches for
2258 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2259 mDevice->sendSlot(FIRST_SLOT);
2260 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2261 mDevice->sendToolType(MT_TOOL_FINGER);
2262 mDevice->sendDown(centerPoint);
2263 auto waitUntil = std::chrono::system_clock::now() +
2264 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002265 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002266 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002267
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002268 // Since the external stylus did not report a pressure value within the timeout,
2269 // it shows up as a finger pointer.
2270 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2271 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002272 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002273 WithPressure(1.f))));
2274
2275 // Change the pressure on the external stylus. Since the pressure was not present at the start
2276 // of the gesture, it is ignored for now.
2277 stylus->setPressure(200);
2278 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2279
2280 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002281 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2282 mDevice->sendSync();
2283 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2284 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002285 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002286
2287 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2288 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2289 mDevice->sendToolType(MT_TOOL_FINGER);
2290 mDevice->sendDown(centerPoint);
2291 mDevice->sendSync();
2292 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2293 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002294 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002295 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2296
2297 // The external stylus did not generate any events.
2298 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2299 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002300}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002301
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002302TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002303 const Point centerPoint = mDevice->getCenterPoint();
2304
2305 // Create an external stylus device that does not support pressure. It should not affect any
2306 // touch pointers.
2307 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2308 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2309 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2310 const auto stylusInfo = findDeviceByName(stylus->getName());
2311 ASSERT_TRUE(stylusInfo);
2312
2313 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2314
2315 const auto touchscreenId = mDeviceInfo.getId();
2316
2317 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2318 // pressure data from the external stylus.
2319 mDevice->sendSlot(FIRST_SLOT);
2320 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2321 mDevice->sendToolType(MT_TOOL_FINGER);
2322 mDevice->sendDown(centerPoint);
2323 auto waitUntil = std::chrono::system_clock::now() +
2324 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2325 mDevice->sendSync();
2326 ASSERT_NO_FATAL_FAILURE(
2327 mTestListener
2328 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2329 WithToolType(
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002330 ToolType::FINGER),
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002331 WithButtonState(0),
2332 WithDeviceId(touchscreenId),
2333 WithPressure(1.f)),
2334 waitUntil));
2335
2336 // The external stylus did not generate any events.
2337 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2338 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2339}
2340
Michael Wrightd02c5b62014-02-10 15:10:22 -08002341// --- InputDeviceTest ---
2342class InputDeviceTest : public testing::Test {
2343protected:
2344 static const char* DEVICE_NAME;
2345 static const char* DEVICE_LOCATION;
2346 static const int32_t DEVICE_ID;
2347 static const int32_t DEVICE_GENERATION;
2348 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002349 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002350 static const int32_t EVENTHUB_ID;
2351 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2352
2353 std::shared_ptr<FakeEventHub> mFakeEventHub;
2354 sp<FakeInputReaderPolicy> mFakePolicy;
2355 std::unique_ptr<TestInputListener> mFakeListener;
2356 std::unique_ptr<InstrumentedInputReader> mReader;
2357 std::shared_ptr<InputDevice> mDevice;
2358
2359 void SetUp() override {
2360 mFakeEventHub = std::make_unique<FakeEventHub>();
2361 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2362 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002363 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002364 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002365 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002366 identifier.name = DEVICE_NAME;
2367 identifier.location = DEVICE_LOCATION;
2368 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2369 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2370 identifier);
2371 mReader->pushNextDevice(mDevice);
2372 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002373 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002374 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002375
2376 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002377 mFakeListener.reset();
2378 mFakePolicy.clear();
2379 }
2380};
2381
2382const char* InputDeviceTest::DEVICE_NAME = "device";
2383const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2384const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2385const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002386const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002387const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2388 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002389const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002390const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2391
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002392TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002393 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002394 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2395 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002396}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002397
Michael Wrightd02c5b62014-02-10 15:10:22 -08002398TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2399 ASSERT_EQ(mDevice->isEnabled(), false);
2400}
2401
2402TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2403 // Configuration.
2404 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002405 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002406
2407 // Reset.
2408 unused += mDevice->reset(ARBITRARY_TIME);
2409
2410 NotifyDeviceResetArgs resetArgs;
2411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2412 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2413 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2414
2415 // Metadata.
2416 ASSERT_TRUE(mDevice->isIgnored());
2417 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2418
2419 InputDeviceInfo info = mDevice->getDeviceInfo();
2420 ASSERT_EQ(DEVICE_ID, info.getId());
2421 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2422 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2423 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2424
2425 // State queries.
2426 ASSERT_EQ(0, mDevice->getMetaState());
2427
2428 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2429 << "Ignored device should return unknown key code state.";
2430 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2431 << "Ignored device should return unknown scan code state.";
2432 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2433 << "Ignored device should return unknown switch state.";
2434
2435 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2436 uint8_t flags[2] = { 0, 1 };
2437 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2438 << "Ignored device should never mark any key codes.";
2439 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2440 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2441}
2442
2443TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2444 // Configuration.
2445 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2446
2447 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002448 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2449 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002450 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2451 mapper1.setMetaState(AMETA_ALT_ON);
2452 mapper1.addSupportedKeyCode(AKEYCODE_A);
2453 mapper1.addSupportedKeyCode(AKEYCODE_B);
2454 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2455 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2456 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2457 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2458 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2459
2460 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002461 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2462 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002463 mapper2.setMetaState(AMETA_SHIFT_ON);
2464
2465 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002466 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002467
Harry Cuttsf13161a2023-03-08 14:15:49 +00002468 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2469 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002470 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002471 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002472
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002473 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2474 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002475
2476 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002477 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002478 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2479 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002480
2481 NotifyDeviceResetArgs resetArgs;
2482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2483 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2484 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2485
2486 // Metadata.
2487 ASSERT_FALSE(mDevice->isIgnored());
2488 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2489
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002490 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002491 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002492 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002493 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2494 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2495
2496 // State queries.
2497 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2498 << "Should query mappers and combine meta states.";
2499
2500 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2501 << "Should return unknown key code state when source not supported.";
2502 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2503 << "Should return unknown scan code state when source not supported.";
2504 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2505 << "Should return unknown switch state when source not supported.";
2506
2507 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2508 << "Should query mapper when source is supported.";
2509 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2510 << "Should query mapper when source is supported.";
2511 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2512 << "Should query mapper when source is supported.";
2513
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002514 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002515 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002516 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002517 << "Should do nothing when source is unsupported.";
2518 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2519 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2520 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2521 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2522
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002523 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002524 << "Should query mapper when source is supported.";
2525 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2526 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2527 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2528 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2529
2530 // Event handling.
2531 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002532 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002533 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002534
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002535 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2536 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002537}
2538
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -07002539TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2540 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2541 FakeInputMapper& mapper =
2542 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2543 AINPUT_SOURCE_KEYBOARD);
2544 NotifyMotionArgs args1;
2545 NotifySwitchArgs args2;
2546 NotifyKeyArgs args3;
2547 mapper.setProcessResult({args1, args2, args3});
2548
2549 InputReaderConfiguration config;
2550 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2551
2552 RawEvent event;
2553 event.deviceId = EVENTHUB_ID;
2554 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2555
2556 for (auto& arg : notifyArgs) {
2557 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2558 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2559 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2560 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2561 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2562 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2563 }
2564 }
2565}
2566
2567TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2568 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2569 FakeInputMapper& mapper =
2570 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2571 AINPUT_SOURCE_KEYBOARD);
2572 NotifyMotionArgs args;
2573 mapper.setProcessResult({args});
2574
2575 InputReaderConfiguration config;
2576 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2577
2578 RawEvent event;
2579 event.deviceId = EVENTHUB_ID;
2580 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2581
2582 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2583 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2584}
2585
2586TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2587 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2588 FakeInputMapper& mapper =
2589 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2590 AINPUT_SOURCE_KEYBOARD);
2591 NotifyMotionArgs args;
2592 args.policyFlags = POLICY_FLAG_WAKE;
2593 mapper.setProcessResult({args});
2594
2595 InputReaderConfiguration config;
2596 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2597
2598 RawEvent event;
2599 event.deviceId = EVENTHUB_ID;
2600 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2601
2602 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2603 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2604}
2605
Arthur Hung2c9a3342019-07-23 14:18:59 +08002606// A single input device is associated with a specific display. Check that:
2607// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh3e56f7e2023-07-07 13:12:37 +00002608// 2. Device is disabled when setEnabled API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002609TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002610 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2611 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002612
2613 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002614 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002615 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2616 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002617
2618 // Device should be enabled by default.
2619 ASSERT_TRUE(mDevice->isEnabled());
2620
2621 // Prepare associated info.
2622 constexpr uint8_t hdmi = 1;
2623 const std::string UNIQUE_ID = "local:1";
2624
2625 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002626 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002627 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002628 // Device should be disabled because it is associated with a specific display via
2629 // input port <-> display port association, but the corresponding display is not found
2630 ASSERT_FALSE(mDevice->isEnabled());
2631
2632 // Prepare displays.
2633 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002634 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002635 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002636 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002637 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002638 ASSERT_TRUE(mDevice->isEnabled());
2639
2640 // Device should be disabled after set disable.
2641 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002642 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002643 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002644 ASSERT_FALSE(mDevice->isEnabled());
2645
2646 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002647 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002648 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002649 ASSERT_FALSE(mDevice->isEnabled());
2650}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002651
Christine Franks1ba71cc2021-04-07 14:37:42 -07002652TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2653 // Device should be enabled by default.
2654 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002655 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2656 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002657 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002658 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2659 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002660 ASSERT_TRUE(mDevice->isEnabled());
2661
2662 // Device should be disabled because it is associated with a specific display, but the
2663 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002664 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002665 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002666 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002667 ASSERT_FALSE(mDevice->isEnabled());
2668
2669 // Device should be enabled when a display is found.
2670 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002671 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002672 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002673 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002674 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002675 ASSERT_TRUE(mDevice->isEnabled());
2676
2677 // Device should be disabled after set disable.
2678 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002679 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002680 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002681 ASSERT_FALSE(mDevice->isEnabled());
2682
2683 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002684 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002685 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002686 ASSERT_FALSE(mDevice->isEnabled());
2687}
2688
Christine Franks2a2293c2022-01-18 11:51:16 -08002689TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2690 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002691 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2692 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002693 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002694 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2695 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002696
Christine Franks2a2293c2022-01-18 11:51:16 -08002697 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2698 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002699 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002700 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002701 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002702 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002703 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2704}
2705
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002706/**
2707 * This test reproduces a crash caused by a dangling reference that remains after device is added
2708 * and removed. The reference is accessed in InputDevice::dump(..);
2709 */
2710TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2711 constexpr int32_t TEST_EVENTHUB_ID = 10;
2712 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2713
Harry Cutts33476232023-01-30 19:57:29 +00002714 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh7f1765e2023-07-07 13:12:37 +00002715 device.addEventHubDevice(TEST_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002716 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2717 std::string dumpStr, eventHubDevStr;
2718 device.dump(dumpStr, eventHubDevStr);
2719}
2720
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002721TEST_F(InputDeviceTest, GetBluetoothAddress) {
2722 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2723 ASSERT_TRUE(address);
2724 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2725}
2726
Michael Wrightd02c5b62014-02-10 15:10:22 -08002727// --- SwitchInputMapperTest ---
2728
2729class SwitchInputMapperTest : public InputMapperTest {
2730protected:
2731};
2732
2733TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002734 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002735
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002736 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002737}
2738
2739TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002740 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002741
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002742 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002743 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002744
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002745 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002746 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002747}
2748
2749TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002750 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002751 std::list<NotifyArgs> out;
2752 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2753 ASSERT_TRUE(out.empty());
2754 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2755 ASSERT_TRUE(out.empty());
2756 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2757 ASSERT_TRUE(out.empty());
2758 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002759
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002760 ASSERT_EQ(1u, out.size());
2761 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002762 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002763 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2764 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002765 args.switchMask);
2766 ASSERT_EQ(uint32_t(0), args.policyFlags);
2767}
2768
Chris Ye87143712020-11-10 05:05:58 +00002769// --- VibratorInputMapperTest ---
2770class VibratorInputMapperTest : public InputMapperTest {
2771protected:
2772 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2773};
2774
2775TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002776 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002777
2778 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2779}
2780
2781TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002782 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002783
2784 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2785}
2786
2787TEST_F(VibratorInputMapperTest, Vibrate) {
2788 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002789 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00002790 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002791
2792 VibrationElement pattern(2);
2793 VibrationSequence sequence(2);
2794 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002795 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2796 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002797 sequence.addElement(pattern);
2798 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002799 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2800 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002801 sequence.addElement(pattern);
2802
2803 std::vector<int64_t> timings = {0, 1};
2804 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2805
2806 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002807 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002808 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002809 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002810 // Verify vibrator state listener was notified.
2811 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002812 ASSERT_EQ(1u, out.size());
2813 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2814 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2815 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002816 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002817 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002818 ASSERT_FALSE(mapper.isVibrating());
2819 // Verify vibrator state listener was notified.
2820 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002821 ASSERT_EQ(1u, out.size());
2822 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2823 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2824 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002825}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002826
Chris Yef59a2f42020-10-16 12:55:26 -07002827// --- SensorInputMapperTest ---
2828
2829class SensorInputMapperTest : public InputMapperTest {
2830protected:
2831 static const int32_t ACCEL_RAW_MIN;
2832 static const int32_t ACCEL_RAW_MAX;
2833 static const int32_t ACCEL_RAW_FUZZ;
2834 static const int32_t ACCEL_RAW_FLAT;
2835 static const int32_t ACCEL_RAW_RESOLUTION;
2836
2837 static const int32_t GYRO_RAW_MIN;
2838 static const int32_t GYRO_RAW_MAX;
2839 static const int32_t GYRO_RAW_FUZZ;
2840 static const int32_t GYRO_RAW_FLAT;
2841 static const int32_t GYRO_RAW_RESOLUTION;
2842
2843 static const float GRAVITY_MS2_UNIT;
2844 static const float DEGREE_RADIAN_UNIT;
2845
2846 void prepareAccelAxes();
2847 void prepareGyroAxes();
2848 void setAccelProperties();
2849 void setGyroProperties();
2850 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2851};
2852
2853const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2854const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2855const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2856const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2857const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2858
2859const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2860const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2861const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2862const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2863const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2864
2865const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2866const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2867
2868void SensorInputMapperTest::prepareAccelAxes() {
2869 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2870 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2871 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2872 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2873 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2874 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2875}
2876
2877void SensorInputMapperTest::prepareGyroAxes() {
2878 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2879 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2880 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2881 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2882 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2883 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2884}
2885
2886void SensorInputMapperTest::setAccelProperties() {
2887 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2888 /* sensorDataIndex */ 0);
2889 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2890 /* sensorDataIndex */ 1);
2891 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2892 /* sensorDataIndex */ 2);
2893 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2894 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2895 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2896 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2897 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2898}
2899
2900void SensorInputMapperTest::setGyroProperties() {
2901 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2902 /* sensorDataIndex */ 0);
2903 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2904 /* sensorDataIndex */ 1);
2905 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2906 /* sensorDataIndex */ 2);
2907 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2908 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2909 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2910 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2911 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2912}
2913
2914TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00002915 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002916
2917 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2918}
2919
2920TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2921 setAccelProperties();
2922 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00002923 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002924
2925 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2926 std::chrono::microseconds(10000),
2927 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002928 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002929 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2930 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2931 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2932 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2933 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002934
2935 NotifySensorArgs args;
2936 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2937 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2938 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2939
2940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2941 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2942 ASSERT_EQ(args.deviceId, DEVICE_ID);
2943 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2944 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2945 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2946 ASSERT_EQ(args.values, values);
2947 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2948}
2949
2950TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2951 setGyroProperties();
2952 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00002953 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002954
2955 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2956 std::chrono::microseconds(10000),
2957 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002958 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2960 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2962 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002964
2965 NotifySensorArgs args;
2966 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2967 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2968 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2969
2970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2971 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2972 ASSERT_EQ(args.deviceId, DEVICE_ID);
2973 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2974 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2975 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2976 ASSERT_EQ(args.values, values);
2977 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2978}
2979
Michael Wrightd02c5b62014-02-10 15:10:22 -08002980// --- KeyboardInputMapperTest ---
2981
2982class KeyboardInputMapperTest : public InputMapperTest {
2983protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002984 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002985 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002986 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002987
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002988 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002989 int32_t originalKeyCode, int32_t rotatedKeyCode,
2990 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002991};
2992
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002993/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2994 * orientation.
2995 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002996void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002997 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2998 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002999}
3000
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003001void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003002 int32_t originalScanCode, int32_t originalKeyCode,
3003 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003004 NotifyKeyArgs args;
3005
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003006 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3008 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3009 ASSERT_EQ(originalScanCode, args.scanCode);
3010 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003011 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003012
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003013 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3015 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3016 ASSERT_EQ(originalScanCode, args.scanCode);
3017 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003018 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003019}
3020
Michael Wrightd02c5b62014-02-10 15:10:22 -08003021TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003022 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003023 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003024 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003025
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003026 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003027}
3028
3029TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3030 const int32_t USAGE_A = 0x070004;
3031 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003032 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3033 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003034 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3035 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3036 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003037
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003038 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003039 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003040 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003041 // Initial metastate is AMETA_NONE.
3042 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003043
3044 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003046 NotifyKeyArgs args;
3047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3048 ASSERT_EQ(DEVICE_ID, args.deviceId);
3049 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3050 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3051 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3052 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3053 ASSERT_EQ(KEY_HOME, args.scanCode);
3054 ASSERT_EQ(AMETA_NONE, args.metaState);
3055 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3056 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3057 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3058
3059 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003060 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3062 ASSERT_EQ(DEVICE_ID, args.deviceId);
3063 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3064 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3065 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3066 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3067 ASSERT_EQ(KEY_HOME, args.scanCode);
3068 ASSERT_EQ(AMETA_NONE, args.metaState);
3069 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3070 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3071 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3072
3073 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3075 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3077 ASSERT_EQ(DEVICE_ID, args.deviceId);
3078 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3079 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3080 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3081 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3082 ASSERT_EQ(0, args.scanCode);
3083 ASSERT_EQ(AMETA_NONE, args.metaState);
3084 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3085 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3086 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3087
3088 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003089 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3090 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3092 ASSERT_EQ(DEVICE_ID, args.deviceId);
3093 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3094 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3095 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3096 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3097 ASSERT_EQ(0, args.scanCode);
3098 ASSERT_EQ(AMETA_NONE, args.metaState);
3099 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3100 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3101 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3102
3103 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003104 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3105 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3107 ASSERT_EQ(DEVICE_ID, args.deviceId);
3108 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3109 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3110 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3111 ASSERT_EQ(0, args.keyCode);
3112 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3113 ASSERT_EQ(AMETA_NONE, args.metaState);
3114 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3115 ASSERT_EQ(0U, args.policyFlags);
3116 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3117
3118 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3120 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3122 ASSERT_EQ(DEVICE_ID, args.deviceId);
3123 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3124 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3125 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3126 ASSERT_EQ(0, args.keyCode);
3127 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3128 ASSERT_EQ(AMETA_NONE, args.metaState);
3129 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3130 ASSERT_EQ(0U, args.policyFlags);
3131 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3132}
3133
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003134TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3135 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3136 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3137 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3138
3139 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003140 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003141 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3142
3143 // Key down by scan code.
3144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3145 NotifyKeyArgs args;
3146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3147 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3148
3149 // Key up by scan code.
3150 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3152 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3153}
3154
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003155/**
3156 * Ensure that the readTime is set to the time when the EV_KEY is received.
3157 */
3158TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3159 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3160
3161 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003162 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003163 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3164 NotifyKeyArgs args;
3165
3166 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003167 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3169 ASSERT_EQ(12, args.readTime);
3170
3171 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003172 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3174 ASSERT_EQ(15, args.readTime);
3175}
3176
Michael Wrightd02c5b62014-02-10 15:10:22 -08003177TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003178 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3179 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003180 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3181 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3182 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003183
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003184 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003185 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003186 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003187
Arthur Hung95f68612022-04-07 14:08:22 +08003188 // Initial metastate is AMETA_NONE.
3189 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190
3191 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003192 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003193 NotifyKeyArgs args;
3194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3195 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003196 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003197 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003198
3199 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003200 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3202 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003203 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003204
3205 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003206 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3208 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003209 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003210
3211 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003212 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3214 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003215 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003216 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003217}
3218
3219TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003220 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3221 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3222 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3223 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003224
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003225 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003226 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003227 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003228
Michael Wrighta9cf4192022-12-01 23:46:39 +00003229 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003230 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3231 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3232 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3233 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3234 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3235 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3236 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3237 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3238}
3239
3240TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003241 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3242 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3243 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3244 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003245
Michael Wrightd02c5b62014-02-10 15:10:22 -08003246 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003247 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003248 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003249 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003250
Michael Wrighta9cf4192022-12-01 23:46:39 +00003251 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003252 ASSERT_NO_FATAL_FAILURE(
3253 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3254 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3255 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3256 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3257 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3258 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3259 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003261 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003262 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003263 ASSERT_NO_FATAL_FAILURE(
3264 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3265 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3266 AKEYCODE_DPAD_UP, DISPLAY_ID));
3267 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3268 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3269 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3270 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003272 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003273 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003274 ASSERT_NO_FATAL_FAILURE(
3275 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3276 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3277 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3278 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3279 AKEYCODE_DPAD_UP, DISPLAY_ID));
3280 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3281 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003283 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003284 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003285 ASSERT_NO_FATAL_FAILURE(
3286 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3287 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3288 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3289 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3290 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3291 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3292 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293
3294 // Special case: if orientation changes while key is down, we still emit the same keycode
3295 // in the key up as we did in the key down.
3296 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003297 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003298 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003299 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3301 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3302 ASSERT_EQ(KEY_UP, args.scanCode);
3303 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3304
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003305 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003306 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3309 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3310 ASSERT_EQ(KEY_UP, args.scanCode);
3311 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3312}
3313
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003314TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3315 // If the keyboard is not orientation aware,
3316 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003317 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003318
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003319 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003320 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003321 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003322 NotifyKeyArgs args;
3323
3324 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003327 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3329 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3330
Michael Wrighta9cf4192022-12-01 23:46:39 +00003331 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3336 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3337}
3338
3339TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3340 // If the keyboard is orientation aware,
3341 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003342 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003343
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003344 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003345 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003346 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003347 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003348 NotifyKeyArgs args;
3349
3350 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3351 // ^--- already checked by the previous test
3352
Michael Wrighta9cf4192022-12-01 23:46:39 +00003353 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003354 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003355 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3359 ASSERT_EQ(DISPLAY_ID, args.displayId);
3360
3361 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003362 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003363 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003364 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003365 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3369 ASSERT_EQ(newDisplayId, args.displayId);
3370}
3371
Michael Wrightd02c5b62014-02-10 15:10:22 -08003372TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003373 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003374 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003375 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003376
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003377 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003378 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003379
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003380 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003381 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003382}
3383
Philip Junker4af3b3d2021-12-14 10:36:55 +01003384TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3385 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003386 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003387 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3388
3389 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3390 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3391 << "If a mapping is available, the result is equal to the mapping";
3392
3393 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3394 << "If no mapping is available, the result is the key location";
3395}
3396
Michael Wrightd02c5b62014-02-10 15:10:22 -08003397TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003398 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003399 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003400 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003401
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003402 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003403 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003404
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003405 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003406 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003407}
3408
3409TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003410 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003411 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003412 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003413
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003414 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003415
Michael Wrightd02c5b62014-02-10 15:10:22 -08003416 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003417 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003418 ASSERT_TRUE(flags[0]);
3419 ASSERT_FALSE(flags[1]);
3420}
3421
3422TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003423 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3424 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3425 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3426 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3427 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3428 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003429
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003430 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003431 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003432 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003433 // Initial metastate is AMETA_NONE.
3434 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003435
3436 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003437 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3438 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3439 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440
3441 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3443 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003444 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3445 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3446 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003447 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448
3449 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3451 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003452 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3453 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3454 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003455 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003456
3457 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003460 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3461 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3462 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003463 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003464
3465 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003466 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003468 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3469 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3470 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003471 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003472
3473 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003476 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3477 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3478 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003479 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480
3481 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3483 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003484 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3485 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3486 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003487 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003488}
3489
Chris Yea52ade12020-08-27 16:49:20 -07003490TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3491 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3492 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3493 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3494 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3495
3496 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003497 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003498 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3499
Chris Yea52ade12020-08-27 16:49:20 -07003500 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003501 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003502 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3503 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3504 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3505 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3506
3507 NotifyKeyArgs args;
3508 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3511 ASSERT_EQ(AMETA_NONE, args.metaState);
3512 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3513 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3514 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3515
3516 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003517 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3519 ASSERT_EQ(AMETA_NONE, args.metaState);
3520 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3521 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3522 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3523}
3524
Arthur Hung2c9a3342019-07-23 14:18:59 +08003525TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3526 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003527 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3528 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3529 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3530 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003531
3532 // keyboard 2.
3533 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003534 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003535 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003536 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003537 std::shared_ptr<InputDevice> device2 =
3538 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003539 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003540
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003541 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3542 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3543 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3544 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003545
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003546 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003547 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003548 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003549
Arpit Singh67ca6842023-04-26 14:43:16 +00003550 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003551 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003552 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3553 mFakePolicy
3554 ->getReaderConfiguration(),
3555 AINPUT_SOURCE_KEYBOARD,
3556 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003557 std::list<NotifyArgs> unused =
3558 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003559 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003560 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003561
3562 // Prepared displays and associated info.
3563 constexpr uint8_t hdmi1 = 0;
3564 constexpr uint8_t hdmi2 = 1;
3565 const std::string SECONDARY_UNIQUE_ID = "local:1";
3566
3567 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3568 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3569
3570 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003571 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003572 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003573 ASSERT_FALSE(device2->isEnabled());
3574
3575 // Prepare second display.
3576 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003577 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003578 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003579 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003580 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003581 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003582 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003583 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003584
3585 // Device should be enabled after the associated display is found.
3586 ASSERT_TRUE(mDevice->isEnabled());
3587 ASSERT_TRUE(device2->isEnabled());
3588
3589 // Test pad key events
3590 ASSERT_NO_FATAL_FAILURE(
3591 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3592 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3593 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3594 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3595 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3596 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3597 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3598
3599 ASSERT_NO_FATAL_FAILURE(
3600 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3601 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3602 AKEYCODE_DPAD_RIGHT, newDisplayId));
3603 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3604 AKEYCODE_DPAD_DOWN, newDisplayId));
3605 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3606 AKEYCODE_DPAD_LEFT, newDisplayId));
3607}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003608
arthurhungc903df12020-08-11 15:08:42 +08003609TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3610 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3611 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3612 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3613 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3614 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3615 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3616
3617 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003618 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003619 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003620 // Initial metastate is AMETA_NONE.
3621 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003622
3623 // Initialization should have turned all of the lights off.
3624 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3625 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3626 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3627
3628 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003629 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3630 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003631 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3632 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3633
3634 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003637 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3638 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3639
3640 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3642 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003643 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3644 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3645
3646 mFakeEventHub->removeDevice(EVENTHUB_ID);
3647 mReader->loopOnce();
3648
3649 // keyboard 2 should default toggle keys.
3650 const std::string USB2 = "USB2";
3651 const std::string DEVICE_NAME2 = "KEYBOARD2";
3652 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3653 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3654 std::shared_ptr<InputDevice> device2 =
3655 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003656 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003657 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3658 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3659 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3660 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3661 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3662 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3663
Arpit Singh67ca6842023-04-26 14:43:16 +00003664 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003665 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003666 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3667 mFakePolicy
3668 ->getReaderConfiguration(),
3669 AINPUT_SOURCE_KEYBOARD,
3670 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003671 std::list<NotifyArgs> unused =
3672 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003673 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003674 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003675
3676 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3677 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3678 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003679 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3680 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003681}
3682
Arthur Hungcb40a002021-08-03 14:31:01 +00003683TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3684 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3685 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3686 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3687
3688 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh67ca6842023-04-26 14:43:16 +00003689 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003690 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3691 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003692 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003693 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003694 // Initial metastate is AMETA_NONE.
3695 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003696
3697 mReader->toggleCapsLockState(DEVICE_ID);
3698 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3699}
3700
Arthur Hungfb3cc112022-04-13 07:39:50 +00003701TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3702 // keyboard 1.
3703 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3704 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3705 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3706 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3707 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3708 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3709
3710 KeyboardInputMapper& mapper1 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003711 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003712 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3713
3714 // keyboard 2.
3715 const std::string USB2 = "USB2";
3716 const std::string DEVICE_NAME2 = "KEYBOARD2";
3717 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3718 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3719 std::shared_ptr<InputDevice> device2 =
3720 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3721 ftl::Flags<InputDeviceClass>(0));
3722 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3723 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3724 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3725 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3726 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3727 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3728
Arpit Singh67ca6842023-04-26 14:43:16 +00003729 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003730 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003731 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3732 mFakePolicy
3733 ->getReaderConfiguration(),
3734 AINPUT_SOURCE_KEYBOARD,
3735 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003736 std::list<NotifyArgs> unused =
3737 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003738 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003739 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003740
Arthur Hung95f68612022-04-07 14:08:22 +08003741 // Initial metastate is AMETA_NONE.
3742 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3743 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3744
3745 // Toggle num lock on and off.
3746 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3747 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003748 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3749 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3750 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3751
3752 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3753 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3754 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3755 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3756 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3757
3758 // Toggle caps lock on and off.
3759 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3760 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3761 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3762 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3763 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3764
3765 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3766 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3767 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3768 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3769 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3770
3771 // Toggle scroll lock on and off.
3772 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3773 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3774 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3775 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3776 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3777
3778 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3779 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3780 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3781 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3782 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3783}
3784
Arthur Hung2141d542022-08-23 07:45:21 +00003785TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3786 const int32_t USAGE_A = 0x070004;
3787 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3788 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3789
3790 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003791 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00003792 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3793 // Key down by scan code.
3794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3795 NotifyKeyArgs args;
3796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3797 ASSERT_EQ(DEVICE_ID, args.deviceId);
3798 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3799 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3800 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3801 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3802 ASSERT_EQ(KEY_HOME, args.scanCode);
3803 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3804
3805 // Disable device, it should synthesize cancellation events for down events.
3806 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003807 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00003808
3809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3810 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3811 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3812 ASSERT_EQ(KEY_HOME, args.scanCode);
3813 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3814}
3815
Zixuan Qufecb6062022-11-12 04:44:31 +00003816TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh67ca6842023-04-26 14:43:16 +00003817 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3818 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00003819 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003820 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3821 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00003822
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003823 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00003824 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3825
3826 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003827 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00003828
3829 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3830 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3831 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3832 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3833 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003834 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
3835
3836 // Call change layout association with the same values: Generation shouldn't change
3837 generation = mReader->getContext()->getGeneration();
3838 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3839 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3840 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
3841 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00003842}
3843
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003844TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3845 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3846 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3847
3848 // Configuration
Arpit Singh67ca6842023-04-26 14:43:16 +00003849 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003850 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3851 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003852 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003853
3854 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3855 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3856}
3857
Justin Chung71ddb432023-03-27 04:29:07 +00003858TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
3859 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
3860 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003861 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Justin Chung71ddb432023-03-27 04:29:07 +00003862 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3863 NotifyKeyArgs args;
3864
3865 // Key down
3866 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
3867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3868 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
3869}
3870
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003871// --- KeyboardInputMapperTest_ExternalDevice ---
3872
3873class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3874protected:
Chris Yea52ade12020-08-27 16:49:20 -07003875 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003876};
3877
3878TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003879 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3880 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003881
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003882 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3883 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3884 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3885 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003886
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003887 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003888 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003889 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003890
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003891 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003892 NotifyKeyArgs args;
3893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3894 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3895
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003896 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3898 ASSERT_EQ(uint32_t(0), args.policyFlags);
3899
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003902 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003903
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003904 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3906 ASSERT_EQ(uint32_t(0), args.policyFlags);
3907
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003908 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3910 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3911
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003912 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3914 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3915}
3916
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003917TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003918 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003919
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003920 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3921 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3922 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003923
Powei Fengd041c5d2019-05-03 17:11:33 -07003924 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003925 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003926 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003927 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003928
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003929 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003930 NotifyKeyArgs args;
3931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3932 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3933
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003934 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3936 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3937
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003938 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3940 ASSERT_EQ(uint32_t(0), args.policyFlags);
3941
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003942 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3944 ASSERT_EQ(uint32_t(0), args.policyFlags);
3945
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3948 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3949
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003950 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3952 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3953}
3954
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955// --- CursorInputMapperTest ---
3956
3957class CursorInputMapperTest : public InputMapperTest {
3958protected:
3959 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3960
Michael Wright17db18e2020-06-26 20:51:44 +01003961 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003962
Chris Yea52ade12020-08-27 16:49:20 -07003963 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003964 InputMapperTest::SetUp();
3965
Michael Wright17db18e2020-06-26 20:51:44 +01003966 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003967 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003968 }
3969
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003970 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3971 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003972
Michael Wrighta9cf4192022-12-01 23:46:39 +00003973 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003974 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3975 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3976 }
3977
3978 void prepareSecondaryDisplay() {
3979 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003980 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003981 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003982 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003983
3984 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3985 float pressure) {
3986 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3987 0.0f, 0.0f, 0.0f, EPSILON));
3988 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003989};
3990
3991const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3992
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003993void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3994 int32_t originalY, int32_t rotatedX,
3995 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003996 NotifyMotionArgs args;
3997
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003998 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4002 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004003 ASSERT_NO_FATAL_FAILURE(
4004 assertCursorPointerCoords(args.pointerCoords[0],
4005 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4006 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007}
4008
4009TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004010 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004011 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004012
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004013 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004014}
4015
4016TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004018 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004019
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004020 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021}
4022
4023TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004024 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004025 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004026
4027 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004028 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004029
4030 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004031 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4032 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004033 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4034 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4035
4036 // When the bounds are set, then there should be a valid motion range.
4037 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4038
4039 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004040 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004041
4042 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4043 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4044 1, 800 - 1, 0.0f, 0.0f));
4045 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4046 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4047 2, 480 - 1, 0.0f, 0.0f));
4048 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4049 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4050 0.0f, 1.0f, 0.0f, 0.0f));
4051}
4052
4053TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004055 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056
4057 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004058 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004059
4060 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4061 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4062 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4063 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4064 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4065 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4066 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4067 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4068 0.0f, 1.0f, 0.0f, 0.0f));
4069}
4070
4071TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004073 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004074
arthurhungdcef2dc2020-08-11 14:47:50 +08004075 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076
4077 NotifyMotionArgs args;
4078
4079 // Button press.
4080 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004081 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4084 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4085 ASSERT_EQ(DEVICE_ID, args.deviceId);
4086 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4087 ASSERT_EQ(uint32_t(0), args.policyFlags);
4088 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4089 ASSERT_EQ(0, args.flags);
4090 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4091 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4092 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004093 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004095 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004096 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004097 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4098 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4099 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4100
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4102 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4103 ASSERT_EQ(DEVICE_ID, args.deviceId);
4104 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4105 ASSERT_EQ(uint32_t(0), args.policyFlags);
4106 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4107 ASSERT_EQ(0, args.flags);
4108 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4109 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4110 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004111 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004112 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004113 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004114 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004115 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4116 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4117 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4118
Michael Wrightd02c5b62014-02-10 15:10:22 -08004119 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004120 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4121 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4123 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4124 ASSERT_EQ(DEVICE_ID, args.deviceId);
4125 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4126 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004127 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4128 ASSERT_EQ(0, args.flags);
4129 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4130 ASSERT_EQ(0, args.buttonState);
4131 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004132 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004133 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004134 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004135 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004136 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4137 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4138 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4139
4140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4141 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4142 ASSERT_EQ(DEVICE_ID, args.deviceId);
4143 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4144 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004145 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4146 ASSERT_EQ(0, args.flags);
4147 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4148 ASSERT_EQ(0, args.buttonState);
4149 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004150 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004151 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004152 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004153 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4155 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4156 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4157}
4158
4159TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004161 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004162
4163 NotifyMotionArgs args;
4164
4165 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4167 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4169 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004170 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4171 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4172 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004173
4174 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004175 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004179 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4180 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181}
4182
4183TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004185 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004186
4187 NotifyMotionArgs args;
4188
4189 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004190 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4193 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004194 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4197 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004198 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004199
Michael Wrightd02c5b62014-02-10 15:10:22 -08004200 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004204 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004205 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004206
4207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004209 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004210}
4211
4212TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004214 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215
4216 NotifyMotionArgs args;
4217
4218 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004219 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4220 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4221 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4222 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4224 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004225 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4226 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4227 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4230 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004231 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4232 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4233 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004234
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4237 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4238 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004241 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4242 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4243 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004244
4245 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004246 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004249 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004250 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004251
4252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004254 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004255}
4256
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004257TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004258 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004259 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004260 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4261 // need to be rotated.
4262 addConfigurationProperty("cursor.orientationAware", "1");
Arpit Singhe036ad72023-04-27 12:48:15 +00004263 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264
Michael Wrighta9cf4192022-12-01 23:46:39 +00004265 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4267 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4268 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4269 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4270 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4271 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4272 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4273 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4274}
4275
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004276TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004277 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004279 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4280 // orientation-aware are affected by display rotation.
Arpit Singhe036ad72023-04-27 12:48:15 +00004281 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004283 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004284 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004285 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4286 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4287 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4288 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4289 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4290 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4291 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4292 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4293
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004294 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004295 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004296 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4297 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4298 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4299 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4300 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4301 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4302 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4303 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004305 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004306 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4308 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4309 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4310 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4311 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4312 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4313 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4314 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4315
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004316 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004317 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004318 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4319 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4320 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4321 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4322 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4323 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4324 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4325 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004326}
4327
4328TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004329 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004330 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331
4332 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4333 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334
4335 NotifyMotionArgs motionArgs;
4336 NotifyKeyArgs keyArgs;
4337
4338 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4342 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4343 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004344 ASSERT_NO_FATAL_FAILURE(
4345 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004346
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4348 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4349 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004350 ASSERT_NO_FATAL_FAILURE(
4351 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004352
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004353 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004356 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004357 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004358 ASSERT_NO_FATAL_FAILURE(
4359 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360
4361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004362 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004364 ASSERT_NO_FATAL_FAILURE(
4365 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004366
4367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004368 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004369 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004370 ASSERT_NO_FATAL_FAILURE(
4371 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372
4373 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004374 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4375 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4378 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4379 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004380 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004381 ASSERT_NO_FATAL_FAILURE(
4382 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004383
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4385 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4386 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004387 ASSERT_NO_FATAL_FAILURE(
4388 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004389
4390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4391 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4392 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004393 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004394 ASSERT_NO_FATAL_FAILURE(
4395 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004396
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4398 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004400 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004401 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004402 ASSERT_NO_FATAL_FAILURE(
4403 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004404
4405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004407 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004408 ASSERT_NO_FATAL_FAILURE(
4409 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004410
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004411 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004414 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4415 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004416 ASSERT_NO_FATAL_FAILURE(
4417 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004420
4421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004424 ASSERT_NO_FATAL_FAILURE(
4425 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004426
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4428 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004430 ASSERT_NO_FATAL_FAILURE(
4431 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432
4433 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004434 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4435 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4437 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4438 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004439
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004441 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004443 ASSERT_NO_FATAL_FAILURE(
4444 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004445
4446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4447 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4448 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004449 ASSERT_NO_FATAL_FAILURE(
4450 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004451
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004452 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4453 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004455 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004456 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004457 ASSERT_NO_FATAL_FAILURE(
4458 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004459
4460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004462 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004463
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004464 ASSERT_NO_FATAL_FAILURE(
4465 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4467 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4468 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4469
4470 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004471 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4472 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4474 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4475 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004476
Michael Wrightd02c5b62014-02-10 15:10:22 -08004477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004478 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004480 ASSERT_NO_FATAL_FAILURE(
4481 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004482
4483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4484 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4485 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004486 ASSERT_NO_FATAL_FAILURE(
4487 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004489 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004492 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004494 ASSERT_NO_FATAL_FAILURE(
4495 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004496
4497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4498 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4499 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004500 ASSERT_NO_FATAL_FAILURE(
4501 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004502
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4504 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4505 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4506
4507 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4511 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4512 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004513
Michael Wrightd02c5b62014-02-10 15:10:22 -08004514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004515 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004517 ASSERT_NO_FATAL_FAILURE(
4518 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004519
4520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4521 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4522 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004523 ASSERT_NO_FATAL_FAILURE(
4524 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004525
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4527 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004529 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004530 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004531 ASSERT_NO_FATAL_FAILURE(
4532 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004533
4534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4535 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4536 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004537 ASSERT_NO_FATAL_FAILURE(
4538 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004539
Michael Wrightd02c5b62014-02-10 15:10:22 -08004540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4541 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4542 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4543
4544 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4548 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4549 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004550
Michael Wrightd02c5b62014-02-10 15:10:22 -08004551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004552 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004554 ASSERT_NO_FATAL_FAILURE(
4555 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004556
4557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4558 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4559 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004560 ASSERT_NO_FATAL_FAILURE(
4561 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004562
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004563 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4564 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004566 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004567 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004568 ASSERT_NO_FATAL_FAILURE(
4569 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004570
4571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4572 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4573 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004574 ASSERT_NO_FATAL_FAILURE(
4575 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004576
Michael Wrightd02c5b62014-02-10 15:10:22 -08004577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4578 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4579 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4580}
4581
4582TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004583 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004584 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004585
4586 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4587 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588
4589 NotifyMotionArgs args;
4590
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004591 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004595 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4596 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4598 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 +00004599 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004600}
4601
4602TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004603 addConfigurationProperty("cursor.mode", "pointer");
4604 mFakePolicy->setPointerCapture(true);
Arpit Singhe036ad72023-04-27 12:48:15 +00004605 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004606
4607 NotifyDeviceResetArgs resetArgs;
4608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4609 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4610 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4611
4612 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4613 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004614
4615 NotifyMotionArgs args;
4616
4617 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004618 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4619 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4620 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4622 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4625 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004626 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004627
4628 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004629 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4630 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4632 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4633 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4634 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4635 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4637 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4638 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4640 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4641
4642 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004643 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4644 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4646 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4647 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4649 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4651 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4652 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4654 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4655
4656 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4658 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4659 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4661 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4662 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4664 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004665 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004666
4667 // Disable pointer capture and check that the device generation got bumped
4668 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004669 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004670 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004671 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004672 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004673
4674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004675 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4676
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4678 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4679 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4681 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004682 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4684 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 +00004685 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004686}
4687
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004688/**
4689 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4690 * pointer acceleration or speed processing should not be applied.
4691 */
4692TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4693 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004694 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4695 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004696 mFakePolicy->setVelocityControlParams(testParams);
Arpit Singhe036ad72023-04-27 12:48:15 +00004697 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004698
4699 NotifyDeviceResetArgs resetArgs;
4700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4701 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4702 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4703
4704 NotifyMotionArgs args;
4705
4706 // Move and verify scale is applied.
4707 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4708 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4709 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4711 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4712 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4713 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4714 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4715 ASSERT_GT(relX, 10);
4716 ASSERT_GT(relY, 20);
4717
4718 // Enable Pointer Capture
4719 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004720 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004721 NotifyPointerCaptureChangedArgs captureArgs;
4722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4723 ASSERT_TRUE(captureArgs.request.enable);
4724
4725 // Move and verify scale is not applied.
4726 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4727 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4728 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4730 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4731 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4732 ASSERT_EQ(10, args.pointerCoords[0].getX());
4733 ASSERT_EQ(20, args.pointerCoords[0].getY());
4734}
4735
Prabir Pradhan208360b2022-06-24 18:37:04 +00004736TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4737 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004738 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan208360b2022-06-24 18:37:04 +00004739
4740 NotifyDeviceResetArgs resetArgs;
4741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4742 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4743 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4744
4745 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004746 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004747
4748 NotifyMotionArgs args;
4749
4750 // Verify that the coordinates are rotated.
4751 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4752 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4755 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4756 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4757 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4758 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4759
4760 // Enable Pointer Capture.
4761 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004762 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004763 NotifyPointerCaptureChangedArgs captureArgs;
4764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4765 ASSERT_TRUE(captureArgs.request.enable);
4766
4767 // Move and verify rotation is not applied.
4768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4772 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4773 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4774 ASSERT_EQ(10, args.pointerCoords[0].getX());
4775 ASSERT_EQ(20, args.pointerCoords[0].getY());
4776}
4777
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004778TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004779 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004780
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004781 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004782 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004783
4784 // Set up the secondary display as the display on which the pointer should be shown.
4785 // The InputDevice is not associated with any display.
4786 prepareSecondaryDisplay();
4787 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004788 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004789
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004790 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004791 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004792
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004793 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4796 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004798 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4799 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4800 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004801 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004802}
4803
4804TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004805 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004806
4807 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004808 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004809
4810 // Set up the secondary display as the display on which the pointer should be shown,
4811 // and associate the InputDevice with the secondary display.
4812 prepareSecondaryDisplay();
4813 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4814 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004815 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004816
4817 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4818 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004819
4820 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4822 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004824 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4825 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4826 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004827 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004828}
4829
4830TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004831 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004832
4833 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004834 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004835 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4836
4837 // Associate the InputDevice with the secondary display.
4838 prepareSecondaryDisplay();
4839 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004840 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004841
4842 // The mapper should not generate any events because it is associated with a display that is
4843 // different from the pointer display.
4844 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4845 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4846 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004848}
4849
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004850// --- BluetoothCursorInputMapperTest ---
4851
4852class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4853protected:
4854 void SetUp() override {
4855 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4856
4857 mFakePointerController = std::make_shared<FakePointerController>();
4858 mFakePolicy->setPointerController(mFakePointerController);
4859 }
4860};
4861
4862TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4863 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004864 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004865
4866 nsecs_t kernelEventTime = ARBITRARY_TIME;
4867 nsecs_t expectedEventTime = ARBITRARY_TIME;
4868 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4869 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4871 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4872 WithEventTime(expectedEventTime))));
4873
4874 // Process several events that come in quick succession, according to their timestamps.
4875 for (int i = 0; i < 3; i++) {
4876 constexpr static nsecs_t delta = ms2ns(1);
4877 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4878 kernelEventTime += delta;
4879 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4880
4881 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4882 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4884 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4885 WithEventTime(expectedEventTime))));
4886 }
4887}
4888
4889TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4890 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004891 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004892
4893 nsecs_t expectedEventTime = ARBITRARY_TIME;
4894 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4897 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4898 WithEventTime(expectedEventTime))));
4899
4900 // Process several events with the same timestamp from the kernel.
4901 // Ensure that we do not generate events too far into the future.
4902 constexpr static int32_t numEvents =
4903 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4904 for (int i = 0; i < numEvents; i++) {
4905 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4906
4907 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4908 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4910 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4911 WithEventTime(expectedEventTime))));
4912 }
4913
4914 // By processing more events with the same timestamp, we should not generate events with a
4915 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4916 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4917 for (int i = 0; i < 3; i++) {
4918 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4919 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4921 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4922 WithEventTime(cappedEventTime))));
4923 }
4924}
4925
4926TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4927 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004928 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004929
4930 nsecs_t kernelEventTime = ARBITRARY_TIME;
4931 nsecs_t expectedEventTime = ARBITRARY_TIME;
4932 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4933 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4935 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4936 WithEventTime(expectedEventTime))));
4937
4938 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4939 // smoothening is not needed, its timestamp is not affected.
4940 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4941 expectedEventTime = kernelEventTime;
4942
4943 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4944 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4946 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4947 WithEventTime(expectedEventTime))));
4948}
4949
Michael Wrightd02c5b62014-02-10 15:10:22 -08004950// --- TouchInputMapperTest ---
4951
4952class TouchInputMapperTest : public InputMapperTest {
4953protected:
4954 static const int32_t RAW_X_MIN;
4955 static const int32_t RAW_X_MAX;
4956 static const int32_t RAW_Y_MIN;
4957 static const int32_t RAW_Y_MAX;
4958 static const int32_t RAW_TOUCH_MIN;
4959 static const int32_t RAW_TOUCH_MAX;
4960 static const int32_t RAW_TOOL_MIN;
4961 static const int32_t RAW_TOOL_MAX;
4962 static const int32_t RAW_PRESSURE_MIN;
4963 static const int32_t RAW_PRESSURE_MAX;
4964 static const int32_t RAW_ORIENTATION_MIN;
4965 static const int32_t RAW_ORIENTATION_MAX;
4966 static const int32_t RAW_DISTANCE_MIN;
4967 static const int32_t RAW_DISTANCE_MAX;
4968 static const int32_t RAW_TILT_MIN;
4969 static const int32_t RAW_TILT_MAX;
4970 static const int32_t RAW_ID_MIN;
4971 static const int32_t RAW_ID_MAX;
4972 static const int32_t RAW_SLOT_MIN;
4973 static const int32_t RAW_SLOT_MAX;
4974 static const float X_PRECISION;
4975 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004976 static const float X_PRECISION_VIRTUAL;
4977 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004978
4979 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004980 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004981
4982 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4983
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004984 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004985 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004986
Michael Wrightd02c5b62014-02-10 15:10:22 -08004987 enum Axes {
4988 POSITION = 1 << 0,
4989 TOUCH = 1 << 1,
4990 TOOL = 1 << 2,
4991 PRESSURE = 1 << 3,
4992 ORIENTATION = 1 << 4,
4993 MINOR = 1 << 5,
4994 ID = 1 << 6,
4995 DISTANCE = 1 << 7,
4996 TILT = 1 << 8,
4997 SLOT = 1 << 9,
4998 TOOL_TYPE = 1 << 10,
4999 };
5000
Michael Wrighta9cf4192022-12-01 23:46:39 +00005001 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005002 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00005003 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005005 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005006 int32_t toRawX(float displayX);
5007 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005008 int32_t toRotatedRawX(float displayX);
5009 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005010 float toCookedX(float rawX, float rawY);
5011 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005012 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005013 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005014 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005015 float toDisplayY(int32_t rawY, int32_t displayHeight);
5016
Michael Wrightd02c5b62014-02-10 15:10:22 -08005017};
5018
5019const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5020const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5021const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5022const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5023const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5024const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5025const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5026const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005027const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5028const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005029const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5030const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5031const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5032const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5033const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5034const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5035const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5036const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5037const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5038const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5039const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5040const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005041const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5042 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5043const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5044 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005045const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5046 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005047
5048const float TouchInputMapperTest::GEOMETRIC_SCALE =
5049 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5050 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5051
5052const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5053 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5054 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5055};
5056
Michael Wrighta9cf4192022-12-01 23:46:39 +00005057void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005058 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5059 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005060}
5061
5062void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5063 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00005064 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005065}
5066
Michael Wrighta9cf4192022-12-01 23:46:39 +00005067void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005068 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5069 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5070 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005071}
5072
Michael Wrightd02c5b62014-02-10 15:10:22 -08005073void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005074 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5075 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5076 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5077 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005078}
5079
Jason Gerecke489fda82012-09-07 17:19:40 -07005080void TouchInputMapperTest::prepareLocationCalibration() {
5081 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5082}
5083
Michael Wrightd02c5b62014-02-10 15:10:22 -08005084int32_t TouchInputMapperTest::toRawX(float displayX) {
5085 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5086}
5087
5088int32_t TouchInputMapperTest::toRawY(float displayY) {
5089 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5090}
5091
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005092int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5093 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5094}
5095
5096int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5097 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5098}
5099
Jason Gerecke489fda82012-09-07 17:19:40 -07005100float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5101 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5102 return rawX;
5103}
5104
5105float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5106 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5107 return rawY;
5108}
5109
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005111 return toDisplayX(rawX, DISPLAY_WIDTH);
5112}
5113
5114float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5115 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116}
5117
5118float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005119 return toDisplayY(rawY, DISPLAY_HEIGHT);
5120}
5121
5122float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5123 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005124}
5125
5126
5127// --- SingleTouchInputMapperTest ---
5128
5129class SingleTouchInputMapperTest : public TouchInputMapperTest {
5130protected:
5131 void prepareButtons();
5132 void prepareAxes(int axes);
5133
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005134 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5135 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5136 void processUp(SingleTouchInputMapper& mappery);
5137 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5138 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5139 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5140 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5141 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5142 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005143};
5144
5145void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005146 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005147}
5148
5149void SingleTouchInputMapperTest::prepareAxes(int axes) {
5150 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005151 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5152 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005153 }
5154 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005155 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5156 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005157 }
5158 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005159 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5160 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005161 }
5162 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005163 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5164 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005165 }
5166 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005167 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5168 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005169 }
5170}
5171
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005172void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005173 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5174 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5175 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005176}
5177
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005178void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005181}
5182
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005183void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005185}
5186
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005187void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005189}
5190
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005191void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5192 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005194}
5195
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005196void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005198}
5199
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005200void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5201 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5203 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005204}
5205
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005206void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5207 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209}
5210
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005211void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005212 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005213}
5214
Michael Wrightd02c5b62014-02-10 15:10:22 -08005215TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005216 prepareButtons();
5217 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005218 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005219
Josep del Río2d8c79a2023-01-23 19:33:50 +00005220 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005221}
5222
Michael Wrightd02c5b62014-02-10 15:10:22 -08005223TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005224 prepareButtons();
5225 prepareAxes(POSITION);
5226 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00005227 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005228
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005229 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005230}
5231
5232TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005233 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005234 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005235 prepareButtons();
5236 prepareAxes(POSITION);
5237 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005238 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005239
5240 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005241 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005242
5243 // Virtual key is down.
5244 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5245 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5246 processDown(mapper, x, y);
5247 processSync(mapper);
5248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5249
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005250 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005251
5252 // Virtual key is up.
5253 processUp(mapper);
5254 processSync(mapper);
5255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5256
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005257 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005258}
5259
5260TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005261 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005262 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005263 prepareButtons();
5264 prepareAxes(POSITION);
5265 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005266 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005267
5268 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005269 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005270
5271 // Virtual key is down.
5272 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5273 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5274 processDown(mapper, x, y);
5275 processSync(mapper);
5276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5277
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005278 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005279
5280 // Virtual key is up.
5281 processUp(mapper);
5282 processSync(mapper);
5283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5284
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005285 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005286}
5287
5288TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005289 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005290 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005291 prepareButtons();
5292 prepareAxes(POSITION);
5293 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005294 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005295
Michael Wrightd02c5b62014-02-10 15:10:22 -08005296 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005297 ASSERT_TRUE(
5298 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005299 ASSERT_TRUE(flags[0]);
5300 ASSERT_FALSE(flags[1]);
5301}
5302
5303TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005304 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005305 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005306 prepareButtons();
5307 prepareAxes(POSITION);
5308 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005309 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005310
arthurhungdcef2dc2020-08-11 14:47:50 +08005311 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005312
5313 NotifyKeyArgs args;
5314
5315 // Press virtual key.
5316 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5317 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5318 processDown(mapper, x, y);
5319 processSync(mapper);
5320
5321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5322 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5323 ASSERT_EQ(DEVICE_ID, args.deviceId);
5324 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5325 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5326 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5327 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5328 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5329 ASSERT_EQ(KEY_HOME, args.scanCode);
5330 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5331 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5332
5333 // Release virtual key.
5334 processUp(mapper);
5335 processSync(mapper);
5336
5337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5338 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5339 ASSERT_EQ(DEVICE_ID, args.deviceId);
5340 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5341 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5342 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5343 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5344 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5345 ASSERT_EQ(KEY_HOME, args.scanCode);
5346 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5347 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5348
5349 // Should not have sent any motions.
5350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5351}
5352
5353TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005354 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005355 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005356 prepareButtons();
5357 prepareAxes(POSITION);
5358 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005359 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005360
arthurhungdcef2dc2020-08-11 14:47:50 +08005361 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005362
5363 NotifyKeyArgs keyArgs;
5364
5365 // Press virtual key.
5366 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5367 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5368 processDown(mapper, x, y);
5369 processSync(mapper);
5370
5371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5372 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5373 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5374 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5375 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5376 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5377 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5378 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5379 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5380 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5381 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5382
5383 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5384 // into the display area.
5385 y -= 100;
5386 processMove(mapper, x, y);
5387 processSync(mapper);
5388
5389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5390 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5391 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5392 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5393 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5394 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5395 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5396 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5397 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5398 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5399 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5400 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5401
5402 NotifyMotionArgs motionArgs;
5403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5404 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5405 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5406 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5407 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5408 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5409 ASSERT_EQ(0, motionArgs.flags);
5410 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5411 ASSERT_EQ(0, motionArgs.buttonState);
5412 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005413 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005414 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005415 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5417 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5418 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5419 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5420 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5421
5422 // Keep moving out of bounds. Should generate a pointer move.
5423 y -= 50;
5424 processMove(mapper, x, y);
5425 processSync(mapper);
5426
5427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5428 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5429 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5430 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5431 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5432 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5433 ASSERT_EQ(0, motionArgs.flags);
5434 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5435 ASSERT_EQ(0, motionArgs.buttonState);
5436 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005437 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005438 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005439 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5441 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5442 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5443 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5444 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5445
5446 // Release out of bounds. Should generate a pointer up.
5447 processUp(mapper);
5448 processSync(mapper);
5449
5450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5451 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5452 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5453 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5454 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5455 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5456 ASSERT_EQ(0, motionArgs.flags);
5457 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5458 ASSERT_EQ(0, motionArgs.buttonState);
5459 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005460 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005461 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005462 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5464 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5465 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5466 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5467 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5468
5469 // Should not have sent any more keys or motions.
5470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5472}
5473
5474TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005475 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005476 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005477 prepareButtons();
5478 prepareAxes(POSITION);
5479 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005480 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005481
arthurhungdcef2dc2020-08-11 14:47:50 +08005482 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005483
5484 NotifyMotionArgs motionArgs;
5485
5486 // Initially go down out of bounds.
5487 int32_t x = -10;
5488 int32_t y = -10;
5489 processDown(mapper, x, y);
5490 processSync(mapper);
5491
5492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5493
5494 // Move into the display area. Should generate a pointer down.
5495 x = 50;
5496 y = 75;
5497 processMove(mapper, x, y);
5498 processSync(mapper);
5499
5500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5501 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5502 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5503 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5504 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5505 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5506 ASSERT_EQ(0, motionArgs.flags);
5507 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5508 ASSERT_EQ(0, motionArgs.buttonState);
5509 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005510 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005511 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005512 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5514 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5515 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5516 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5517 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5518
5519 // Release. Should generate a pointer up.
5520 processUp(mapper);
5521 processSync(mapper);
5522
5523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5524 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5525 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5526 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5527 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5528 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5529 ASSERT_EQ(0, motionArgs.flags);
5530 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5531 ASSERT_EQ(0, motionArgs.buttonState);
5532 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005533 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005534 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005535 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005536 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5537 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5538 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5539 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5540 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5541
5542 // Should not have sent any more keys or motions.
5543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5545}
5546
Santos Cordonfa5cf462017-04-05 10:37:00 -07005547TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005548 addConfigurationProperty("touch.deviceType", "touchScreen");
5549 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5550
Michael Wrighta9cf4192022-12-01 23:46:39 +00005551 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005552 prepareButtons();
5553 prepareAxes(POSITION);
5554 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005555 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005556
arthurhungdcef2dc2020-08-11 14:47:50 +08005557 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005558
5559 NotifyMotionArgs motionArgs;
5560
5561 // Down.
5562 int32_t x = 100;
5563 int32_t y = 125;
5564 processDown(mapper, x, y);
5565 processSync(mapper);
5566
5567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5568 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5569 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5570 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5571 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5572 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5573 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5574 ASSERT_EQ(0, motionArgs.flags);
5575 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5576 ASSERT_EQ(0, motionArgs.buttonState);
5577 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005578 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005579 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005580 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5582 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5583 1, 0, 0, 0, 0, 0, 0, 0));
5584 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5585 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5586 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5587
5588 // Move.
5589 x += 50;
5590 y += 75;
5591 processMove(mapper, x, y);
5592 processSync(mapper);
5593
5594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5595 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5596 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5597 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5598 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5599 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5601 ASSERT_EQ(0, motionArgs.flags);
5602 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5603 ASSERT_EQ(0, motionArgs.buttonState);
5604 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005605 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005606 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005607 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5609 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5610 1, 0, 0, 0, 0, 0, 0, 0));
5611 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5612 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5613 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5614
5615 // Up.
5616 processUp(mapper);
5617 processSync(mapper);
5618
5619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5620 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5621 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5622 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5623 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5624 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5625 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5626 ASSERT_EQ(0, motionArgs.flags);
5627 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5628 ASSERT_EQ(0, motionArgs.buttonState);
5629 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005630 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005631 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005632 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5634 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5635 1, 0, 0, 0, 0, 0, 0, 0));
5636 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5637 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5638 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5639
5640 // Should not have sent any more keys or motions.
5641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5643}
5644
Michael Wrightd02c5b62014-02-10 15:10:22 -08005645TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005646 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005647 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005648 prepareButtons();
5649 prepareAxes(POSITION);
5650 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005651 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005652
arthurhungdcef2dc2020-08-11 14:47:50 +08005653 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005654
5655 NotifyMotionArgs motionArgs;
5656
5657 // Down.
5658 int32_t x = 100;
5659 int32_t y = 125;
5660 processDown(mapper, x, y);
5661 processSync(mapper);
5662
5663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5664 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5665 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5666 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5667 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5668 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5669 ASSERT_EQ(0, motionArgs.flags);
5670 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5671 ASSERT_EQ(0, motionArgs.buttonState);
5672 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005673 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005674 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005675 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5677 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5678 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5679 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5680 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5681
5682 // Move.
5683 x += 50;
5684 y += 75;
5685 processMove(mapper, x, y);
5686 processSync(mapper);
5687
5688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5689 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5690 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5691 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5692 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5693 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5694 ASSERT_EQ(0, motionArgs.flags);
5695 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5696 ASSERT_EQ(0, motionArgs.buttonState);
5697 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005698 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005699 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005700 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5702 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5703 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5704 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5705 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5706
5707 // Up.
5708 processUp(mapper);
5709 processSync(mapper);
5710
5711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5712 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5713 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5714 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5715 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5716 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5717 ASSERT_EQ(0, motionArgs.flags);
5718 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5719 ASSERT_EQ(0, motionArgs.buttonState);
5720 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005721 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005722 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005723 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5725 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5726 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5727 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5728 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5729
5730 // Should not have sent any more keys or motions.
5731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5733}
5734
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005735TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005736 addConfigurationProperty("touch.deviceType", "touchScreen");
5737 prepareButtons();
5738 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005739 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5740 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005741 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005742
5743 NotifyMotionArgs args;
5744
5745 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005746 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005747 processDown(mapper, toRawX(50), toRawY(75));
5748 processSync(mapper);
5749
5750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5751 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5752 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5753
5754 processUp(mapper);
5755 processSync(mapper);
5756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5757}
5758
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005759TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005760 addConfigurationProperty("touch.deviceType", "touchScreen");
5761 prepareButtons();
5762 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005763 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5764 // orientation-aware are affected by display rotation.
5765 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005766 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005767
5768 NotifyMotionArgs args;
5769
5770 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005771 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005772 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005773 processDown(mapper, toRawX(50), toRawY(75));
5774 processSync(mapper);
5775
5776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5777 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5778 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5779
5780 processUp(mapper);
5781 processSync(mapper);
5782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5783
5784 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005785 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005786 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005787 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005788 processSync(mapper);
5789
5790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5791 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5792 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5793
5794 processUp(mapper);
5795 processSync(mapper);
5796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5797
5798 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005799 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005800 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005801 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5802 processSync(mapper);
5803
5804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5805 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5806 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5807
5808 processUp(mapper);
5809 processSync(mapper);
5810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5811
5812 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005813 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005814 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005815 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005816 processSync(mapper);
5817
5818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5819 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5820 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5821
5822 processUp(mapper);
5823 processSync(mapper);
5824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5825}
5826
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005827TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5828 addConfigurationProperty("touch.deviceType", "touchScreen");
5829 prepareButtons();
5830 prepareAxes(POSITION);
5831 addConfigurationProperty("touch.orientationAware", "1");
5832 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5833 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005834 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005835 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005836 NotifyMotionArgs args;
5837
5838 // Orientation 0.
5839 processDown(mapper, toRawX(50), toRawY(75));
5840 processSync(mapper);
5841
5842 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5843 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5844 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5845
5846 processUp(mapper);
5847 processSync(mapper);
5848 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5849}
5850
5851TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5852 addConfigurationProperty("touch.deviceType", "touchScreen");
5853 prepareButtons();
5854 prepareAxes(POSITION);
5855 addConfigurationProperty("touch.orientationAware", "1");
5856 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5857 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005858 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005859 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005860 NotifyMotionArgs args;
5861
5862 // Orientation 90.
5863 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5864 processSync(mapper);
5865
5866 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5867 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5868 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5869
5870 processUp(mapper);
5871 processSync(mapper);
5872 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5873}
5874
5875TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5876 addConfigurationProperty("touch.deviceType", "touchScreen");
5877 prepareButtons();
5878 prepareAxes(POSITION);
5879 addConfigurationProperty("touch.orientationAware", "1");
5880 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5881 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005882 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005883 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005884 NotifyMotionArgs args;
5885
5886 // Orientation 180.
5887 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5888 processSync(mapper);
5889
5890 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5891 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5892 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5893
5894 processUp(mapper);
5895 processSync(mapper);
5896 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5897}
5898
5899TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5900 addConfigurationProperty("touch.deviceType", "touchScreen");
5901 prepareButtons();
5902 prepareAxes(POSITION);
5903 addConfigurationProperty("touch.orientationAware", "1");
5904 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5905 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005906 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005907 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005908 NotifyMotionArgs args;
5909
5910 // Orientation 270.
5911 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5912 processSync(mapper);
5913
5914 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5915 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5916 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5917
5918 processUp(mapper);
5919 processSync(mapper);
5920 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5921}
5922
5923TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5924 addConfigurationProperty("touch.deviceType", "touchScreen");
5925 prepareButtons();
5926 prepareAxes(POSITION);
5927 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5928 // orientation-aware are affected by display rotation.
5929 addConfigurationProperty("touch.orientationAware", "0");
5930 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00005931 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005932
5933 NotifyMotionArgs args;
5934
5935 // Orientation 90, Rotation 0.
5936 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005937 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005938 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5939 processSync(mapper);
5940
5941 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5942 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5943 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5944
5945 processUp(mapper);
5946 processSync(mapper);
5947 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5948
5949 // Orientation 90, Rotation 90.
5950 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005951 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005952 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005953 processSync(mapper);
5954
5955 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5956 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5957 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5958
5959 processUp(mapper);
5960 processSync(mapper);
5961 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5962
5963 // Orientation 90, Rotation 180.
5964 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005965 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005966 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5967 processSync(mapper);
5968
5969 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5970 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5971 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5972
5973 processUp(mapper);
5974 processSync(mapper);
5975 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5976
5977 // Orientation 90, Rotation 270.
5978 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005979 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005980 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 -07005981 processSync(mapper);
5982
5983 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5984 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5985 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5986
5987 processUp(mapper);
5988 processSync(mapper);
5989 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5990}
5991
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005992TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5993 addConfigurationProperty("touch.deviceType", "touchScreen");
5994 prepareButtons();
5995 prepareAxes(POSITION);
5996 addConfigurationProperty("touch.orientationAware", "1");
5997 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005998 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005999
6000 // Set a physical frame in the display viewport.
6001 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6002 viewport->physicalLeft = 20;
6003 viewport->physicalTop = 600;
6004 viewport->physicalRight = 30;
6005 viewport->physicalBottom = 610;
6006 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006007 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006008
6009 // Start the touch.
6010 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6011 processSync(mapper);
6012
6013 // Expect all input starting outside the physical frame to be ignored.
6014 const std::array<Point, 6> outsidePoints = {
6015 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6016 for (const auto& p : outsidePoints) {
6017 processMove(mapper, toRawX(p.x), toRawY(p.y));
6018 processSync(mapper);
6019 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6020 }
6021
6022 // Move the touch into the physical frame.
6023 processMove(mapper, toRawX(25), toRawY(605));
6024 processSync(mapper);
6025 NotifyMotionArgs args;
6026 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6027 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6028 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6029 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6030
6031 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
6032 for (const auto& p : outsidePoints) {
6033 processMove(mapper, toRawX(p.x), toRawY(p.y));
6034 processSync(mapper);
6035 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6036 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
6037 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6038 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6039 }
6040
6041 processUp(mapper);
6042 processSync(mapper);
6043 EXPECT_NO_FATAL_FAILURE(
6044 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
6045}
6046
Harry Cutts1db43992023-06-19 17:05:07 +00006047TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
6048 std::shared_ptr<FakePointerController> fakePointerController =
6049 std::make_shared<FakePointerController>();
6050 mFakePolicy->setPointerController(fakePointerController);
6051
6052 addConfigurationProperty("touch.deviceType", "pointer");
6053 prepareAxes(POSITION);
6054 prepareDisplay(ui::ROTATION_0);
6055 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
6056
6057 // Set a physical frame in the display viewport.
6058 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6059 viewport->physicalLeft = 20;
6060 viewport->physicalTop = 600;
6061 viewport->physicalRight = 30;
6062 viewport->physicalBottom = 610;
6063 mFakePolicy->updateViewport(*viewport);
6064 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
6065
6066 // Start the touch.
6067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6068 processSync(mapper);
6069
6070 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
6071 // produced.
6072 const std::array<Point, 6> outsidePoints = {
6073 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6074 for (const auto& p : outsidePoints) {
6075 processMove(mapper, toRawX(p.x), toRawY(p.y));
6076 processSync(mapper);
6077 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6078 }
6079}
6080
Michael Wrightd02c5b62014-02-10 15:10:22 -08006081TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006082 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006083 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006084 prepareButtons();
6085 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00006086 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006087
6088 // These calculations are based on the input device calibration documentation.
6089 int32_t rawX = 100;
6090 int32_t rawY = 200;
6091 int32_t rawPressure = 10;
6092 int32_t rawToolMajor = 12;
6093 int32_t rawDistance = 2;
6094 int32_t rawTiltX = 30;
6095 int32_t rawTiltY = 110;
6096
6097 float x = toDisplayX(rawX);
6098 float y = toDisplayY(rawY);
6099 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6100 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6101 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6102 float distance = float(rawDistance);
6103
6104 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6105 float tiltScale = M_PI / 180;
6106 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6107 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6108 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6109 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6110
6111 processDown(mapper, rawX, rawY);
6112 processPressure(mapper, rawPressure);
6113 processToolMajor(mapper, rawToolMajor);
6114 processDistance(mapper, rawDistance);
6115 processTilt(mapper, rawTiltX, rawTiltY);
6116 processSync(mapper);
6117
6118 NotifyMotionArgs args;
6119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6121 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6122 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6123}
6124
Jason Gerecke489fda82012-09-07 17:19:40 -07006125TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006126 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006127 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07006128 prepareLocationCalibration();
6129 prepareButtons();
6130 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006131 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006132
6133 int32_t rawX = 100;
6134 int32_t rawY = 200;
6135
6136 float x = toDisplayX(toCookedX(rawX, rawY));
6137 float y = toDisplayY(toCookedY(rawX, rawY));
6138
6139 processDown(mapper, rawX, rawY);
6140 processSync(mapper);
6141
6142 NotifyMotionArgs args;
6143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6144 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6145 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6146}
6147
Michael Wrightd02c5b62014-02-10 15:10:22 -08006148TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006149 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006150 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006151 prepareButtons();
6152 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006153 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006154
6155 NotifyMotionArgs motionArgs;
6156 NotifyKeyArgs keyArgs;
6157
6158 processDown(mapper, 100, 200);
6159 processSync(mapper);
6160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6161 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6162 ASSERT_EQ(0, motionArgs.buttonState);
6163
6164 // press BTN_LEFT, release BTN_LEFT
6165 processKey(mapper, BTN_LEFT, 1);
6166 processSync(mapper);
6167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6169 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6170
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6172 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6173 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6174
Michael Wrightd02c5b62014-02-10 15:10:22 -08006175 processKey(mapper, BTN_LEFT, 0);
6176 processSync(mapper);
6177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006178 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006180
6181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006183 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006184
6185 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6186 processKey(mapper, BTN_RIGHT, 1);
6187 processKey(mapper, BTN_MIDDLE, 1);
6188 processSync(mapper);
6189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6191 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6192 motionArgs.buttonState);
6193
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6195 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6196 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6197
6198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6199 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6200 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6201 motionArgs.buttonState);
6202
Michael Wrightd02c5b62014-02-10 15:10:22 -08006203 processKey(mapper, BTN_RIGHT, 0);
6204 processSync(mapper);
6205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006206 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006207 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006208
6209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006211 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006212
6213 processKey(mapper, BTN_MIDDLE, 0);
6214 processSync(mapper);
6215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006216 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006217 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006218
6219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006221 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006222
6223 // press BTN_BACK, release BTN_BACK
6224 processKey(mapper, BTN_BACK, 1);
6225 processSync(mapper);
6226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6227 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6228 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006229
Michael Wrightd02c5b62014-02-10 15:10:22 -08006230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006231 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006232 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6233
6234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6235 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6236 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006237
6238 processKey(mapper, BTN_BACK, 0);
6239 processSync(mapper);
6240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006241 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006242 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006243
6244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006246 ASSERT_EQ(0, motionArgs.buttonState);
6247
Michael Wrightd02c5b62014-02-10 15:10:22 -08006248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6249 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6250 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6251
6252 // press BTN_SIDE, release BTN_SIDE
6253 processKey(mapper, BTN_SIDE, 1);
6254 processSync(mapper);
6255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6256 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6257 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006258
Michael Wrightd02c5b62014-02-10 15:10:22 -08006259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006261 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6262
6263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6264 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6265 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006266
6267 processKey(mapper, BTN_SIDE, 0);
6268 processSync(mapper);
6269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006270 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006271 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006272
6273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006275 ASSERT_EQ(0, motionArgs.buttonState);
6276
Michael Wrightd02c5b62014-02-10 15:10:22 -08006277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6278 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6279 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6280
6281 // press BTN_FORWARD, release BTN_FORWARD
6282 processKey(mapper, BTN_FORWARD, 1);
6283 processSync(mapper);
6284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6285 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6286 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006287
Michael Wrightd02c5b62014-02-10 15:10:22 -08006288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006290 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6291
6292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6293 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6294 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006295
6296 processKey(mapper, BTN_FORWARD, 0);
6297 processSync(mapper);
6298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006299 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006300 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006301
6302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006304 ASSERT_EQ(0, motionArgs.buttonState);
6305
Michael Wrightd02c5b62014-02-10 15:10:22 -08006306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6307 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6308 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6309
6310 // press BTN_EXTRA, release BTN_EXTRA
6311 processKey(mapper, BTN_EXTRA, 1);
6312 processSync(mapper);
6313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6314 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6315 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006316
Michael Wrightd02c5b62014-02-10 15:10:22 -08006317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006319 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6320
6321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6322 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6323 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006324
6325 processKey(mapper, BTN_EXTRA, 0);
6326 processSync(mapper);
6327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006328 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006329 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006330
6331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006333 ASSERT_EQ(0, motionArgs.buttonState);
6334
Michael Wrightd02c5b62014-02-10 15:10:22 -08006335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6336 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6337 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6338
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6340
Michael Wrightd02c5b62014-02-10 15:10:22 -08006341 // press BTN_STYLUS, release BTN_STYLUS
6342 processKey(mapper, BTN_STYLUS, 1);
6343 processSync(mapper);
6344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006346 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6347
6348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6349 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6350 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006351
6352 processKey(mapper, BTN_STYLUS, 0);
6353 processSync(mapper);
6354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006355 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006356 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006357
6358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006360 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006361
6362 // press BTN_STYLUS2, release BTN_STYLUS2
6363 processKey(mapper, BTN_STYLUS2, 1);
6364 processSync(mapper);
6365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006367 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6368
6369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6370 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6371 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006372
6373 processKey(mapper, BTN_STYLUS2, 0);
6374 processSync(mapper);
6375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006376 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006377 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006378
6379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006380 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006381 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006382
6383 // release touch
6384 processUp(mapper);
6385 processSync(mapper);
6386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6387 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6388 ASSERT_EQ(0, motionArgs.buttonState);
6389}
6390
6391TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006392 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006393 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006394 prepareButtons();
6395 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006396 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006397
6398 NotifyMotionArgs motionArgs;
6399
6400 // default tool type is finger
6401 processDown(mapper, 100, 200);
6402 processSync(mapper);
6403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6404 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006405 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006406
6407 // eraser
6408 processKey(mapper, BTN_TOOL_RUBBER, 1);
6409 processSync(mapper);
6410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006412 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006413
6414 // stylus
6415 processKey(mapper, BTN_TOOL_RUBBER, 0);
6416 processKey(mapper, BTN_TOOL_PEN, 1);
6417 processSync(mapper);
6418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006420 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006421
6422 // brush
6423 processKey(mapper, BTN_TOOL_PEN, 0);
6424 processKey(mapper, BTN_TOOL_BRUSH, 1);
6425 processSync(mapper);
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6427 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006428 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006429
6430 // pencil
6431 processKey(mapper, BTN_TOOL_BRUSH, 0);
6432 processKey(mapper, BTN_TOOL_PENCIL, 1);
6433 processSync(mapper);
6434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6435 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006436 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006437
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006438 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006439 processKey(mapper, BTN_TOOL_PENCIL, 0);
6440 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6441 processSync(mapper);
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6443 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006444 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006445
6446 // mouse
6447 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6448 processKey(mapper, BTN_TOOL_MOUSE, 1);
6449 processSync(mapper);
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006452 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006453
6454 // lens
6455 processKey(mapper, BTN_TOOL_MOUSE, 0);
6456 processKey(mapper, BTN_TOOL_LENS, 1);
6457 processSync(mapper);
6458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006460 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006461
6462 // double-tap
6463 processKey(mapper, BTN_TOOL_LENS, 0);
6464 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6465 processSync(mapper);
6466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006468 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006469
6470 // triple-tap
6471 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6472 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6473 processSync(mapper);
6474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6475 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006476 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006477
6478 // quad-tap
6479 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6480 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6481 processSync(mapper);
6482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6483 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006484 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006485
6486 // finger
6487 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6488 processKey(mapper, BTN_TOOL_FINGER, 1);
6489 processSync(mapper);
6490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6491 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006492 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006493
6494 // stylus trumps finger
6495 processKey(mapper, BTN_TOOL_PEN, 1);
6496 processSync(mapper);
6497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6498 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006499 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006500
6501 // eraser trumps stylus
6502 processKey(mapper, BTN_TOOL_RUBBER, 1);
6503 processSync(mapper);
6504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6505 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006506 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006507
6508 // mouse trumps eraser
6509 processKey(mapper, BTN_TOOL_MOUSE, 1);
6510 processSync(mapper);
6511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006513 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006514
6515 // back to default tool type
6516 processKey(mapper, BTN_TOOL_MOUSE, 0);
6517 processKey(mapper, BTN_TOOL_RUBBER, 0);
6518 processKey(mapper, BTN_TOOL_PEN, 0);
6519 processKey(mapper, BTN_TOOL_FINGER, 0);
6520 processSync(mapper);
6521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6522 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006523 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006524}
6525
6526TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006527 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006528 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006529 prepareButtons();
6530 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006531 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006532 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006533
6534 NotifyMotionArgs motionArgs;
6535
6536 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6537 processKey(mapper, BTN_TOOL_FINGER, 1);
6538 processMove(mapper, 100, 200);
6539 processSync(mapper);
6540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6541 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6543 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6544
6545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6546 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6548 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6549
6550 // move a little
6551 processMove(mapper, 150, 250);
6552 processSync(mapper);
6553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6554 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6556 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6557
6558 // down when BTN_TOUCH is pressed, pressure defaults to 1
6559 processKey(mapper, BTN_TOUCH, 1);
6560 processSync(mapper);
6561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6562 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6564 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6565
6566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6567 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6569 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6570
6571 // up when BTN_TOUCH is released, hover restored
6572 processKey(mapper, BTN_TOUCH, 0);
6573 processSync(mapper);
6574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6575 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6577 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6578
6579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6580 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6582 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6583
6584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6585 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6587 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6588
6589 // exit hover when pointer goes away
6590 processKey(mapper, BTN_TOOL_FINGER, 0);
6591 processSync(mapper);
6592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6593 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6595 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6596}
6597
6598TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006599 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006600 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006601 prepareButtons();
6602 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006603 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006604
6605 NotifyMotionArgs motionArgs;
6606
6607 // initially hovering because pressure is 0
6608 processDown(mapper, 100, 200);
6609 processPressure(mapper, 0);
6610 processSync(mapper);
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6612 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6614 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6615
6616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6617 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6619 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6620
6621 // move a little
6622 processMove(mapper, 150, 250);
6623 processSync(mapper);
6624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6625 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6627 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6628
6629 // down when pressure is non-zero
6630 processPressure(mapper, RAW_PRESSURE_MAX);
6631 processSync(mapper);
6632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6633 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6634 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6635 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6636
6637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6638 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6640 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6641
6642 // up when pressure becomes 0, hover restored
6643 processPressure(mapper, 0);
6644 processSync(mapper);
6645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6646 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6648 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6649
6650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6651 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6653 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6654
6655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6656 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6657 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6658 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6659
6660 // exit hover when pointer goes away
6661 processUp(mapper);
6662 processSync(mapper);
6663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6664 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6666 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6667}
6668
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006669TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6670 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006671 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006672 prepareButtons();
6673 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006674 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006675
6676 // Touch down.
6677 processDown(mapper, 100, 200);
6678 processPressure(mapper, 1);
6679 processSync(mapper);
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6681 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6682
6683 // Reset the mapper. This should cancel the ongoing gesture.
6684 resetMapper(mapper, ARBITRARY_TIME);
6685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6686 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6687
6688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6689}
6690
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006691TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6692 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006693 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006694 prepareButtons();
6695 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006696 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006697
6698 // Set the initial state for the touch pointer.
6699 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6700 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6701 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6702 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6703
6704 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006705 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6706 // does not generate any events.
6707 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006708
6709 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6710 // the recreated touch state to generate a down event.
6711 processSync(mapper);
6712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6713 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6714
6715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6716}
6717
lilinnan687e58f2022-07-19 16:00:50 +08006718TEST_F(SingleTouchInputMapperTest,
6719 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6720 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006721 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006722 prepareButtons();
6723 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006724 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006725 NotifyMotionArgs motionArgs;
6726
6727 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006728 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006729 processSync(mapper);
6730
6731 // We should receive a down event
6732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6733 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6734
6735 // Change display id
6736 clearViewports();
6737 prepareSecondaryDisplay(ViewportType::INTERNAL);
6738
6739 // We should receive a cancel event
6740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6741 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6742 // Then receive reset called
6743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6744}
6745
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006746TEST_F(SingleTouchInputMapperTest,
6747 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6748 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006749 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006750 prepareButtons();
6751 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006752 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6754 NotifyMotionArgs motionArgs;
6755
6756 // Start a new gesture.
6757 processDown(mapper, 100, 200);
6758 processSync(mapper);
6759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6760 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6761
6762 // Make the viewport inactive. This will put the device in disabled mode.
6763 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6764 viewport->isActive = false;
6765 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006766 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006767
6768 // We should receive a cancel event for the ongoing gesture.
6769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6770 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6771 // Then we should be notified that the device was reset.
6772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6773
6774 // No events are generated while the viewport is inactive.
6775 processMove(mapper, 101, 201);
6776 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006777 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006778 processSync(mapper);
6779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6780
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006781 // Start a new gesture while the viewport is still inactive.
6782 processDown(mapper, 300, 400);
6783 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6784 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6785 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6786 processSync(mapper);
6787
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006788 // Make the viewport active again. The device should resume processing events.
6789 viewport->isActive = true;
6790 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006791 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006792
6793 // The device is reset because it changes back to direct mode, without generating any events.
6794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6796
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006797 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006798 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6800 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006801
6802 // No more events.
6803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6805}
6806
Prabir Pradhan211ba622022-10-31 21:09:21 +00006807TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6808 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006809 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006810 prepareButtons();
6811 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006812 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6814
6815 // Press a stylus button.
6816 processKey(mapper, BTN_STYLUS, 1);
6817 processSync(mapper);
6818
6819 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6820 processDown(mapper, 100, 200);
6821 processSync(mapper);
6822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6823 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6824 WithCoords(toDisplayX(100), toDisplayY(200)),
6825 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6827 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6828 WithCoords(toDisplayX(100), toDisplayY(200)),
6829 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6830
6831 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6832 // the button has not actually been released, since there will be no pointers through which the
6833 // button state can be reported. The event is generated at the location of the pointer before
6834 // it went up.
6835 processUp(mapper);
6836 processSync(mapper);
6837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6838 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6839 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6841 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6842 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6843}
6844
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006845TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6846 addConfigurationProperty("touch.deviceType", "touchScreen");
6847 prepareDisplay(ui::ROTATION_0);
6848 prepareButtons();
6849 prepareAxes(POSITION);
6850
6851 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6852
Arpit Singha8c236b2023-04-25 13:56:05 +00006853 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6855
6856 // Press a stylus button.
6857 processKey(mapper, BTN_STYLUS, 1);
6858 processSync(mapper);
6859
6860 // Start a touch gesture and ensure that the stylus button is not reported.
6861 processDown(mapper, 100, 200);
6862 processSync(mapper);
6863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6864 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6865
6866 // Release and press the stylus button again.
6867 processKey(mapper, BTN_STYLUS, 0);
6868 processSync(mapper);
6869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6870 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6871 processKey(mapper, BTN_STYLUS, 1);
6872 processSync(mapper);
6873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6874 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6875
6876 // Release the touch gesture.
6877 processUp(mapper);
6878 processSync(mapper);
6879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6880 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6881
6882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6883}
6884
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006885TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6886 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6887 prepareDisplay(ui::ROTATION_0);
6888 prepareButtons();
6889 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006890 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6892
6893 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6894}
6895
Seunghwan Choi356026c2023-02-01 14:37:25 +09006896TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6897 std::shared_ptr<FakePointerController> fakePointerController =
6898 std::make_shared<FakePointerController>();
6899 addConfigurationProperty("touch.deviceType", "touchScreen");
6900 prepareDisplay(ui::ROTATION_0);
6901 prepareButtons();
6902 prepareAxes(POSITION);
6903 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6904 mFakePolicy->setPointerController(fakePointerController);
6905 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00006906 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006907
6908 processKey(mapper, BTN_TOOL_PEN, 1);
6909 processMove(mapper, 100, 200);
6910 processSync(mapper);
6911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6912 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006913 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006914 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6915 ASSERT_TRUE(fakePointerController->isPointerShown());
6916 ASSERT_NO_FATAL_FAILURE(
6917 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6918}
6919
6920TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6921 std::shared_ptr<FakePointerController> fakePointerController =
6922 std::make_shared<FakePointerController>();
6923 addConfigurationProperty("touch.deviceType", "touchScreen");
6924 prepareDisplay(ui::ROTATION_0);
6925 prepareButtons();
6926 prepareAxes(POSITION);
6927 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6928 mFakePolicy->setPointerController(fakePointerController);
6929 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00006930 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006931
6932 processKey(mapper, BTN_TOOL_PEN, 1);
6933 processMove(mapper, 100, 200);
6934 processSync(mapper);
6935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6936 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006937 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006938 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6939 ASSERT_FALSE(fakePointerController->isPointerShown());
6940}
6941
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006942TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6943 // Initialize the device without setting device source to touch navigation.
6944 addConfigurationProperty("touch.deviceType", "touchScreen");
6945 prepareDisplay(ui::ROTATION_0);
6946 prepareButtons();
6947 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006948 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006949
6950 // Ensure that the device is created as a touchscreen, not touch navigation.
6951 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6952
6953 // Add device type association after the device was created.
6954 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6955
6956 // Send update to the mapper.
6957 std::list<NotifyArgs> unused2 =
6958 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006959 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006960
6961 // Check whether device type update was successful.
6962 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6963}
6964
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006965TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6966 // Initialize the device without setting device source to touch navigation.
6967 addConfigurationProperty("touch.deviceType", "touchScreen");
6968 prepareDisplay(ui::ROTATION_0);
6969 prepareButtons();
6970 prepareAxes(POSITION);
6971 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6972
6973 // Set a physical frame in the display viewport.
6974 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6975 viewport->physicalLeft = 0;
6976 viewport->physicalTop = 0;
6977 viewport->physicalRight = DISPLAY_WIDTH / 2;
6978 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6979 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006980 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006981
Arpit Singha8c236b2023-04-25 13:56:05 +00006982 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006983
6984 // Hovering inside the physical frame produces events.
6985 processKey(mapper, BTN_TOOL_PEN, 1);
6986 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6987 processSync(mapper);
6988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6989 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6991 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6992
6993 // Leaving the physical frame ends the hovering gesture.
6994 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6995 processSync(mapper);
6996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6997 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6998
6999 // Moving outside the physical frame does not produce events.
7000 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
7001 processSync(mapper);
7002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7003
7004 // Re-entering the physical frame produces events.
7005 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
7006 processSync(mapper);
7007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7008 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
7009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7010 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
7011}
7012
Prabir Pradhan5632d622021-09-06 07:57:20 -07007013// --- TouchDisplayProjectionTest ---
7014
7015class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7016public:
7017 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7018 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7019 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00007020 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
7021 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
7022 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007023 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007024 auto rotatedWidth = naturalDisplayWidth;
7025 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007026 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00007027 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007028 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007029 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007030 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007031 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007032 inverseRotationFlags = ui::Transform::ROT_180;
7033 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007034 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007035 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007036 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007037 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007038 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007039 inverseRotationFlags = ui::Transform::ROT_0;
7040 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007041 }
7042
Prabir Pradhana9df3162022-12-05 23:57:27 +00007043 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007044 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7045
7046 std::optional<DisplayViewport> internalViewport =
7047 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7048 DisplayViewport& v = *internalViewport;
7049 v.displayId = DISPLAY_ID;
7050 v.orientation = orientation;
7051
7052 v.logicalLeft = 0;
7053 v.logicalTop = 0;
7054 v.logicalRight = 100;
7055 v.logicalBottom = 100;
7056
7057 v.physicalLeft = rotatedPhysicalDisplay.left;
7058 v.physicalTop = rotatedPhysicalDisplay.top;
7059 v.physicalRight = rotatedPhysicalDisplay.right;
7060 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7061
Prabir Pradhana9df3162022-12-05 23:57:27 +00007062 v.deviceWidth = rotatedWidth;
7063 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007064
7065 v.isActive = true;
7066 v.uniqueId = UNIQUE_ID;
7067 v.type = ViewportType::INTERNAL;
7068 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007069 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007070 }
7071
7072 void assertReceivedMove(const Point& point) {
7073 NotifyMotionArgs motionArgs;
7074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7075 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007076 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7078 1, 0, 0, 0, 0, 0, 0, 0));
7079 }
7080};
7081
7082TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7083 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007084 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007085
7086 prepareButtons();
7087 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007088 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007089
7090 NotifyMotionArgs motionArgs;
7091
7092 // Configure the DisplayViewport such that the logical display maps to a subsection of
7093 // the display panel called the physical display. Here, the physical display is bounded by the
7094 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7095 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7096 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7097 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7098
Michael Wrighta9cf4192022-12-01 23:46:39 +00007099 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007100 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7101
7102 // Touches outside the physical display should be ignored, and should not generate any
7103 // events. Ensure touches at the following points that lie outside of the physical display
7104 // area do not generate any events.
7105 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7106 processDown(mapper, toRawX(point.x), toRawY(point.y));
7107 processSync(mapper);
7108 processUp(mapper);
7109 processSync(mapper);
7110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7111 << "Unexpected event generated for touch outside physical display at point: "
7112 << point.x << ", " << point.y;
7113 }
7114 }
7115}
7116
7117TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7118 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007119 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007120
7121 prepareButtons();
7122 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007123 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007124
7125 NotifyMotionArgs motionArgs;
7126
7127 // Configure the DisplayViewport such that the logical display maps to a subsection of
7128 // the display panel called the physical display. Here, the physical display is bounded by the
7129 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7130 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7131
Michael Wrighta9cf4192022-12-01 23:46:39 +00007132 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007133 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7134
7135 // Touches that start outside the physical display should be ignored until it enters the
7136 // physical display bounds, at which point it should generate a down event. Start a touch at
7137 // the point (5, 100), which is outside the physical display bounds.
7138 static const Point kOutsidePoint{5, 100};
7139 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7140 processSync(mapper);
7141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7142
7143 // Move the touch into the physical display area. This should generate a pointer down.
7144 processMove(mapper, toRawX(11), toRawY(21));
7145 processSync(mapper);
7146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7147 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007148 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007149 ASSERT_NO_FATAL_FAILURE(
7150 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7151
7152 // Move the touch inside the physical display area. This should generate a pointer move.
7153 processMove(mapper, toRawX(69), toRawY(159));
7154 processSync(mapper);
7155 assertReceivedMove({69, 159});
7156
7157 // Move outside the physical display area. Since the pointer is already down, this should
7158 // now continue generating events.
7159 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7160 processSync(mapper);
7161 assertReceivedMove(kOutsidePoint);
7162
7163 // Release. This should generate a pointer up.
7164 processUp(mapper);
7165 processSync(mapper);
7166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7167 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7169 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7170
7171 // Ensure no more events were generated.
7172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7174 }
7175}
7176
Prabir Pradhana9df3162022-12-05 23:57:27 +00007177// --- TouchscreenPrecisionTests ---
7178
7179// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
7180// in various orientations and with different display rotations. We configure the touchscreen to
7181// have a higher resolution than that of the display by an integer scale factor in each axis so that
7182// we can enforce that coordinates match precisely as expected.
7183class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
7184 public ::testing::WithParamInterface<ui::Rotation> {
7185public:
7186 void SetUp() override {
7187 SingleTouchInputMapperTest::SetUp();
7188
7189 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7190 // four times the resolution of the display in the Y axis.
7191 prepareButtons();
7192 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007193 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7194 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007195 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007196 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7197 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007198 }
7199
7200 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7201 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7202 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7203 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7204
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007205 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7206 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7207
7208 static const int32_t PRECISION_RAW_X_FLAT = 16;
7209 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7210
7211 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7212 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7213
Prabir Pradhana9df3162022-12-05 23:57:27 +00007214 static const std::array<Point, 4> kRawCorners;
7215};
7216
7217const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7218 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7219 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7220 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7221 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7222}};
7223
7224// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7225// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7226// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7227TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7228 enum class Orientation {
7229 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7230 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7231 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7232 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7233 ftl_last = ORIENTATION_270,
7234 };
7235 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7236 Orientation::ORIENTATION_270;
7237 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7238 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7239 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7240 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7241 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7242 };
7243
7244 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7245
7246 // Configure the touchscreen as being installed in the one of the four different orientations
7247 // relative to the display.
7248 addConfigurationProperty("touch.deviceType", "touchScreen");
7249 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7250 prepareDisplay(ui::ROTATION_0);
7251
Arpit Singha8c236b2023-04-25 13:56:05 +00007252 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00007253
7254 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7255 // orientations of either 90 or 270) this means the display's natural resolution will be
7256 // flipped.
7257 const bool displayRotated =
7258 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7259 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7260 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7261 const Rect physicalFrame{0, 0, width, height};
7262 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7263
7264 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7265 const float expectedPrecisionX = displayRotated ? 4 : 2;
7266 const float expectedPrecisionY = displayRotated ? 2 : 4;
7267
7268 // Test all four corners.
7269 for (int i = 0; i < 4; i++) {
7270 const auto& raw = kRawCorners[i];
7271 processDown(mapper, raw.x, raw.y);
7272 processSync(mapper);
7273 const auto& expected = expectedPoints[i];
7274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7275 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7276 WithCoords(expected.x, expected.y),
7277 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7278 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7279 << "with touchscreen orientation "
7280 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7281 << expected.x << ", " << expected.y << ").";
7282 processUp(mapper);
7283 processSync(mapper);
7284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7285 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7286 WithCoords(expected.x, expected.y))));
7287 }
7288}
7289
Prabir Pradhan82687402022-12-06 01:32:53 +00007290TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7291 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7292 kMappedCorners = {
7293 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7294 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7295 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7296 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7297 };
7298
7299 const ui::Rotation displayRotation = GetParam();
7300
7301 addConfigurationProperty("touch.deviceType", "touchScreen");
7302 prepareDisplay(displayRotation);
7303
Arpit Singha8c236b2023-04-25 13:56:05 +00007304 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00007305
7306 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7307
7308 // Test all four corners.
7309 for (int i = 0; i < 4; i++) {
7310 const auto& expected = expectedPoints[i];
7311 const auto& raw = kRawCorners[i];
7312 processDown(mapper, raw.x, raw.y);
7313 processSync(mapper);
7314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7315 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7316 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7317 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7318 << "with display rotation " << ui::toCString(displayRotation)
7319 << ", expected point (" << expected.x << ", " << expected.y << ").";
7320 processUp(mapper);
7321 processSync(mapper);
7322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7323 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7324 WithCoords(expected.x, expected.y))));
7325 }
7326}
7327
Prabir Pradhan3e798762022-12-02 21:02:11 +00007328TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7329 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7330 kMappedCorners = {
7331 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7332 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7333 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7334 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7335 };
7336
7337 const ui::Rotation displayRotation = GetParam();
7338
7339 addConfigurationProperty("touch.deviceType", "touchScreen");
7340 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7341
Arpit Singha8c236b2023-04-25 13:56:05 +00007342 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00007343
7344 // Ori 270, so width and height swapped
7345 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7346 prepareDisplay(displayRotation);
7347 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7348
7349 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7350
7351 // Test all four corners.
7352 for (int i = 0; i < 4; i++) {
7353 const auto& expected = expectedPoints[i];
7354 const auto& raw = kRawCorners[i];
7355 processDown(mapper, raw.x, raw.y);
7356 processSync(mapper);
7357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7358 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7359 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7360 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7361 << "with display rotation " << ui::toCString(displayRotation)
7362 << ", expected point (" << expected.x << ", " << expected.y << ").";
7363 processUp(mapper);
7364 processSync(mapper);
7365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7366 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7367 WithCoords(expected.x, expected.y))));
7368 }
7369}
7370
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007371TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7372 const ui::Rotation displayRotation = GetParam();
7373
7374 addConfigurationProperty("touch.deviceType", "touchScreen");
7375 prepareDisplay(displayRotation);
7376
7377 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00007378 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007379
7380 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7381 // MotionRanges use display pixels as their units
7382 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7383 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7384
7385 // The MotionRanges should be oriented in the rotated display's coordinate space
7386 const bool displayRotated =
7387 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7388
7389 constexpr float MAX_X = 479.5;
7390 constexpr float MAX_Y = 799.75;
7391 EXPECT_EQ(xRange->min, 0.f);
7392 EXPECT_EQ(yRange->min, 0.f);
7393 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7394 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7395
7396 EXPECT_EQ(xRange->flat, 8.f);
7397 EXPECT_EQ(yRange->flat, 8.f);
7398
7399 EXPECT_EQ(xRange->fuzz, 2.f);
7400 EXPECT_EQ(yRange->fuzz, 2.f);
7401
7402 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7403 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7404}
7405
Prabir Pradhana9df3162022-12-05 23:57:27 +00007406// Run the precision tests for all rotations.
7407INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7408 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7409 ui::ROTATION_270),
7410 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7411 return ftl::enum_string(testParamInfo.param);
7412 });
7413
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007414// --- ExternalStylusFusionTest ---
7415
7416class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7417public:
7418 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7419 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007420 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007421 prepareButtons();
7422 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007423 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007424
7425 mStylusState.when = ARBITRARY_TIME;
7426 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007427 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007428 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007429 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007430 processExternalStylusState(mapper);
7431 return mapper;
7432 }
7433
7434 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7435 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7436 for (const NotifyArgs& args : generatedArgs) {
7437 mFakeListener->notify(args);
7438 }
7439 // Loop the reader to flush the input listener queue.
7440 mReader->loopOnce();
7441 return generatedArgs;
7442 }
7443
7444protected:
7445 StylusState mStylusState{};
7446 static constexpr uint32_t EXPECTED_SOURCE =
7447 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7448
7449 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7450 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007451 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007452
7453 // The first pointer is withheld.
7454 processDown(mapper, 100, 200);
7455 processSync(mapper);
7456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7457 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7458 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7459
7460 // The external stylus reports pressure. The withheld finger pointer is released as a
7461 // stylus.
7462 mStylusState.pressure = 1.f;
7463 processExternalStylusState(mapper);
7464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7465 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7466 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7467
7468 // Subsequent pointer events are not withheld.
7469 processMove(mapper, 101, 201);
7470 processSync(mapper);
7471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7472 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7473
7474 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7476 }
7477
7478 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7479 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7480
7481 // Releasing the touch pointer ends the gesture.
7482 processUp(mapper);
7483 processSync(mapper);
7484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7485 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007486 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007487
7488 mStylusState.pressure = 0.f;
7489 processExternalStylusState(mapper);
7490 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7492 }
7493
7494 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7495 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007496 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007497
7498 // The first pointer is withheld when an external stylus is connected,
7499 // and a timeout is requested.
7500 processDown(mapper, 100, 200);
7501 processSync(mapper);
7502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7503 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7504 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7505
7506 // If the timeout expires early, it is requested again.
7507 handleTimeout(mapper, ARBITRARY_TIME + 1);
7508 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7509 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7510
7511 // When the timeout expires, the withheld touch is released as a finger pointer.
7512 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7514 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7515
7516 // Subsequent pointer events are not withheld.
7517 processMove(mapper, 101, 201);
7518 processSync(mapper);
7519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7520 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7521 processUp(mapper);
7522 processSync(mapper);
7523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7524 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7525
7526 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7528 }
7529
7530private:
7531 InputDeviceInfo mExternalStylusDeviceInfo{};
7532};
7533
7534TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7535 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7536 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7537}
7538
7539TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7540 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7541 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7542}
7543
7544TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7545 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7546 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7547}
7548
7549// Test a successful stylus fusion gesture where the pressure is reported by the external
7550// before the touch is reported by the touchscreen.
7551TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7552 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7553 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007554 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007555
7556 // The external stylus reports pressure first. It is ignored for now.
7557 mStylusState.pressure = 1.f;
7558 processExternalStylusState(mapper);
7559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7560 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7561
7562 // When the touch goes down afterwards, it is reported as a stylus pointer.
7563 processDown(mapper, 100, 200);
7564 processSync(mapper);
7565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7566 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7567 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7568
7569 processMove(mapper, 101, 201);
7570 processSync(mapper);
7571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7572 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7573 processUp(mapper);
7574 processSync(mapper);
7575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7576 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7577
7578 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7580}
7581
7582TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7583 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7584
7585 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7586 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7587
7588 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7589 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7590 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7591 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7592}
7593
7594TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7595 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7596 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007597 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007598
7599 mStylusState.pressure = 0.8f;
7600 processExternalStylusState(mapper);
7601 processDown(mapper, 100, 200);
7602 processSync(mapper);
7603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7604 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7605 WithPressure(0.8f))));
7606 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7607
7608 // The external stylus reports a pressure change. We wait for some time for a touch event.
7609 mStylusState.pressure = 0.6f;
7610 processExternalStylusState(mapper);
7611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7612 ASSERT_NO_FATAL_FAILURE(
7613 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7614
7615 // If a touch is reported within the timeout, it reports the updated pressure.
7616 processMove(mapper, 101, 201);
7617 processSync(mapper);
7618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7619 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7620 WithPressure(0.6f))));
7621 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7622
7623 // There is another pressure change.
7624 mStylusState.pressure = 0.5f;
7625 processExternalStylusState(mapper);
7626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7627 ASSERT_NO_FATAL_FAILURE(
7628 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7629
7630 // If a touch is not reported within the timeout, a move event is generated to report
7631 // the new pressure.
7632 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7634 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7635 WithPressure(0.5f))));
7636
7637 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7638 // repeated indefinitely.
7639 mStylusState.pressure = 0.0f;
7640 processExternalStylusState(mapper);
7641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7642 ASSERT_NO_FATAL_FAILURE(
7643 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7644 processMove(mapper, 102, 202);
7645 processSync(mapper);
7646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7647 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7648 WithPressure(0.5f))));
7649 processMove(mapper, 103, 203);
7650 processSync(mapper);
7651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7652 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7653 WithPressure(0.5f))));
7654
7655 processUp(mapper);
7656 processSync(mapper);
7657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7658 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007659 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007660
7661 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7663}
7664
7665TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7666 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7667 auto source = WithSource(EXPECTED_SOURCE);
7668
7669 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007670 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007671 processExternalStylusState(mapper);
7672 processDown(mapper, 100, 200);
7673 processSync(mapper);
7674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7675 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007676 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007677 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7678
7679 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007680 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007681 processExternalStylusState(mapper);
7682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7683 ASSERT_NO_FATAL_FAILURE(
7684 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7685
7686 // If a touch is reported within the timeout, it reports the updated pressure.
7687 processMove(mapper, 101, 201);
7688 processSync(mapper);
7689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7690 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007691 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007692 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7693
7694 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007695 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007696 processExternalStylusState(mapper);
7697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7698 ASSERT_NO_FATAL_FAILURE(
7699 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7700
7701 // If a touch is not reported within the timeout, a move event is generated to report
7702 // the new tool type.
7703 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7705 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007706 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007707
7708 processUp(mapper);
7709 processSync(mapper);
7710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7711 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007712 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007713
7714 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7716}
7717
7718TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7719 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7720 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007721 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007722
7723 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7724
7725 // The external stylus reports a button change. We wait for some time for a touch event.
7726 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7727 processExternalStylusState(mapper);
7728 ASSERT_NO_FATAL_FAILURE(
7729 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7730
7731 // If a touch is reported within the timeout, it reports the updated button state.
7732 processMove(mapper, 101, 201);
7733 processSync(mapper);
7734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7735 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7736 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7738 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7739 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7740 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7741
7742 // The button is now released.
7743 mStylusState.buttons = 0;
7744 processExternalStylusState(mapper);
7745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7746 ASSERT_NO_FATAL_FAILURE(
7747 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7748
7749 // If a touch is not reported within the timeout, a move event is generated to report
7750 // the new button state.
7751 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7753 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7754 WithButtonState(0))));
7755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007756 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7757 WithButtonState(0))));
7758
7759 processUp(mapper);
7760 processSync(mapper);
7761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007762 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7763
7764 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7766}
7767
Michael Wrightd02c5b62014-02-10 15:10:22 -08007768// --- MultiTouchInputMapperTest ---
7769
7770class MultiTouchInputMapperTest : public TouchInputMapperTest {
7771protected:
7772 void prepareAxes(int axes);
7773
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007774 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7775 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7776 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7777 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7778 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7779 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7780 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7781 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7782 void processId(MultiTouchInputMapper& mapper, int32_t id);
7783 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7784 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7785 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007786 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007787 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007788 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7789 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007790};
7791
7792void MultiTouchInputMapperTest::prepareAxes(int axes) {
7793 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007794 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7795 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007796 }
7797 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007798 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7799 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007800 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007801 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7802 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007803 }
7804 }
7805 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007806 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7807 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007808 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007809 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007810 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007811 }
7812 }
7813 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007814 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7815 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007816 }
7817 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007818 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7819 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007820 }
7821 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007822 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7823 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007824 }
7825 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007826 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7827 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007828 }
7829 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007830 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7831 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007832 }
7833 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007834 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007835 }
7836}
7837
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007838void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7839 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7841 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007842}
7843
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007844void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7845 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007846 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007847}
7848
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007849void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7850 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007851 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007852}
7853
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007854void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007856}
7857
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007858void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007860}
7861
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007862void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7863 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007864 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007865}
7866
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007867void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007869}
7870
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007871void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007872 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007873}
7874
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007875void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007876 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007877}
7878
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007879void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007881}
7882
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007883void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007884 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007885}
7886
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007887void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7888 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007890}
7891
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007892void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7893 int32_t value) {
7894 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7896}
7897
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007898void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007900}
7901
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007902void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7903 nsecs_t readTime) {
7904 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007905}
7906
Michael Wrightd02c5b62014-02-10 15:10:22 -08007907TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007908 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007909 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007910 prepareAxes(POSITION);
7911 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007912 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007913
arthurhungdcef2dc2020-08-11 14:47:50 +08007914 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007915
7916 NotifyMotionArgs motionArgs;
7917
7918 // Two fingers down at once.
7919 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7920 processPosition(mapper, x1, y1);
7921 processMTSync(mapper);
7922 processPosition(mapper, x2, y2);
7923 processMTSync(mapper);
7924 processSync(mapper);
7925
7926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7927 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7928 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7929 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7930 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7931 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7932 ASSERT_EQ(0, motionArgs.flags);
7933 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7934 ASSERT_EQ(0, motionArgs.buttonState);
7935 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007936 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007937 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007938 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007939 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7940 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7941 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7942 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7943 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7944
7945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7946 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7947 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7948 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7949 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007950 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007951 ASSERT_EQ(0, motionArgs.flags);
7952 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7953 ASSERT_EQ(0, motionArgs.buttonState);
7954 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007955 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007956 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007957 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007958 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007959 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007960 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7961 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7963 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7964 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7965 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7966 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7967
7968 // Move.
7969 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7970 processPosition(mapper, x1, y1);
7971 processMTSync(mapper);
7972 processPosition(mapper, x2, y2);
7973 processMTSync(mapper);
7974 processSync(mapper);
7975
7976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7977 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7978 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7979 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7980 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7982 ASSERT_EQ(0, motionArgs.flags);
7983 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7984 ASSERT_EQ(0, motionArgs.buttonState);
7985 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007986 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007987 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007988 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007989 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007990 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7992 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7994 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7995 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7996 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7997 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7998
7999 // First finger up.
8000 x2 += 15; y2 -= 20;
8001 processPosition(mapper, x2, y2);
8002 processMTSync(mapper);
8003 processSync(mapper);
8004
8005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8006 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8007 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8008 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8009 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008010 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008011 ASSERT_EQ(0, motionArgs.flags);
8012 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8013 ASSERT_EQ(0, motionArgs.buttonState);
8014 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008015 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008016 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008017 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008018 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008019 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008020 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8021 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8023 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8024 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8025 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8026 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8027
8028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8029 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8030 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8031 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8032 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8033 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8034 ASSERT_EQ(0, motionArgs.flags);
8035 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8036 ASSERT_EQ(0, motionArgs.buttonState);
8037 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008038 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008039 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008040 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8042 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8043 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8044 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8045 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8046
8047 // Move.
8048 x2 += 20; y2 -= 25;
8049 processPosition(mapper, x2, y2);
8050 processMTSync(mapper);
8051 processSync(mapper);
8052
8053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8054 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8055 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8056 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8057 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8058 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8059 ASSERT_EQ(0, motionArgs.flags);
8060 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8061 ASSERT_EQ(0, motionArgs.buttonState);
8062 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008063 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008064 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008065 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008066 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8067 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8068 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8069 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8070 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8071
8072 // New finger down.
8073 int32_t x3 = 700, y3 = 300;
8074 processPosition(mapper, x2, y2);
8075 processMTSync(mapper);
8076 processPosition(mapper, x3, y3);
8077 processMTSync(mapper);
8078 processSync(mapper);
8079
8080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8081 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8082 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8083 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8084 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008085 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008086 ASSERT_EQ(0, motionArgs.flags);
8087 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8088 ASSERT_EQ(0, motionArgs.buttonState);
8089 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008090 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008091 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008092 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008093 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008094 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008095 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8096 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
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 // Second finger up.
8104 x3 += 30; y3 -= 20;
8105 processPosition(mapper, x3, y3);
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);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008114 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008115 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(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008120 ASSERT_EQ(0, 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_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008123 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008124 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8125 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8127 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8128 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8129 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8130 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8131
8132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8133 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8134 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8135 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8136 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8138 ASSERT_EQ(0, motionArgs.flags);
8139 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8140 ASSERT_EQ(0, motionArgs.buttonState);
8141 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008142 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008143 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008144 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8146 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8147 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8148 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8149 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8150
8151 // Last finger up.
8152 processMTSync(mapper);
8153 processSync(mapper);
8154
8155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8156 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8157 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8158 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8159 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8160 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8161 ASSERT_EQ(0, motionArgs.flags);
8162 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8163 ASSERT_EQ(0, motionArgs.buttonState);
8164 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008165 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008166 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008167 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8169 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8170 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8171 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8172 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8173
8174 // Should not have sent any more keys or motions.
8175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8177}
8178
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008179TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8180 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008181 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008182
8183 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8184 /*fuzz*/ 0, /*resolution*/ 10);
8185 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8186 /*fuzz*/ 0, /*resolution*/ 11);
8187 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8188 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8189 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8190 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8191 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8192 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8193 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8194 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8195
Arpit Singha8c236b2023-04-25 13:56:05 +00008196 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008197
8198 // X and Y axes
8199 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8200 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8201 // Touch major and minor
8202 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8203 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8204 // Tool major and minor
8205 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8206 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8207}
8208
8209TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8210 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008211 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008212
8213 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8214 /*fuzz*/ 0, /*resolution*/ 10);
8215 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8216 /*fuzz*/ 0, /*resolution*/ 11);
8217
8218 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8219
Arpit Singha8c236b2023-04-25 13:56:05 +00008220 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008221
8222 // Touch major and minor
8223 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8224 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8225 // Tool major and minor
8226 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8227 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8228}
8229
Michael Wrightd02c5b62014-02-10 15:10:22 -08008230TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008231 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008232 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008233 prepareAxes(POSITION | ID);
8234 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008235 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008236
arthurhungdcef2dc2020-08-11 14:47:50 +08008237 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008238
8239 NotifyMotionArgs motionArgs;
8240
8241 // Two fingers down at once.
8242 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8243 processPosition(mapper, x1, y1);
8244 processId(mapper, 1);
8245 processMTSync(mapper);
8246 processPosition(mapper, x2, y2);
8247 processId(mapper, 2);
8248 processMTSync(mapper);
8249 processSync(mapper);
8250
8251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8252 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008253 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008254 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008255 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008256 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8257 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8258
8259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008260 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008261 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008262 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008263 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008264 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008265 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008266 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8267 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8269 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8270
8271 // Move.
8272 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8273 processPosition(mapper, x1, y1);
8274 processId(mapper, 1);
8275 processMTSync(mapper);
8276 processPosition(mapper, x2, y2);
8277 processId(mapper, 2);
8278 processMTSync(mapper);
8279 processSync(mapper);
8280
8281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008283 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008284 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008285 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008286 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008287 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8289 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8291 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8292
8293 // First finger up.
8294 x2 += 15; y2 -= 20;
8295 processPosition(mapper, x2, y2);
8296 processId(mapper, 2);
8297 processMTSync(mapper);
8298 processSync(mapper);
8299
8300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008301 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008302 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008303 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008304 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008305 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008306 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8308 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8310 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8311
8312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008314 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008315 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008316 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8318 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8319
8320 // Move.
8321 x2 += 20; y2 -= 25;
8322 processPosition(mapper, x2, y2);
8323 processId(mapper, 2);
8324 processMTSync(mapper);
8325 processSync(mapper);
8326
8327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008329 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008330 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008331 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8333 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8334
8335 // New finger down.
8336 int32_t x3 = 700, y3 = 300;
8337 processPosition(mapper, x2, y2);
8338 processId(mapper, 2);
8339 processMTSync(mapper);
8340 processPosition(mapper, x3, y3);
8341 processId(mapper, 3);
8342 processMTSync(mapper);
8343 processSync(mapper);
8344
8345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008346 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008347 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008348 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008349 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008350 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008351 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008352 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8353 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8355 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8356
8357 // Second finger up.
8358 x3 += 30; y3 -= 20;
8359 processPosition(mapper, x3, y3);
8360 processId(mapper, 3);
8361 processMTSync(mapper);
8362 processSync(mapper);
8363
8364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008365 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008366 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008367 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008368 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008369 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008370 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8372 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8374 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8375
8376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008378 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008379 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008380 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008381 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8382 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8383
8384 // Last finger up.
8385 processMTSync(mapper);
8386 processSync(mapper);
8387
8388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8389 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008390 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008391 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008392 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8394 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8395
8396 // Should not have sent any more keys or motions.
8397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8399}
8400
8401TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008402 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008403 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008404 prepareAxes(POSITION | ID | SLOT);
8405 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008406 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008407
arthurhungdcef2dc2020-08-11 14:47:50 +08008408 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008409
8410 NotifyMotionArgs motionArgs;
8411
8412 // Two fingers down at once.
8413 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8414 processPosition(mapper, x1, y1);
8415 processId(mapper, 1);
8416 processSlot(mapper, 1);
8417 processPosition(mapper, x2, y2);
8418 processId(mapper, 2);
8419 processSync(mapper);
8420
8421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8422 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008423 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008424 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008425 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008426 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8427 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8428
8429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008430 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008431 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008432 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008433 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008434 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008435 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8437 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8439 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8440
8441 // Move.
8442 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8443 processSlot(mapper, 0);
8444 processPosition(mapper, x1, y1);
8445 processSlot(mapper, 1);
8446 processPosition(mapper, x2, y2);
8447 processSync(mapper);
8448
8449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8450 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008451 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008452 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008453 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008454 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008455 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8457 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8459 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8460
8461 // First finger up.
8462 x2 += 15; y2 -= 20;
8463 processSlot(mapper, 0);
8464 processId(mapper, -1);
8465 processSlot(mapper, 1);
8466 processPosition(mapper, x2, y2);
8467 processSync(mapper);
8468
8469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008470 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008471 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008472 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008473 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008474 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008475 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8477 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8479 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8480
8481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8482 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008483 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008484 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008485 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8487 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8488
8489 // Move.
8490 x2 += 20; y2 -= 25;
8491 processPosition(mapper, x2, y2);
8492 processSync(mapper);
8493
8494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8495 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008496 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008497 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008498 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8500 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8501
8502 // New finger down.
8503 int32_t x3 = 700, y3 = 300;
8504 processPosition(mapper, x2, y2);
8505 processSlot(mapper, 0);
8506 processId(mapper, 3);
8507 processPosition(mapper, x3, y3);
8508 processSync(mapper);
8509
8510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008511 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008512 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008513 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008514 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008515 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008516 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8518 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8520 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8521
8522 // Second finger up.
8523 x3 += 30; y3 -= 20;
8524 processSlot(mapper, 1);
8525 processId(mapper, -1);
8526 processSlot(mapper, 0);
8527 processPosition(mapper, x3, y3);
8528 processSync(mapper);
8529
8530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008531 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008532 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008533 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008534 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008535 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008536 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8538 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8540 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8541
8542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8543 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008544 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008545 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008546 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8548 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8549
8550 // Last finger up.
8551 processId(mapper, -1);
8552 processSync(mapper);
8553
8554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8555 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008556 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008557 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008558 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8560 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8561
8562 // Should not have sent any more keys or motions.
8563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8565}
8566
8567TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008568 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008569 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008570 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008571 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008572
8573 // These calculations are based on the input device calibration documentation.
8574 int32_t rawX = 100;
8575 int32_t rawY = 200;
8576 int32_t rawTouchMajor = 7;
8577 int32_t rawTouchMinor = 6;
8578 int32_t rawToolMajor = 9;
8579 int32_t rawToolMinor = 8;
8580 int32_t rawPressure = 11;
8581 int32_t rawDistance = 0;
8582 int32_t rawOrientation = 3;
8583 int32_t id = 5;
8584
8585 float x = toDisplayX(rawX);
8586 float y = toDisplayY(rawY);
8587 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8588 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8589 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8590 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8591 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8592 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8593 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8594 float distance = float(rawDistance);
8595
8596 processPosition(mapper, rawX, rawY);
8597 processTouchMajor(mapper, rawTouchMajor);
8598 processTouchMinor(mapper, rawTouchMinor);
8599 processToolMajor(mapper, rawToolMajor);
8600 processToolMinor(mapper, rawToolMinor);
8601 processPressure(mapper, rawPressure);
8602 processOrientation(mapper, rawOrientation);
8603 processDistance(mapper, rawDistance);
8604 processId(mapper, id);
8605 processMTSync(mapper);
8606 processSync(mapper);
8607
8608 NotifyMotionArgs args;
8609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8610 ASSERT_EQ(0, args.pointerProperties[0].id);
8611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8612 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8613 orientation, distance));
8614}
8615
8616TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008617 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008618 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008619 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8620 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00008621 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008622
8623 // These calculations are based on the input device calibration documentation.
8624 int32_t rawX = 100;
8625 int32_t rawY = 200;
8626 int32_t rawTouchMajor = 140;
8627 int32_t rawTouchMinor = 120;
8628 int32_t rawToolMajor = 180;
8629 int32_t rawToolMinor = 160;
8630
8631 float x = toDisplayX(rawX);
8632 float y = toDisplayY(rawY);
8633 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8634 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8635 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8636 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8637 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8638
8639 processPosition(mapper, rawX, rawY);
8640 processTouchMajor(mapper, rawTouchMajor);
8641 processTouchMinor(mapper, rawTouchMinor);
8642 processToolMajor(mapper, rawToolMajor);
8643 processToolMinor(mapper, rawToolMinor);
8644 processMTSync(mapper);
8645 processSync(mapper);
8646
8647 NotifyMotionArgs args;
8648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8650 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8651}
8652
8653TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008654 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008655 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008656 prepareAxes(POSITION | TOUCH | TOOL);
8657 addConfigurationProperty("touch.size.calibration", "diameter");
8658 addConfigurationProperty("touch.size.scale", "10");
8659 addConfigurationProperty("touch.size.bias", "160");
8660 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00008661 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008662
8663 // These calculations are based on the input device calibration documentation.
8664 // Note: We only provide a single common touch/tool value because the device is assumed
8665 // not to emit separate values for each pointer (isSummed = 1).
8666 int32_t rawX = 100;
8667 int32_t rawY = 200;
8668 int32_t rawX2 = 150;
8669 int32_t rawY2 = 250;
8670 int32_t rawTouchMajor = 5;
8671 int32_t rawToolMajor = 8;
8672
8673 float x = toDisplayX(rawX);
8674 float y = toDisplayY(rawY);
8675 float x2 = toDisplayX(rawX2);
8676 float y2 = toDisplayY(rawY2);
8677 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8678 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8679 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8680
8681 processPosition(mapper, rawX, rawY);
8682 processTouchMajor(mapper, rawTouchMajor);
8683 processToolMajor(mapper, rawToolMajor);
8684 processMTSync(mapper);
8685 processPosition(mapper, rawX2, rawY2);
8686 processTouchMajor(mapper, rawTouchMajor);
8687 processToolMajor(mapper, rawToolMajor);
8688 processMTSync(mapper);
8689 processSync(mapper);
8690
8691 NotifyMotionArgs args;
8692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8693 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8694
8695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008696 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008697 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8699 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8701 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8702}
8703
8704TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008705 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008706 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008707 prepareAxes(POSITION | TOUCH | TOOL);
8708 addConfigurationProperty("touch.size.calibration", "area");
8709 addConfigurationProperty("touch.size.scale", "43");
8710 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00008711 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008712
8713 // These calculations are based on the input device calibration documentation.
8714 int32_t rawX = 100;
8715 int32_t rawY = 200;
8716 int32_t rawTouchMajor = 5;
8717 int32_t rawToolMajor = 8;
8718
8719 float x = toDisplayX(rawX);
8720 float y = toDisplayY(rawY);
8721 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8722 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8723 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8724
8725 processPosition(mapper, rawX, rawY);
8726 processTouchMajor(mapper, rawTouchMajor);
8727 processToolMajor(mapper, rawToolMajor);
8728 processMTSync(mapper);
8729 processSync(mapper);
8730
8731 NotifyMotionArgs args;
8732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8734 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8735}
8736
8737TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008738 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008739 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008740 prepareAxes(POSITION | PRESSURE);
8741 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8742 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00008743 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008744
Michael Wrightaa449c92017-12-13 21:21:43 +00008745 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008746 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008747 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8748 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8749 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8750
Michael Wrightd02c5b62014-02-10 15:10:22 -08008751 // These calculations are based on the input device calibration documentation.
8752 int32_t rawX = 100;
8753 int32_t rawY = 200;
8754 int32_t rawPressure = 60;
8755
8756 float x = toDisplayX(rawX);
8757 float y = toDisplayY(rawY);
8758 float pressure = float(rawPressure) * 0.01f;
8759
8760 processPosition(mapper, rawX, rawY);
8761 processPressure(mapper, rawPressure);
8762 processMTSync(mapper);
8763 processSync(mapper);
8764
8765 NotifyMotionArgs args;
8766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8768 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8769}
8770
8771TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008772 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008773 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008774 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008775 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008776
8777 NotifyMotionArgs motionArgs;
8778 NotifyKeyArgs keyArgs;
8779
8780 processId(mapper, 1);
8781 processPosition(mapper, 100, 200);
8782 processSync(mapper);
8783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8784 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8785 ASSERT_EQ(0, motionArgs.buttonState);
8786
8787 // press BTN_LEFT, release BTN_LEFT
8788 processKey(mapper, BTN_LEFT, 1);
8789 processSync(mapper);
8790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8792 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8793
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8795 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8796 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8797
Michael Wrightd02c5b62014-02-10 15:10:22 -08008798 processKey(mapper, BTN_LEFT, 0);
8799 processSync(mapper);
8800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008801 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008802 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008803
8804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008805 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008806 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008807
8808 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8809 processKey(mapper, BTN_RIGHT, 1);
8810 processKey(mapper, BTN_MIDDLE, 1);
8811 processSync(mapper);
8812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8813 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8814 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8815 motionArgs.buttonState);
8816
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8818 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8819 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8820
8821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8822 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8823 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8824 motionArgs.buttonState);
8825
Michael Wrightd02c5b62014-02-10 15:10:22 -08008826 processKey(mapper, BTN_RIGHT, 0);
8827 processSync(mapper);
8828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008829 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008830 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008831
8832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008834 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008835
8836 processKey(mapper, BTN_MIDDLE, 0);
8837 processSync(mapper);
8838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008839 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008840 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008841
8842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008843 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008844 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008845
8846 // press BTN_BACK, release BTN_BACK
8847 processKey(mapper, BTN_BACK, 1);
8848 processSync(mapper);
8849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8850 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8851 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008852
Michael Wrightd02c5b62014-02-10 15:10:22 -08008853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008854 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008855 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8856
8857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8858 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8859 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008860
8861 processKey(mapper, BTN_BACK, 0);
8862 processSync(mapper);
8863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008864 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008865 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008866
8867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008868 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008869 ASSERT_EQ(0, motionArgs.buttonState);
8870
Michael Wrightd02c5b62014-02-10 15:10:22 -08008871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8872 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8873 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8874
8875 // press BTN_SIDE, release BTN_SIDE
8876 processKey(mapper, BTN_SIDE, 1);
8877 processSync(mapper);
8878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8879 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8880 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008881
Michael Wrightd02c5b62014-02-10 15:10:22 -08008882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008883 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008884 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8885
8886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8887 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8888 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008889
8890 processKey(mapper, BTN_SIDE, 0);
8891 processSync(mapper);
8892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008893 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008894 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008895
8896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008897 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008898 ASSERT_EQ(0, motionArgs.buttonState);
8899
Michael Wrightd02c5b62014-02-10 15:10:22 -08008900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8901 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8902 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8903
8904 // press BTN_FORWARD, release BTN_FORWARD
8905 processKey(mapper, BTN_FORWARD, 1);
8906 processSync(mapper);
8907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8908 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8909 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008910
Michael Wrightd02c5b62014-02-10 15:10:22 -08008911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008912 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008913 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8914
8915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8916 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8917 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008918
8919 processKey(mapper, BTN_FORWARD, 0);
8920 processSync(mapper);
8921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008922 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008923 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008924
8925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008926 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008927 ASSERT_EQ(0, motionArgs.buttonState);
8928
Michael Wrightd02c5b62014-02-10 15:10:22 -08008929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8930 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8931 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8932
8933 // press BTN_EXTRA, release BTN_EXTRA
8934 processKey(mapper, BTN_EXTRA, 1);
8935 processSync(mapper);
8936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8937 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8938 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008939
Michael Wrightd02c5b62014-02-10 15:10:22 -08008940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008941 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008942 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8943
8944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8945 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8946 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008947
8948 processKey(mapper, BTN_EXTRA, 0);
8949 processSync(mapper);
8950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008951 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008952 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008953
8954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008955 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008956 ASSERT_EQ(0, motionArgs.buttonState);
8957
Michael Wrightd02c5b62014-02-10 15:10:22 -08008958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8959 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8960 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8961
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8963
Michael Wrightd02c5b62014-02-10 15:10:22 -08008964 // press BTN_STYLUS, release BTN_STYLUS
8965 processKey(mapper, BTN_STYLUS, 1);
8966 processSync(mapper);
8967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8968 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008969 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, 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_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008974
8975 processKey(mapper, BTN_STYLUS, 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);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008984
8985 // press BTN_STYLUS2, release BTN_STYLUS2
8986 processKey(mapper, BTN_STYLUS2, 1);
8987 processSync(mapper);
8988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8989 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008990 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8991
8992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8993 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8994 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008995
8996 processKey(mapper, BTN_STYLUS2, 0);
8997 processSync(mapper);
8998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008999 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009000 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009001
9002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009004 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009005
9006 // release touch
9007 processId(mapper, -1);
9008 processSync(mapper);
9009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9010 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9011 ASSERT_EQ(0, motionArgs.buttonState);
9012}
9013
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009014TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
9015 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009016 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009017 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00009018 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009019
9020 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
9021 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
9022
9023 // Touch down.
9024 processId(mapper, 1);
9025 processPosition(mapper, 100, 200);
9026 processSync(mapper);
9027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9028 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
9029
9030 // Press and release button mapped to the primary stylus button.
9031 processKey(mapper, BTN_A, 1);
9032 processSync(mapper);
9033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9034 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9035 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9037 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9038 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9039
9040 processKey(mapper, BTN_A, 0);
9041 processSync(mapper);
9042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9043 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9045 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9046
9047 // Press and release the HID usage mapped to the secondary stylus button.
9048 processHidUsage(mapper, 0xabcd, 1);
9049 processSync(mapper);
9050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9051 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9052 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9054 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9055 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9056
9057 processHidUsage(mapper, 0xabcd, 0);
9058 processSync(mapper);
9059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9060 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9062 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9063
9064 // Release touch.
9065 processId(mapper, -1);
9066 processSync(mapper);
9067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9068 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
9069}
9070
Michael Wrightd02c5b62014-02-10 15:10:22 -08009071TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009072 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009073 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009074 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009075 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009076
9077 NotifyMotionArgs motionArgs;
9078
9079 // default tool type is finger
9080 processId(mapper, 1);
9081 processPosition(mapper, 100, 200);
9082 processSync(mapper);
9083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9084 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009085 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009086
9087 // eraser
9088 processKey(mapper, BTN_TOOL_RUBBER, 1);
9089 processSync(mapper);
9090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9091 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009092 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009093
9094 // stylus
9095 processKey(mapper, BTN_TOOL_RUBBER, 0);
9096 processKey(mapper, BTN_TOOL_PEN, 1);
9097 processSync(mapper);
9098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9099 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009100 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009101
9102 // brush
9103 processKey(mapper, BTN_TOOL_PEN, 0);
9104 processKey(mapper, BTN_TOOL_BRUSH, 1);
9105 processSync(mapper);
9106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009108 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009109
9110 // pencil
9111 processKey(mapper, BTN_TOOL_BRUSH, 0);
9112 processKey(mapper, BTN_TOOL_PENCIL, 1);
9113 processSync(mapper);
9114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009116 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009117
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009118 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009119 processKey(mapper, BTN_TOOL_PENCIL, 0);
9120 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
9121 processSync(mapper);
9122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009124 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009125
9126 // mouse
9127 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9128 processKey(mapper, BTN_TOOL_MOUSE, 1);
9129 processSync(mapper);
9130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009132 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009133
9134 // lens
9135 processKey(mapper, BTN_TOOL_MOUSE, 0);
9136 processKey(mapper, BTN_TOOL_LENS, 1);
9137 processSync(mapper);
9138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009140 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009141
9142 // double-tap
9143 processKey(mapper, BTN_TOOL_LENS, 0);
9144 processKey(mapper, BTN_TOOL_DOUBLETAP, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009149
9150 // triple-tap
9151 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9152 processKey(mapper, BTN_TOOL_TRIPLETAP, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009157
9158 // quad-tap
9159 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9160 processKey(mapper, BTN_TOOL_QUADTAP, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009165
9166 // finger
9167 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9168 processKey(mapper, BTN_TOOL_FINGER, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009173
9174 // stylus trumps finger
9175 processKey(mapper, BTN_TOOL_PEN, 1);
9176 processSync(mapper);
9177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009179 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009180
9181 // eraser trumps stylus
9182 processKey(mapper, BTN_TOOL_RUBBER, 1);
9183 processSync(mapper);
9184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9185 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009186 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009187
9188 // mouse trumps eraser
9189 processKey(mapper, BTN_TOOL_MOUSE, 1);
9190 processSync(mapper);
9191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009193 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009194
9195 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9196 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9197 processSync(mapper);
9198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9199 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009200 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009201
9202 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9203 processToolType(mapper, MT_TOOL_PEN);
9204 processSync(mapper);
9205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9206 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009207 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009208
9209 // back to default tool type
9210 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9211 processKey(mapper, BTN_TOOL_MOUSE, 0);
9212 processKey(mapper, BTN_TOOL_RUBBER, 0);
9213 processKey(mapper, BTN_TOOL_PEN, 0);
9214 processKey(mapper, BTN_TOOL_FINGER, 0);
9215 processSync(mapper);
9216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009218 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009219}
9220
9221TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009222 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009223 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009224 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009225 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009226 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009227
9228 NotifyMotionArgs motionArgs;
9229
9230 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9231 processId(mapper, 1);
9232 processPosition(mapper, 100, 200);
9233 processSync(mapper);
9234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9235 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9237 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9238
9239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9240 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9242 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9243
9244 // move a little
9245 processPosition(mapper, 150, 250);
9246 processSync(mapper);
9247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9248 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9250 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9251
9252 // down when BTN_TOUCH is pressed, pressure defaults to 1
9253 processKey(mapper, BTN_TOUCH, 1);
9254 processSync(mapper);
9255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9256 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9258 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9259
9260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9261 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9263 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9264
9265 // up when BTN_TOUCH is released, hover restored
9266 processKey(mapper, BTN_TOUCH, 0);
9267 processSync(mapper);
9268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9269 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9271 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9272
9273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9274 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9276 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9277
9278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9279 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9281 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9282
9283 // exit hover when pointer goes away
9284 processId(mapper, -1);
9285 processSync(mapper);
9286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9287 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9289 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9290}
9291
9292TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009293 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009294 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009295 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009296 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009297
9298 NotifyMotionArgs motionArgs;
9299
9300 // initially hovering because pressure is 0
9301 processId(mapper, 1);
9302 processPosition(mapper, 100, 200);
9303 processPressure(mapper, 0);
9304 processSync(mapper);
9305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9306 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9308 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9309
9310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9311 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9313 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9314
9315 // move a little
9316 processPosition(mapper, 150, 250);
9317 processSync(mapper);
9318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9319 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9321 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9322
9323 // down when pressure becomes non-zero
9324 processPressure(mapper, RAW_PRESSURE_MAX);
9325 processSync(mapper);
9326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9327 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9329 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9330
9331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9332 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9334 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9335
9336 // up when pressure becomes 0, hover restored
9337 processPressure(mapper, 0);
9338 processSync(mapper);
9339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9340 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9342 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9343
9344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9345 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9347 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9348
9349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9350 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9352 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9353
9354 // exit hover when pointer goes away
9355 processId(mapper, -1);
9356 processSync(mapper);
9357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9358 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9360 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9361}
9362
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009363/**
9364 * Set the input device port <--> display port associations, and check that the
9365 * events are routed to the display that matches the display port.
9366 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9367 */
9368TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009369 const std::string usb2 = "USB2";
9370 const uint8_t hdmi1 = 0;
9371 const uint8_t hdmi2 = 1;
9372 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009373 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009374
9375 addConfigurationProperty("touch.deviceType", "touchScreen");
9376 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009377 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009378
9379 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9380 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9381
9382 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9383 // for this input device is specified, and the matching viewport is not present,
9384 // the input device should be disabled (at the mapper level).
9385
9386 // Add viewport for display 2 on hdmi2
9387 prepareSecondaryDisplay(type, hdmi2);
9388 // Send a touch event
9389 processPosition(mapper, 100, 100);
9390 processSync(mapper);
9391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9392
9393 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009394 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009395 // Send a touch event again
9396 processPosition(mapper, 100, 100);
9397 processSync(mapper);
9398
9399 NotifyMotionArgs args;
9400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9401 ASSERT_EQ(DISPLAY_ID, args.displayId);
9402}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009403
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009404TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9405 addConfigurationProperty("touch.deviceType", "touchScreen");
9406 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009407 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009408
9409 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9410
Michael Wrighta9cf4192022-12-01 23:46:39 +00009411 prepareDisplay(ui::ROTATION_0);
9412 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009413
9414 // Send a touch event
9415 processPosition(mapper, 100, 100);
9416 processSync(mapper);
9417
9418 NotifyMotionArgs args;
9419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9420 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9421}
9422
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009423TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009424 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009425 std::shared_ptr<FakePointerController> fakePointerController =
9426 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009427 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009428 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009429 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009430
Garfield Tan888a6a42020-01-09 11:39:16 -08009431 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009432 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009433
Michael Wrighta9cf4192022-12-01 23:46:39 +00009434 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009435 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009436 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009437
Josep del Río2d8c79a2023-01-23 19:33:50 +00009438 // Check source is mouse that would obtain the PointerController.
9439 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009440
9441 NotifyMotionArgs motionArgs;
9442 processPosition(mapper, 100, 100);
9443 processSync(mapper);
9444
9445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9446 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9447 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9448}
9449
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009450/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009451 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9452 */
9453TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9454 addConfigurationProperty("touch.deviceType", "touchScreen");
9455 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009456 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009457
Michael Wrighta9cf4192022-12-01 23:46:39 +00009458 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009459 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9460 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9461 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9462 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009463
9464 NotifyMotionArgs args;
9465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9466 ASSERT_EQ(26, args.readTime);
9467
Harry Cutts33476232023-01-30 19:57:29 +00009468 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9469 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9470 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009471
9472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9473 ASSERT_EQ(33, args.readTime);
9474}
9475
9476/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009477 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9478 * events should not be delivered to the listener.
9479 */
9480TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9481 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009482 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009483 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009484 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009485 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009486 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009487 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009488
9489 NotifyMotionArgs motionArgs;
9490 processPosition(mapper, 100, 100);
9491 processSync(mapper);
9492
9493 mFakeListener->assertNotifyMotionWasNotCalled();
9494}
9495
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009496/**
9497 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9498 * the touch mapper can process the events and the events can be delivered to the listener.
9499 */
9500TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9501 addConfigurationProperty("touch.deviceType", "touchScreen");
9502 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009503 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009504 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009505 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009506 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009507 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009508
9509 NotifyMotionArgs motionArgs;
9510 processPosition(mapper, 100, 100);
9511 processSync(mapper);
9512
9513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9514 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9515}
9516
Josh Thielene986aed2023-06-01 14:17:30 +00009517/**
9518 * When the viewport is deactivated (isActive transitions from true to false),
9519 * and touch.enableForInactiveViewport is false, touches prior to the transition
9520 * should be cancelled.
9521 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08009522TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9523 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009524 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009525 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009526 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009527 std::optional<DisplayViewport> optionalDisplayViewport =
9528 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9529 ASSERT_TRUE(optionalDisplayViewport.has_value());
9530 DisplayViewport displayViewport = *optionalDisplayViewport;
9531
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009532 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009533 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009534 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08009535
9536 // Finger down
9537 int32_t x = 100, y = 100;
9538 processPosition(mapper, x, y);
9539 processSync(mapper);
9540
9541 NotifyMotionArgs motionArgs;
9542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9543 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9544
9545 // Deactivate display viewport
9546 displayViewport.isActive = false;
9547 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009548 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009549
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009550 // The ongoing touch should be canceled immediately
9551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9552 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9553
9554 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009555 x += 10, y += 10;
9556 processPosition(mapper, x, y);
9557 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009559
9560 // Reactivate display viewport
9561 displayViewport.isActive = true;
9562 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009563 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009564
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009565 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009566 x += 10, y += 10;
9567 processPosition(mapper, x, y);
9568 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9570 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009571}
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 true, touches prior to the transition
9576 * should not be cancelled.
9577 */
9578TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
9579 addConfigurationProperty("touch.deviceType", "touchScreen");
9580 addConfigurationProperty("touch.enableForInactiveViewport", "1");
9581 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
9582 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
9583 std::optional<DisplayViewport> optionalDisplayViewport =
9584 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9585 ASSERT_TRUE(optionalDisplayViewport.has_value());
9586 DisplayViewport displayViewport = *optionalDisplayViewport;
9587
9588 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9589 prepareAxes(POSITION);
9590 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
9591
9592 // Finger down
9593 int32_t x = 100, y = 100;
9594 processPosition(mapper, x, y);
9595 processSync(mapper);
9596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9597 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9598
9599 // Deactivate display viewport
9600 displayViewport.isActive = false;
9601 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9602 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9603
9604 // The ongoing touch should not be canceled
9605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9606
9607 // Finger move is not ignored
9608 x += 10, y += 10;
9609 processPosition(mapper, x, y);
9610 processSync(mapper);
9611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9612 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
9613
9614 // Reactivate display viewport
9615 displayViewport.isActive = true;
9616 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9617 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9618
9619 // Finger move continues and does not start new gesture
9620 x += 10, y += 10;
9621 processPosition(mapper, x, y);
9622 processSync(mapper);
9623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9624 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
9625}
9626
Arthur Hung7c645402019-01-25 17:45:42 +08009627TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9628 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009629 prepareAxes(POSITION | ID | SLOT);
9630 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009631 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009632
9633 // Create the second touch screen device, and enable multi fingers.
9634 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009635 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009636 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009637 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009638 std::shared_ptr<InputDevice> device2 =
9639 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009640 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009641
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009642 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009643 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009644 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009645 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009646 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009647 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009648 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009649 /*flat=*/0, /*fuzz=*/0);
9650 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009651 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9652 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009653
9654 // Setup the second touch screen device.
Arpit Singha8c236b2023-04-25 13:56:05 +00009655 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
9656 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
9657 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009658 std::list<NotifyArgs> unused =
9659 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009660 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009661 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009662
9663 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009664 std::shared_ptr<FakePointerController> fakePointerController =
9665 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009666 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009667
9668 // Setup policy for associated displays and show touches.
9669 const uint8_t hdmi1 = 0;
9670 const uint8_t hdmi2 = 1;
9671 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9672 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9673 mFakePolicy->setShowTouches(true);
9674
9675 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009676 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009677 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009678
9679 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009680 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009681 InputReaderConfiguration::Change::DISPLAY_INFO |
9682 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009683
9684 // Two fingers down at default display.
9685 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9686 processPosition(mapper, x1, y1);
9687 processId(mapper, 1);
9688 processSlot(mapper, 1);
9689 processPosition(mapper, x2, y2);
9690 processId(mapper, 2);
9691 processSync(mapper);
9692
9693 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9694 fakePointerController->getSpots().find(DISPLAY_ID);
9695 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9696 ASSERT_EQ(size_t(2), iter->second.size());
9697
9698 // Two fingers down at second display.
9699 processPosition(mapper2, x1, y1);
9700 processId(mapper2, 1);
9701 processSlot(mapper2, 1);
9702 processPosition(mapper2, x2, y2);
9703 processId(mapper2, 2);
9704 processSync(mapper2);
9705
9706 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9707 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9708 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009709
9710 // Disable the show touches configuration and ensure the spots are cleared.
9711 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009712 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009713 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009714
9715 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009716}
9717
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009718TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009719 prepareAxes(POSITION);
9720 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009721 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009722 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009723
9724 NotifyMotionArgs motionArgs;
9725 // Unrotated video frame
9726 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9727 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009728 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009729 processPosition(mapper, 100, 200);
9730 processSync(mapper);
9731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9732 ASSERT_EQ(frames, motionArgs.videoFrames);
9733
9734 // Subsequent touch events should not have any videoframes
9735 // This is implemented separately in FakeEventHub,
9736 // but that should match the behaviour of TouchVideoDevice.
9737 processPosition(mapper, 200, 200);
9738 processSync(mapper);
9739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9740 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9741}
9742
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009743TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009744 prepareAxes(POSITION);
9745 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009746 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009747 // Unrotated video frame
9748 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9749 NotifyMotionArgs motionArgs;
9750
9751 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009752 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009753 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9754 clearViewports();
9755 prepareDisplay(orientation);
9756 std::vector<TouchVideoFrame> frames{frame};
9757 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9758 processPosition(mapper, 100, 200);
9759 processSync(mapper);
9760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9761 ASSERT_EQ(frames, motionArgs.videoFrames);
9762 }
9763}
9764
9765TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9766 prepareAxes(POSITION);
9767 addConfigurationProperty("touch.deviceType", "touchScreen");
9768 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9769 // orientation-aware are affected by display rotation.
9770 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009771 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009772 // Unrotated video frame
9773 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9774 NotifyMotionArgs motionArgs;
9775
9776 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009777 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009778 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9779 clearViewports();
9780 prepareDisplay(orientation);
9781 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009782 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009783 processPosition(mapper, 100, 200);
9784 processSync(mapper);
9785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009786 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9787 // compared to the display. This is so that when the window transform (which contains the
9788 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9789 // window's coordinate space.
9790 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009791 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009792
9793 // Release finger.
9794 processSync(mapper);
9795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009796 }
9797}
9798
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009799TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
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 frames. There's no rule that they must all have the same dimensions,
9804 // so mix these.
9805 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9806 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9807 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9808 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9809 NotifyMotionArgs motionArgs;
9810
Michael Wrighta9cf4192022-12-01 23:46:39 +00009811 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009812 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009813 processPosition(mapper, 100, 200);
9814 processSync(mapper);
9815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009816 ASSERT_EQ(frames, motionArgs.videoFrames);
9817}
9818
9819TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9820 prepareAxes(POSITION);
9821 addConfigurationProperty("touch.deviceType", "touchScreen");
9822 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9823 // orientation-aware are affected by display rotation.
9824 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009825 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009826 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9827 // so mix these.
9828 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9829 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9830 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9831 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9832 NotifyMotionArgs motionArgs;
9833
Michael Wrighta9cf4192022-12-01 23:46:39 +00009834 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009835 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9836 processPosition(mapper, 100, 200);
9837 processSync(mapper);
9838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9839 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9840 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9841 // compared to the display. This is so that when the window transform (which contains the
9842 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9843 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009844 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009845 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009846 ASSERT_EQ(frames, motionArgs.videoFrames);
9847}
9848
Arthur Hung9da14732019-09-02 16:16:58 +08009849/**
9850 * If we had defined port associations, but the viewport is not ready, the touch device would be
9851 * expected to be disabled, and it should be enabled after the viewport has found.
9852 */
9853TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009854 constexpr uint8_t hdmi2 = 1;
9855 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009856 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009857
9858 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9859
9860 addConfigurationProperty("touch.deviceType", "touchScreen");
9861 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009862 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009863
9864 ASSERT_EQ(mDevice->isEnabled(), false);
9865
9866 // Add display on hdmi2, the device should be enabled and can receive touch event.
9867 prepareSecondaryDisplay(type, hdmi2);
9868 ASSERT_EQ(mDevice->isEnabled(), true);
9869
9870 // Send a touch event.
9871 processPosition(mapper, 100, 100);
9872 processSync(mapper);
9873
9874 NotifyMotionArgs args;
9875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9876 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9877}
9878
Arthur Hung421eb1c2020-01-16 00:09:42 +08009879TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009880 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009881 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009882 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009883 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009884
9885 NotifyMotionArgs motionArgs;
9886
9887 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9888 // finger down
9889 processId(mapper, 1);
9890 processPosition(mapper, x1, y1);
9891 processSync(mapper);
9892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9893 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009894 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009895
9896 // finger move
9897 processId(mapper, 1);
9898 processPosition(mapper, x2, y2);
9899 processSync(mapper);
9900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9901 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009902 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009903
9904 // finger up.
9905 processId(mapper, -1);
9906 processSync(mapper);
9907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9908 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009909 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009910
9911 // new finger down
9912 processId(mapper, 1);
9913 processPosition(mapper, x3, y3);
9914 processSync(mapper);
9915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9916 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009917 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009918}
9919
9920/**
arthurhungcc7f9802020-04-30 17:55:40 +08009921 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9922 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009923 */
arthurhungcc7f9802020-04-30 17:55:40 +08009924TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009925 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009926 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009927 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009928 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009929
9930 NotifyMotionArgs motionArgs;
9931
9932 // default tool type is finger
9933 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009934 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009935 processPosition(mapper, x1, y1);
9936 processSync(mapper);
9937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9938 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009939 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009940
9941 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9942 processToolType(mapper, MT_TOOL_PALM);
9943 processSync(mapper);
9944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9945 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9946
9947 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009948 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009949 processPosition(mapper, x2, y2);
9950 processSync(mapper);
9951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9952
9953 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009954 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009955 processSync(mapper);
9956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9957
9958 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009959 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009960 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009961 processPosition(mapper, x3, y3);
9962 processSync(mapper);
9963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9964 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, 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
arthurhungbf89a482020-04-17 17:37:55 +08009968/**
arthurhungcc7f9802020-04-30 17:55:40 +08009969 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9970 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009971 */
arthurhungcc7f9802020-04-30 17:55:40 +08009972TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009973 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009974 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009975 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009976 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009977
9978 NotifyMotionArgs motionArgs;
9979
9980 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009981 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9982 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009983 processPosition(mapper, x1, y1);
9984 processSync(mapper);
9985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9986 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009987 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009988
9989 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009990 processSlot(mapper, SECOND_SLOT);
9991 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009992 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009993 processSync(mapper);
9994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009995 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009996 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009997
9998 // If the tool type of the first finger changes to MT_TOOL_PALM,
9999 // we expect to receive ACTION_POINTER_UP with cancel flag.
10000 processSlot(mapper, FIRST_SLOT);
10001 processId(mapper, FIRST_TRACKING_ID);
10002 processToolType(mapper, MT_TOOL_PALM);
10003 processSync(mapper);
10004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010005 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010006 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10007
10008 // The following MOVE events of second finger should be processed.
10009 processSlot(mapper, SECOND_SLOT);
10010 processId(mapper, SECOND_TRACKING_ID);
10011 processPosition(mapper, x2 + 1, y2 + 1);
10012 processSync(mapper);
10013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10014 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010015 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010016
10017 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
10018 // it. Second finger receive move.
10019 processSlot(mapper, FIRST_SLOT);
10020 processId(mapper, INVALID_TRACKING_ID);
10021 processSync(mapper);
10022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010024 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010025
10026 // Second finger keeps moving.
10027 processSlot(mapper, SECOND_SLOT);
10028 processId(mapper, SECOND_TRACKING_ID);
10029 processPosition(mapper, x2 + 2, y2 + 2);
10030 processSync(mapper);
10031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10032 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010033 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010034
10035 // Second finger up.
10036 processId(mapper, INVALID_TRACKING_ID);
10037 processSync(mapper);
10038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10039 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10040 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10041}
10042
10043/**
10044 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
10045 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
10046 */
10047TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
10048 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010049 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010050 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010051 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010052
10053 NotifyMotionArgs motionArgs;
10054
10055 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10056 // First finger down.
10057 processId(mapper, FIRST_TRACKING_ID);
10058 processPosition(mapper, x1, y1);
10059 processSync(mapper);
10060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10061 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010062 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010063
10064 // Second finger down.
10065 processSlot(mapper, SECOND_SLOT);
10066 processId(mapper, SECOND_TRACKING_ID);
10067 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +080010068 processSync(mapper);
10069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010070 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010071 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +080010072
arthurhungcc7f9802020-04-30 17:55:40 +080010073 // If the tool type of the first finger changes to MT_TOOL_PALM,
10074 // we expect to receive ACTION_POINTER_UP with cancel flag.
10075 processSlot(mapper, FIRST_SLOT);
10076 processId(mapper, FIRST_TRACKING_ID);
10077 processToolType(mapper, MT_TOOL_PALM);
10078 processSync(mapper);
10079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010080 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010081 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10082
10083 // Second finger keeps moving.
10084 processSlot(mapper, SECOND_SLOT);
10085 processId(mapper, SECOND_TRACKING_ID);
10086 processPosition(mapper, x2 + 1, y2 + 1);
10087 processSync(mapper);
10088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10089 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10090
10091 // second finger becomes palm, receive cancel due to only 1 finger is active.
10092 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010093 processToolType(mapper, MT_TOOL_PALM);
10094 processSync(mapper);
10095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10096 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10097
arthurhungcc7f9802020-04-30 17:55:40 +080010098 // third finger down.
10099 processSlot(mapper, THIRD_SLOT);
10100 processId(mapper, THIRD_TRACKING_ID);
10101 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +080010102 processPosition(mapper, x3, y3);
10103 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +080010104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10105 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010106 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010107 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010108
10109 // third finger move
10110 processId(mapper, THIRD_TRACKING_ID);
10111 processPosition(mapper, x3 + 1, y3 + 1);
10112 processSync(mapper);
10113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10115
10116 // first finger up, third finger receive move.
10117 processSlot(mapper, FIRST_SLOT);
10118 processId(mapper, INVALID_TRACKING_ID);
10119 processSync(mapper);
10120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10121 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010122 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010123
10124 // second finger up, third finger receive move.
10125 processSlot(mapper, SECOND_SLOT);
10126 processId(mapper, INVALID_TRACKING_ID);
10127 processSync(mapper);
10128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010130 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010131
10132 // third finger up.
10133 processSlot(mapper, THIRD_SLOT);
10134 processId(mapper, INVALID_TRACKING_ID);
10135 processSync(mapper);
10136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10137 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10138 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10139}
10140
10141/**
10142 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10143 * and the active finger could still be allowed to receive the events
10144 */
10145TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10146 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010147 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010148 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010149 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010150
10151 NotifyMotionArgs motionArgs;
10152
10153 // default tool type is finger
10154 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10155 processId(mapper, FIRST_TRACKING_ID);
10156 processPosition(mapper, x1, y1);
10157 processSync(mapper);
10158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10159 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010160 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010161
10162 // Second finger down.
10163 processSlot(mapper, SECOND_SLOT);
10164 processId(mapper, SECOND_TRACKING_ID);
10165 processPosition(mapper, x2, y2);
10166 processSync(mapper);
10167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010168 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010169 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010170
10171 // If the tool type of the second finger changes to MT_TOOL_PALM,
10172 // we expect to receive ACTION_POINTER_UP with cancel flag.
10173 processId(mapper, SECOND_TRACKING_ID);
10174 processToolType(mapper, MT_TOOL_PALM);
10175 processSync(mapper);
10176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010177 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010178 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10179
10180 // The following MOVE event should be processed.
10181 processSlot(mapper, FIRST_SLOT);
10182 processId(mapper, FIRST_TRACKING_ID);
10183 processPosition(mapper, x1 + 1, y1 + 1);
10184 processSync(mapper);
10185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10186 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010187 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010188
10189 // second finger up.
10190 processSlot(mapper, SECOND_SLOT);
10191 processId(mapper, INVALID_TRACKING_ID);
10192 processSync(mapper);
10193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10195
10196 // first finger keep moving
10197 processSlot(mapper, FIRST_SLOT);
10198 processId(mapper, FIRST_TRACKING_ID);
10199 processPosition(mapper, x1 + 2, y1 + 2);
10200 processSync(mapper);
10201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10202 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10203
10204 // first finger up.
10205 processId(mapper, INVALID_TRACKING_ID);
10206 processSync(mapper);
10207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10208 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10209 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010210}
10211
Arthur Hung9ad18942021-06-19 02:04:46 +000010212/**
10213 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10214 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10215 * cause slot be valid again.
10216 */
10217TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10218 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010219 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +000010220 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010221 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +000010222
10223 NotifyMotionArgs motionArgs;
10224
10225 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10226 // First finger down.
10227 processId(mapper, FIRST_TRACKING_ID);
10228 processPosition(mapper, x1, y1);
10229 processPressure(mapper, RAW_PRESSURE_MAX);
10230 processSync(mapper);
10231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10232 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010233 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010234
10235 // First finger move.
10236 processId(mapper, FIRST_TRACKING_ID);
10237 processPosition(mapper, x1 + 1, y1 + 1);
10238 processPressure(mapper, RAW_PRESSURE_MAX);
10239 processSync(mapper);
10240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10241 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010242 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010243
10244 // Second finger down.
10245 processSlot(mapper, SECOND_SLOT);
10246 processId(mapper, SECOND_TRACKING_ID);
10247 processPosition(mapper, x2, y2);
10248 processPressure(mapper, RAW_PRESSURE_MAX);
10249 processSync(mapper);
10250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010251 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010252 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010253
10254 // second finger up with some unexpected data.
10255 processSlot(mapper, SECOND_SLOT);
10256 processId(mapper, INVALID_TRACKING_ID);
10257 processPosition(mapper, x2, y2);
10258 processSync(mapper);
10259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010260 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010261 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010262
10263 // first finger up with some unexpected data.
10264 processSlot(mapper, FIRST_SLOT);
10265 processId(mapper, INVALID_TRACKING_ID);
10266 processPosition(mapper, x2, y2);
10267 processPressure(mapper, RAW_PRESSURE_MAX);
10268 processSync(mapper);
10269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10270 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010271 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010272}
10273
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010274TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10275 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010276 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010277 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010278 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010279
10280 // First finger down.
10281 processId(mapper, FIRST_TRACKING_ID);
10282 processPosition(mapper, 100, 200);
10283 processPressure(mapper, RAW_PRESSURE_MAX);
10284 processSync(mapper);
10285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10286 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10287
10288 // Second finger down.
10289 processSlot(mapper, SECOND_SLOT);
10290 processId(mapper, SECOND_TRACKING_ID);
10291 processPosition(mapper, 300, 400);
10292 processPressure(mapper, RAW_PRESSURE_MAX);
10293 processSync(mapper);
10294 ASSERT_NO_FATAL_FAILURE(
10295 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10296
10297 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010298 // preserved. Resetting should cancel the ongoing gesture.
10299 resetMapper(mapper, ARBITRARY_TIME);
10300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10301 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010302
10303 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10304 // the existing touch state to generate a down event.
10305 processPosition(mapper, 301, 302);
10306 processSync(mapper);
10307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10308 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10310 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10311
10312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10313}
10314
10315TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10316 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010317 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010318 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010319 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010320
10321 // First finger touches down and releases.
10322 processId(mapper, FIRST_TRACKING_ID);
10323 processPosition(mapper, 100, 200);
10324 processPressure(mapper, RAW_PRESSURE_MAX);
10325 processSync(mapper);
10326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10327 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10328 processId(mapper, INVALID_TRACKING_ID);
10329 processSync(mapper);
10330 ASSERT_NO_FATAL_FAILURE(
10331 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10332
10333 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10334 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010335 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10337
10338 // Send an empty sync frame. Since there are no pointers, no events are generated.
10339 processSync(mapper);
10340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10341}
10342
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010343TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010344 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010345 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010346 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010347 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010349
10350 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10351 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10352 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10353 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10354 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10355
10356 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010357 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010358 processId(mapper, FIRST_TRACKING_ID);
10359 processToolType(mapper, MT_TOOL_PEN);
10360 processPosition(mapper, 100, 200);
10361 processPressure(mapper, RAW_PRESSURE_MAX);
10362 processSync(mapper);
10363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10364 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10365 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010366 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010367
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010368 // Now that we know the device supports styluses, ensure that the device is re-configured with
10369 // the stylus source.
10370 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10371 {
10372 const auto& devices = mReader->getInputDevices();
10373 auto deviceInfo =
10374 std::find_if(devices.begin(), devices.end(),
10375 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10376 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10377 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10378 }
10379
10380 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10382
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010383 processId(mapper, INVALID_TRACKING_ID);
10384 processSync(mapper);
10385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10386 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10387 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010388 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010389}
10390
Seunghwan Choi356026c2023-02-01 14:37:25 +090010391TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10392 addConfigurationProperty("touch.deviceType", "touchScreen");
10393 prepareDisplay(ui::ROTATION_0);
10394 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10395 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10396 // indicate stylus presence dynamically.
10397 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10398 std::shared_ptr<FakePointerController> fakePointerController =
10399 std::make_shared<FakePointerController>();
10400 mFakePolicy->setPointerController(fakePointerController);
10401 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +000010402 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010403
10404 processId(mapper, FIRST_TRACKING_ID);
10405 processPressure(mapper, RAW_PRESSURE_MIN);
10406 processPosition(mapper, 100, 200);
10407 processToolType(mapper, MT_TOOL_PEN);
10408 processSync(mapper);
10409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10410 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010411 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010412 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10413 ASSERT_TRUE(fakePointerController->isPointerShown());
10414 ASSERT_NO_FATAL_FAILURE(
10415 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10416}
10417
10418TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10419 addConfigurationProperty("touch.deviceType", "touchScreen");
10420 prepareDisplay(ui::ROTATION_0);
10421 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10422 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10423 // indicate stylus presence dynamically.
10424 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10425 std::shared_ptr<FakePointerController> fakePointerController =
10426 std::make_shared<FakePointerController>();
10427 mFakePolicy->setPointerController(fakePointerController);
10428 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010429 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010430
10431 processId(mapper, FIRST_TRACKING_ID);
10432 processPressure(mapper, RAW_PRESSURE_MIN);
10433 processPosition(mapper, 100, 200);
10434 processToolType(mapper, MT_TOOL_PEN);
10435 processSync(mapper);
10436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10437 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010438 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010439 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10440 ASSERT_FALSE(fakePointerController->isPointerShown());
10441}
10442
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010443// --- MultiTouchInputMapperTest_ExternalDevice ---
10444
10445class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10446protected:
Chris Yea52ade12020-08-27 16:49:20 -070010447 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010448};
10449
10450/**
10451 * Expect fallback to internal viewport if device is external and external viewport is not present.
10452 */
10453TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10454 prepareAxes(POSITION);
10455 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010456 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010457 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010458
10459 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10460
10461 NotifyMotionArgs motionArgs;
10462
10463 // Expect the event to be sent to the internal viewport,
10464 // because an external viewport is not present.
10465 processPosition(mapper, 100, 100);
10466 processSync(mapper);
10467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10468 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10469
10470 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010471 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010472 processPosition(mapper, 100, 100);
10473 processSync(mapper);
10474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10475 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10476}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010477
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010478TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10479 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10480 std::shared_ptr<FakePointerController> fakePointerController =
10481 std::make_shared<FakePointerController>();
10482 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10483 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010484
10485 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010486 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010487 prepareAxes(POSITION | ID | SLOT);
10488 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10489 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10490 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010491 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010492 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010493
10494 // captured touchpad should be a touchpad source
10495 NotifyDeviceResetArgs resetArgs;
10496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10497 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10498
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010499 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010500
10501 const InputDeviceInfo::MotionRange* relRangeX =
10502 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10503 ASSERT_NE(relRangeX, nullptr);
10504 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10505 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10506 const InputDeviceInfo::MotionRange* relRangeY =
10507 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10508 ASSERT_NE(relRangeY, nullptr);
10509 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10510 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10511
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010512 // run captured pointer tests - note that this is unscaled, so input listener events should be
10513 // identical to what the hardware sends (accounting for any
10514 // calibration).
10515 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010516 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010517 processId(mapper, 1);
10518 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10519 processKey(mapper, BTN_TOUCH, 1);
10520 processSync(mapper);
10521
10522 // expect coord[0] to contain initial location of touch 0
10523 NotifyMotionArgs args;
10524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10525 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010526 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010527 ASSERT_EQ(0, args.pointerProperties[0].id);
10528 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10529 ASSERT_NO_FATAL_FAILURE(
10530 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10531
10532 // FINGER 1 DOWN
10533 processSlot(mapper, 1);
10534 processId(mapper, 2);
10535 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10536 processSync(mapper);
10537
10538 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010540 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010541 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010542 ASSERT_EQ(0, args.pointerProperties[0].id);
10543 ASSERT_EQ(1, args.pointerProperties[1].id);
10544 ASSERT_NO_FATAL_FAILURE(
10545 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10546 ASSERT_NO_FATAL_FAILURE(
10547 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10548
10549 // FINGER 1 MOVE
10550 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10551 processSync(mapper);
10552
10553 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10554 // from move
10555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10556 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10557 ASSERT_NO_FATAL_FAILURE(
10558 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10559 ASSERT_NO_FATAL_FAILURE(
10560 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10561
10562 // FINGER 0 MOVE
10563 processSlot(mapper, 0);
10564 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10565 processSync(mapper);
10566
10567 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10570 ASSERT_NO_FATAL_FAILURE(
10571 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10572 ASSERT_NO_FATAL_FAILURE(
10573 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10574
10575 // BUTTON DOWN
10576 processKey(mapper, BTN_LEFT, 1);
10577 processSync(mapper);
10578
10579 // touchinputmapper design sends a move before button press
10580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10581 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10583 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10584
10585 // BUTTON UP
10586 processKey(mapper, BTN_LEFT, 0);
10587 processSync(mapper);
10588
10589 // touchinputmapper design sends a move after button release
10590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10591 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10593 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10594
10595 // FINGER 0 UP
10596 processId(mapper, -1);
10597 processSync(mapper);
10598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10599 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10600
10601 // FINGER 1 MOVE
10602 processSlot(mapper, 1);
10603 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10604 processSync(mapper);
10605
10606 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10608 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010609 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010610 ASSERT_EQ(1, args.pointerProperties[0].id);
10611 ASSERT_NO_FATAL_FAILURE(
10612 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10613
10614 // FINGER 1 UP
10615 processId(mapper, -1);
10616 processKey(mapper, BTN_TOUCH, 0);
10617 processSync(mapper);
10618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10619 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10620
Josep del Río2d8c79a2023-01-23 19:33:50 +000010621 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010622 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010623 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010625 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010626}
10627
10628TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10629 std::shared_ptr<FakePointerController> fakePointerController =
10630 std::make_shared<FakePointerController>();
10631 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10632 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010633
10634 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010635 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010636 prepareAxes(POSITION | ID | SLOT);
10637 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10638 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010639 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010640 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010641 // run uncaptured pointer tests - pushes out generic events
10642 // FINGER 0 DOWN
10643 processId(mapper, 3);
10644 processPosition(mapper, 100, 100);
10645 processKey(mapper, BTN_TOUCH, 1);
10646 processSync(mapper);
10647
10648 // start at (100,100), cursor should be at (0,0) * scale
10649 NotifyMotionArgs args;
10650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10651 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10652 ASSERT_NO_FATAL_FAILURE(
10653 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10654
10655 // FINGER 0 MOVE
10656 processPosition(mapper, 200, 200);
10657 processSync(mapper);
10658
10659 // compute scaling to help with touch position checking
10660 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10661 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10662 float scale =
10663 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10664
10665 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10667 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10669 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010670
10671 // BUTTON DOWN
10672 processKey(mapper, BTN_LEFT, 1);
10673 processSync(mapper);
10674
10675 // touchinputmapper design sends a move before button press
10676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10677 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10679 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10680
10681 // BUTTON UP
10682 processKey(mapper, BTN_LEFT, 0);
10683 processSync(mapper);
10684
10685 // touchinputmapper design sends a move after button release
10686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10687 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10689 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010690}
10691
10692TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10693 std::shared_ptr<FakePointerController> fakePointerController =
10694 std::make_shared<FakePointerController>();
10695
Michael Wrighta9cf4192022-12-01 23:46:39 +000010696 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010697 prepareAxes(POSITION | ID | SLOT);
10698 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010699 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010700 mFakePolicy->setPointerCapture(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010701 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010702
Josep del Río2d8c79a2023-01-23 19:33:50 +000010703 // uncaptured touchpad should be a pointer device
10704 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010705
Josep del Río2d8c79a2023-01-23 19:33:50 +000010706 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010707 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010708 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010709 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10710}
10711
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010712// --- BluetoothMultiTouchInputMapperTest ---
10713
10714class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10715protected:
10716 void SetUp() override {
10717 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10718 }
10719};
10720
10721TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10722 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010723 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010724 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010725 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010726
10727 nsecs_t kernelEventTime = ARBITRARY_TIME;
10728 nsecs_t expectedEventTime = ARBITRARY_TIME;
10729 // Touch down.
10730 processId(mapper, FIRST_TRACKING_ID);
10731 processPosition(mapper, 100, 200);
10732 processPressure(mapper, RAW_PRESSURE_MAX);
10733 processSync(mapper, ARBITRARY_TIME);
10734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10735 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10736
10737 // Process several events that come in quick succession, according to their timestamps.
10738 for (int i = 0; i < 3; i++) {
10739 constexpr static nsecs_t delta = ms2ns(1);
10740 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10741 kernelEventTime += delta;
10742 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10743
10744 processPosition(mapper, 101 + i, 201 + i);
10745 processSync(mapper, kernelEventTime);
10746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10747 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10748 WithEventTime(expectedEventTime))));
10749 }
10750
10751 // Release the touch.
10752 processId(mapper, INVALID_TRACKING_ID);
10753 processPressure(mapper, RAW_PRESSURE_MIN);
10754 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10756 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10757 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10758}
10759
10760// --- MultiTouchPointerModeTest ---
10761
HQ Liue6983c72022-04-19 22:14:56 +000010762class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10763protected:
10764 float mPointerMovementScale;
10765 float mPointerXZoomScale;
10766 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10767 addConfigurationProperty("touch.deviceType", "pointer");
10768 std::shared_ptr<FakePointerController> fakePointerController =
10769 std::make_shared<FakePointerController>();
10770 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10771 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010772 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010773
10774 prepareAxes(POSITION);
10775 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10776 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10777 // needs to be disabled, and the pointer gesture needs to be enabled.
10778 mFakePolicy->setPointerCapture(false);
10779 mFakePolicy->setPointerGestureEnabled(true);
10780 mFakePolicy->setPointerController(fakePointerController);
10781
10782 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10783 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10784 mPointerMovementScale =
10785 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10786 mPointerXZoomScale =
10787 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10788 }
10789
10790 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10791 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10792 /*flat*/ 0,
10793 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10794 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10795 /*flat*/ 0,
10796 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10797 }
10798};
10799
10800/**
10801 * Two fingers down on a pointer mode touch pad. The width
10802 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10803 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10804 * be greater than the both value to be freeform gesture, so that after two
10805 * fingers start to move downwards, the gesture should be swipe.
10806 */
10807TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10808 // The min freeform gesture width is 25units/mm x 30mm = 750
10809 // which is greater than fraction of the diagnal length of the touchpad (349).
10810 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010811 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010812 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010813 NotifyMotionArgs motionArgs;
10814
10815 // Two fingers down at once.
10816 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10817 // Pointer's initial position is used the [0,0] coordinate.
10818 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10819
10820 processId(mapper, FIRST_TRACKING_ID);
10821 processPosition(mapper, x1, y1);
10822 processMTSync(mapper);
10823 processId(mapper, SECOND_TRACKING_ID);
10824 processPosition(mapper, x2, y2);
10825 processMTSync(mapper);
10826 processSync(mapper);
10827
10828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010829 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010830 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010831 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010832 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010833 ASSERT_NO_FATAL_FAILURE(
10834 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10835
10836 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10837 // that there should be 1 pointer.
10838 int32_t movingDistance = 200;
10839 y1 += movingDistance;
10840 y2 += movingDistance;
10841
10842 processId(mapper, FIRST_TRACKING_ID);
10843 processPosition(mapper, x1, y1);
10844 processMTSync(mapper);
10845 processId(mapper, SECOND_TRACKING_ID);
10846 processPosition(mapper, x2, y2);
10847 processMTSync(mapper);
10848 processSync(mapper);
10849
10850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010851 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010852 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010853 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010854 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10856 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10857 0, 0, 0, 0));
10858}
10859
10860/**
10861 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10862 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10863 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10864 * value to be freeform gesture, so that after two fingers start to move downwards,
10865 * the gesture should be swipe.
10866 */
10867TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10868 // The min freeform gesture width is 5units/mm x 30mm = 150
10869 // which is greater than fraction of the diagnal length of the touchpad (349).
10870 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010871 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +000010872 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010873 NotifyMotionArgs motionArgs;
10874
10875 // Two fingers down at once.
10876 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10877 // Pointer's initial position is used the [0,0] coordinate.
10878 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10879
10880 processId(mapper, FIRST_TRACKING_ID);
10881 processPosition(mapper, x1, y1);
10882 processMTSync(mapper);
10883 processId(mapper, SECOND_TRACKING_ID);
10884 processPosition(mapper, x2, y2);
10885 processMTSync(mapper);
10886 processSync(mapper);
10887
10888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010889 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010890 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010891 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010892 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010893 ASSERT_NO_FATAL_FAILURE(
10894 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10895
10896 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10897 // and there should be 1 pointer.
10898 int32_t movingDistance = 200;
10899 y1 += movingDistance;
10900 y2 += movingDistance;
10901
10902 processId(mapper, FIRST_TRACKING_ID);
10903 processPosition(mapper, x1, y1);
10904 processMTSync(mapper);
10905 processId(mapper, SECOND_TRACKING_ID);
10906 processPosition(mapper, x2, y2);
10907 processMTSync(mapper);
10908 processSync(mapper);
10909
10910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010911 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010912 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010913 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010914 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010915 // New coordinate is the scaled relative coordinate from the initial coordinate.
10916 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10917 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10918 0, 0, 0, 0));
10919}
10920
10921/**
10922 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10923 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10924 * freeform gestures after two fingers start to move downwards.
10925 */
10926TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010927 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010928 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010929
10930 NotifyMotionArgs motionArgs;
10931
10932 // Two fingers down at once. Wider than the max swipe width.
10933 // The gesture is expected to be PRESS, then transformed to FREEFORM
10934 int32_t x1 = 100, y1 = 125, x2 = 900, 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 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10950 ASSERT_NO_FATAL_FAILURE(
10951 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10952
10953 int32_t movingDistance = 200;
10954
10955 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10956 // then two down events for two pointers.
10957 y1 += movingDistance;
10958 y2 += movingDistance;
10959
10960 processId(mapper, FIRST_TRACKING_ID);
10961 processPosition(mapper, x1, y1);
10962 processMTSync(mapper);
10963 processId(mapper, SECOND_TRACKING_ID);
10964 processPosition(mapper, x2, y2);
10965 processMTSync(mapper);
10966 processSync(mapper);
10967
10968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10969 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010970 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010971 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010973 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010974 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010975 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010977 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010978 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010979 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010980 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010981 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010982 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010983 // Two pointers' scaled relative coordinates from their initial centroid.
10984 // Initial y coordinates are 0 as y1 and y2 have the same value.
10985 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10986 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10987 // When pointers move, the new coordinates equal to the initial coordinates plus
10988 // scaled moving distance.
10989 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10990 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10991 0, 0, 0, 0));
10992 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10993 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10994 0, 0, 0, 0));
10995
10996 // Move two fingers down again, expect one MOVE motion event.
10997 y1 += movingDistance;
10998 y2 += movingDistance;
10999
11000 processId(mapper, FIRST_TRACKING_ID);
11001 processPosition(mapper, x1, y1);
11002 processMTSync(mapper);
11003 processId(mapper, SECOND_TRACKING_ID);
11004 processPosition(mapper, x2, y2);
11005 processMTSync(mapper);
11006 processSync(mapper);
11007
11008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011009 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011010 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011011 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011012 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11014 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11015 0, 0, 0, 0, 0));
11016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11017 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11018 0, 0, 0, 0, 0));
11019}
11020
Harry Cutts39b7ca22022-10-05 15:55:48 +000011021TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000011022 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000011023 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000011024 NotifyMotionArgs motionArgs;
11025
11026 // Place two fingers down.
11027 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
11028
11029 processId(mapper, FIRST_TRACKING_ID);
11030 processPosition(mapper, x1, y1);
11031 processMTSync(mapper);
11032 processId(mapper, SECOND_TRACKING_ID);
11033 processPosition(mapper, x2, y2);
11034 processMTSync(mapper);
11035 processSync(mapper);
11036
11037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011038 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000011039 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11040 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
11041 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
11042 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
11043
11044 // Move the two fingers down and to the left.
11045 int32_t movingDistance = 200;
11046 x1 -= movingDistance;
11047 y1 += movingDistance;
11048 x2 -= movingDistance;
11049 y2 += movingDistance;
11050
11051 processId(mapper, FIRST_TRACKING_ID);
11052 processPosition(mapper, x1, y1);
11053 processMTSync(mapper);
11054 processId(mapper, SECOND_TRACKING_ID);
11055 processPosition(mapper, x2, y2);
11056 processMTSync(mapper);
11057 processSync(mapper);
11058
11059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011060 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000011061 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11062 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
11063 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
11064 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
11065}
11066
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011067TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000011068 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011069 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000011070 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
11072
11073 // Start a stylus gesture.
11074 processKey(mapper, BTN_TOOL_PEN, 1);
11075 processId(mapper, FIRST_TRACKING_ID);
11076 processPosition(mapper, 100, 200);
11077 processSync(mapper);
11078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11079 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
11080 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011081 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011082 // TODO(b/257078296): Pointer mode generates extra event.
11083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11084 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11085 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011086 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11088
11089 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
11090 // gesture should be disabled.
11091 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
11092 viewport->isActive = false;
11093 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000011094 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11096 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11097 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011098 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011099 // TODO(b/257078296): Pointer mode generates extra event.
11100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11101 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11102 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011103 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11105}
11106
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011107// --- JoystickInputMapperTest ---
11108
11109class JoystickInputMapperTest : public InputMapperTest {
11110protected:
11111 static const int32_t RAW_X_MIN;
11112 static const int32_t RAW_X_MAX;
11113 static const int32_t RAW_Y_MIN;
11114 static const int32_t RAW_Y_MAX;
11115
11116 void SetUp() override {
11117 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
11118 }
11119 void prepareAxes() {
11120 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
11121 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
11122 }
11123
11124 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
11125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
11126 }
11127
11128 void processSync(JoystickInputMapper& mapper) {
11129 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
11130 }
11131
Michael Wrighta9cf4192022-12-01 23:46:39 +000011132 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011133 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
11134 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
11135 NO_PORT, ViewportType::VIRTUAL);
11136 }
11137};
11138
11139const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
11140const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
11141const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11142const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11143
11144TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11145 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000011146 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011147
11148 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11149
Michael Wrighta9cf4192022-12-01 23:46:39 +000011150 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011151
11152 // Send an axis event
11153 processAxis(mapper, ABS_X, 100);
11154 processSync(mapper);
11155
11156 NotifyMotionArgs args;
11157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11158 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11159
11160 // Send another axis event
11161 processAxis(mapper, ABS_Y, 100);
11162 processSync(mapper);
11163
11164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11165 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11166}
11167
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011168// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011169
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011170class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011171protected:
11172 static const char* DEVICE_NAME;
11173 static const char* DEVICE_LOCATION;
11174 static const int32_t DEVICE_ID;
11175 static const int32_t DEVICE_GENERATION;
11176 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011177 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011178 static const int32_t EVENTHUB_ID;
11179
11180 std::shared_ptr<FakeEventHub> mFakeEventHub;
11181 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011182 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011183 std::unique_ptr<InstrumentedInputReader> mReader;
11184 std::shared_ptr<InputDevice> mDevice;
11185
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011186 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011187 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011188 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011189 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011190 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011191 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011192 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11193 }
11194
11195 void SetUp() override { SetUp(DEVICE_CLASSES); }
11196
11197 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011198 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011199 mFakePolicy.clear();
11200 }
11201
Chris Yee2b1e5c2021-03-10 22:45:12 -080011202 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11203 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011204 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011205 InputDeviceIdentifier identifier;
11206 identifier.name = name;
11207 identifier.location = location;
11208 std::shared_ptr<InputDevice> device =
11209 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11210 identifier);
11211 mReader->pushNextDevice(device);
11212 mFakeEventHub->addDevice(eventHubId, name, classes);
11213 mReader->loopOnce();
11214 return device;
11215 }
11216
11217 template <class T, typename... Args>
11218 T& addControllerAndConfigure(Args... args) {
11219 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11220
11221 return controller;
11222 }
11223};
11224
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011225const char* PeripheralControllerTest::DEVICE_NAME = "device";
11226const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11227const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11228const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11229const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011230const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11231 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011232const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011233
11234// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011235class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011236protected:
11237 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011238 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011239 }
11240};
11241
11242TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011243 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011244
Harry Cuttsa5b71292022-11-28 12:56:17 +000011245 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11246 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11247 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011248}
11249
11250TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011251 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011252
Harry Cuttsa5b71292022-11-28 12:56:17 +000011253 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11254 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11255 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011256}
11257
11258// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011259class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011260protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011261 void SetUp() override {
11262 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11263 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011264};
11265
Chris Ye85758332021-05-16 23:05:17 -070011266TEST_F(LightControllerTest, MonoLight) {
11267 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011268 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011269 .maxBrightness = 255,
11270 .flags = InputLightClass::BRIGHTNESS,
11271 .path = ""};
11272 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011273
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011274 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011275 InputDeviceInfo info;
11276 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011277 std::vector<InputDeviceLightInfo> lights = info.getLights();
11278 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011279 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11280 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11281
11282 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11283 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11284}
11285
11286TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11287 RawLightInfo infoMono = {.id = 1,
11288 .name = "mono_keyboard_backlight",
11289 .maxBrightness = 255,
11290 .flags = InputLightClass::BRIGHTNESS |
11291 InputLightClass::KEYBOARD_BACKLIGHT,
11292 .path = ""};
11293 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11294
11295 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11296 InputDeviceInfo info;
11297 controller.populateDeviceInfo(&info);
11298 std::vector<InputDeviceLightInfo> lights = info.getLights();
11299 ASSERT_EQ(1U, lights.size());
11300 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11301 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011302
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011303 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11304 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011305}
11306
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000011307TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
11308 RawLightInfo infoMono = {.id = 1,
11309 .name = "mono_light",
11310 .maxBrightness = 255,
11311 .flags = InputLightClass::BRIGHTNESS,
11312 .path = ""};
11313 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11314 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11315 "0,100,200");
11316
11317 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11318 std::list<NotifyArgs> unused =
11319 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11320 /*changes=*/{});
11321
11322 InputDeviceInfo info;
11323 controller.populateDeviceInfo(&info);
11324 std::vector<InputDeviceLightInfo> lights = info.getLights();
11325 ASSERT_EQ(1U, lights.size());
11326 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11327}
11328
11329TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
11330 RawLightInfo infoMono = {.id = 1,
11331 .name = "mono_keyboard_backlight",
11332 .maxBrightness = 255,
11333 .flags = InputLightClass::BRIGHTNESS |
11334 InputLightClass::KEYBOARD_BACKLIGHT,
11335 .path = ""};
11336 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11337
11338 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11339 std::list<NotifyArgs> unused =
11340 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11341 /*changes=*/{});
11342
11343 InputDeviceInfo info;
11344 controller.populateDeviceInfo(&info);
11345 std::vector<InputDeviceLightInfo> lights = info.getLights();
11346 ASSERT_EQ(1U, lights.size());
11347 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11348}
11349
11350TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
11351 RawLightInfo infoMono = {.id = 1,
11352 .name = "mono_keyboard_backlight",
11353 .maxBrightness = 255,
11354 .flags = InputLightClass::BRIGHTNESS |
11355 InputLightClass::KEYBOARD_BACKLIGHT,
11356 .path = ""};
11357 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11358 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11359 "0,100,200");
11360
11361 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11362 std::list<NotifyArgs> unused =
11363 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11364 /*changes=*/{});
11365
11366 InputDeviceInfo info;
11367 controller.populateDeviceInfo(&info);
11368 std::vector<InputDeviceLightInfo> lights = info.getLights();
11369 ASSERT_EQ(1U, lights.size());
11370 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
11371 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
11372 ASSERT_EQ(BrightnessLevel(0), *it);
11373 std::advance(it, 1);
11374 ASSERT_EQ(BrightnessLevel(100), *it);
11375 std::advance(it, 1);
11376 ASSERT_EQ(BrightnessLevel(200), *it);
11377}
11378
11379TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
11380 RawLightInfo infoMono = {.id = 1,
11381 .name = "mono_keyboard_backlight",
11382 .maxBrightness = 255,
11383 .flags = InputLightClass::BRIGHTNESS |
11384 InputLightClass::KEYBOARD_BACKLIGHT,
11385 .path = ""};
11386 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11387 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11388 "0,100,200,300,400,500");
11389
11390 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11391 std::list<NotifyArgs> unused =
11392 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11393 /*changes=*/{});
11394
11395 InputDeviceInfo info;
11396 controller.populateDeviceInfo(&info);
11397 std::vector<InputDeviceLightInfo> lights = info.getLights();
11398 ASSERT_EQ(1U, lights.size());
11399 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11400}
11401
Chris Yee2b1e5c2021-03-10 22:45:12 -080011402TEST_F(LightControllerTest, RGBLight) {
11403 RawLightInfo infoRed = {.id = 1,
11404 .name = "red",
11405 .maxBrightness = 255,
11406 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11407 .path = ""};
11408 RawLightInfo infoGreen = {.id = 2,
11409 .name = "green",
11410 .maxBrightness = 255,
11411 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11412 .path = ""};
11413 RawLightInfo infoBlue = {.id = 3,
11414 .name = "blue",
11415 .maxBrightness = 255,
11416 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11417 .path = ""};
11418 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11419 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11420 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11421
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011422 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011423 InputDeviceInfo info;
11424 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011425 std::vector<InputDeviceLightInfo> lights = info.getLights();
11426 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011427 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11428 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11429 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11430
11431 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11432 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11433}
11434
11435TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11436 RawLightInfo infoRed = {.id = 1,
11437 .name = "red_keyboard_backlight",
11438 .maxBrightness = 255,
11439 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11440 InputLightClass::KEYBOARD_BACKLIGHT,
11441 .path = ""};
11442 RawLightInfo infoGreen = {.id = 2,
11443 .name = "green_keyboard_backlight",
11444 .maxBrightness = 255,
11445 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11446 InputLightClass::KEYBOARD_BACKLIGHT,
11447 .path = ""};
11448 RawLightInfo infoBlue = {.id = 3,
11449 .name = "blue_keyboard_backlight",
11450 .maxBrightness = 255,
11451 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11452 InputLightClass::KEYBOARD_BACKLIGHT,
11453 .path = ""};
11454 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11455 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11456 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11457
11458 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11459 InputDeviceInfo info;
11460 controller.populateDeviceInfo(&info);
11461 std::vector<InputDeviceLightInfo> lights = info.getLights();
11462 ASSERT_EQ(1U, lights.size());
11463 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11464 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11465 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11466
11467 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11468 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11469}
11470
11471TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11472 RawLightInfo infoRed = {.id = 1,
11473 .name = "red",
11474 .maxBrightness = 255,
11475 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11476 .path = ""};
11477 RawLightInfo infoGreen = {.id = 2,
11478 .name = "green",
11479 .maxBrightness = 255,
11480 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11481 .path = ""};
11482 RawLightInfo infoBlue = {.id = 3,
11483 .name = "blue",
11484 .maxBrightness = 255,
11485 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11486 .path = ""};
11487 RawLightInfo infoGlobal = {.id = 3,
11488 .name = "global_keyboard_backlight",
11489 .maxBrightness = 255,
11490 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11491 InputLightClass::KEYBOARD_BACKLIGHT,
11492 .path = ""};
11493 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11494 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11495 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11496 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11497
11498 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11499 InputDeviceInfo info;
11500 controller.populateDeviceInfo(&info);
11501 std::vector<InputDeviceLightInfo> lights = info.getLights();
11502 ASSERT_EQ(1U, lights.size());
11503 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11504 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11505 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011506
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011507 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11508 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011509}
11510
11511TEST_F(LightControllerTest, MultiColorRGBLight) {
11512 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011513 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011514 .maxBrightness = 255,
11515 .flags = InputLightClass::BRIGHTNESS |
11516 InputLightClass::MULTI_INTENSITY |
11517 InputLightClass::MULTI_INDEX,
11518 .path = ""};
11519
11520 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11521
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011522 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011523 InputDeviceInfo info;
11524 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011525 std::vector<InputDeviceLightInfo> lights = info.getLights();
11526 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011527 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11528 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11529 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11530
11531 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11532 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11533}
11534
11535TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11536 RawLightInfo infoColor = {.id = 1,
11537 .name = "multi_color_keyboard_backlight",
11538 .maxBrightness = 255,
11539 .flags = InputLightClass::BRIGHTNESS |
11540 InputLightClass::MULTI_INTENSITY |
11541 InputLightClass::MULTI_INDEX |
11542 InputLightClass::KEYBOARD_BACKLIGHT,
11543 .path = ""};
11544
11545 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11546
11547 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11548 InputDeviceInfo info;
11549 controller.populateDeviceInfo(&info);
11550 std::vector<InputDeviceLightInfo> lights = info.getLights();
11551 ASSERT_EQ(1U, lights.size());
11552 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11553 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11554 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011555
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011556 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11557 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011558}
11559
11560TEST_F(LightControllerTest, PlayerIdLight) {
11561 RawLightInfo info1 = {.id = 1,
11562 .name = "player1",
11563 .maxBrightness = 255,
11564 .flags = InputLightClass::BRIGHTNESS,
11565 .path = ""};
11566 RawLightInfo info2 = {.id = 2,
11567 .name = "player2",
11568 .maxBrightness = 255,
11569 .flags = InputLightClass::BRIGHTNESS,
11570 .path = ""};
11571 RawLightInfo info3 = {.id = 3,
11572 .name = "player3",
11573 .maxBrightness = 255,
11574 .flags = InputLightClass::BRIGHTNESS,
11575 .path = ""};
11576 RawLightInfo info4 = {.id = 4,
11577 .name = "player4",
11578 .maxBrightness = 255,
11579 .flags = InputLightClass::BRIGHTNESS,
11580 .path = ""};
11581 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11582 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11583 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11584 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11585
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011586 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011587 InputDeviceInfo info;
11588 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011589 std::vector<InputDeviceLightInfo> lights = info.getLights();
11590 ASSERT_EQ(1U, lights.size());
11591 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011592 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11593 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011594
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011595 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11596 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11597 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011598}
11599
Michael Wrightd02c5b62014-02-10 15:10:22 -080011600} // namespace android