blob: 9d2aea215ab848168843440161bfedb31d4fb2da [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);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001346
Arpit Singh440bf652023-08-09 09:23:43 +00001347 setupInputReader();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001348 }
1349
Chris Yea52ade12020-08-27 16:49:20 -07001350 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001351#if !defined(__ANDROID__)
1352 return;
1353#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001354 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001355 mReader.reset();
1356 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001357 mFakePolicy.clear();
1358 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001359
1360 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1361 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1362 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1363 [&name](const InputDeviceInfo& info) {
1364 return info.getIdentifier().name == name;
1365 });
1366 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1367 }
Arpit Singh440bf652023-08-09 09:23:43 +00001368
1369 void setupInputReader() {
1370 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1371 /*eventDidNotHappenTimeout=*/30ms);
1372
1373 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1374 *mTestListener);
1375 ASSERT_EQ(mReader->start(), OK);
1376
1377 // Since this test is run on a real device, all the input devices connected
1378 // to the test device will show up in mReader. We wait for those input devices to
1379 // show up before beginning the tests.
1380 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1381 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
1382 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1383 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001384};
1385
1386TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1387 // An invalid input device that is only used for this test.
1388 class InvalidUinputDevice : public UinputDevice {
1389 public:
Harry Cutts33476232023-01-30 19:57:29 +00001390 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001391
1392 private:
1393 void configureDevice(int fd, uinput_user_dev* device) override {}
1394 };
1395
1396 const size_t numDevices = mFakePolicy->getInputDevices().size();
1397
1398 // UinputDevice does not set any event or key bits, so InputReader should not
1399 // consider it as a valid device.
1400 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1401 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1402 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1403 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1404
1405 invalidDevice.reset();
1406 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1407 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1408 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1409}
1410
1411TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1412 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1413
1414 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1415 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1416 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1417 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1418
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001419 const auto device = findDeviceByName(keyboard->getName());
1420 ASSERT_TRUE(device.has_value());
1421 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1422 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1423 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001424
1425 keyboard.reset();
1426 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1427 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1428 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1429}
1430
1431TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1432 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1433 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1434
1435 NotifyConfigurationChangedArgs configChangedArgs;
1436 ASSERT_NO_FATAL_FAILURE(
1437 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001438 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001439 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1440
1441 NotifyKeyArgs keyArgs;
1442 keyboard->pressAndReleaseHomeKey();
1443 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1444 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001445 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001446 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001447 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001448 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001449 prevTimestamp = keyArgs.eventTime;
1450
1451 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1452 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001453 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001454 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001455 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001456}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001457
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001458TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1459 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1460 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1461
1462 const auto device = findDeviceByName(stylus->getName());
1463 ASSERT_TRUE(device.has_value());
1464
Prabir Pradhana3621852022-10-14 18:57:23 +00001465 // An external stylus with buttons should also be recognized as a keyboard.
1466 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001467 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1468 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1469
1470 const auto DOWN =
1471 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1472 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1473
1474 stylus->pressAndReleaseKey(BTN_STYLUS);
1475 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1476 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1477 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1478 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1479
1480 stylus->pressAndReleaseKey(BTN_STYLUS2);
1481 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1482 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1483 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1484 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1485
1486 stylus->pressAndReleaseKey(BTN_STYLUS3);
1487 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1488 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1489 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1490 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1491}
1492
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001493TEST_F(InputReaderIntegrationTest, KeyboardWithStylusButtons) {
1494 std::unique_ptr<UinputKeyboard> keyboard =
1495 createUinputDevice<UinputKeyboard>("KeyboardWithStylusButtons", /*productId=*/99,
1496 std::initializer_list<int>{KEY_Q, KEY_W, KEY_E,
1497 KEY_R, KEY_T, KEY_Y,
1498 BTN_STYLUS, BTN_STYLUS2,
1499 BTN_STYLUS3});
1500 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1501
1502 const auto device = findDeviceByName(keyboard->getName());
1503 ASSERT_TRUE(device.has_value());
1504
1505 // An alphabetical keyboard that reports stylus buttons should not be recognized as a stylus.
1506 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1507 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1508 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, device->getKeyboardType());
1509}
1510
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001511TEST_F(InputReaderIntegrationTest, HidUsageKeyboardIsNotAStylus) {
1512 // Create a Uinput keyboard that simulates a keyboard that can report HID usage codes. The
1513 // hid-input driver reports HID usage codes using the value for EV_MSC MSC_SCAN event.
1514 std::unique_ptr<UinputKeyboardWithHidUsage> keyboard =
1515 createUinputDevice<UinputKeyboardWithHidUsage>(
1516 std::initializer_list<int>{KEY_VOLUMEUP, KEY_VOLUMEDOWN});
1517 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1518
1519 const auto device = findDeviceByName(keyboard->getName());
1520 ASSERT_TRUE(device.has_value());
1521
1522 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1523 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1524
1525 // If a device supports reporting HID usage codes, it shouldn't automatically support
1526 // stylus keys.
1527 const std::vector<int> keycodes{AKEYCODE_STYLUS_BUTTON_PRIMARY};
1528 uint8_t outFlags[] = {0};
1529 ASSERT_TRUE(mReader->hasKeys(device->getId(), AINPUT_SOURCE_KEYBOARD, keycodes, outFlags));
1530 ASSERT_EQ(0, outFlags[0]) << "Keyboard should not have stylus button";
1531}
1532
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001533/**
1534 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1535 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1536 * are passed to the listener.
1537 */
1538static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1539TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1540 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1541 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1542 NotifyKeyArgs keyArgs;
1543
1544 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1545 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1546 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1547 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1548
1549 controller->pressAndReleaseKey(BTN_GEAR_UP);
1550 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1551 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1552 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1553}
1554
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001555// --- TouchIntegrationTest ---
1556
Arpit Singh440bf652023-08-09 09:23:43 +00001557class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
Arthur Hungaab25622020-01-16 11:22:11 +08001558protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001559 const std::string UNIQUE_ID = "local:0";
1560
Chris Yea52ade12020-08-27 16:49:20 -07001561 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001562#if !defined(__ANDROID__)
1563 GTEST_SKIP();
1564#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001565 InputReaderIntegrationTest::SetUp();
1566 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001567 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1568 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001569
1570 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1571 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1572 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001573 const auto info = findDeviceByName(mDevice->getName());
1574 ASSERT_TRUE(info);
1575 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001576 }
1577
1578 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001579 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001580 std::optional<uint8_t> physicalPort,
1581 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001582 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001583 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001584 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001585 }
1586
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001587 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1588 NotifyMotionArgs args;
1589 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1590 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001591 ASSERT_EQ(points.size(), args.getPointerCount());
1592 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001593 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1594 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1595 }
1596 }
1597
Arthur Hungaab25622020-01-16 11:22:11 +08001598 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001599 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001600};
1601
Arpit Singh440bf652023-08-09 09:23:43 +00001602enum class TouchIntegrationTestDisplays { DISPLAY_INTERNAL, DISPLAY_INPUT_PORT, DISPLAY_UNIQUE_ID };
1603
1604class TouchIntegrationTest : public BaseTouchIntegrationTest,
1605 public testing::WithParamInterface<TouchIntegrationTestDisplays> {
1606protected:
1607 static constexpr std::optional<uint8_t> DISPLAY_PORT = 0;
1608 const std::string INPUT_PORT = "uinput_touch/input0";
1609
1610 void SetUp() override {
1611#if !defined(__ANDROID__)
1612 GTEST_SKIP();
1613#endif
1614 if (GetParam() == TouchIntegrationTestDisplays::DISPLAY_INTERNAL) {
1615 BaseTouchIntegrationTest::SetUp();
1616 return;
1617 }
1618
1619 // setup policy with a input-port or UniqueId association to the display
1620 bool isInputPortAssociation =
1621 GetParam() == TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT;
1622
1623 mFakePolicy = sp<FakeInputReaderPolicy>::make();
1624 if (isInputPortAssociation) {
1625 mFakePolicy->addInputPortAssociation(INPUT_PORT, DISPLAY_PORT.value());
1626 } else {
1627 mFakePolicy->addInputUniqueIdAssociation(INPUT_PORT, UNIQUE_ID);
1628 }
1629 mFakePointerController = std::make_shared<FakePointerController>();
1630 mFakePolicy->setPointerController(mFakePointerController);
1631
1632 InputReaderIntegrationTest::setupInputReader();
1633
1634 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
1635 INPUT_PORT);
1636 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1637
1638 // Add a display linked to a physical port or UniqueId.
1639 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1640 UNIQUE_ID, isInputPortAssociation ? DISPLAY_PORT : NO_PORT,
1641 ViewportType::INTERNAL);
1642 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1643 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1644 const auto info = findDeviceByName(mDevice->getName());
1645 ASSERT_TRUE(info);
1646 mDeviceInfo = *info;
1647 }
1648};
1649
1650TEST_P(TouchIntegrationTest, MultiTouchDeviceSource) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001651 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1652 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1653 // presses).
1654 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1655 mDeviceInfo.getSources());
1656}
1657
Arpit Singh440bf652023-08-09 09:23:43 +00001658TEST_P(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001659 NotifyMotionArgs args;
1660 const Point centerPoint = mDevice->getCenterPoint();
1661
1662 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001663 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001664 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001665 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001666 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1667 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1668
1669 // ACTION_MOVE
1670 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001671 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001672 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1673 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1674
1675 // ACTION_UP
1676 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001677 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001678 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1679 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1680}
1681
Arpit Singh440bf652023-08-09 09:23:43 +00001682TEST_P(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001683 NotifyMotionArgs args;
1684 const Point centerPoint = mDevice->getCenterPoint();
1685
1686 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001687 mDevice->sendSlot(FIRST_SLOT);
1688 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001689 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001690 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001691 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1692 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1693
1694 // ACTION_POINTER_DOWN (Second slot)
1695 const Point secondPoint = centerPoint + Point(100, 100);
1696 mDevice->sendSlot(SECOND_SLOT);
1697 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001698 mDevice->sendDown(secondPoint);
1699 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001700 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001701 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001702
1703 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001704 mDevice->sendMove(secondPoint + Point(1, 1));
1705 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001706 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1707 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1708
1709 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001710 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001711 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001712 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001713 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001714
1715 // ACTION_UP
1716 mDevice->sendSlot(FIRST_SLOT);
1717 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001718 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001719 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1720 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1721}
1722
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001723/**
1724 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1725 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1726 * data?
1727 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1728 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1729 * for Pointer 0 only is generated after.
1730 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1731 * events, we will not miss any information.
1732 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1733 * event generated afterwards that contains the newest movement of pointer 0.
1734 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1735 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1736 * losing information about non-palm pointers.
1737 */
Arpit Singh440bf652023-08-09 09:23:43 +00001738TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001739 NotifyMotionArgs args;
1740 const Point centerPoint = mDevice->getCenterPoint();
1741
1742 // ACTION_DOWN
1743 mDevice->sendSlot(FIRST_SLOT);
1744 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1745 mDevice->sendDown(centerPoint);
1746 mDevice->sendSync();
1747 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1748
1749 // ACTION_POINTER_DOWN (Second slot)
1750 const Point secondPoint = centerPoint + Point(100, 100);
1751 mDevice->sendSlot(SECOND_SLOT);
1752 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1753 mDevice->sendDown(secondPoint);
1754 mDevice->sendSync();
1755 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1756
1757 // ACTION_MOVE (First slot)
1758 mDevice->sendSlot(FIRST_SLOT);
1759 mDevice->sendMove(centerPoint + Point(5, 5));
1760 // ACTION_POINTER_UP (Second slot)
1761 mDevice->sendSlot(SECOND_SLOT);
1762 mDevice->sendPointerUp();
1763 // Send a single sync for the above 2 pointer updates
1764 mDevice->sendSync();
1765
1766 // First, we should get POINTER_UP for the second pointer
1767 assertReceivedMotion(ACTION_POINTER_1_UP,
1768 {/*first pointer */ centerPoint + Point(5, 5),
1769 /*second pointer*/ secondPoint});
1770
1771 // Next, the MOVE event for the first pointer
1772 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1773}
1774
1775/**
1776 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1777 * move, and then it will go up, all in the same frame.
1778 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1779 * gets sent to the listener.
1780 */
Arpit Singh440bf652023-08-09 09:23:43 +00001781TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001782 NotifyMotionArgs args;
1783 const Point centerPoint = mDevice->getCenterPoint();
1784
1785 // ACTION_DOWN
1786 mDevice->sendSlot(FIRST_SLOT);
1787 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1788 mDevice->sendDown(centerPoint);
1789 mDevice->sendSync();
1790 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1791
1792 // ACTION_POINTER_DOWN (Second slot)
1793 const Point secondPoint = centerPoint + Point(100, 100);
1794 mDevice->sendSlot(SECOND_SLOT);
1795 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1796 mDevice->sendDown(secondPoint);
1797 mDevice->sendSync();
1798 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1799
1800 // ACTION_MOVE (First slot)
1801 mDevice->sendSlot(FIRST_SLOT);
1802 mDevice->sendMove(centerPoint + Point(5, 5));
1803 // ACTION_POINTER_UP (Second slot)
1804 mDevice->sendSlot(SECOND_SLOT);
1805 mDevice->sendMove(secondPoint + Point(6, 6));
1806 mDevice->sendPointerUp();
1807 // Send a single sync for the above 2 pointer updates
1808 mDevice->sendSync();
1809
1810 // First, we should get POINTER_UP for the second pointer
1811 // The movement of the second pointer during the liftoff frame is ignored.
1812 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1813 assertReceivedMotion(ACTION_POINTER_1_UP,
1814 {/*first pointer */ centerPoint + Point(5, 5),
1815 /*second pointer*/ secondPoint});
1816
1817 // Next, the MOVE event for the first pointer
1818 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1819}
1820
Arpit Singh440bf652023-08-09 09:23:43 +00001821TEST_P(TouchIntegrationTest, InputEvent_ProcessPalm) {
Arthur Hungaab25622020-01-16 11:22:11 +08001822 NotifyMotionArgs args;
1823 const Point centerPoint = mDevice->getCenterPoint();
1824
1825 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001826 mDevice->sendSlot(FIRST_SLOT);
1827 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001828 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001829 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001830 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1831 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1832
arthurhungcc7f9802020-04-30 17:55:40 +08001833 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001834 const Point secondPoint = centerPoint + Point(100, 100);
1835 mDevice->sendSlot(SECOND_SLOT);
1836 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1837 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001838 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001839 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001840 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001841
arthurhungcc7f9802020-04-30 17:55:40 +08001842 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001843 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001844 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001845 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1846 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1847
arthurhungcc7f9802020-04-30 17:55:40 +08001848 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1849 // a palm event.
1850 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001851 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001852 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001853 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001854 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001855 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001856
arthurhungcc7f9802020-04-30 17:55:40 +08001857 // Send up to second slot, expect first slot send moving.
1858 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001859 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001860 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1861 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001862
arthurhungcc7f9802020-04-30 17:55:40 +08001863 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001864 mDevice->sendSlot(FIRST_SLOT);
1865 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001866 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001867
arthurhungcc7f9802020-04-30 17:55:40 +08001868 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1869 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001870}
1871
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +00001872/**
1873 * Some drivers historically have reported axis values outside of the range specified in the
1874 * evdev axis info. Ensure we don't crash when this happens. For example, a driver may report a
1875 * pressure value greater than the reported maximum, since it unclear what specific meaning the
1876 * maximum value for pressure has (beyond the maximum value that can be produced by a sensor),
1877 * and no units for pressure (resolution) is specified by the evdev documentation.
1878 */
1879TEST_P(TouchIntegrationTest, AcceptsAxisValuesOutsideReportedRange) {
1880 const Point centerPoint = mDevice->getCenterPoint();
1881
1882 // Down with pressure outside the reported range
1883 mDevice->sendSlot(FIRST_SLOT);
1884 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1885 mDevice->sendDown(centerPoint);
1886 mDevice->sendPressure(UinputTouchScreen::RAW_PRESSURE_MAX + 2);
1887 mDevice->sendSync();
1888 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1889 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1890
1891 // Move to a point outside the reported range
1892 mDevice->sendMove(Point(DISPLAY_WIDTH, DISPLAY_HEIGHT) + Point(1, 1));
1893 mDevice->sendSync();
1894 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1895 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1896
1897 // Up
1898 mDevice->sendUp();
1899 mDevice->sendSync();
1900 ASSERT_NO_FATAL_FAILURE(
1901 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1902}
1903
Arpit Singh440bf652023-08-09 09:23:43 +00001904TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00001905 const Point centerPoint = mDevice->getCenterPoint();
1906
1907 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1908 mDevice->sendSlot(FIRST_SLOT);
1909 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1910 mDevice->sendToolType(MT_TOOL_PEN);
1911 mDevice->sendDown(centerPoint);
1912 mDevice->sendSync();
1913 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1914 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001915 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001916
1917 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1918
1919 // Release the stylus touch.
1920 mDevice->sendUp();
1921 mDevice->sendSync();
1922 ASSERT_NO_FATAL_FAILURE(
1923 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1924
1925 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1926
1927 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1928 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1929 mDevice->sendToolType(MT_TOOL_FINGER);
1930 mDevice->sendDown(centerPoint);
1931 mDevice->sendSync();
1932 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1933 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001934 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001935
1936 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1937
1938 mDevice->sendUp();
1939 mDevice->sendSync();
1940 ASSERT_NO_FATAL_FAILURE(
1941 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1942
1943 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1944 // The policy should be notified of the stylus presence.
1945 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1946 mDevice->sendToolType(MT_TOOL_PEN);
1947 mDevice->sendMove(centerPoint);
1948 mDevice->sendSync();
1949 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1950 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001951 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001952
1953 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1954}
1955
Arpit Singh440bf652023-08-09 09:23:43 +00001956TEST_P(TouchIntegrationTest, ExternalStylusConnectedDuringTouchGesture) {
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001957 const Point centerPoint = mDevice->getCenterPoint();
1958
1959 // Down
1960 mDevice->sendSlot(FIRST_SLOT);
1961 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1962 mDevice->sendDown(centerPoint);
1963 mDevice->sendSync();
1964 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1965 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1966
1967 // Move
1968 mDevice->sendMove(centerPoint + Point(1, 1));
1969 mDevice->sendSync();
1970 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1971 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1972
1973 // Connecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
1974 auto externalStylus = createUinputDevice<UinputExternalStylus>();
1975 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1976 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1977 const auto stylusInfo = findDeviceByName(externalStylus->getName());
1978 ASSERT_TRUE(stylusInfo);
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001979
1980 // Move
1981 mDevice->sendMove(centerPoint + Point(2, 2));
1982 mDevice->sendSync();
1983 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1984 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1985
1986 // Disconnecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
1987 externalStylus.reset();
1988 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1989 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1990 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
1991
1992 // Up
1993 mDevice->sendUp();
1994 mDevice->sendSync();
1995 ASSERT_NO_FATAL_FAILURE(
1996 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1997
1998 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
1999}
2000
Arpit Singh440bf652023-08-09 09:23:43 +00002001INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
2002 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
2003 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
2004 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
2005
Prabir Pradhan124ea442022-10-28 20:27:44 +00002006// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002007
Prabir Pradhan124ea442022-10-28 20:27:44 +00002008// Verify the behavior of button presses reported by various kinds of styluses, including buttons
2009// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
2010// stylus.
2011template <typename UinputStylusDevice>
Arpit Singh440bf652023-08-09 09:23:43 +00002012class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002013protected:
2014 void SetUp() override {
2015#if !defined(__ANDROID__)
2016 GTEST_SKIP();
2017#endif
Arpit Singh440bf652023-08-09 09:23:43 +00002018 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00002019 mTouchscreen = mDevice.get();
2020 mTouchscreenInfo = mDeviceInfo;
2021
2022 setUpStylusDevice();
2023 }
2024
2025 UinputStylusDevice* mStylus{nullptr};
2026 InputDeviceInfo mStylusInfo{};
2027
2028 UinputTouchScreen* mTouchscreen{nullptr};
2029 InputDeviceInfo mTouchscreenInfo{};
2030
2031private:
2032 // When we are attempting to test stylus button events that are sent from the touchscreen,
2033 // use the same Uinput device for the touchscreen and the stylus.
2034 template <typename T = UinputStylusDevice>
2035 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2036 mStylus = mDevice.get();
2037 mStylusInfo = mDeviceInfo;
2038 }
2039
2040 // When we are attempting to stylus buttons from an external stylus being merged with touches
2041 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2042 template <typename T = UinputStylusDevice>
2043 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2044 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2045 mStylus = mStylusDeviceLifecycleTracker.get();
2046 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2047 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2048 const auto info = findDeviceByName(mStylus->getName());
2049 ASSERT_TRUE(info);
2050 mStylusInfo = *info;
2051 }
2052
2053 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2054
2055 // Hide the base class's device to expose it with a different name for readability.
Arpit Singh440bf652023-08-09 09:23:43 +00002056 using BaseTouchIntegrationTest::mDevice;
2057 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00002058};
2059
2060using StylusButtonIntegrationTestTypes =
2061 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2062TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2063
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002064TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002065 const auto stylusId = TestFixture::mStylusInfo.getId();
2066
2067 TestFixture::mStylus->pressKey(BTN_STYLUS);
2068 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2069 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2070 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2071
2072 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2073 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002074 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002075 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002076}
2077
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002078TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002079 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2080 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2081 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002082
2083 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002084 TestFixture::mStylus->pressKey(BTN_STYLUS);
2085 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002086 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002087 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002088
2089 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002090 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2091 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2092 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2093 TestFixture::mTouchscreen->sendDown(centerPoint);
2094 TestFixture::mTouchscreen->sendSync();
2095 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002096 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002097 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002098 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2099 WithDeviceId(touchscreenId))));
2100 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002101 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002102 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002103 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2104 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002105
Prabir Pradhan124ea442022-10-28 20:27:44 +00002106 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2107 TestFixture::mTouchscreen->sendSync();
2108 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002109 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002110 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002111 WithDeviceId(touchscreenId))));
2112 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002113 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002114 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002115 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002116
2117 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002118 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2119 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002120 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002121 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002122}
2123
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002124TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002125 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2126 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2127 const auto stylusId = TestFixture::mStylusInfo.getId();
2128 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002129 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002130
2131 // Press the stylus button.
2132 TestFixture::mStylus->pressKey(BTN_STYLUS);
2133 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2134 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2135 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2136
2137 // Start hovering with the stylus.
2138 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2139 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2140 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2141 TestFixture::mTouchscreen->sendMove(centerPoint);
2142 TestFixture::mTouchscreen->sendSync();
2143 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2144 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2145 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2146 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2147 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2148 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2149 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2150 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2151 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2152
2153 // Touch down with the stylus.
2154 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2155 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2156 TestFixture::mTouchscreen->sendDown(centerPoint);
2157 TestFixture::mTouchscreen->sendSync();
2158 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2159 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2160 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2161
2162 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2163 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2164 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2165
2166 // Stop touching with the stylus, and start hovering.
2167 TestFixture::mTouchscreen->sendUp();
2168 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2169 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2170 TestFixture::mTouchscreen->sendMove(centerPoint);
2171 TestFixture::mTouchscreen->sendSync();
2172 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2173 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2174 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2175 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2176 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2177 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2178 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2179 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2180 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2181
2182 // Stop hovering.
2183 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2184 TestFixture::mTouchscreen->sendSync();
2185 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2186 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2187 WithButtonState(0))));
2188 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2189 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2190 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2191 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2192
2193 // Release the stylus button.
2194 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2195 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2196 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2197 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2198}
2199
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002200TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002201 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2202 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2203 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002204
2205 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002206 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2207 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2208 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2209 TestFixture::mTouchscreen->sendDown(centerPoint);
2210 TestFixture::mTouchscreen->sendSync();
2211 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002212 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002213 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002214 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002215
2216 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002217 TestFixture::mStylus->pressKey(BTN_STYLUS);
2218 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002219 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002220 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2221 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002222 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002223 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002224 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2225 WithDeviceId(touchscreenId))));
2226 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002227 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002228 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002229 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2230 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002231
Prabir Pradhan124ea442022-10-28 20:27:44 +00002232 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2233 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002234 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002235 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2236 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002237 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002238 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002239 WithDeviceId(touchscreenId))));
2240 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002241 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002242 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002243 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002244
2245 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002246 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2247 TestFixture::mTouchscreen->sendSync();
2248 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002249 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002250 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002251 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002252}
2253
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002254TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002255 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2256 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002257 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002258
2259 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2260 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2261 const auto stylusId = TestFixture::mStylusInfo.getId();
2262
2263 // Start a stylus gesture. By the time this event is processed, the configuration change that
2264 // was requested is guaranteed to be completed.
2265 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2266 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2267 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2268 TestFixture::mTouchscreen->sendDown(centerPoint);
2269 TestFixture::mTouchscreen->sendSync();
2270 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2271 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002272 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002273 WithDeviceId(touchscreenId))));
2274
2275 // Press and release a stylus button. Each change only generates a MOVE motion event.
2276 // Key events are unaffected.
2277 TestFixture::mStylus->pressKey(BTN_STYLUS);
2278 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2279 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2280 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2281 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2282 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002283 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002284 WithDeviceId(touchscreenId))));
2285
2286 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2287 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2288 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2289 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2290 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2291 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002292 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002293 WithDeviceId(touchscreenId))));
2294
2295 // Finish the stylus gesture.
2296 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2297 TestFixture::mTouchscreen->sendSync();
2298 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2299 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002300 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002301 WithDeviceId(touchscreenId))));
2302}
2303
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002304// --- ExternalStylusIntegrationTest ---
2305
2306// Verify the behavior of an external stylus. An external stylus can report pressure or button
2307// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2308// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singh440bf652023-08-09 09:23:43 +00002309using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002310
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002311TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002312 const Point centerPoint = mDevice->getCenterPoint();
2313
2314 // Create an external stylus capable of reporting pressure data that
2315 // should be fused with a touch pointer.
2316 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2317 createUinputDevice<UinputExternalStylusWithPressure>();
2318 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2319 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2320 const auto stylusInfo = findDeviceByName(stylus->getName());
2321 ASSERT_TRUE(stylusInfo);
2322
2323 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2324
2325 const auto touchscreenId = mDeviceInfo.getId();
2326
2327 // Set a pressure value on the stylus. It doesn't generate any events.
2328 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2329 stylus->setPressure(100);
2330 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2331
2332 // Start a finger gesture, and ensure it shows up as stylus gesture
2333 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002334 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002335 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002336 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002337 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002338 mDevice->sendSync();
2339 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2340 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002341 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002342 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002343
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002344 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2345 // event with the updated pressure.
2346 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002347 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2348 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002349 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002350 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002351
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002352 // The external stylus did not generate any events.
2353 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2354 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2355}
2356
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002357TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002358 const Point centerPoint = mDevice->getCenterPoint();
2359
2360 // Create an external stylus capable of reporting pressure data that
2361 // should be fused with a touch pointer.
2362 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2363 createUinputDevice<UinputExternalStylusWithPressure>();
2364 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2365 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2366 const auto stylusInfo = findDeviceByName(stylus->getName());
2367 ASSERT_TRUE(stylusInfo);
2368
2369 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2370
2371 const auto touchscreenId = mDeviceInfo.getId();
2372
2373 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2374 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002375 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2376 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002377 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002378 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002379
2380 // Start a finger gesture. The touch device will withhold generating any touches for
2381 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2382 mDevice->sendSlot(FIRST_SLOT);
2383 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2384 mDevice->sendToolType(MT_TOOL_FINGER);
2385 mDevice->sendDown(centerPoint);
2386 auto waitUntil = std::chrono::system_clock::now() +
2387 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002388 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002389 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002390
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002391 // Since the external stylus did not report a pressure value within the timeout,
2392 // it shows up as a finger pointer.
2393 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2394 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002395 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002396 WithPressure(1.f))));
2397
2398 // Change the pressure on the external stylus. Since the pressure was not present at the start
2399 // of the gesture, it is ignored for now.
2400 stylus->setPressure(200);
2401 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2402
2403 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002404 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2405 mDevice->sendSync();
2406 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2407 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002408 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002409
2410 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2411 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2412 mDevice->sendToolType(MT_TOOL_FINGER);
2413 mDevice->sendDown(centerPoint);
2414 mDevice->sendSync();
2415 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2416 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002417 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002418 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2419
2420 // The external stylus did not generate any events.
2421 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2422 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002423}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002424
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002425TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002426 const Point centerPoint = mDevice->getCenterPoint();
2427
2428 // Create an external stylus device that does not support pressure. It should not affect any
2429 // touch pointers.
2430 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2431 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2432 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2433 const auto stylusInfo = findDeviceByName(stylus->getName());
2434 ASSERT_TRUE(stylusInfo);
2435
2436 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2437
2438 const auto touchscreenId = mDeviceInfo.getId();
2439
2440 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2441 // pressure data from the external stylus.
2442 mDevice->sendSlot(FIRST_SLOT);
2443 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2444 mDevice->sendToolType(MT_TOOL_FINGER);
2445 mDevice->sendDown(centerPoint);
2446 auto waitUntil = std::chrono::system_clock::now() +
2447 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2448 mDevice->sendSync();
2449 ASSERT_NO_FATAL_FAILURE(
2450 mTestListener
2451 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2452 WithToolType(
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002453 ToolType::FINGER),
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002454 WithButtonState(0),
2455 WithDeviceId(touchscreenId),
2456 WithPressure(1.f)),
2457 waitUntil));
2458
2459 // The external stylus did not generate any events.
2460 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2461 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2462}
2463
Michael Wrightd02c5b62014-02-10 15:10:22 -08002464// --- InputDeviceTest ---
2465class InputDeviceTest : public testing::Test {
2466protected:
2467 static const char* DEVICE_NAME;
2468 static const char* DEVICE_LOCATION;
2469 static const int32_t DEVICE_ID;
2470 static const int32_t DEVICE_GENERATION;
2471 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002472 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002473 static const int32_t EVENTHUB_ID;
2474 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2475
2476 std::shared_ptr<FakeEventHub> mFakeEventHub;
2477 sp<FakeInputReaderPolicy> mFakePolicy;
2478 std::unique_ptr<TestInputListener> mFakeListener;
2479 std::unique_ptr<InstrumentedInputReader> mReader;
2480 std::shared_ptr<InputDevice> mDevice;
2481
2482 void SetUp() override {
2483 mFakeEventHub = std::make_unique<FakeEventHub>();
2484 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2485 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002486 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002487 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002488 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002489 identifier.name = DEVICE_NAME;
2490 identifier.location = DEVICE_LOCATION;
2491 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2492 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2493 identifier);
2494 mReader->pushNextDevice(mDevice);
2495 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002496 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002497 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002498
2499 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002500 mFakeListener.reset();
2501 mFakePolicy.clear();
2502 }
2503};
2504
2505const char* InputDeviceTest::DEVICE_NAME = "device";
2506const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2507const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2508const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002509const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002510const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2511 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002512const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002513const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2514
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002515TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002516 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002517 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2518 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002519}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002520
Michael Wrightd02c5b62014-02-10 15:10:22 -08002521TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2522 ASSERT_EQ(mDevice->isEnabled(), false);
2523}
2524
2525TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2526 // Configuration.
2527 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002528 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002529
2530 // Reset.
2531 unused += mDevice->reset(ARBITRARY_TIME);
2532
2533 NotifyDeviceResetArgs resetArgs;
2534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2535 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2536 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2537
2538 // Metadata.
2539 ASSERT_TRUE(mDevice->isIgnored());
2540 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2541
2542 InputDeviceInfo info = mDevice->getDeviceInfo();
2543 ASSERT_EQ(DEVICE_ID, info.getId());
2544 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2545 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2546 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2547
2548 // State queries.
2549 ASSERT_EQ(0, mDevice->getMetaState());
2550
2551 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2552 << "Ignored device should return unknown key code state.";
2553 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2554 << "Ignored device should return unknown scan code state.";
2555 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2556 << "Ignored device should return unknown switch state.";
2557
2558 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2559 uint8_t flags[2] = { 0, 1 };
2560 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2561 << "Ignored device should never mark any key codes.";
2562 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2563 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2564}
2565
2566TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2567 // Configuration.
2568 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2569
2570 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002571 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2572 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002573 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2574 mapper1.setMetaState(AMETA_ALT_ON);
2575 mapper1.addSupportedKeyCode(AKEYCODE_A);
2576 mapper1.addSupportedKeyCode(AKEYCODE_B);
2577 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2578 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2579 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2580 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2581 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2582
2583 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002584 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2585 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002586 mapper2.setMetaState(AMETA_SHIFT_ON);
2587
2588 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002589 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002590
Harry Cuttsf13161a2023-03-08 14:15:49 +00002591 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2592 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002593 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002594 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002595
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002596 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2597 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002598
2599 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002600 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002601 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2602 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002603
2604 NotifyDeviceResetArgs resetArgs;
2605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2606 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2607 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2608
2609 // Metadata.
2610 ASSERT_FALSE(mDevice->isIgnored());
2611 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2612
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002613 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002614 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002615 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2617 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2618
2619 // State queries.
2620 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2621 << "Should query mappers and combine meta states.";
2622
2623 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2624 << "Should return unknown key code state when source not supported.";
2625 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2626 << "Should return unknown scan code state when source not supported.";
2627 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2628 << "Should return unknown switch state when source not supported.";
2629
2630 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2631 << "Should query mapper when source is supported.";
2632 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2633 << "Should query mapper when source is supported.";
2634 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2635 << "Should query mapper when source is supported.";
2636
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002637 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002638 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002639 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002640 << "Should do nothing when source is unsupported.";
2641 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2642 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2643 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2644 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2645
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002646 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002647 << "Should query mapper when source is supported.";
2648 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2649 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2650 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2651 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2652
2653 // Event handling.
2654 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002655 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002656 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002657
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002658 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2659 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002660}
2661
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -07002662TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2663 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2664 FakeInputMapper& mapper =
2665 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2666 AINPUT_SOURCE_KEYBOARD);
2667 NotifyMotionArgs args1;
2668 NotifySwitchArgs args2;
2669 NotifyKeyArgs args3;
2670 mapper.setProcessResult({args1, args2, args3});
2671
2672 InputReaderConfiguration config;
2673 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2674
2675 RawEvent event;
2676 event.deviceId = EVENTHUB_ID;
2677 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2678
2679 for (auto& arg : notifyArgs) {
2680 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2681 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2682 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2683 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2684 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2685 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2686 }
2687 }
2688}
2689
2690TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2691 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2692 FakeInputMapper& mapper =
2693 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2694 AINPUT_SOURCE_KEYBOARD);
2695 NotifyMotionArgs args;
2696 mapper.setProcessResult({args});
2697
2698 InputReaderConfiguration config;
2699 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2700
2701 RawEvent event;
2702 event.deviceId = EVENTHUB_ID;
2703 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2704
2705 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2706 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2707}
2708
2709TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2710 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2711 FakeInputMapper& mapper =
2712 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2713 AINPUT_SOURCE_KEYBOARD);
2714 NotifyMotionArgs args;
2715 args.policyFlags = POLICY_FLAG_WAKE;
2716 mapper.setProcessResult({args});
2717
2718 InputReaderConfiguration config;
2719 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2720
2721 RawEvent event;
2722 event.deviceId = EVENTHUB_ID;
2723 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2724
2725 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2726 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2727}
2728
Arthur Hung2c9a3342019-07-23 14:18:59 +08002729// A single input device is associated with a specific display. Check that:
2730// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh48189772023-05-30 14:12:49 +00002731// 2. Device is disabled when configure API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002732TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002733 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2734 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002735
2736 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002737 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002738 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2739 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002740
2741 // Device should be enabled by default.
2742 ASSERT_TRUE(mDevice->isEnabled());
2743
2744 // Prepare associated info.
2745 constexpr uint8_t hdmi = 1;
2746 const std::string UNIQUE_ID = "local:1";
2747
2748 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002749 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002750 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002751 // Device should be disabled because it is associated with a specific display via
2752 // input port <-> display port association, but the corresponding display is not found
2753 ASSERT_FALSE(mDevice->isEnabled());
2754
2755 // Prepare displays.
2756 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002757 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002758 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002759 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002760 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002761 ASSERT_TRUE(mDevice->isEnabled());
2762
2763 // Device should be disabled after set disable.
2764 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002765 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002766 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002767 ASSERT_FALSE(mDevice->isEnabled());
2768
2769 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002770 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002771 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002772 ASSERT_FALSE(mDevice->isEnabled());
2773}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002774
Christine Franks1ba71cc2021-04-07 14:37:42 -07002775TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2776 // Device should be enabled by default.
2777 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002778 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2779 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002780 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002781 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2782 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002783 ASSERT_TRUE(mDevice->isEnabled());
2784
2785 // Device should be disabled because it is associated with a specific display, but the
2786 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002787 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002788 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002789 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002790 ASSERT_FALSE(mDevice->isEnabled());
2791
2792 // Device should be enabled when a display is found.
2793 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002794 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002795 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002796 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002797 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002798 ASSERT_TRUE(mDevice->isEnabled());
2799
2800 // Device should be disabled after set disable.
2801 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002802 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002803 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002804 ASSERT_FALSE(mDevice->isEnabled());
2805
2806 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002807 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002808 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002809 ASSERT_FALSE(mDevice->isEnabled());
2810}
2811
Christine Franks2a2293c2022-01-18 11:51:16 -08002812TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2813 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002814 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2815 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002816 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002817 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2818 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002819
Christine Franks2a2293c2022-01-18 11:51:16 -08002820 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2821 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002822 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002823 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002824 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002825 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002826 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2827}
2828
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002829/**
2830 * This test reproduces a crash caused by a dangling reference that remains after device is added
2831 * and removed. The reference is accessed in InputDevice::dump(..);
2832 */
2833TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2834 constexpr int32_t TEST_EVENTHUB_ID = 10;
2835 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2836
Harry Cutts33476232023-01-30 19:57:29 +00002837 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh7f1765e2023-07-07 13:12:37 +00002838 device.addEventHubDevice(TEST_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002839 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2840 std::string dumpStr, eventHubDevStr;
2841 device.dump(dumpStr, eventHubDevStr);
2842}
2843
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002844TEST_F(InputDeviceTest, GetBluetoothAddress) {
2845 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2846 ASSERT_TRUE(address);
2847 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2848}
2849
Michael Wrightd02c5b62014-02-10 15:10:22 -08002850// --- SwitchInputMapperTest ---
2851
2852class SwitchInputMapperTest : public InputMapperTest {
2853protected:
2854};
2855
2856TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002857 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002858
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002859 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002860}
2861
2862TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002863 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002864
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002865 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002866 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002867
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002868 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002869 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002870}
2871
2872TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002873 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002874 std::list<NotifyArgs> out;
2875 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2876 ASSERT_TRUE(out.empty());
2877 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2878 ASSERT_TRUE(out.empty());
2879 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2880 ASSERT_TRUE(out.empty());
2881 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002882
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002883 ASSERT_EQ(1u, out.size());
2884 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002885 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002886 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2887 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002888 args.switchMask);
2889 ASSERT_EQ(uint32_t(0), args.policyFlags);
2890}
2891
Chris Ye87143712020-11-10 05:05:58 +00002892// --- VibratorInputMapperTest ---
2893class VibratorInputMapperTest : public InputMapperTest {
2894protected:
2895 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2896};
2897
2898TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002899 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002900
2901 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2902}
2903
2904TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002905 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002906
2907 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2908}
2909
2910TEST_F(VibratorInputMapperTest, Vibrate) {
2911 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002912 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00002913 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002914
2915 VibrationElement pattern(2);
2916 VibrationSequence sequence(2);
2917 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002918 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2919 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002920 sequence.addElement(pattern);
2921 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002922 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2923 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002924 sequence.addElement(pattern);
2925
2926 std::vector<int64_t> timings = {0, 1};
2927 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2928
2929 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002930 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002931 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002932 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002933 // Verify vibrator state listener was notified.
2934 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002935 ASSERT_EQ(1u, out.size());
2936 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2937 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2938 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002939 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002940 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002941 ASSERT_FALSE(mapper.isVibrating());
2942 // Verify vibrator state listener was notified.
2943 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002944 ASSERT_EQ(1u, out.size());
2945 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2946 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2947 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002948}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002949
Chris Yef59a2f42020-10-16 12:55:26 -07002950// --- SensorInputMapperTest ---
2951
2952class SensorInputMapperTest : public InputMapperTest {
2953protected:
2954 static const int32_t ACCEL_RAW_MIN;
2955 static const int32_t ACCEL_RAW_MAX;
2956 static const int32_t ACCEL_RAW_FUZZ;
2957 static const int32_t ACCEL_RAW_FLAT;
2958 static const int32_t ACCEL_RAW_RESOLUTION;
2959
2960 static const int32_t GYRO_RAW_MIN;
2961 static const int32_t GYRO_RAW_MAX;
2962 static const int32_t GYRO_RAW_FUZZ;
2963 static const int32_t GYRO_RAW_FLAT;
2964 static const int32_t GYRO_RAW_RESOLUTION;
2965
2966 static const float GRAVITY_MS2_UNIT;
2967 static const float DEGREE_RADIAN_UNIT;
2968
2969 void prepareAccelAxes();
2970 void prepareGyroAxes();
2971 void setAccelProperties();
2972 void setGyroProperties();
2973 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2974};
2975
2976const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2977const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2978const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2979const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2980const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2981
2982const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2983const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2984const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2985const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2986const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2987
2988const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2989const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2990
2991void SensorInputMapperTest::prepareAccelAxes() {
2992 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2993 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2994 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2995 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2996 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2997 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2998}
2999
3000void SensorInputMapperTest::prepareGyroAxes() {
3001 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3002 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3003 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3004 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3005 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3006 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3007}
3008
3009void SensorInputMapperTest::setAccelProperties() {
3010 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3011 /* sensorDataIndex */ 0);
3012 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3013 /* sensorDataIndex */ 1);
3014 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3015 /* sensorDataIndex */ 2);
3016 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3017 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3018 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3019 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3020 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3021}
3022
3023void SensorInputMapperTest::setGyroProperties() {
3024 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3025 /* sensorDataIndex */ 0);
3026 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3027 /* sensorDataIndex */ 1);
3028 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3029 /* sensorDataIndex */ 2);
3030 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3031 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3032 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3033 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3034 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3035}
3036
3037TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00003038 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003039
3040 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3041}
3042
3043TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3044 setAccelProperties();
3045 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003046 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003047
3048 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3049 std::chrono::microseconds(10000),
3050 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003051 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3054 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3055 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3056 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003057
3058 NotifySensorArgs args;
3059 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3060 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3061 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3062
3063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3064 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3065 ASSERT_EQ(args.deviceId, DEVICE_ID);
3066 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3067 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3068 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3069 ASSERT_EQ(args.values, values);
3070 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3071}
3072
3073TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3074 setGyroProperties();
3075 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003076 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003077
3078 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3079 std::chrono::microseconds(10000),
3080 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003081 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3084 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3085 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3086 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003087
3088 NotifySensorArgs args;
3089 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3090 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3091 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3092
3093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3094 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3095 ASSERT_EQ(args.deviceId, DEVICE_ID);
3096 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3097 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3098 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3099 ASSERT_EQ(args.values, values);
3100 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3101}
3102
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103// --- KeyboardInputMapperTest ---
3104
3105class KeyboardInputMapperTest : public InputMapperTest {
3106protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003107 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003108 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003109 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003110
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003111 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003112 int32_t originalKeyCode, int32_t rotatedKeyCode,
3113 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003114};
3115
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003116/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3117 * orientation.
3118 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003119void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003120 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3121 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003122}
3123
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003124void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003125 int32_t originalScanCode, int32_t originalKeyCode,
3126 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003127 NotifyKeyArgs args;
3128
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003129 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3131 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3132 ASSERT_EQ(originalScanCode, args.scanCode);
3133 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003134 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003135
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3138 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3139 ASSERT_EQ(originalScanCode, args.scanCode);
3140 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003141 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003142}
3143
Michael Wrightd02c5b62014-02-10 15:10:22 -08003144TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003145 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003146 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003147 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003148
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003149 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003150}
3151
3152TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3153 const int32_t USAGE_A = 0x070004;
3154 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003155 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3156 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003157 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3158 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3159 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003160
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003161 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003162 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003163 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003164 // Initial metastate is AMETA_NONE.
3165 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003166
3167 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003169 NotifyKeyArgs args;
3170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3171 ASSERT_EQ(DEVICE_ID, args.deviceId);
3172 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3173 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3174 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3175 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3176 ASSERT_EQ(KEY_HOME, args.scanCode);
3177 ASSERT_EQ(AMETA_NONE, args.metaState);
3178 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3179 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3180 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3181
3182 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003183 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3185 ASSERT_EQ(DEVICE_ID, args.deviceId);
3186 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3187 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3188 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3189 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3190 ASSERT_EQ(KEY_HOME, args.scanCode);
3191 ASSERT_EQ(AMETA_NONE, args.metaState);
3192 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3193 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3194 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3195
3196 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3198 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3200 ASSERT_EQ(DEVICE_ID, args.deviceId);
3201 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3202 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3203 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3204 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3205 ASSERT_EQ(0, args.scanCode);
3206 ASSERT_EQ(AMETA_NONE, args.metaState);
3207 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3208 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3209 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3210
3211 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003212 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3213 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3215 ASSERT_EQ(DEVICE_ID, args.deviceId);
3216 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3217 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3218 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3219 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3220 ASSERT_EQ(0, args.scanCode);
3221 ASSERT_EQ(AMETA_NONE, args.metaState);
3222 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3223 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3224 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3225
3226 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003227 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3228 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3230 ASSERT_EQ(DEVICE_ID, args.deviceId);
3231 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3232 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3233 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3234 ASSERT_EQ(0, args.keyCode);
3235 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3236 ASSERT_EQ(AMETA_NONE, args.metaState);
3237 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3238 ASSERT_EQ(0U, args.policyFlags);
3239 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3240
3241 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003242 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3243 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3245 ASSERT_EQ(DEVICE_ID, args.deviceId);
3246 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3247 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3248 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3249 ASSERT_EQ(0, args.keyCode);
3250 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3251 ASSERT_EQ(AMETA_NONE, args.metaState);
3252 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3253 ASSERT_EQ(0U, args.policyFlags);
3254 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3255}
3256
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003257TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3258 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3259 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3260 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3261
3262 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003263 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003264 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3265
3266 // Key down by scan code.
3267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3268 NotifyKeyArgs args;
3269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3270 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3271
3272 // Key up by scan code.
3273 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3275 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3276}
3277
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003278/**
3279 * Ensure that the readTime is set to the time when the EV_KEY is received.
3280 */
3281TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3282 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3283
3284 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003285 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003286 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3287 NotifyKeyArgs args;
3288
3289 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003290 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3292 ASSERT_EQ(12, args.readTime);
3293
3294 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003295 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3297 ASSERT_EQ(15, args.readTime);
3298}
3299
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003301 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3302 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003303 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3304 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3305 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003306
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003307 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003308 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003309 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003310
Arthur Hung95f68612022-04-07 14:08:22 +08003311 // Initial metastate is AMETA_NONE.
3312 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003313
3314 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003316 NotifyKeyArgs args;
3317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3318 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003319 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003320 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003321
3322 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003323 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3325 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003326 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327
3328 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003329 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3331 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003332 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333
3334 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003335 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3337 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003338 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003339 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340}
3341
3342TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003343 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3344 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3345 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3346 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003347
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003348 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003349 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003350 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003351
Michael Wrighta9cf4192022-12-01 23:46:39 +00003352 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3354 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3355 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3356 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3357 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3358 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3359 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3360 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3361}
3362
3363TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003364 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3365 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3366 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3367 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003368
Michael Wrightd02c5b62014-02-10 15:10:22 -08003369 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003370 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003371 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003372 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003373
Michael Wrighta9cf4192022-12-01 23:46:39 +00003374 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003375 ASSERT_NO_FATAL_FAILURE(
3376 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3377 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3378 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3379 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3380 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3381 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3382 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003383
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003384 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003385 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003386 ASSERT_NO_FATAL_FAILURE(
3387 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3388 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3389 AKEYCODE_DPAD_UP, DISPLAY_ID));
3390 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3391 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3392 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3393 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003394
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003395 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003396 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003397 ASSERT_NO_FATAL_FAILURE(
3398 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3399 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3400 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3401 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3402 AKEYCODE_DPAD_UP, DISPLAY_ID));
3403 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3404 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003405
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003406 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003407 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003408 ASSERT_NO_FATAL_FAILURE(
3409 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3410 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3411 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3412 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3413 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3414 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3415 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003416
3417 // Special case: if orientation changes while key is down, we still emit the same keycode
3418 // in the key up as we did in the key down.
3419 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003420 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003421 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003422 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3424 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3425 ASSERT_EQ(KEY_UP, args.scanCode);
3426 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3427
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003428 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003429 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3432 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3433 ASSERT_EQ(KEY_UP, args.scanCode);
3434 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3435}
3436
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003437TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3438 // If the keyboard is not orientation aware,
3439 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003440 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003441
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003442 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003443 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003444 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003445 NotifyKeyArgs args;
3446
3447 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3452 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3453
Michael Wrighta9cf4192022-12-01 23:46:39 +00003454 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3459 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3460}
3461
3462TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3463 // If the keyboard is orientation aware,
3464 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003465 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003466
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003467 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003468 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003469 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003470 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003471 NotifyKeyArgs args;
3472
3473 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3474 // ^--- already checked by the previous test
3475
Michael Wrighta9cf4192022-12-01 23:46:39 +00003476 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003477 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3482 ASSERT_EQ(DISPLAY_ID, args.displayId);
3483
3484 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003485 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003486 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003487 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003488 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3492 ASSERT_EQ(newDisplayId, args.displayId);
3493}
3494
Michael Wrightd02c5b62014-02-10 15:10:22 -08003495TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003496 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003497 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003498 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003499
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003500 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003501 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003502
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003503 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003504 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003505}
3506
Philip Junker4af3b3d2021-12-14 10:36:55 +01003507TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3508 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003509 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003510 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3511
3512 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3513 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3514 << "If a mapping is available, the result is equal to the mapping";
3515
3516 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3517 << "If no mapping is available, the result is the key location";
3518}
3519
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003521 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003522 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003523 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003525 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003526 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003528 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003529 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003530}
3531
3532TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003533 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003534 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003535 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003536
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003537 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003538
Michael Wrightd02c5b62014-02-10 15:10:22 -08003539 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003540 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003541 ASSERT_TRUE(flags[0]);
3542 ASSERT_FALSE(flags[1]);
3543}
3544
3545TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003546 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3547 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3548 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3549 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3550 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3551 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003552
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003553 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003554 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003555 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003556 // Initial metastate is AMETA_NONE.
3557 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003558
3559 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003560 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3561 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3562 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003563
3564 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003565 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003567 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3568 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3569 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003570 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003571
3572 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003573 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3574 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003575 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3576 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3577 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003578 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003579
3580 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003581 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3582 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003583 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3584 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3585 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003586 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587
3588 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3590 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003591 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3592 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3593 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003594 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003595
3596 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003597 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3598 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003599 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3600 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3601 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003602 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003603
3604 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003605 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3606 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003607 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3608 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3609 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003610 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003611}
3612
Chris Yea52ade12020-08-27 16:49:20 -07003613TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3614 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3615 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3616 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3617 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3618
3619 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003620 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003621 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3622
Chris Yea52ade12020-08-27 16:49:20 -07003623 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003624 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003625 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3626 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3627 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3628 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3629
3630 NotifyKeyArgs args;
3631 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003632 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3634 ASSERT_EQ(AMETA_NONE, args.metaState);
3635 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3636 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3637 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3638
3639 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003640 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3642 ASSERT_EQ(AMETA_NONE, args.metaState);
3643 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3644 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3645 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3646}
3647
Arthur Hung2c9a3342019-07-23 14:18:59 +08003648TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3649 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003650 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3651 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3652 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3653 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003654
3655 // keyboard 2.
3656 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003657 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003658 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003659 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003660 std::shared_ptr<InputDevice> device2 =
3661 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003662 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003663
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003664 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3665 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3666 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3667 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003668
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003669 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003670 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003671 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003672
Arpit Singh67ca6842023-04-26 14:43:16 +00003673 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003674 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003675 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3676 mFakePolicy
3677 ->getReaderConfiguration(),
3678 AINPUT_SOURCE_KEYBOARD,
3679 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003680 std::list<NotifyArgs> unused =
3681 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003682 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003683 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003684
3685 // Prepared displays and associated info.
3686 constexpr uint8_t hdmi1 = 0;
3687 constexpr uint8_t hdmi2 = 1;
3688 const std::string SECONDARY_UNIQUE_ID = "local:1";
3689
3690 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3691 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3692
3693 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003694 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003695 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003696 ASSERT_FALSE(device2->isEnabled());
3697
3698 // Prepare second display.
3699 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003700 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003701 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003702 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003703 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003704 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003705 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003706 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003707
3708 // Device should be enabled after the associated display is found.
3709 ASSERT_TRUE(mDevice->isEnabled());
3710 ASSERT_TRUE(device2->isEnabled());
3711
3712 // Test pad key events
3713 ASSERT_NO_FATAL_FAILURE(
3714 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3715 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3716 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3717 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3718 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3719 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3720 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3721
3722 ASSERT_NO_FATAL_FAILURE(
3723 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3724 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3725 AKEYCODE_DPAD_RIGHT, newDisplayId));
3726 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3727 AKEYCODE_DPAD_DOWN, newDisplayId));
3728 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3729 AKEYCODE_DPAD_LEFT, newDisplayId));
3730}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003731
arthurhungc903df12020-08-11 15:08:42 +08003732TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3733 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3734 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3735 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3736 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3737 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3738 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3739
3740 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003741 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003742 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003743 // Initial metastate is AMETA_NONE.
3744 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003745
3746 // Initialization should have turned all of the lights off.
3747 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3748 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3749 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3750
3751 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003752 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003754 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3755 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3756
3757 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003758 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003760 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3761 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3762
3763 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003764 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3765 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003766 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3767 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3768
3769 mFakeEventHub->removeDevice(EVENTHUB_ID);
3770 mReader->loopOnce();
3771
3772 // keyboard 2 should default toggle keys.
3773 const std::string USB2 = "USB2";
3774 const std::string DEVICE_NAME2 = "KEYBOARD2";
3775 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3776 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3777 std::shared_ptr<InputDevice> device2 =
3778 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003779 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003780 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3781 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3782 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3783 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3784 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3785 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3786
Arpit Singh67ca6842023-04-26 14:43:16 +00003787 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003788 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003789 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3790 mFakePolicy
3791 ->getReaderConfiguration(),
3792 AINPUT_SOURCE_KEYBOARD,
3793 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003794 std::list<NotifyArgs> unused =
3795 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003796 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003797 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003798
3799 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3800 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3801 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003802 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3803 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003804}
3805
Arthur Hungcb40a002021-08-03 14:31:01 +00003806TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3807 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3808 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3809 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3810
3811 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh67ca6842023-04-26 14:43:16 +00003812 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003813 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3814 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003815 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003816 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003817 // Initial metastate is AMETA_NONE.
3818 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003819
3820 mReader->toggleCapsLockState(DEVICE_ID);
3821 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3822}
3823
Arthur Hungfb3cc112022-04-13 07:39:50 +00003824TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3825 // keyboard 1.
3826 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3827 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3828 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3829 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3830 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3831 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3832
3833 KeyboardInputMapper& mapper1 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003834 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003835 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3836
3837 // keyboard 2.
3838 const std::string USB2 = "USB2";
3839 const std::string DEVICE_NAME2 = "KEYBOARD2";
3840 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3841 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3842 std::shared_ptr<InputDevice> device2 =
3843 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3844 ftl::Flags<InputDeviceClass>(0));
3845 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3846 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3847 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3848 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3849 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3850 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3851
Arpit Singh67ca6842023-04-26 14:43:16 +00003852 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003853 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003854 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3855 mFakePolicy
3856 ->getReaderConfiguration(),
3857 AINPUT_SOURCE_KEYBOARD,
3858 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003859 std::list<NotifyArgs> unused =
3860 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003861 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003862 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003863
Arthur Hung95f68612022-04-07 14:08:22 +08003864 // Initial metastate is AMETA_NONE.
3865 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3866 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3867
3868 // Toggle num lock on and off.
3869 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3870 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003871 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3872 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3873 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3874
3875 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3876 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3877 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3878 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3879 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3880
3881 // Toggle caps lock on and off.
3882 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3883 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3884 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3885 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3886 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3887
3888 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3889 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3890 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3891 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3892 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3893
3894 // Toggle scroll lock on and off.
3895 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3896 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3897 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3898 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3899 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3900
3901 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3902 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3903 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3904 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3905 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3906}
3907
Arthur Hung2141d542022-08-23 07:45:21 +00003908TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3909 const int32_t USAGE_A = 0x070004;
3910 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3911 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3912
3913 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003914 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00003915 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3916 // Key down by scan code.
3917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3918 NotifyKeyArgs args;
3919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3920 ASSERT_EQ(DEVICE_ID, args.deviceId);
3921 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3922 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3923 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3924 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3925 ASSERT_EQ(KEY_HOME, args.scanCode);
3926 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3927
3928 // Disable device, it should synthesize cancellation events for down events.
3929 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003930 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00003931
3932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3933 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3934 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3935 ASSERT_EQ(KEY_HOME, args.scanCode);
3936 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3937}
3938
Zixuan Qufecb6062022-11-12 04:44:31 +00003939TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh67ca6842023-04-26 14:43:16 +00003940 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3941 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00003942 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003943 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3944 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00003945
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003946 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00003947 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3948
3949 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003950 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00003951
3952 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3953 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3954 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3955 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3956 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003957 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
3958
3959 // Call change layout association with the same values: Generation shouldn't change
3960 generation = mReader->getContext()->getGeneration();
3961 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3962 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3963 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
3964 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00003965}
3966
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003967TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3968 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3969 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3970
3971 // Configuration
Arpit Singh67ca6842023-04-26 14:43:16 +00003972 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003973 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3974 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003975 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003976
3977 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3978 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3979}
3980
Justin Chung71ddb432023-03-27 04:29:07 +00003981TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
3982 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
3983 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003984 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Justin Chung71ddb432023-03-27 04:29:07 +00003985 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3986 NotifyKeyArgs args;
3987
3988 // Key down
3989 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
3990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3991 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
3992}
3993
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003994// --- KeyboardInputMapperTest_ExternalDevice ---
3995
3996class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3997protected:
Chris Yea52ade12020-08-27 16:49:20 -07003998 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003999};
4000
4001TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004002 // For external devices, keys will trigger wake on key down. Media keys should also trigger
4003 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07004004
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004005 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4006 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4007 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4008 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004009
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004010 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004011 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004012 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004013
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004015 NotifyKeyArgs args;
4016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4017 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4018
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004019 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4021 ASSERT_EQ(uint32_t(0), args.policyFlags);
4022
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004025 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07004026
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004027 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4029 ASSERT_EQ(uint32_t(0), args.policyFlags);
4030
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4033 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4034
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004035 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4037 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4038}
4039
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004040TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004041 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004042
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004043 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4044 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4045 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004046
Powei Fengd041c5d2019-05-03 17:11:33 -07004047 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004048 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004049 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004050 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004051
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004053 NotifyKeyArgs args;
4054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4055 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4056
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004057 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4059 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4060
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4063 ASSERT_EQ(uint32_t(0), args.policyFlags);
4064
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004065 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4067 ASSERT_EQ(uint32_t(0), args.policyFlags);
4068
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004069 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4071 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4072
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004073 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4075 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4076}
4077
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078// --- CursorInputMapperTest ---
4079
4080class CursorInputMapperTest : public InputMapperTest {
4081protected:
4082 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4083
Michael Wright17db18e2020-06-26 20:51:44 +01004084 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004085
Chris Yea52ade12020-08-27 16:49:20 -07004086 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087 InputMapperTest::SetUp();
4088
Michael Wright17db18e2020-06-26 20:51:44 +01004089 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004090 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091 }
4092
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004093 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4094 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004095
Michael Wrighta9cf4192022-12-01 23:46:39 +00004096 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004097 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4098 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4099 }
4100
4101 void prepareSecondaryDisplay() {
4102 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004103 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004104 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004105 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004106
4107 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4108 float pressure) {
4109 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4110 0.0f, 0.0f, 0.0f, EPSILON));
4111 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112};
4113
4114const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4115
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004116void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4117 int32_t originalY, int32_t rotatedX,
4118 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004119 NotifyMotionArgs args;
4120
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4125 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004126 ASSERT_NO_FATAL_FAILURE(
4127 assertCursorPointerCoords(args.pointerCoords[0],
4128 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4129 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130}
4131
4132TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004133 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004134 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004135
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004136 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004137}
4138
4139TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004141 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004143 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144}
4145
4146TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004147 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004148 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004149
4150 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004151 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152
4153 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004154 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4155 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004156 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4157 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4158
4159 // When the bounds are set, then there should be a valid motion range.
4160 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4161
4162 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004163 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004164
4165 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4166 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4167 1, 800 - 1, 0.0f, 0.0f));
4168 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4169 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4170 2, 480 - 1, 0.0f, 0.0f));
4171 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4172 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4173 0.0f, 1.0f, 0.0f, 0.0f));
4174}
4175
4176TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004178 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179
4180 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004181 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004182
4183 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4184 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4185 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4186 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4187 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4188 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4189 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4190 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4191 0.0f, 1.0f, 0.0f, 0.0f));
4192}
4193
4194TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004196 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004197
arthurhungdcef2dc2020-08-11 14:47:50 +08004198 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199
4200 NotifyMotionArgs args;
4201
4202 // Button press.
4203 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4207 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4208 ASSERT_EQ(DEVICE_ID, args.deviceId);
4209 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4210 ASSERT_EQ(uint32_t(0), args.policyFlags);
4211 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4212 ASSERT_EQ(0, args.flags);
4213 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4214 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4215 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004216 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004218 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004219 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004220 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4221 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4222 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4223
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4225 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4226 ASSERT_EQ(DEVICE_ID, args.deviceId);
4227 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4228 ASSERT_EQ(uint32_t(0), args.policyFlags);
4229 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4230 ASSERT_EQ(0, args.flags);
4231 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4232 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4233 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004234 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004235 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004236 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004237 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004238 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4239 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4240 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4241
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004243 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4244 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4246 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4247 ASSERT_EQ(DEVICE_ID, args.deviceId);
4248 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4249 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004250 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4251 ASSERT_EQ(0, args.flags);
4252 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4253 ASSERT_EQ(0, args.buttonState);
4254 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004255 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004256 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004257 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004258 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004259 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4260 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4261 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4262
4263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4264 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4265 ASSERT_EQ(DEVICE_ID, args.deviceId);
4266 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4267 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4269 ASSERT_EQ(0, args.flags);
4270 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4271 ASSERT_EQ(0, args.buttonState);
4272 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004273 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004275 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004276 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4278 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4279 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4280}
4281
4282TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004284 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004285
4286 NotifyMotionArgs args;
4287
4288 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4292 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004293 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4294 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4295 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296
4297 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4299 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4301 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004302 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4303 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304}
4305
4306TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004308 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309
4310 NotifyMotionArgs args;
4311
4312 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4316 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004317 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4320 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004321 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004322
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004324 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004327 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004328 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004329
4330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004332 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004333}
4334
4335TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004337 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338
4339 NotifyMotionArgs args;
4340
4341 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4343 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4345 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4347 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004348 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4349 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4350 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4353 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004354 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4355 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4356 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004357
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004359 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4360 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4361 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4363 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004364 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4365 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4366 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004367
4368 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4370 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004372 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004373 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004374
4375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004377 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004378}
4379
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004380TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004381 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004383 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4384 // need to be rotated.
4385 addConfigurationProperty("cursor.orientationAware", "1");
Arpit Singhe036ad72023-04-27 12:48:15 +00004386 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004387
Michael Wrighta9cf4192022-12-01 23:46:39 +00004388 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4390 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4391 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4392 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4393 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4394 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4395 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4396 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4397}
4398
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004399TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004400 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004401 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004402 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4403 // orientation-aware are affected by display rotation.
Arpit Singhe036ad72023-04-27 12:48:15 +00004404 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004405
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004406 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004407 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004408 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4409 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4410 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4411 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4412 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4413 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4414 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4415 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4416
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004417 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004418 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004419 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4420 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4421 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4422 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4423 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4424 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4425 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4426 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004428 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004429 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4431 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4432 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4433 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4434 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4435 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4436 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4437 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4438
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004439 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004440 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004441 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4442 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4443 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4444 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4445 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4446 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4447 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4448 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004449}
4450
4451TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004453 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454
4455 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4456 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457
4458 NotifyMotionArgs motionArgs;
4459 NotifyKeyArgs keyArgs;
4460
4461 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004462 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4463 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4465 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4466 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004467 ASSERT_NO_FATAL_FAILURE(
4468 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4471 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4472 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004473 ASSERT_NO_FATAL_FAILURE(
4474 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004475
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004479 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004480 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004481 ASSERT_NO_FATAL_FAILURE(
4482 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004483
4484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004485 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004487 ASSERT_NO_FATAL_FAILURE(
4488 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004489
4490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004492 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004493 ASSERT_NO_FATAL_FAILURE(
4494 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004495
4496 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004497 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4498 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4499 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4501 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4502 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004503 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004504 ASSERT_NO_FATAL_FAILURE(
4505 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4508 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4509 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004510 ASSERT_NO_FATAL_FAILURE(
4511 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004512
4513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4514 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4515 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004516 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004517 ASSERT_NO_FATAL_FAILURE(
4518 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004519
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4521 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004523 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004524 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004525 ASSERT_NO_FATAL_FAILURE(
4526 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004527
4528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004530 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004531 ASSERT_NO_FATAL_FAILURE(
4532 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004534 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004537 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4538 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004539 ASSERT_NO_FATAL_FAILURE(
4540 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004543
4544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004546 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004547 ASSERT_NO_FATAL_FAILURE(
4548 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004549
Michael Wrightd02c5b62014-02-10 15:10:22 -08004550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4551 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004552 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004553 ASSERT_NO_FATAL_FAILURE(
4554 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555
4556 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4558 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4560 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4561 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004562
Michael Wrightd02c5b62014-02-10 15:10:22 -08004563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004564 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004565 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004566 ASSERT_NO_FATAL_FAILURE(
4567 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004568
4569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4570 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4571 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004572 ASSERT_NO_FATAL_FAILURE(
4573 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004575 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4576 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004578 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004579 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004580 ASSERT_NO_FATAL_FAILURE(
4581 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004582
4583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004585 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004586
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004587 ASSERT_NO_FATAL_FAILURE(
4588 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4590 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4591 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4592
4593 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004594 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4597 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4598 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004599
Michael Wrightd02c5b62014-02-10 15:10:22 -08004600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004601 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004603 ASSERT_NO_FATAL_FAILURE(
4604 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004605
4606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4607 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4608 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004609 ASSERT_NO_FATAL_FAILURE(
4610 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004615 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004617 ASSERT_NO_FATAL_FAILURE(
4618 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004619
4620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4621 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4622 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004623 ASSERT_NO_FATAL_FAILURE(
4624 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004625
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4627 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4628 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4629
4630 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004631 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4632 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4634 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4635 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004636
Michael Wrightd02c5b62014-02-10 15:10:22 -08004637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004638 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004639 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004640 ASSERT_NO_FATAL_FAILURE(
4641 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004642
4643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4644 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4645 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004646 ASSERT_NO_FATAL_FAILURE(
4647 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4650 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004652 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004653 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004654 ASSERT_NO_FATAL_FAILURE(
4655 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004656
4657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4658 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4659 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004660 ASSERT_NO_FATAL_FAILURE(
4661 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004662
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4664 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4665 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4666
4667 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4671 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4672 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004673
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004675 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004676 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004677 ASSERT_NO_FATAL_FAILURE(
4678 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004679
4680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4681 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4682 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004683 ASSERT_NO_FATAL_FAILURE(
4684 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004685
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004689 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004690 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004691 ASSERT_NO_FATAL_FAILURE(
4692 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004693
4694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4695 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4696 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004697 ASSERT_NO_FATAL_FAILURE(
4698 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004699
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4701 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4702 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4703}
4704
4705TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004707 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004708
4709 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4710 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711
4712 NotifyMotionArgs args;
4713
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4715 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4716 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004718 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4719 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4721 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 +00004722 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004723}
4724
4725TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004726 addConfigurationProperty("cursor.mode", "pointer");
4727 mFakePolicy->setPointerCapture(true);
Arpit Singhe036ad72023-04-27 12:48:15 +00004728 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004729
4730 NotifyDeviceResetArgs resetArgs;
4731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4732 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4733 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4734
4735 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4736 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004737
4738 NotifyMotionArgs args;
4739
4740 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004741 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4742 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4743 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4745 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4748 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 +00004749 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004750
4751 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004752 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4755 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4756 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4758 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4760 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4761 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4763 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4764
4765 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004766 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4767 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4769 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4770 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4772 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4774 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4775 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4776 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4777 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4778
4779 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4784 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4785 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4786 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4787 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 +00004788 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004789
4790 // Disable pointer capture and check that the device generation got bumped
4791 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004792 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004793 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004794 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004795 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004796
4797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004798 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4799
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4804 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4806 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4807 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 +00004808 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004809}
4810
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004811/**
4812 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4813 * pointer acceleration or speed processing should not be applied.
4814 */
4815TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4816 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004817 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4818 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004819 mFakePolicy->setVelocityControlParams(testParams);
Arpit Singhe036ad72023-04-27 12:48:15 +00004820 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004821
4822 NotifyDeviceResetArgs resetArgs;
4823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4824 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4825 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4826
4827 NotifyMotionArgs args;
4828
4829 // Move and verify scale is applied.
4830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4834 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4835 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4836 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4837 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4838 ASSERT_GT(relX, 10);
4839 ASSERT_GT(relY, 20);
4840
4841 // Enable Pointer Capture
4842 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004843 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004844 NotifyPointerCaptureChangedArgs captureArgs;
4845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4846 ASSERT_TRUE(captureArgs.request.enable);
4847
4848 // Move and verify scale is not applied.
4849 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4850 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4851 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4853 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4854 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4855 ASSERT_EQ(10, args.pointerCoords[0].getX());
4856 ASSERT_EQ(20, args.pointerCoords[0].getY());
4857}
4858
Prabir Pradhan208360b2022-06-24 18:37:04 +00004859TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4860 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004861 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan208360b2022-06-24 18:37:04 +00004862
4863 NotifyDeviceResetArgs resetArgs;
4864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4865 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4866 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4867
4868 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004869 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004870
4871 NotifyMotionArgs args;
4872
4873 // Verify that the coordinates are rotated.
4874 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4875 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4876 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4878 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4879 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4880 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4881 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4882
4883 // Enable Pointer Capture.
4884 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004885 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004886 NotifyPointerCaptureChangedArgs captureArgs;
4887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4888 ASSERT_TRUE(captureArgs.request.enable);
4889
4890 // Move and verify rotation is not applied.
4891 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4895 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4897 ASSERT_EQ(10, args.pointerCoords[0].getX());
4898 ASSERT_EQ(20, args.pointerCoords[0].getY());
4899}
4900
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004901TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004902 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004903
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004904 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004905 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004906
4907 // Set up the secondary display as the display on which the pointer should be shown.
4908 // The InputDevice is not associated with any display.
4909 prepareSecondaryDisplay();
4910 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004911 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004912
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004913 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004914 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004915
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004916 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4918 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4919 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004921 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4922 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4923 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004924 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004925}
4926
4927TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004928 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004929
4930 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004931 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004932
4933 // Set up the secondary display as the display on which the pointer should be shown,
4934 // and associate the InputDevice with the secondary display.
4935 prepareSecondaryDisplay();
4936 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4937 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004938 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004939
4940 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4941 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004942
4943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4944 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4945 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004947 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4948 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4949 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004950 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004951}
4952
4953TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004954 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004955
4956 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004957 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004958 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4959
4960 // Associate the InputDevice with the secondary display.
4961 prepareSecondaryDisplay();
4962 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004963 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004964
4965 // The mapper should not generate any events because it is associated with a display that is
4966 // different from the pointer display.
4967 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4968 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004971}
4972
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004973// --- BluetoothCursorInputMapperTest ---
4974
4975class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4976protected:
4977 void SetUp() override {
4978 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4979
4980 mFakePointerController = std::make_shared<FakePointerController>();
4981 mFakePolicy->setPointerController(mFakePointerController);
4982 }
4983};
4984
4985TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4986 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004987 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004988
4989 nsecs_t kernelEventTime = ARBITRARY_TIME;
4990 nsecs_t expectedEventTime = ARBITRARY_TIME;
4991 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4992 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4994 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4995 WithEventTime(expectedEventTime))));
4996
4997 // Process several events that come in quick succession, according to their timestamps.
4998 for (int i = 0; i < 3; i++) {
4999 constexpr static nsecs_t delta = ms2ns(1);
5000 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
5001 kernelEventTime += delta;
5002 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5003
5004 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5005 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5007 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5008 WithEventTime(expectedEventTime))));
5009 }
5010}
5011
5012TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
5013 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00005014 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005015
5016 nsecs_t expectedEventTime = ARBITRARY_TIME;
5017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5020 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5021 WithEventTime(expectedEventTime))));
5022
5023 // Process several events with the same timestamp from the kernel.
5024 // Ensure that we do not generate events too far into the future.
5025 constexpr static int32_t numEvents =
5026 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
5027 for (int i = 0; i < numEvents; i++) {
5028 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5029
5030 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5033 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5034 WithEventTime(expectedEventTime))));
5035 }
5036
5037 // By processing more events with the same timestamp, we should not generate events with a
5038 // timestamp that is more than the specified max time delta from the timestamp at its injection.
5039 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
5040 for (int i = 0; i < 3; i++) {
5041 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5042 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5044 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5045 WithEventTime(cappedEventTime))));
5046 }
5047}
5048
5049TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
5050 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00005051 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005052
5053 nsecs_t kernelEventTime = ARBITRARY_TIME;
5054 nsecs_t expectedEventTime = ARBITRARY_TIME;
5055 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5056 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5058 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5059 WithEventTime(expectedEventTime))));
5060
5061 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
5062 // smoothening is not needed, its timestamp is not affected.
5063 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
5064 expectedEventTime = kernelEventTime;
5065
5066 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5067 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5069 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5070 WithEventTime(expectedEventTime))));
5071}
5072
Michael Wrightd02c5b62014-02-10 15:10:22 -08005073// --- TouchInputMapperTest ---
5074
5075class TouchInputMapperTest : public InputMapperTest {
5076protected:
5077 static const int32_t RAW_X_MIN;
5078 static const int32_t RAW_X_MAX;
5079 static const int32_t RAW_Y_MIN;
5080 static const int32_t RAW_Y_MAX;
5081 static const int32_t RAW_TOUCH_MIN;
5082 static const int32_t RAW_TOUCH_MAX;
5083 static const int32_t RAW_TOOL_MIN;
5084 static const int32_t RAW_TOOL_MAX;
5085 static const int32_t RAW_PRESSURE_MIN;
5086 static const int32_t RAW_PRESSURE_MAX;
5087 static const int32_t RAW_ORIENTATION_MIN;
5088 static const int32_t RAW_ORIENTATION_MAX;
5089 static const int32_t RAW_DISTANCE_MIN;
5090 static const int32_t RAW_DISTANCE_MAX;
5091 static const int32_t RAW_TILT_MIN;
5092 static const int32_t RAW_TILT_MAX;
5093 static const int32_t RAW_ID_MIN;
5094 static const int32_t RAW_ID_MAX;
5095 static const int32_t RAW_SLOT_MIN;
5096 static const int32_t RAW_SLOT_MAX;
5097 static const float X_PRECISION;
5098 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005099 static const float X_PRECISION_VIRTUAL;
5100 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005101
5102 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005103 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005104
5105 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5106
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005107 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005108 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005109
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110 enum Axes {
5111 POSITION = 1 << 0,
5112 TOUCH = 1 << 1,
5113 TOOL = 1 << 2,
5114 PRESSURE = 1 << 3,
5115 ORIENTATION = 1 << 4,
5116 MINOR = 1 << 5,
5117 ID = 1 << 6,
5118 DISTANCE = 1 << 7,
5119 TILT = 1 << 8,
5120 SLOT = 1 << 9,
5121 TOOL_TYPE = 1 << 10,
5122 };
5123
Michael Wrighta9cf4192022-12-01 23:46:39 +00005124 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005125 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00005126 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005127 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005128 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129 int32_t toRawX(float displayX);
5130 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005131 int32_t toRotatedRawX(float displayX);
5132 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005133 float toCookedX(float rawX, float rawY);
5134 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005135 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005136 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005137 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005138 float toDisplayY(int32_t rawY, int32_t displayHeight);
5139
Michael Wrightd02c5b62014-02-10 15:10:22 -08005140};
5141
5142const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5143const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5144const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5145const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5146const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5147const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5148const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5149const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005150const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5151const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005152const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5153const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5154const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5155const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5156const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5157const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5158const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5159const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5160const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5161const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5162const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5163const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005164const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5165 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5166const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5167 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005168const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5169 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005170
5171const float TouchInputMapperTest::GEOMETRIC_SCALE =
5172 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5173 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5174
5175const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5176 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5177 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5178};
5179
Michael Wrighta9cf4192022-12-01 23:46:39 +00005180void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005181 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5182 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005183}
5184
5185void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5186 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00005187 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005188}
5189
Michael Wrighta9cf4192022-12-01 23:46:39 +00005190void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005191 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5192 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5193 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005194}
5195
Michael Wrightd02c5b62014-02-10 15:10:22 -08005196void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005197 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5198 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5199 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5200 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005201}
5202
Jason Gerecke489fda82012-09-07 17:19:40 -07005203void TouchInputMapperTest::prepareLocationCalibration() {
5204 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5205}
5206
Michael Wrightd02c5b62014-02-10 15:10:22 -08005207int32_t TouchInputMapperTest::toRawX(float displayX) {
5208 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5209}
5210
5211int32_t TouchInputMapperTest::toRawY(float displayY) {
5212 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5213}
5214
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005215int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5216 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5217}
5218
5219int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5220 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5221}
5222
Jason Gerecke489fda82012-09-07 17:19:40 -07005223float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5224 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5225 return rawX;
5226}
5227
5228float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5229 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5230 return rawY;
5231}
5232
Michael Wrightd02c5b62014-02-10 15:10:22 -08005233float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005234 return toDisplayX(rawX, DISPLAY_WIDTH);
5235}
5236
5237float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5238 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005239}
5240
5241float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005242 return toDisplayY(rawY, DISPLAY_HEIGHT);
5243}
5244
5245float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5246 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005247}
5248
5249
5250// --- SingleTouchInputMapperTest ---
5251
5252class SingleTouchInputMapperTest : public TouchInputMapperTest {
5253protected:
5254 void prepareButtons();
5255 void prepareAxes(int axes);
5256
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005257 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5258 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5259 void processUp(SingleTouchInputMapper& mappery);
5260 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5261 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5262 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5263 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5264 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5265 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005266};
5267
5268void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005269 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005270}
5271
5272void SingleTouchInputMapperTest::prepareAxes(int axes) {
5273 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005274 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5275 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005276 }
5277 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005278 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5279 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005280 }
5281 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005282 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5283 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005284 }
5285 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005286 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5287 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005288 }
5289 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005290 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5291 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005292 }
5293}
5294
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005295void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005299}
5300
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005301void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005304}
5305
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005306void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005308}
5309
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005310void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005312}
5313
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005314void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5315 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005317}
5318
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005319void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005320 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005321}
5322
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005323void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5324 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5326 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005327}
5328
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005329void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5330 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005332}
5333
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005334void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005336}
5337
Michael Wrightd02c5b62014-02-10 15:10:22 -08005338TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005339 prepareButtons();
5340 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005341 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005342
Josep del Río2d8c79a2023-01-23 19:33:50 +00005343 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005344}
5345
Michael Wrightd02c5b62014-02-10 15:10:22 -08005346TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005347 prepareButtons();
5348 prepareAxes(POSITION);
5349 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00005350 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005351
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005352 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005353}
5354
5355TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005356 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005357 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005358 prepareButtons();
5359 prepareAxes(POSITION);
5360 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005361 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005362
5363 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005364 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005365
5366 // Virtual key is down.
5367 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5368 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5369 processDown(mapper, x, y);
5370 processSync(mapper);
5371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5372
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005373 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374
5375 // Virtual key is up.
5376 processUp(mapper);
5377 processSync(mapper);
5378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5379
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005380 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005381}
5382
5383TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005384 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005385 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005386 prepareButtons();
5387 prepareAxes(POSITION);
5388 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005389 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005390
5391 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005392 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005393
5394 // Virtual key is down.
5395 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5396 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5397 processDown(mapper, x, y);
5398 processSync(mapper);
5399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5400
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005401 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005402
5403 // Virtual key is up.
5404 processUp(mapper);
5405 processSync(mapper);
5406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5407
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005408 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005409}
5410
5411TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005412 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005413 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005414 prepareButtons();
5415 prepareAxes(POSITION);
5416 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005417 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005418
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005420 ASSERT_TRUE(
5421 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005422 ASSERT_TRUE(flags[0]);
5423 ASSERT_FALSE(flags[1]);
5424}
5425
5426TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005427 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005428 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005429 prepareButtons();
5430 prepareAxes(POSITION);
5431 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005432 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005433
arthurhungdcef2dc2020-08-11 14:47:50 +08005434 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005435
5436 NotifyKeyArgs args;
5437
5438 // Press virtual key.
5439 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5440 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5441 processDown(mapper, x, y);
5442 processSync(mapper);
5443
5444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5445 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5446 ASSERT_EQ(DEVICE_ID, args.deviceId);
5447 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5448 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5449 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5450 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5451 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5452 ASSERT_EQ(KEY_HOME, args.scanCode);
5453 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5454 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5455
5456 // Release virtual key.
5457 processUp(mapper);
5458 processSync(mapper);
5459
5460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5461 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5462 ASSERT_EQ(DEVICE_ID, args.deviceId);
5463 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5464 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5465 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5466 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5467 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5468 ASSERT_EQ(KEY_HOME, args.scanCode);
5469 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5470 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5471
5472 // Should not have sent any motions.
5473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5474}
5475
5476TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005477 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005478 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005479 prepareButtons();
5480 prepareAxes(POSITION);
5481 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005482 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005483
arthurhungdcef2dc2020-08-11 14:47:50 +08005484 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005485
5486 NotifyKeyArgs keyArgs;
5487
5488 // Press virtual key.
5489 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5490 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5491 processDown(mapper, x, y);
5492 processSync(mapper);
5493
5494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5495 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5496 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5497 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5498 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5499 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5500 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5501 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5502 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5503 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5504 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5505
5506 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5507 // into the display area.
5508 y -= 100;
5509 processMove(mapper, x, y);
5510 processSync(mapper);
5511
5512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5513 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5514 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5515 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5516 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5517 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5518 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5519 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5520 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5521 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5522 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5523 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5524
5525 NotifyMotionArgs motionArgs;
5526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5527 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5528 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5529 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5530 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5531 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5532 ASSERT_EQ(0, motionArgs.flags);
5533 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5534 ASSERT_EQ(0, motionArgs.buttonState);
5535 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005536 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005537 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005538 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5540 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5541 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5542 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5543 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5544
5545 // Keep moving out of bounds. Should generate a pointer move.
5546 y -= 50;
5547 processMove(mapper, x, y);
5548 processSync(mapper);
5549
5550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5551 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5552 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5553 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5554 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5555 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5556 ASSERT_EQ(0, motionArgs.flags);
5557 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5558 ASSERT_EQ(0, motionArgs.buttonState);
5559 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005560 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005561 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005562 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5564 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5565 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5566 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5567 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5568
5569 // Release out of bounds. Should generate a pointer up.
5570 processUp(mapper);
5571 processSync(mapper);
5572
5573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5574 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5575 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5576 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5577 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5578 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5579 ASSERT_EQ(0, motionArgs.flags);
5580 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5581 ASSERT_EQ(0, motionArgs.buttonState);
5582 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005583 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005584 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005585 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5587 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5588 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5589 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5590 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5591
5592 // Should not have sent any more keys or motions.
5593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5595}
5596
5597TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005598 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005599 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005600 prepareButtons();
5601 prepareAxes(POSITION);
5602 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005603 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005604
arthurhungdcef2dc2020-08-11 14:47:50 +08005605 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005606
5607 NotifyMotionArgs motionArgs;
5608
5609 // Initially go down out of bounds.
5610 int32_t x = -10;
5611 int32_t y = -10;
5612 processDown(mapper, x, y);
5613 processSync(mapper);
5614
5615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5616
5617 // Move into the display area. Should generate a pointer down.
5618 x = 50;
5619 y = 75;
5620 processMove(mapper, x, y);
5621 processSync(mapper);
5622
5623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5624 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5625 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5626 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5627 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5628 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5629 ASSERT_EQ(0, motionArgs.flags);
5630 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5631 ASSERT_EQ(0, motionArgs.buttonState);
5632 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005633 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005634 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005635 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5637 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5638 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5639 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5640 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5641
5642 // Release. Should generate a pointer up.
5643 processUp(mapper);
5644 processSync(mapper);
5645
5646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5647 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5648 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5649 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5650 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5651 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5652 ASSERT_EQ(0, motionArgs.flags);
5653 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5654 ASSERT_EQ(0, motionArgs.buttonState);
5655 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005656 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005657 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005658 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5660 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5661 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5662 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5663 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5664
5665 // Should not have sent any more keys or motions.
5666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5668}
5669
Santos Cordonfa5cf462017-04-05 10:37:00 -07005670TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005671 addConfigurationProperty("touch.deviceType", "touchScreen");
5672 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5673
Michael Wrighta9cf4192022-12-01 23:46:39 +00005674 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005675 prepareButtons();
5676 prepareAxes(POSITION);
5677 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005678 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005679
arthurhungdcef2dc2020-08-11 14:47:50 +08005680 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005681
5682 NotifyMotionArgs motionArgs;
5683
5684 // Down.
5685 int32_t x = 100;
5686 int32_t y = 125;
5687 processDown(mapper, x, y);
5688 processSync(mapper);
5689
5690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5691 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5692 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5693 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5694 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5695 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5696 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5697 ASSERT_EQ(0, motionArgs.flags);
5698 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5699 ASSERT_EQ(0, motionArgs.buttonState);
5700 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005701 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005702 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005703 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5705 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5706 1, 0, 0, 0, 0, 0, 0, 0));
5707 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5708 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5709 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5710
5711 // Move.
5712 x += 50;
5713 y += 75;
5714 processMove(mapper, x, y);
5715 processSync(mapper);
5716
5717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5718 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5719 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5720 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5721 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5722 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5723 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5724 ASSERT_EQ(0, motionArgs.flags);
5725 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5726 ASSERT_EQ(0, motionArgs.buttonState);
5727 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005728 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005729 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005730 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5732 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5733 1, 0, 0, 0, 0, 0, 0, 0));
5734 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5735 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5736 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5737
5738 // Up.
5739 processUp(mapper);
5740 processSync(mapper);
5741
5742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5743 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5744 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5745 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5746 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5747 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5748 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5749 ASSERT_EQ(0, motionArgs.flags);
5750 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5751 ASSERT_EQ(0, motionArgs.buttonState);
5752 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005753 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005754 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005755 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005756 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5757 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5758 1, 0, 0, 0, 0, 0, 0, 0));
5759 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5760 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5761 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5762
5763 // Should not have sent any more keys or motions.
5764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5766}
5767
Michael Wrightd02c5b62014-02-10 15:10:22 -08005768TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005769 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005770 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005771 prepareButtons();
5772 prepareAxes(POSITION);
5773 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005774 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005775
arthurhungdcef2dc2020-08-11 14:47:50 +08005776 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005777
5778 NotifyMotionArgs motionArgs;
5779
5780 // Down.
5781 int32_t x = 100;
5782 int32_t y = 125;
5783 processDown(mapper, x, y);
5784 processSync(mapper);
5785
5786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5787 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5788 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5789 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5790 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5791 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5792 ASSERT_EQ(0, motionArgs.flags);
5793 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5794 ASSERT_EQ(0, motionArgs.buttonState);
5795 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005796 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005797 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005798 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005799 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5800 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5801 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5802 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5803 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5804
5805 // Move.
5806 x += 50;
5807 y += 75;
5808 processMove(mapper, x, y);
5809 processSync(mapper);
5810
5811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5812 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5813 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5814 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5815 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5816 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5817 ASSERT_EQ(0, motionArgs.flags);
5818 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5819 ASSERT_EQ(0, motionArgs.buttonState);
5820 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005821 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005822 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005823 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5825 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5826 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5827 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5828 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5829
5830 // Up.
5831 processUp(mapper);
5832 processSync(mapper);
5833
5834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5835 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5836 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5837 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5838 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5839 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5840 ASSERT_EQ(0, motionArgs.flags);
5841 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5842 ASSERT_EQ(0, motionArgs.buttonState);
5843 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005844 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005845 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005846 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005847 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5848 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5849 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5850 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5851 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5852
5853 // Should not have sent any more keys or motions.
5854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5856}
5857
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005858TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005859 addConfigurationProperty("touch.deviceType", "touchScreen");
5860 prepareButtons();
5861 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005862 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5863 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005864 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005865
5866 NotifyMotionArgs args;
5867
5868 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005869 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005870 processDown(mapper, toRawX(50), toRawY(75));
5871 processSync(mapper);
5872
5873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5874 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5875 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5876
5877 processUp(mapper);
5878 processSync(mapper);
5879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5880}
5881
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005882TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005883 addConfigurationProperty("touch.deviceType", "touchScreen");
5884 prepareButtons();
5885 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005886 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5887 // orientation-aware are affected by display rotation.
5888 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005889 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005890
5891 NotifyMotionArgs args;
5892
5893 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005894 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005895 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005896 processDown(mapper, toRawX(50), toRawY(75));
5897 processSync(mapper);
5898
5899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5900 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5901 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5902
5903 processUp(mapper);
5904 processSync(mapper);
5905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5906
5907 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005908 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005909 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005910 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005911 processSync(mapper);
5912
5913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5914 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5915 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5916
5917 processUp(mapper);
5918 processSync(mapper);
5919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5920
5921 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005922 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005923 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005924 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5925 processSync(mapper);
5926
5927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5928 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5929 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5930
5931 processUp(mapper);
5932 processSync(mapper);
5933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5934
5935 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005936 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005937 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005938 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005939 processSync(mapper);
5940
5941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5942 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5943 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5944
5945 processUp(mapper);
5946 processSync(mapper);
5947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5948}
5949
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005950TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5951 addConfigurationProperty("touch.deviceType", "touchScreen");
5952 prepareButtons();
5953 prepareAxes(POSITION);
5954 addConfigurationProperty("touch.orientationAware", "1");
5955 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5956 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005957 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005958 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005959 NotifyMotionArgs args;
5960
5961 // Orientation 0.
5962 processDown(mapper, toRawX(50), toRawY(75));
5963 processSync(mapper);
5964
5965 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5966 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5967 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5968
5969 processUp(mapper);
5970 processSync(mapper);
5971 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5972}
5973
5974TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5975 addConfigurationProperty("touch.deviceType", "touchScreen");
5976 prepareButtons();
5977 prepareAxes(POSITION);
5978 addConfigurationProperty("touch.orientationAware", "1");
5979 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5980 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005981 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005982 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005983 NotifyMotionArgs args;
5984
5985 // Orientation 90.
5986 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5987 processSync(mapper);
5988
5989 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5990 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5991 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5992
5993 processUp(mapper);
5994 processSync(mapper);
5995 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5996}
5997
5998TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5999 addConfigurationProperty("touch.deviceType", "touchScreen");
6000 prepareButtons();
6001 prepareAxes(POSITION);
6002 addConfigurationProperty("touch.orientationAware", "1");
6003 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6004 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006005 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006006 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006007 NotifyMotionArgs args;
6008
6009 // Orientation 180.
6010 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6011 processSync(mapper);
6012
6013 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6014 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6015 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6016
6017 processUp(mapper);
6018 processSync(mapper);
6019 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6020}
6021
6022TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6023 addConfigurationProperty("touch.deviceType", "touchScreen");
6024 prepareButtons();
6025 prepareAxes(POSITION);
6026 addConfigurationProperty("touch.orientationAware", "1");
6027 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6028 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006029 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006030 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006031 NotifyMotionArgs args;
6032
6033 // Orientation 270.
6034 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6035 processSync(mapper);
6036
6037 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6038 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6039 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6040
6041 processUp(mapper);
6042 processSync(mapper);
6043 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6044}
6045
6046TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6047 addConfigurationProperty("touch.deviceType", "touchScreen");
6048 prepareButtons();
6049 prepareAxes(POSITION);
6050 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6051 // orientation-aware are affected by display rotation.
6052 addConfigurationProperty("touch.orientationAware", "0");
6053 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00006054 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006055
6056 NotifyMotionArgs args;
6057
6058 // Orientation 90, Rotation 0.
6059 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006060 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006061 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6062 processSync(mapper);
6063
6064 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6065 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6066 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6067
6068 processUp(mapper);
6069 processSync(mapper);
6070 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6071
6072 // Orientation 90, Rotation 90.
6073 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006074 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006075 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006076 processSync(mapper);
6077
6078 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6079 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6080 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6081
6082 processUp(mapper);
6083 processSync(mapper);
6084 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6085
6086 // Orientation 90, Rotation 180.
6087 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006088 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006089 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6090 processSync(mapper);
6091
6092 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6093 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6094 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6095
6096 processUp(mapper);
6097 processSync(mapper);
6098 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6099
6100 // Orientation 90, Rotation 270.
6101 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006102 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006103 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 -07006104 processSync(mapper);
6105
6106 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6107 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6108 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6109
6110 processUp(mapper);
6111 processSync(mapper);
6112 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6113}
6114
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006115TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
6116 addConfigurationProperty("touch.deviceType", "touchScreen");
6117 prepareButtons();
6118 prepareAxes(POSITION);
6119 addConfigurationProperty("touch.orientationAware", "1");
6120 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006121 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006122
6123 // Set a physical frame in the display viewport.
6124 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6125 viewport->physicalLeft = 20;
6126 viewport->physicalTop = 600;
6127 viewport->physicalRight = 30;
6128 viewport->physicalBottom = 610;
6129 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006130 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006131
6132 // Start the touch.
6133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6134 processSync(mapper);
6135
6136 // Expect all input starting outside the physical frame to be ignored.
6137 const std::array<Point, 6> outsidePoints = {
6138 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6139 for (const auto& p : outsidePoints) {
6140 processMove(mapper, toRawX(p.x), toRawY(p.y));
6141 processSync(mapper);
6142 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6143 }
6144
6145 // Move the touch into the physical frame.
6146 processMove(mapper, toRawX(25), toRawY(605));
6147 processSync(mapper);
6148 NotifyMotionArgs args;
6149 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6150 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6151 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6152 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6153
6154 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
6155 for (const auto& p : outsidePoints) {
6156 processMove(mapper, toRawX(p.x), toRawY(p.y));
6157 processSync(mapper);
6158 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6159 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
6160 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6161 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6162 }
6163
6164 processUp(mapper);
6165 processSync(mapper);
6166 EXPECT_NO_FATAL_FAILURE(
6167 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
6168}
6169
Harry Cutts1db43992023-06-19 17:05:07 +00006170TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
6171 std::shared_ptr<FakePointerController> fakePointerController =
6172 std::make_shared<FakePointerController>();
6173 mFakePolicy->setPointerController(fakePointerController);
6174
6175 addConfigurationProperty("touch.deviceType", "pointer");
6176 prepareAxes(POSITION);
6177 prepareDisplay(ui::ROTATION_0);
6178 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
6179
6180 // Set a physical frame in the display viewport.
6181 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6182 viewport->physicalLeft = 20;
6183 viewport->physicalTop = 600;
6184 viewport->physicalRight = 30;
6185 viewport->physicalBottom = 610;
6186 mFakePolicy->updateViewport(*viewport);
6187 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
6188
6189 // Start the touch.
6190 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6191 processSync(mapper);
6192
6193 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
6194 // produced.
6195 const std::array<Point, 6> outsidePoints = {
6196 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6197 for (const auto& p : outsidePoints) {
6198 processMove(mapper, toRawX(p.x), toRawY(p.y));
6199 processSync(mapper);
6200 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6201 }
6202}
6203
Michael Wrightd02c5b62014-02-10 15:10:22 -08006204TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006205 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006206 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006207 prepareButtons();
6208 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00006209 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210
6211 // These calculations are based on the input device calibration documentation.
6212 int32_t rawX = 100;
6213 int32_t rawY = 200;
6214 int32_t rawPressure = 10;
6215 int32_t rawToolMajor = 12;
6216 int32_t rawDistance = 2;
6217 int32_t rawTiltX = 30;
6218 int32_t rawTiltY = 110;
6219
6220 float x = toDisplayX(rawX);
6221 float y = toDisplayY(rawY);
6222 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6223 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6224 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6225 float distance = float(rawDistance);
6226
6227 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6228 float tiltScale = M_PI / 180;
6229 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6230 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6231 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6232 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6233
6234 processDown(mapper, rawX, rawY);
6235 processPressure(mapper, rawPressure);
6236 processToolMajor(mapper, rawToolMajor);
6237 processDistance(mapper, rawDistance);
6238 processTilt(mapper, rawTiltX, rawTiltY);
6239 processSync(mapper);
6240
6241 NotifyMotionArgs args;
6242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6244 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6245 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6246}
6247
Jason Gerecke489fda82012-09-07 17:19:40 -07006248TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006249 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006250 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07006251 prepareLocationCalibration();
6252 prepareButtons();
6253 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006254 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006255
6256 int32_t rawX = 100;
6257 int32_t rawY = 200;
6258
6259 float x = toDisplayX(toCookedX(rawX, rawY));
6260 float y = toDisplayY(toCookedY(rawX, rawY));
6261
6262 processDown(mapper, rawX, rawY);
6263 processSync(mapper);
6264
6265 NotifyMotionArgs args;
6266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6267 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6268 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6269}
6270
Michael Wrightd02c5b62014-02-10 15:10:22 -08006271TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006272 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006273 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006274 prepareButtons();
6275 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006276 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006277
6278 NotifyMotionArgs motionArgs;
6279 NotifyKeyArgs keyArgs;
6280
6281 processDown(mapper, 100, 200);
6282 processSync(mapper);
6283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6284 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6285 ASSERT_EQ(0, motionArgs.buttonState);
6286
6287 // press BTN_LEFT, release BTN_LEFT
6288 processKey(mapper, BTN_LEFT, 1);
6289 processSync(mapper);
6290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6291 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6292 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6293
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6295 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6296 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6297
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298 processKey(mapper, BTN_LEFT, 0);
6299 processSync(mapper);
6300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006301 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006302 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006303
6304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006306 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006307
6308 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6309 processKey(mapper, BTN_RIGHT, 1);
6310 processKey(mapper, BTN_MIDDLE, 1);
6311 processSync(mapper);
6312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6314 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6315 motionArgs.buttonState);
6316
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6318 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6319 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, 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_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6324 motionArgs.buttonState);
6325
Michael Wrightd02c5b62014-02-10 15:10:22 -08006326 processKey(mapper, BTN_RIGHT, 0);
6327 processSync(mapper);
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006330 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006331
6332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006334 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006335
6336 processKey(mapper, BTN_MIDDLE, 0);
6337 processSync(mapper);
6338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006339 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006340 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006341
6342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006343 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006344 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006345
6346 // press BTN_BACK, release BTN_BACK
6347 processKey(mapper, BTN_BACK, 1);
6348 processSync(mapper);
6349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6350 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6351 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006352
Michael Wrightd02c5b62014-02-10 15:10:22 -08006353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006355 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6356
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6358 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6359 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006360
6361 processKey(mapper, BTN_BACK, 0);
6362 processSync(mapper);
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006364 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006366
6367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006369 ASSERT_EQ(0, motionArgs.buttonState);
6370
Michael Wrightd02c5b62014-02-10 15:10:22 -08006371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6372 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6373 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6374
6375 // press BTN_SIDE, release BTN_SIDE
6376 processKey(mapper, BTN_SIDE, 1);
6377 processSync(mapper);
6378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6379 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6380 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006381
Michael Wrightd02c5b62014-02-10 15:10:22 -08006382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006384 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6385
6386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6387 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6388 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389
6390 processKey(mapper, BTN_SIDE, 0);
6391 processSync(mapper);
6392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006393 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006394 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006395
6396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006398 ASSERT_EQ(0, motionArgs.buttonState);
6399
Michael Wrightd02c5b62014-02-10 15:10:22 -08006400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6401 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6402 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6403
6404 // press BTN_FORWARD, release BTN_FORWARD
6405 processKey(mapper, BTN_FORWARD, 1);
6406 processSync(mapper);
6407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6408 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6409 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006410
Michael Wrightd02c5b62014-02-10 15:10:22 -08006411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006412 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006413 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6414
6415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6416 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6417 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006418
6419 processKey(mapper, BTN_FORWARD, 0);
6420 processSync(mapper);
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006422 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006423 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006424
6425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006426 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006427 ASSERT_EQ(0, motionArgs.buttonState);
6428
Michael Wrightd02c5b62014-02-10 15:10:22 -08006429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6430 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6431 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6432
6433 // press BTN_EXTRA, release BTN_EXTRA
6434 processKey(mapper, BTN_EXTRA, 1);
6435 processSync(mapper);
6436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6437 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6438 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006439
Michael Wrightd02c5b62014-02-10 15:10:22 -08006440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006441 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006442 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6443
6444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6445 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6446 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006447
6448 processKey(mapper, BTN_EXTRA, 0);
6449 processSync(mapper);
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006451 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006452 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006453
6454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006455 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006456 ASSERT_EQ(0, motionArgs.buttonState);
6457
Michael Wrightd02c5b62014-02-10 15:10:22 -08006458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6459 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6460 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6461
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6463
Michael Wrightd02c5b62014-02-10 15:10:22 -08006464 // press BTN_STYLUS, release BTN_STYLUS
6465 processKey(mapper, BTN_STYLUS, 1);
6466 processSync(mapper);
6467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006469 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6470
6471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6472 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6473 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006474
6475 processKey(mapper, BTN_STYLUS, 0);
6476 processSync(mapper);
6477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006478 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006479 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006480
6481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006482 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006483 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006484
6485 // press BTN_STYLUS2, release BTN_STYLUS2
6486 processKey(mapper, BTN_STYLUS2, 1);
6487 processSync(mapper);
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006490 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6491
6492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6493 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6494 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006495
6496 processKey(mapper, BTN_STYLUS2, 0);
6497 processSync(mapper);
6498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006499 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006500 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006501
6502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006503 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006504 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006505
6506 // release touch
6507 processUp(mapper);
6508 processSync(mapper);
6509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6510 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6511 ASSERT_EQ(0, motionArgs.buttonState);
6512}
6513
6514TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006515 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006516 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006517 prepareButtons();
6518 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006519 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006520
6521 NotifyMotionArgs motionArgs;
6522
6523 // default tool type is finger
6524 processDown(mapper, 100, 200);
6525 processSync(mapper);
6526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6527 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006528 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006529
6530 // eraser
6531 processKey(mapper, BTN_TOOL_RUBBER, 1);
6532 processSync(mapper);
6533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006535 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006536
6537 // stylus
6538 processKey(mapper, BTN_TOOL_RUBBER, 0);
6539 processKey(mapper, BTN_TOOL_PEN, 1);
6540 processSync(mapper);
6541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006543 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006544
6545 // brush
6546 processKey(mapper, BTN_TOOL_PEN, 0);
6547 processKey(mapper, BTN_TOOL_BRUSH, 1);
6548 processSync(mapper);
6549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6550 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006551 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006552
6553 // pencil
6554 processKey(mapper, BTN_TOOL_BRUSH, 0);
6555 processKey(mapper, BTN_TOOL_PENCIL, 1);
6556 processSync(mapper);
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006559 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006560
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006561 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006562 processKey(mapper, BTN_TOOL_PENCIL, 0);
6563 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6564 processSync(mapper);
6565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6566 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006567 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006568
6569 // mouse
6570 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6571 processKey(mapper, BTN_TOOL_MOUSE, 1);
6572 processSync(mapper);
6573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006575 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006576
6577 // lens
6578 processKey(mapper, BTN_TOOL_MOUSE, 0);
6579 processKey(mapper, BTN_TOOL_LENS, 1);
6580 processSync(mapper);
6581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6582 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006583 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006584
6585 // double-tap
6586 processKey(mapper, BTN_TOOL_LENS, 0);
6587 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6588 processSync(mapper);
6589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006591 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006592
6593 // triple-tap
6594 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6595 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6596 processSync(mapper);
6597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006599 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006600
6601 // quad-tap
6602 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6603 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6604 processSync(mapper);
6605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6606 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006607 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006608
6609 // finger
6610 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6611 processKey(mapper, BTN_TOOL_FINGER, 1);
6612 processSync(mapper);
6613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006615 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006616
6617 // stylus trumps finger
6618 processKey(mapper, BTN_TOOL_PEN, 1);
6619 processSync(mapper);
6620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006622 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006623
6624 // eraser trumps stylus
6625 processKey(mapper, BTN_TOOL_RUBBER, 1);
6626 processSync(mapper);
6627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6628 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006629 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006630
6631 // mouse trumps eraser
6632 processKey(mapper, BTN_TOOL_MOUSE, 1);
6633 processSync(mapper);
6634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6635 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006636 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006637
6638 // back to default tool type
6639 processKey(mapper, BTN_TOOL_MOUSE, 0);
6640 processKey(mapper, BTN_TOOL_RUBBER, 0);
6641 processKey(mapper, BTN_TOOL_PEN, 0);
6642 processKey(mapper, BTN_TOOL_FINGER, 0);
6643 processSync(mapper);
6644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6645 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006646 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006647}
6648
6649TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006650 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006651 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006652 prepareButtons();
6653 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006654 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006655 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006656
6657 NotifyMotionArgs motionArgs;
6658
6659 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6660 processKey(mapper, BTN_TOOL_FINGER, 1);
6661 processMove(mapper, 100, 200);
6662 processSync(mapper);
6663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6664 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6666 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6667
6668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6669 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6671 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6672
6673 // move a little
6674 processMove(mapper, 150, 250);
6675 processSync(mapper);
6676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6677 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6679 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6680
6681 // down when BTN_TOUCH is pressed, pressure defaults to 1
6682 processKey(mapper, BTN_TOUCH, 1);
6683 processSync(mapper);
6684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6685 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6686 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6687 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6688
6689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6690 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6692 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6693
6694 // up when BTN_TOUCH is released, hover restored
6695 processKey(mapper, BTN_TOUCH, 0);
6696 processSync(mapper);
6697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6698 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6700 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6701
6702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6703 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6705 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6706
6707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6708 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6710 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6711
6712 // exit hover when pointer goes away
6713 processKey(mapper, BTN_TOOL_FINGER, 0);
6714 processSync(mapper);
6715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6716 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6718 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6719}
6720
6721TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006722 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006723 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006724 prepareButtons();
6725 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006726 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006727
6728 NotifyMotionArgs motionArgs;
6729
6730 // initially hovering because pressure is 0
6731 processDown(mapper, 100, 200);
6732 processPressure(mapper, 0);
6733 processSync(mapper);
6734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6735 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6737 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6738
6739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6740 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6742 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6743
6744 // move a little
6745 processMove(mapper, 150, 250);
6746 processSync(mapper);
6747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6748 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6750 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6751
6752 // down when pressure is non-zero
6753 processPressure(mapper, RAW_PRESSURE_MAX);
6754 processSync(mapper);
6755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6756 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6758 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6759
6760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6761 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6763 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6764
6765 // up when pressure becomes 0, hover restored
6766 processPressure(mapper, 0);
6767 processSync(mapper);
6768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6769 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6771 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6772
6773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6774 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6776 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6777
6778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6779 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6781 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6782
6783 // exit hover when pointer goes away
6784 processUp(mapper);
6785 processSync(mapper);
6786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6787 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6789 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6790}
6791
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006792TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6793 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006794 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006795 prepareButtons();
6796 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006797 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006798
6799 // Touch down.
6800 processDown(mapper, 100, 200);
6801 processPressure(mapper, 1);
6802 processSync(mapper);
6803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6804 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6805
6806 // Reset the mapper. This should cancel the ongoing gesture.
6807 resetMapper(mapper, ARBITRARY_TIME);
6808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6809 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6810
6811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6812}
6813
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006814TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6815 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006816 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006817 prepareButtons();
6818 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006819 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006820
6821 // Set the initial state for the touch pointer.
6822 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6823 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6824 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6825 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6826
6827 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006828 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6829 // does not generate any events.
6830 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006831
6832 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6833 // the recreated touch state to generate a down event.
6834 processSync(mapper);
6835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6836 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6837
6838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6839}
6840
lilinnan687e58f2022-07-19 16:00:50 +08006841TEST_F(SingleTouchInputMapperTest,
6842 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6843 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006844 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006845 prepareButtons();
6846 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006847 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006848 NotifyMotionArgs motionArgs;
6849
6850 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006851 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006852 processSync(mapper);
6853
6854 // We should receive a down event
6855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6856 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6857
6858 // Change display id
6859 clearViewports();
6860 prepareSecondaryDisplay(ViewportType::INTERNAL);
6861
6862 // We should receive a cancel event
6863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6864 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6865 // Then receive reset called
6866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6867}
6868
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006869TEST_F(SingleTouchInputMapperTest,
6870 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6871 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006872 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006873 prepareButtons();
6874 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006875 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6877 NotifyMotionArgs motionArgs;
6878
6879 // Start a new gesture.
6880 processDown(mapper, 100, 200);
6881 processSync(mapper);
6882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6883 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6884
6885 // Make the viewport inactive. This will put the device in disabled mode.
6886 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6887 viewport->isActive = false;
6888 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006889 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006890
6891 // We should receive a cancel event for the ongoing gesture.
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6893 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6894 // Then we should be notified that the device was reset.
6895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6896
6897 // No events are generated while the viewport is inactive.
6898 processMove(mapper, 101, 201);
6899 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006900 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006901 processSync(mapper);
6902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6903
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006904 // Start a new gesture while the viewport is still inactive.
6905 processDown(mapper, 300, 400);
6906 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6907 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6908 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6909 processSync(mapper);
6910
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006911 // Make the viewport active again. The device should resume processing events.
6912 viewport->isActive = true;
6913 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006914 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006915
6916 // The device is reset because it changes back to direct mode, without generating any events.
6917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6919
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006920 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006921 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6923 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006924
6925 // No more events.
6926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6928}
6929
Prabir Pradhan211ba622022-10-31 21:09:21 +00006930TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6931 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006932 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006933 prepareButtons();
6934 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006935 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6937
6938 // Press a stylus button.
6939 processKey(mapper, BTN_STYLUS, 1);
6940 processSync(mapper);
6941
6942 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6943 processDown(mapper, 100, 200);
6944 processSync(mapper);
6945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6946 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6947 WithCoords(toDisplayX(100), toDisplayY(200)),
6948 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6950 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6951 WithCoords(toDisplayX(100), toDisplayY(200)),
6952 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6953
6954 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6955 // the button has not actually been released, since there will be no pointers through which the
6956 // button state can be reported. The event is generated at the location of the pointer before
6957 // it went up.
6958 processUp(mapper);
6959 processSync(mapper);
6960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6961 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6962 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6964 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6965 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6966}
6967
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006968TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6969 addConfigurationProperty("touch.deviceType", "touchScreen");
6970 prepareDisplay(ui::ROTATION_0);
6971 prepareButtons();
6972 prepareAxes(POSITION);
6973
6974 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6975
Arpit Singha8c236b2023-04-25 13:56:05 +00006976 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6978
6979 // Press a stylus button.
6980 processKey(mapper, BTN_STYLUS, 1);
6981 processSync(mapper);
6982
6983 // Start a touch gesture and ensure that the stylus button is not reported.
6984 processDown(mapper, 100, 200);
6985 processSync(mapper);
6986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6987 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6988
6989 // Release and press the stylus button again.
6990 processKey(mapper, BTN_STYLUS, 0);
6991 processSync(mapper);
6992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6993 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6994 processKey(mapper, BTN_STYLUS, 1);
6995 processSync(mapper);
6996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6997 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6998
6999 // Release the touch gesture.
7000 processUp(mapper);
7001 processSync(mapper);
7002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7003 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7004
7005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7006}
7007
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00007008TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
7009 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
7010 prepareDisplay(ui::ROTATION_0);
7011 prepareButtons();
7012 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007013 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00007014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7015
7016 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
7017}
7018
Seunghwan Choi356026c2023-02-01 14:37:25 +09007019TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
7020 std::shared_ptr<FakePointerController> fakePointerController =
7021 std::make_shared<FakePointerController>();
7022 addConfigurationProperty("touch.deviceType", "touchScreen");
7023 prepareDisplay(ui::ROTATION_0);
7024 prepareButtons();
7025 prepareAxes(POSITION);
7026 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7027 mFakePolicy->setPointerController(fakePointerController);
7028 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00007029 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09007030
7031 processKey(mapper, BTN_TOOL_PEN, 1);
7032 processMove(mapper, 100, 200);
7033 processSync(mapper);
7034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7035 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007036 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09007037 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
7038 ASSERT_TRUE(fakePointerController->isPointerShown());
7039 ASSERT_NO_FATAL_FAILURE(
7040 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
7041}
7042
7043TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
7044 std::shared_ptr<FakePointerController> fakePointerController =
7045 std::make_shared<FakePointerController>();
7046 addConfigurationProperty("touch.deviceType", "touchScreen");
7047 prepareDisplay(ui::ROTATION_0);
7048 prepareButtons();
7049 prepareAxes(POSITION);
7050 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7051 mFakePolicy->setPointerController(fakePointerController);
7052 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00007053 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09007054
7055 processKey(mapper, BTN_TOOL_PEN, 1);
7056 processMove(mapper, 100, 200);
7057 processSync(mapper);
7058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7059 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007060 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09007061 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
7062 ASSERT_FALSE(fakePointerController->isPointerShown());
7063}
7064
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007065TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
7066 // Initialize the device without setting device source to touch navigation.
7067 addConfigurationProperty("touch.deviceType", "touchScreen");
7068 prepareDisplay(ui::ROTATION_0);
7069 prepareButtons();
7070 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007071 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007072
7073 // Ensure that the device is created as a touchscreen, not touch navigation.
7074 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7075
7076 // Add device type association after the device was created.
7077 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
7078
7079 // Send update to the mapper.
7080 std::list<NotifyArgs> unused2 =
7081 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007082 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007083
7084 // Check whether device type update was successful.
7085 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
7086}
7087
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007088TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
7089 // Initialize the device without setting device source to touch navigation.
7090 addConfigurationProperty("touch.deviceType", "touchScreen");
7091 prepareDisplay(ui::ROTATION_0);
7092 prepareButtons();
7093 prepareAxes(POSITION);
7094 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7095
7096 // Set a physical frame in the display viewport.
7097 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7098 viewport->physicalLeft = 0;
7099 viewport->physicalTop = 0;
7100 viewport->physicalRight = DISPLAY_WIDTH / 2;
7101 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
7102 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007103 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007104
Arpit Singha8c236b2023-04-25 13:56:05 +00007105 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007106
7107 // Hovering inside the physical frame produces events.
7108 processKey(mapper, BTN_TOOL_PEN, 1);
7109 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
7110 processSync(mapper);
7111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7112 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
7113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7114 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
7115
7116 // Leaving the physical frame ends the hovering gesture.
7117 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
7118 processSync(mapper);
7119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7120 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
7121
7122 // Moving outside the physical frame does not produce events.
7123 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
7124 processSync(mapper);
7125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7126
7127 // Re-entering the physical frame produces events.
7128 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
7129 processSync(mapper);
7130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7131 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
7132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7133 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
7134}
7135
Prabir Pradhan5632d622021-09-06 07:57:20 -07007136// --- TouchDisplayProjectionTest ---
7137
7138class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7139public:
7140 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7141 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7142 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00007143 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
7144 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
7145 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007146 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007147 auto rotatedWidth = naturalDisplayWidth;
7148 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007149 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00007150 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007151 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007152 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007153 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007154 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007155 inverseRotationFlags = ui::Transform::ROT_180;
7156 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007157 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007158 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007159 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007160 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007161 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007162 inverseRotationFlags = ui::Transform::ROT_0;
7163 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007164 }
7165
Prabir Pradhana9df3162022-12-05 23:57:27 +00007166 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007167 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7168
7169 std::optional<DisplayViewport> internalViewport =
7170 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7171 DisplayViewport& v = *internalViewport;
7172 v.displayId = DISPLAY_ID;
7173 v.orientation = orientation;
7174
7175 v.logicalLeft = 0;
7176 v.logicalTop = 0;
7177 v.logicalRight = 100;
7178 v.logicalBottom = 100;
7179
7180 v.physicalLeft = rotatedPhysicalDisplay.left;
7181 v.physicalTop = rotatedPhysicalDisplay.top;
7182 v.physicalRight = rotatedPhysicalDisplay.right;
7183 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7184
Prabir Pradhana9df3162022-12-05 23:57:27 +00007185 v.deviceWidth = rotatedWidth;
7186 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007187
7188 v.isActive = true;
7189 v.uniqueId = UNIQUE_ID;
7190 v.type = ViewportType::INTERNAL;
7191 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007192 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007193 }
7194
7195 void assertReceivedMove(const Point& point) {
7196 NotifyMotionArgs motionArgs;
7197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007199 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007200 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7201 1, 0, 0, 0, 0, 0, 0, 0));
7202 }
7203};
7204
7205TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7206 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007207 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007208
7209 prepareButtons();
7210 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007211 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007212
7213 NotifyMotionArgs motionArgs;
7214
7215 // Configure the DisplayViewport such that the logical display maps to a subsection of
7216 // the display panel called the physical display. Here, the physical display is bounded by the
7217 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7218 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7219 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7220 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7221
Michael Wrighta9cf4192022-12-01 23:46:39 +00007222 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007223 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7224
7225 // Touches outside the physical display should be ignored, and should not generate any
7226 // events. Ensure touches at the following points that lie outside of the physical display
7227 // area do not generate any events.
7228 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7229 processDown(mapper, toRawX(point.x), toRawY(point.y));
7230 processSync(mapper);
7231 processUp(mapper);
7232 processSync(mapper);
7233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7234 << "Unexpected event generated for touch outside physical display at point: "
7235 << point.x << ", " << point.y;
7236 }
7237 }
7238}
7239
7240TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7241 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007242 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007243
7244 prepareButtons();
7245 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007246 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007247
7248 NotifyMotionArgs motionArgs;
7249
7250 // Configure the DisplayViewport such that the logical display maps to a subsection of
7251 // the display panel called the physical display. Here, the physical display is bounded by the
7252 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7253 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7254
Michael Wrighta9cf4192022-12-01 23:46:39 +00007255 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007256 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7257
7258 // Touches that start outside the physical display should be ignored until it enters the
7259 // physical display bounds, at which point it should generate a down event. Start a touch at
7260 // the point (5, 100), which is outside the physical display bounds.
7261 static const Point kOutsidePoint{5, 100};
7262 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7263 processSync(mapper);
7264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7265
7266 // Move the touch into the physical display area. This should generate a pointer down.
7267 processMove(mapper, toRawX(11), toRawY(21));
7268 processSync(mapper);
7269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7270 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007271 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007272 ASSERT_NO_FATAL_FAILURE(
7273 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7274
7275 // Move the touch inside the physical display area. This should generate a pointer move.
7276 processMove(mapper, toRawX(69), toRawY(159));
7277 processSync(mapper);
7278 assertReceivedMove({69, 159});
7279
7280 // Move outside the physical display area. Since the pointer is already down, this should
7281 // now continue generating events.
7282 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7283 processSync(mapper);
7284 assertReceivedMove(kOutsidePoint);
7285
7286 // Release. This should generate a pointer up.
7287 processUp(mapper);
7288 processSync(mapper);
7289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7290 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7292 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7293
7294 // Ensure no more events were generated.
7295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7297 }
7298}
7299
Prabir Pradhana9df3162022-12-05 23:57:27 +00007300// --- TouchscreenPrecisionTests ---
7301
7302// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
7303// in various orientations and with different display rotations. We configure the touchscreen to
7304// have a higher resolution than that of the display by an integer scale factor in each axis so that
7305// we can enforce that coordinates match precisely as expected.
7306class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
7307 public ::testing::WithParamInterface<ui::Rotation> {
7308public:
7309 void SetUp() override {
7310 SingleTouchInputMapperTest::SetUp();
7311
7312 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7313 // four times the resolution of the display in the Y axis.
7314 prepareButtons();
7315 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007316 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7317 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007318 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007319 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7320 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007321 }
7322
7323 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7324 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7325 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7326 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7327
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007328 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7329 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7330
7331 static const int32_t PRECISION_RAW_X_FLAT = 16;
7332 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7333
7334 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7335 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7336
Prabir Pradhana9df3162022-12-05 23:57:27 +00007337 static const std::array<Point, 4> kRawCorners;
7338};
7339
7340const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7341 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7342 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7343 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7344 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7345}};
7346
7347// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7348// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7349// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7350TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7351 enum class Orientation {
7352 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7353 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7354 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7355 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7356 ftl_last = ORIENTATION_270,
7357 };
7358 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7359 Orientation::ORIENTATION_270;
7360 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7361 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7362 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7363 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7364 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7365 };
7366
7367 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7368
7369 // Configure the touchscreen as being installed in the one of the four different orientations
7370 // relative to the display.
7371 addConfigurationProperty("touch.deviceType", "touchScreen");
7372 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7373 prepareDisplay(ui::ROTATION_0);
7374
Arpit Singha8c236b2023-04-25 13:56:05 +00007375 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00007376
7377 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7378 // orientations of either 90 or 270) this means the display's natural resolution will be
7379 // flipped.
7380 const bool displayRotated =
7381 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7382 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7383 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7384 const Rect physicalFrame{0, 0, width, height};
7385 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7386
7387 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7388 const float expectedPrecisionX = displayRotated ? 4 : 2;
7389 const float expectedPrecisionY = displayRotated ? 2 : 4;
7390
7391 // Test all four corners.
7392 for (int i = 0; i < 4; i++) {
7393 const auto& raw = kRawCorners[i];
7394 processDown(mapper, raw.x, raw.y);
7395 processSync(mapper);
7396 const auto& expected = expectedPoints[i];
7397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7398 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7399 WithCoords(expected.x, expected.y),
7400 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7401 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7402 << "with touchscreen orientation "
7403 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7404 << expected.x << ", " << expected.y << ").";
7405 processUp(mapper);
7406 processSync(mapper);
7407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7408 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7409 WithCoords(expected.x, expected.y))));
7410 }
7411}
7412
Prabir Pradhan82687402022-12-06 01:32:53 +00007413TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7414 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7415 kMappedCorners = {
7416 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7417 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7418 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7419 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7420 };
7421
7422 const ui::Rotation displayRotation = GetParam();
7423
7424 addConfigurationProperty("touch.deviceType", "touchScreen");
7425 prepareDisplay(displayRotation);
7426
Arpit Singha8c236b2023-04-25 13:56:05 +00007427 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00007428
7429 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7430
7431 // Test all four corners.
7432 for (int i = 0; i < 4; i++) {
7433 const auto& expected = expectedPoints[i];
7434 const auto& raw = kRawCorners[i];
7435 processDown(mapper, raw.x, raw.y);
7436 processSync(mapper);
7437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7438 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7439 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7440 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7441 << "with display rotation " << ui::toCString(displayRotation)
7442 << ", expected point (" << expected.x << ", " << expected.y << ").";
7443 processUp(mapper);
7444 processSync(mapper);
7445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7446 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7447 WithCoords(expected.x, expected.y))));
7448 }
7449}
7450
Prabir Pradhan3e798762022-12-02 21:02:11 +00007451TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7452 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7453 kMappedCorners = {
7454 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7455 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7456 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7457 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7458 };
7459
7460 const ui::Rotation displayRotation = GetParam();
7461
7462 addConfigurationProperty("touch.deviceType", "touchScreen");
7463 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7464
Arpit Singha8c236b2023-04-25 13:56:05 +00007465 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00007466
7467 // Ori 270, so width and height swapped
7468 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7469 prepareDisplay(displayRotation);
7470 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7471
7472 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7473
7474 // Test all four corners.
7475 for (int i = 0; i < 4; i++) {
7476 const auto& expected = expectedPoints[i];
7477 const auto& raw = kRawCorners[i];
7478 processDown(mapper, raw.x, raw.y);
7479 processSync(mapper);
7480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7481 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7482 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7483 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7484 << "with display rotation " << ui::toCString(displayRotation)
7485 << ", expected point (" << expected.x << ", " << expected.y << ").";
7486 processUp(mapper);
7487 processSync(mapper);
7488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7489 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7490 WithCoords(expected.x, expected.y))));
7491 }
7492}
7493
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007494TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7495 const ui::Rotation displayRotation = GetParam();
7496
7497 addConfigurationProperty("touch.deviceType", "touchScreen");
7498 prepareDisplay(displayRotation);
7499
7500 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00007501 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007502
7503 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7504 // MotionRanges use display pixels as their units
7505 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7506 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7507
7508 // The MotionRanges should be oriented in the rotated display's coordinate space
7509 const bool displayRotated =
7510 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7511
7512 constexpr float MAX_X = 479.5;
7513 constexpr float MAX_Y = 799.75;
7514 EXPECT_EQ(xRange->min, 0.f);
7515 EXPECT_EQ(yRange->min, 0.f);
7516 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7517 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7518
7519 EXPECT_EQ(xRange->flat, 8.f);
7520 EXPECT_EQ(yRange->flat, 8.f);
7521
7522 EXPECT_EQ(xRange->fuzz, 2.f);
7523 EXPECT_EQ(yRange->fuzz, 2.f);
7524
7525 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7526 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7527}
7528
Prabir Pradhana9df3162022-12-05 23:57:27 +00007529// Run the precision tests for all rotations.
7530INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7531 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7532 ui::ROTATION_270),
7533 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7534 return ftl::enum_string(testParamInfo.param);
7535 });
7536
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007537// --- ExternalStylusFusionTest ---
7538
7539class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7540public:
7541 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7542 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007543 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007544 prepareButtons();
7545 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007546 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007547
7548 mStylusState.when = ARBITRARY_TIME;
7549 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007550 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007551 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007552 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007553 processExternalStylusState(mapper);
7554 return mapper;
7555 }
7556
7557 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7558 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7559 for (const NotifyArgs& args : generatedArgs) {
7560 mFakeListener->notify(args);
7561 }
7562 // Loop the reader to flush the input listener queue.
7563 mReader->loopOnce();
7564 return generatedArgs;
7565 }
7566
7567protected:
7568 StylusState mStylusState{};
7569 static constexpr uint32_t EXPECTED_SOURCE =
7570 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7571
7572 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7573 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007574 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007575
7576 // The first pointer is withheld.
7577 processDown(mapper, 100, 200);
7578 processSync(mapper);
7579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7580 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7581 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7582
7583 // The external stylus reports pressure. The withheld finger pointer is released as a
7584 // stylus.
7585 mStylusState.pressure = 1.f;
7586 processExternalStylusState(mapper);
7587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7588 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7589 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7590
7591 // Subsequent pointer events are not withheld.
7592 processMove(mapper, 101, 201);
7593 processSync(mapper);
7594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7595 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7596
7597 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7599 }
7600
7601 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7602 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7603
7604 // Releasing the touch pointer ends the gesture.
7605 processUp(mapper);
7606 processSync(mapper);
7607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7608 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007609 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007610
7611 mStylusState.pressure = 0.f;
7612 processExternalStylusState(mapper);
7613 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7615 }
7616
7617 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7618 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007619 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007620
7621 // The first pointer is withheld when an external stylus is connected,
7622 // and a timeout is requested.
7623 processDown(mapper, 100, 200);
7624 processSync(mapper);
7625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7626 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7627 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7628
7629 // If the timeout expires early, it is requested again.
7630 handleTimeout(mapper, ARBITRARY_TIME + 1);
7631 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7632 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7633
7634 // When the timeout expires, the withheld touch is released as a finger pointer.
7635 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7637 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7638
7639 // Subsequent pointer events are not withheld.
7640 processMove(mapper, 101, 201);
7641 processSync(mapper);
7642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7643 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7644 processUp(mapper);
7645 processSync(mapper);
7646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7647 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7648
7649 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7651 }
7652
7653private:
7654 InputDeviceInfo mExternalStylusDeviceInfo{};
7655};
7656
7657TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7658 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7659 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7660}
7661
7662TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7663 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7664 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7665}
7666
7667TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7668 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7669 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7670}
7671
7672// Test a successful stylus fusion gesture where the pressure is reported by the external
7673// before the touch is reported by the touchscreen.
7674TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7675 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7676 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007677 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007678
7679 // The external stylus reports pressure first. It is ignored for now.
7680 mStylusState.pressure = 1.f;
7681 processExternalStylusState(mapper);
7682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7683 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7684
7685 // When the touch goes down afterwards, it is reported as a stylus pointer.
7686 processDown(mapper, 100, 200);
7687 processSync(mapper);
7688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7689 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7690 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7691
7692 processMove(mapper, 101, 201);
7693 processSync(mapper);
7694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7695 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7696 processUp(mapper);
7697 processSync(mapper);
7698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7699 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7700
7701 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7703}
7704
7705TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7706 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7707
7708 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7709 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7710
7711 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7712 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7713 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7714 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7715}
7716
7717TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7718 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7719 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007720 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007721
7722 mStylusState.pressure = 0.8f;
7723 processExternalStylusState(mapper);
7724 processDown(mapper, 100, 200);
7725 processSync(mapper);
7726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7727 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7728 WithPressure(0.8f))));
7729 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7730
7731 // The external stylus reports a pressure change. We wait for some time for a touch event.
7732 mStylusState.pressure = 0.6f;
7733 processExternalStylusState(mapper);
7734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7735 ASSERT_NO_FATAL_FAILURE(
7736 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7737
7738 // If a touch is reported within the timeout, it reports the updated pressure.
7739 processMove(mapper, 101, 201);
7740 processSync(mapper);
7741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7742 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7743 WithPressure(0.6f))));
7744 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7745
7746 // There is another pressure change.
7747 mStylusState.pressure = 0.5f;
7748 processExternalStylusState(mapper);
7749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7750 ASSERT_NO_FATAL_FAILURE(
7751 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7752
7753 // If a touch is not reported within the timeout, a move event is generated to report
7754 // the new pressure.
7755 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7757 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7758 WithPressure(0.5f))));
7759
7760 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7761 // repeated indefinitely.
7762 mStylusState.pressure = 0.0f;
7763 processExternalStylusState(mapper);
7764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7765 ASSERT_NO_FATAL_FAILURE(
7766 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7767 processMove(mapper, 102, 202);
7768 processSync(mapper);
7769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7770 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7771 WithPressure(0.5f))));
7772 processMove(mapper, 103, 203);
7773 processSync(mapper);
7774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7775 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7776 WithPressure(0.5f))));
7777
7778 processUp(mapper);
7779 processSync(mapper);
7780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7781 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007782 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007783
7784 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7786}
7787
7788TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7789 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7790 auto source = WithSource(EXPECTED_SOURCE);
7791
7792 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007793 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007794 processExternalStylusState(mapper);
7795 processDown(mapper, 100, 200);
7796 processSync(mapper);
7797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7798 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007799 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007800 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7801
7802 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007803 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007804 processExternalStylusState(mapper);
7805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7806 ASSERT_NO_FATAL_FAILURE(
7807 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7808
7809 // If a touch is reported within the timeout, it reports the updated pressure.
7810 processMove(mapper, 101, 201);
7811 processSync(mapper);
7812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7813 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007814 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007815 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7816
7817 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007818 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007819 processExternalStylusState(mapper);
7820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7821 ASSERT_NO_FATAL_FAILURE(
7822 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7823
7824 // If a touch is not reported within the timeout, a move event is generated to report
7825 // the new tool type.
7826 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7828 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007829 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007830
7831 processUp(mapper);
7832 processSync(mapper);
7833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7834 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007835 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007836
7837 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7839}
7840
7841TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7842 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7843 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007844 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007845
7846 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7847
7848 // The external stylus reports a button change. We wait for some time for a touch event.
7849 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7850 processExternalStylusState(mapper);
7851 ASSERT_NO_FATAL_FAILURE(
7852 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7853
7854 // If a touch is reported within the timeout, it reports the updated button state.
7855 processMove(mapper, 101, 201);
7856 processSync(mapper);
7857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7858 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7859 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7861 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7862 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7863 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7864
7865 // The button is now released.
7866 mStylusState.buttons = 0;
7867 processExternalStylusState(mapper);
7868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7869 ASSERT_NO_FATAL_FAILURE(
7870 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7871
7872 // If a touch is not reported within the timeout, a move event is generated to report
7873 // the new button state.
7874 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7876 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7877 WithButtonState(0))));
7878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007879 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7880 WithButtonState(0))));
7881
7882 processUp(mapper);
7883 processSync(mapper);
7884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007885 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7886
7887 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7889}
7890
Michael Wrightd02c5b62014-02-10 15:10:22 -08007891// --- MultiTouchInputMapperTest ---
7892
7893class MultiTouchInputMapperTest : public TouchInputMapperTest {
7894protected:
7895 void prepareAxes(int axes);
7896
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007897 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7898 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7899 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7900 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7901 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7902 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7903 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7904 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7905 void processId(MultiTouchInputMapper& mapper, int32_t id);
7906 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7907 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7908 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007909 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007910 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007911 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7912 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007913};
7914
7915void MultiTouchInputMapperTest::prepareAxes(int axes) {
7916 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007917 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7918 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007919 }
7920 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007921 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7922 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007923 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007924 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7925 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007926 }
7927 }
7928 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007929 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7930 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007931 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007932 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007933 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007934 }
7935 }
7936 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007937 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7938 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007939 }
7940 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007941 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7942 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007943 }
7944 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007945 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7946 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007947 }
7948 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007949 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7950 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007951 }
7952 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007953 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7954 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007955 }
7956 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007957 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007958 }
7959}
7960
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007961void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7962 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7964 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007965}
7966
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007967void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7968 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007970}
7971
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007972void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7973 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007974 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007975}
7976
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007977void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007978 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007979}
7980
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007981void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007982 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007983}
7984
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007985void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7986 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007987 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007988}
7989
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007990void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007992}
7993
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007994void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007996}
7997
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007998void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008000}
8001
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008002void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008004}
8005
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008006void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008007 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008008}
8009
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008010void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
8011 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008013}
8014
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008015void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
8016 int32_t value) {
8017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
8018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
8019}
8020
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008021void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008023}
8024
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00008025void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
8026 nsecs_t readTime) {
8027 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008028}
8029
Michael Wrightd02c5b62014-02-10 15:10:22 -08008030TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008031 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008032 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008033 prepareAxes(POSITION);
8034 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008035 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008036
arthurhungdcef2dc2020-08-11 14:47:50 +08008037 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008038
8039 NotifyMotionArgs motionArgs;
8040
8041 // Two fingers down at once.
8042 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8043 processPosition(mapper, x1, y1);
8044 processMTSync(mapper);
8045 processPosition(mapper, x2, y2);
8046 processMTSync(mapper);
8047 processSync(mapper);
8048
8049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8050 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8051 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8052 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8053 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8054 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8055 ASSERT_EQ(0, motionArgs.flags);
8056 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8057 ASSERT_EQ(0, motionArgs.buttonState);
8058 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008059 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008060 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008061 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8063 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8064 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8065 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8066 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8067
8068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8069 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8070 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8071 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8072 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008073 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008074 ASSERT_EQ(0, motionArgs.flags);
8075 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8076 ASSERT_EQ(0, motionArgs.buttonState);
8077 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008078 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008079 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008080 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008081 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008082 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008083 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8084 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8085 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8086 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8087 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8088 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8089 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8090
8091 // Move.
8092 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8093 processPosition(mapper, x1, y1);
8094 processMTSync(mapper);
8095 processPosition(mapper, x2, y2);
8096 processMTSync(mapper);
8097 processSync(mapper);
8098
8099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8100 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8101 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8102 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8103 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8105 ASSERT_EQ(0, motionArgs.flags);
8106 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8107 ASSERT_EQ(0, motionArgs.buttonState);
8108 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008109 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008110 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008111 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008112 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008113 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008114 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8115 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8117 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8118 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8119 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8120 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8121
8122 // First finger up.
8123 x2 += 15; y2 -= 20;
8124 processPosition(mapper, x2, y2);
8125 processMTSync(mapper);
8126 processSync(mapper);
8127
8128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8129 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8130 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8131 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8132 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008133 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008134 ASSERT_EQ(0, motionArgs.flags);
8135 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8136 ASSERT_EQ(0, motionArgs.buttonState);
8137 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008138 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008139 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008140 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008141 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008142 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008143 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8144 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8146 toDisplayX(x2), toDisplayY(y2), 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8152 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8153 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8154 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8155 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8157 ASSERT_EQ(0, motionArgs.flags);
8158 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8159 ASSERT_EQ(0, motionArgs.buttonState);
8160 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008161 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008162 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008163 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008164 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8165 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8166 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8167 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8168 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8169
8170 // Move.
8171 x2 += 20; y2 -= 25;
8172 processPosition(mapper, x2, y2);
8173 processMTSync(mapper);
8174 processSync(mapper);
8175
8176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8177 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8178 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8179 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8180 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8181 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8182 ASSERT_EQ(0, motionArgs.flags);
8183 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8184 ASSERT_EQ(0, motionArgs.buttonState);
8185 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008186 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008187 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008188 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008189 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8190 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8191 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8192 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8193 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8194
8195 // New finger down.
8196 int32_t x3 = 700, y3 = 300;
8197 processPosition(mapper, x2, y2);
8198 processMTSync(mapper);
8199 processPosition(mapper, x3, y3);
8200 processMTSync(mapper);
8201 processSync(mapper);
8202
8203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8204 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8205 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8206 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8207 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008208 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008209 ASSERT_EQ(0, motionArgs.flags);
8210 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8211 ASSERT_EQ(0, motionArgs.buttonState);
8212 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008213 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008214 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008215 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008216 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008217 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8219 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8220 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8221 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8222 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8223 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8224 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8225
8226 // Second finger up.
8227 x3 += 30; y3 -= 20;
8228 processPosition(mapper, x3, y3);
8229 processMTSync(mapper);
8230 processSync(mapper);
8231
8232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8233 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8234 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8235 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8236 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008237 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008238 ASSERT_EQ(0, motionArgs.flags);
8239 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8240 ASSERT_EQ(0, motionArgs.buttonState);
8241 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008242 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008243 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008244 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008245 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008246 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008247 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8248 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8250 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8251 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8252 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8253 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8254
8255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8256 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8257 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8258 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8259 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8261 ASSERT_EQ(0, motionArgs.flags);
8262 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8263 ASSERT_EQ(0, motionArgs.buttonState);
8264 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008265 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008266 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008267 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8269 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8270 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8271 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8272 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8273
8274 // Last finger up.
8275 processMTSync(mapper);
8276 processSync(mapper);
8277
8278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8279 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8280 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8281 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8282 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8283 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8284 ASSERT_EQ(0, motionArgs.flags);
8285 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8286 ASSERT_EQ(0, motionArgs.buttonState);
8287 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008288 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008289 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008290 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8292 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8293 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8294 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8295 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8296
8297 // Should not have sent any more keys or motions.
8298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8300}
8301
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008302TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8303 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008304 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008305
8306 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8307 /*fuzz*/ 0, /*resolution*/ 10);
8308 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8309 /*fuzz*/ 0, /*resolution*/ 11);
8310 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8311 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8312 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8313 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8314 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8315 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8316 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8317 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8318
Arpit Singha8c236b2023-04-25 13:56:05 +00008319 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008320
8321 // X and Y axes
8322 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8323 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8324 // Touch major and minor
8325 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8326 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8327 // Tool major and minor
8328 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8329 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8330}
8331
8332TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8333 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008334 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008335
8336 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8337 /*fuzz*/ 0, /*resolution*/ 10);
8338 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8339 /*fuzz*/ 0, /*resolution*/ 11);
8340
8341 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8342
Arpit Singha8c236b2023-04-25 13:56:05 +00008343 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008344
8345 // Touch major and minor
8346 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8347 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8348 // Tool major and minor
8349 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8350 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8351}
8352
Michael Wrightd02c5b62014-02-10 15:10:22 -08008353TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008354 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008355 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008356 prepareAxes(POSITION | ID);
8357 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008358 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008359
arthurhungdcef2dc2020-08-11 14:47:50 +08008360 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008361
8362 NotifyMotionArgs motionArgs;
8363
8364 // Two fingers down at once.
8365 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8366 processPosition(mapper, x1, y1);
8367 processId(mapper, 1);
8368 processMTSync(mapper);
8369 processPosition(mapper, x2, y2);
8370 processId(mapper, 2);
8371 processMTSync(mapper);
8372 processSync(mapper);
8373
8374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8375 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008376 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008377 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008378 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8380 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8381
8382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008383 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008384 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008385 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008386 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008387 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008388 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8390 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8392 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8393
8394 // Move.
8395 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8396 processPosition(mapper, x1, y1);
8397 processId(mapper, 1);
8398 processMTSync(mapper);
8399 processPosition(mapper, x2, y2);
8400 processId(mapper, 2);
8401 processMTSync(mapper);
8402 processSync(mapper);
8403
8404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8405 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008406 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008407 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008408 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008409 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008410 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8412 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8414 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8415
8416 // First finger up.
8417 x2 += 15; y2 -= 20;
8418 processPosition(mapper, x2, y2);
8419 processId(mapper, 2);
8420 processMTSync(mapper);
8421 processSync(mapper);
8422
8423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008424 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008425 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008426 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008427 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008428 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008429 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8431 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8433 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8434
8435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8436 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008437 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008438 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008439 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8441 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8442
8443 // Move.
8444 x2 += 20; y2 -= 25;
8445 processPosition(mapper, x2, y2);
8446 processId(mapper, 2);
8447 processMTSync(mapper);
8448 processSync(mapper);
8449
8450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008452 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008453 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008454 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8456 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8457
8458 // New finger down.
8459 int32_t x3 = 700, y3 = 300;
8460 processPosition(mapper, x2, y2);
8461 processId(mapper, 2);
8462 processMTSync(mapper);
8463 processPosition(mapper, x3, y3);
8464 processId(mapper, 3);
8465 processMTSync(mapper);
8466 processSync(mapper);
8467
8468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008469 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008470 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008471 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008472 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008473 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008474 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8476 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8478 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8479
8480 // Second finger up.
8481 x3 += 30; y3 -= 20;
8482 processPosition(mapper, x3, y3);
8483 processId(mapper, 3);
8484 processMTSync(mapper);
8485 processSync(mapper);
8486
8487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008488 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008489 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008490 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008491 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008492 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008493 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8495 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8497 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8498
8499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8500 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008501 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008502 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008503 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008504 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8505 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8506
8507 // Last finger up.
8508 processMTSync(mapper);
8509 processSync(mapper);
8510
8511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8512 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008513 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008514 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008515 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8517 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8518
8519 // Should not have sent any more keys or motions.
8520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8522}
8523
8524TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008525 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008526 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008527 prepareAxes(POSITION | ID | SLOT);
8528 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008529 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008530
arthurhungdcef2dc2020-08-11 14:47:50 +08008531 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008532
8533 NotifyMotionArgs motionArgs;
8534
8535 // Two fingers down at once.
8536 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8537 processPosition(mapper, x1, y1);
8538 processId(mapper, 1);
8539 processSlot(mapper, 1);
8540 processPosition(mapper, x2, y2);
8541 processId(mapper, 2);
8542 processSync(mapper);
8543
8544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8545 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008546 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008547 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008548 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8550 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8551
8552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008553 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008554 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008555 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008556 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008557 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008558 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8560 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8562 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8563
8564 // Move.
8565 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8566 processSlot(mapper, 0);
8567 processPosition(mapper, x1, y1);
8568 processSlot(mapper, 1);
8569 processPosition(mapper, x2, y2);
8570 processSync(mapper);
8571
8572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8573 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008574 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008575 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008576 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008577 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008578 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8580 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8582 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8583
8584 // First finger up.
8585 x2 += 15; y2 -= 20;
8586 processSlot(mapper, 0);
8587 processId(mapper, -1);
8588 processSlot(mapper, 1);
8589 processPosition(mapper, x2, y2);
8590 processSync(mapper);
8591
8592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008593 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008594 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008595 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008596 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008597 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008598 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8600 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8602 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8603
8604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8605 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008606 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008607 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008608 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008609 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8610 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8611
8612 // Move.
8613 x2 += 20; y2 -= 25;
8614 processPosition(mapper, x2, y2);
8615 processSync(mapper);
8616
8617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008619 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008620 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008621 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8623 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8624
8625 // New finger down.
8626 int32_t x3 = 700, y3 = 300;
8627 processPosition(mapper, x2, y2);
8628 processSlot(mapper, 0);
8629 processId(mapper, 3);
8630 processPosition(mapper, x3, y3);
8631 processSync(mapper);
8632
8633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008634 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008635 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008636 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008637 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008638 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008639 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8641 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8642 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8643 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8644
8645 // Second finger up.
8646 x3 += 30; y3 -= 20;
8647 processSlot(mapper, 1);
8648 processId(mapper, -1);
8649 processSlot(mapper, 0);
8650 processPosition(mapper, x3, y3);
8651 processSync(mapper);
8652
8653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008654 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008655 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008656 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008657 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008658 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008659 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8661 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8663 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8664
8665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008667 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008668 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008669 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8671 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8672
8673 // Last finger up.
8674 processId(mapper, -1);
8675 processSync(mapper);
8676
8677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8678 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008679 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008680 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008681 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8683 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8684
8685 // Should not have sent any more keys or motions.
8686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8688}
8689
8690TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008691 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008692 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008693 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008694 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008695
8696 // These calculations are based on the input device calibration documentation.
8697 int32_t rawX = 100;
8698 int32_t rawY = 200;
8699 int32_t rawTouchMajor = 7;
8700 int32_t rawTouchMinor = 6;
8701 int32_t rawToolMajor = 9;
8702 int32_t rawToolMinor = 8;
8703 int32_t rawPressure = 11;
8704 int32_t rawDistance = 0;
8705 int32_t rawOrientation = 3;
8706 int32_t id = 5;
8707
8708 float x = toDisplayX(rawX);
8709 float y = toDisplayY(rawY);
8710 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8711 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8712 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8713 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8714 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8715 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8716 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8717 float distance = float(rawDistance);
8718
8719 processPosition(mapper, rawX, rawY);
8720 processTouchMajor(mapper, rawTouchMajor);
8721 processTouchMinor(mapper, rawTouchMinor);
8722 processToolMajor(mapper, rawToolMajor);
8723 processToolMinor(mapper, rawToolMinor);
8724 processPressure(mapper, rawPressure);
8725 processOrientation(mapper, rawOrientation);
8726 processDistance(mapper, rawDistance);
8727 processId(mapper, id);
8728 processMTSync(mapper);
8729 processSync(mapper);
8730
8731 NotifyMotionArgs args;
8732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8733 ASSERT_EQ(0, args.pointerProperties[0].id);
8734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8735 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8736 orientation, distance));
8737}
8738
8739TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008740 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008741 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008742 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8743 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00008744 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008745
8746 // These calculations are based on the input device calibration documentation.
8747 int32_t rawX = 100;
8748 int32_t rawY = 200;
8749 int32_t rawTouchMajor = 140;
8750 int32_t rawTouchMinor = 120;
8751 int32_t rawToolMajor = 180;
8752 int32_t rawToolMinor = 160;
8753
8754 float x = toDisplayX(rawX);
8755 float y = toDisplayY(rawY);
8756 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8757 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8758 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8759 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8760 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8761
8762 processPosition(mapper, rawX, rawY);
8763 processTouchMajor(mapper, rawTouchMajor);
8764 processTouchMinor(mapper, rawTouchMinor);
8765 processToolMajor(mapper, rawToolMajor);
8766 processToolMinor(mapper, rawToolMinor);
8767 processMTSync(mapper);
8768 processSync(mapper);
8769
8770 NotifyMotionArgs args;
8771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8772 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8773 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8774}
8775
8776TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008777 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008778 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008779 prepareAxes(POSITION | TOUCH | TOOL);
8780 addConfigurationProperty("touch.size.calibration", "diameter");
8781 addConfigurationProperty("touch.size.scale", "10");
8782 addConfigurationProperty("touch.size.bias", "160");
8783 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00008784 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008785
8786 // These calculations are based on the input device calibration documentation.
8787 // Note: We only provide a single common touch/tool value because the device is assumed
8788 // not to emit separate values for each pointer (isSummed = 1).
8789 int32_t rawX = 100;
8790 int32_t rawY = 200;
8791 int32_t rawX2 = 150;
8792 int32_t rawY2 = 250;
8793 int32_t rawTouchMajor = 5;
8794 int32_t rawToolMajor = 8;
8795
8796 float x = toDisplayX(rawX);
8797 float y = toDisplayY(rawY);
8798 float x2 = toDisplayX(rawX2);
8799 float y2 = toDisplayY(rawY2);
8800 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8801 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8802 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8803
8804 processPosition(mapper, rawX, rawY);
8805 processTouchMajor(mapper, rawTouchMajor);
8806 processToolMajor(mapper, rawToolMajor);
8807 processMTSync(mapper);
8808 processPosition(mapper, rawX2, rawY2);
8809 processTouchMajor(mapper, rawTouchMajor);
8810 processToolMajor(mapper, rawToolMajor);
8811 processMTSync(mapper);
8812 processSync(mapper);
8813
8814 NotifyMotionArgs args;
8815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8816 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8817
8818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008819 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008820 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8822 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8823 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8824 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8825}
8826
8827TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008828 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008829 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008830 prepareAxes(POSITION | TOUCH | TOOL);
8831 addConfigurationProperty("touch.size.calibration", "area");
8832 addConfigurationProperty("touch.size.scale", "43");
8833 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00008834 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008835
8836 // These calculations are based on the input device calibration documentation.
8837 int32_t rawX = 100;
8838 int32_t rawY = 200;
8839 int32_t rawTouchMajor = 5;
8840 int32_t rawToolMajor = 8;
8841
8842 float x = toDisplayX(rawX);
8843 float y = toDisplayY(rawY);
8844 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8845 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8846 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8847
8848 processPosition(mapper, rawX, rawY);
8849 processTouchMajor(mapper, rawTouchMajor);
8850 processToolMajor(mapper, rawToolMajor);
8851 processMTSync(mapper);
8852 processSync(mapper);
8853
8854 NotifyMotionArgs args;
8855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8856 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8857 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8858}
8859
8860TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008861 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008862 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008863 prepareAxes(POSITION | PRESSURE);
8864 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8865 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00008866 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008867
Michael Wrightaa449c92017-12-13 21:21:43 +00008868 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008869 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008870 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8871 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8872 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8873
Michael Wrightd02c5b62014-02-10 15:10:22 -08008874 // These calculations are based on the input device calibration documentation.
8875 int32_t rawX = 100;
8876 int32_t rawY = 200;
8877 int32_t rawPressure = 60;
8878
8879 float x = toDisplayX(rawX);
8880 float y = toDisplayY(rawY);
8881 float pressure = float(rawPressure) * 0.01f;
8882
8883 processPosition(mapper, rawX, rawY);
8884 processPressure(mapper, rawPressure);
8885 processMTSync(mapper);
8886 processSync(mapper);
8887
8888 NotifyMotionArgs args;
8889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8891 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8892}
8893
8894TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008895 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008896 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008897 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008898 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008899
8900 NotifyMotionArgs motionArgs;
8901 NotifyKeyArgs keyArgs;
8902
8903 processId(mapper, 1);
8904 processPosition(mapper, 100, 200);
8905 processSync(mapper);
8906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8907 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8908 ASSERT_EQ(0, motionArgs.buttonState);
8909
8910 // press BTN_LEFT, release BTN_LEFT
8911 processKey(mapper, BTN_LEFT, 1);
8912 processSync(mapper);
8913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8914 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8915 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8916
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8918 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8919 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8920
Michael Wrightd02c5b62014-02-10 15:10:22 -08008921 processKey(mapper, BTN_LEFT, 0);
8922 processSync(mapper);
8923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008924 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008925 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008926
8927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008928 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008929 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008930
8931 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8932 processKey(mapper, BTN_RIGHT, 1);
8933 processKey(mapper, BTN_MIDDLE, 1);
8934 processSync(mapper);
8935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8936 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8937 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8938 motionArgs.buttonState);
8939
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8941 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8942 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, 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_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8947 motionArgs.buttonState);
8948
Michael Wrightd02c5b62014-02-10 15:10:22 -08008949 processKey(mapper, BTN_RIGHT, 0);
8950 processSync(mapper);
8951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008952 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008953 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008954
8955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008957 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008958
8959 processKey(mapper, BTN_MIDDLE, 0);
8960 processSync(mapper);
8961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008962 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008963 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008964
8965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008967 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008968
8969 // press BTN_BACK, release BTN_BACK
8970 processKey(mapper, BTN_BACK, 1);
8971 processSync(mapper);
8972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8973 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8974 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008975
Michael Wrightd02c5b62014-02-10 15:10:22 -08008976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008977 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008978 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8979
8980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8981 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8982 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008983
8984 processKey(mapper, BTN_BACK, 0);
8985 processSync(mapper);
8986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008987 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008988 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008989
8990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008991 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008992 ASSERT_EQ(0, motionArgs.buttonState);
8993
Michael Wrightd02c5b62014-02-10 15:10:22 -08008994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8995 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8996 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8997
8998 // press BTN_SIDE, release BTN_SIDE
8999 processKey(mapper, BTN_SIDE, 1);
9000 processSync(mapper);
9001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9002 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9003 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009004
Michael Wrightd02c5b62014-02-10 15:10:22 -08009005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009007 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
9008
9009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9010 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9011 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009012
9013 processKey(mapper, BTN_SIDE, 0);
9014 processSync(mapper);
9015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009016 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009017 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009018
9019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009021 ASSERT_EQ(0, motionArgs.buttonState);
9022
Michael Wrightd02c5b62014-02-10 15:10:22 -08009023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9024 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9025 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9026
9027 // press BTN_FORWARD, release BTN_FORWARD
9028 processKey(mapper, BTN_FORWARD, 1);
9029 processSync(mapper);
9030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9031 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9032 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009033
Michael Wrightd02c5b62014-02-10 15:10:22 -08009034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009035 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009036 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9037
9038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9039 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9040 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009041
9042 processKey(mapper, BTN_FORWARD, 0);
9043 processSync(mapper);
9044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009045 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009046 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009047
9048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009049 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009050 ASSERT_EQ(0, motionArgs.buttonState);
9051
Michael Wrightd02c5b62014-02-10 15:10:22 -08009052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9053 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9054 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9055
9056 // press BTN_EXTRA, release BTN_EXTRA
9057 processKey(mapper, BTN_EXTRA, 1);
9058 processSync(mapper);
9059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9060 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9061 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009062
Michael Wrightd02c5b62014-02-10 15:10:22 -08009063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009065 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9066
9067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9068 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9069 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009070
9071 processKey(mapper, BTN_EXTRA, 0);
9072 processSync(mapper);
9073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009074 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009075 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009076
9077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009078 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009079 ASSERT_EQ(0, motionArgs.buttonState);
9080
Michael Wrightd02c5b62014-02-10 15:10:22 -08009081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9082 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9083 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9084
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9086
Michael Wrightd02c5b62014-02-10 15:10:22 -08009087 // press BTN_STYLUS, release BTN_STYLUS
9088 processKey(mapper, BTN_STYLUS, 1);
9089 processSync(mapper);
9090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9091 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009092 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
9093
9094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9095 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9096 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009097
9098 processKey(mapper, BTN_STYLUS, 0);
9099 processSync(mapper);
9100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009101 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009102 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009103
9104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009105 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009106 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009107
9108 // press BTN_STYLUS2, release BTN_STYLUS2
9109 processKey(mapper, BTN_STYLUS2, 1);
9110 processSync(mapper);
9111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9112 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009113 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
9114
9115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9116 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9117 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009118
9119 processKey(mapper, BTN_STYLUS2, 0);
9120 processSync(mapper);
9121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009122 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009123 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009124
9125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009127 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009128
9129 // release touch
9130 processId(mapper, -1);
9131 processSync(mapper);
9132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9133 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9134 ASSERT_EQ(0, motionArgs.buttonState);
9135}
9136
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009137TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
9138 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009139 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009140 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00009141 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009142
9143 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
9144 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
9145
9146 // Touch down.
9147 processId(mapper, 1);
9148 processPosition(mapper, 100, 200);
9149 processSync(mapper);
9150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9151 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
9152
9153 // Press and release button mapped to the primary stylus button.
9154 processKey(mapper, BTN_A, 1);
9155 processSync(mapper);
9156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9157 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9158 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9160 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9161 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9162
9163 processKey(mapper, BTN_A, 0);
9164 processSync(mapper);
9165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9166 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9168 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9169
9170 // Press and release the HID usage mapped to the secondary stylus button.
9171 processHidUsage(mapper, 0xabcd, 1);
9172 processSync(mapper);
9173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9174 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9175 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9177 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9178 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9179
9180 processHidUsage(mapper, 0xabcd, 0);
9181 processSync(mapper);
9182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9183 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9185 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9186
9187 // Release touch.
9188 processId(mapper, -1);
9189 processSync(mapper);
9190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9191 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
9192}
9193
Michael Wrightd02c5b62014-02-10 15:10:22 -08009194TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009195 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009196 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009197 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009198 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009199
9200 NotifyMotionArgs motionArgs;
9201
9202 // default tool type is finger
9203 processId(mapper, 1);
9204 processPosition(mapper, 100, 200);
9205 processSync(mapper);
9206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9207 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009208 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009209
9210 // eraser
9211 processKey(mapper, BTN_TOOL_RUBBER, 1);
9212 processSync(mapper);
9213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009215 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009216
9217 // stylus
9218 processKey(mapper, BTN_TOOL_RUBBER, 0);
9219 processKey(mapper, BTN_TOOL_PEN, 1);
9220 processSync(mapper);
9221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009223 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009224
9225 // brush
9226 processKey(mapper, BTN_TOOL_PEN, 0);
9227 processKey(mapper, BTN_TOOL_BRUSH, 1);
9228 processSync(mapper);
9229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9230 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009231 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009232
9233 // pencil
9234 processKey(mapper, BTN_TOOL_BRUSH, 0);
9235 processKey(mapper, BTN_TOOL_PENCIL, 1);
9236 processSync(mapper);
9237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9238 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009239 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009240
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009241 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009242 processKey(mapper, BTN_TOOL_PENCIL, 0);
9243 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
9244 processSync(mapper);
9245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009247 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009248
9249 // mouse
9250 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9251 processKey(mapper, BTN_TOOL_MOUSE, 1);
9252 processSync(mapper);
9253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9254 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009255 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009256
9257 // lens
9258 processKey(mapper, BTN_TOOL_MOUSE, 0);
9259 processKey(mapper, BTN_TOOL_LENS, 1);
9260 processSync(mapper);
9261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9262 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009263 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009264
9265 // double-tap
9266 processKey(mapper, BTN_TOOL_LENS, 0);
9267 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9268 processSync(mapper);
9269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9270 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009271 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009272
9273 // triple-tap
9274 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9275 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9276 processSync(mapper);
9277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9278 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009279 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009280
9281 // quad-tap
9282 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9283 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9284 processSync(mapper);
9285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9286 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009287 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009288
9289 // finger
9290 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9291 processKey(mapper, BTN_TOOL_FINGER, 1);
9292 processSync(mapper);
9293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9294 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009295 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009296
9297 // stylus trumps finger
9298 processKey(mapper, BTN_TOOL_PEN, 1);
9299 processSync(mapper);
9300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9301 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009302 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009303
9304 // eraser trumps stylus
9305 processKey(mapper, BTN_TOOL_RUBBER, 1);
9306 processSync(mapper);
9307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009309 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009310
9311 // mouse trumps eraser
9312 processKey(mapper, BTN_TOOL_MOUSE, 1);
9313 processSync(mapper);
9314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9315 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009316 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009317
9318 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9319 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9320 processSync(mapper);
9321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9322 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009323 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009324
9325 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9326 processToolType(mapper, MT_TOOL_PEN);
9327 processSync(mapper);
9328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009330 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009331
9332 // back to default tool type
9333 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9334 processKey(mapper, BTN_TOOL_MOUSE, 0);
9335 processKey(mapper, BTN_TOOL_RUBBER, 0);
9336 processKey(mapper, BTN_TOOL_PEN, 0);
9337 processKey(mapper, BTN_TOOL_FINGER, 0);
9338 processSync(mapper);
9339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9340 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009341 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009342}
9343
9344TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009345 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009346 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009347 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009348 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009349 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009350
9351 NotifyMotionArgs motionArgs;
9352
9353 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9354 processId(mapper, 1);
9355 processPosition(mapper, 100, 200);
9356 processSync(mapper);
9357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9358 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9360 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9361
9362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9363 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9365 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9366
9367 // move a little
9368 processPosition(mapper, 150, 250);
9369 processSync(mapper);
9370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9371 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9373 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9374
9375 // down when BTN_TOUCH is pressed, pressure defaults to 1
9376 processKey(mapper, BTN_TOUCH, 1);
9377 processSync(mapper);
9378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9379 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9381 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9382
9383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9384 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9386 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9387
9388 // up when BTN_TOUCH is released, hover restored
9389 processKey(mapper, BTN_TOUCH, 0);
9390 processSync(mapper);
9391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9392 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9394 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9395
9396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9397 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9399 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9400
9401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9402 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9404 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9405
9406 // exit hover when pointer goes away
9407 processId(mapper, -1);
9408 processSync(mapper);
9409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9410 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9412 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9413}
9414
9415TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009416 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009417 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009418 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009419 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009420
9421 NotifyMotionArgs motionArgs;
9422
9423 // initially hovering because pressure is 0
9424 processId(mapper, 1);
9425 processPosition(mapper, 100, 200);
9426 processPressure(mapper, 0);
9427 processSync(mapper);
9428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9429 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9431 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9432
9433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9434 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9436 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9437
9438 // move a little
9439 processPosition(mapper, 150, 250);
9440 processSync(mapper);
9441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9442 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9444 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9445
9446 // down when pressure becomes non-zero
9447 processPressure(mapper, RAW_PRESSURE_MAX);
9448 processSync(mapper);
9449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9450 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9452 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9453
9454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9455 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9457 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9458
9459 // up when pressure becomes 0, hover restored
9460 processPressure(mapper, 0);
9461 processSync(mapper);
9462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9463 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9465 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9466
9467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9468 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9470 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9471
9472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9473 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9475 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9476
9477 // exit hover when pointer goes away
9478 processId(mapper, -1);
9479 processSync(mapper);
9480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9481 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9482 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9483 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9484}
9485
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009486/**
9487 * Set the input device port <--> display port associations, and check that the
9488 * events are routed to the display that matches the display port.
9489 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9490 */
9491TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009492 const std::string usb2 = "USB2";
9493 const uint8_t hdmi1 = 0;
9494 const uint8_t hdmi2 = 1;
9495 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009496 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009497
9498 addConfigurationProperty("touch.deviceType", "touchScreen");
9499 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009500 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009501
9502 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9503 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9504
9505 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9506 // for this input device is specified, and the matching viewport is not present,
9507 // the input device should be disabled (at the mapper level).
9508
9509 // Add viewport for display 2 on hdmi2
9510 prepareSecondaryDisplay(type, hdmi2);
9511 // Send a touch event
9512 processPosition(mapper, 100, 100);
9513 processSync(mapper);
9514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9515
9516 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009517 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009518 // Send a touch event again
9519 processPosition(mapper, 100, 100);
9520 processSync(mapper);
9521
9522 NotifyMotionArgs args;
9523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9524 ASSERT_EQ(DISPLAY_ID, args.displayId);
9525}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009526
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009527TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9528 addConfigurationProperty("touch.deviceType", "touchScreen");
9529 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009530 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009531
9532 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9533
Michael Wrighta9cf4192022-12-01 23:46:39 +00009534 prepareDisplay(ui::ROTATION_0);
9535 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009536
9537 // Send a touch event
9538 processPosition(mapper, 100, 100);
9539 processSync(mapper);
9540
9541 NotifyMotionArgs args;
9542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9543 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9544}
9545
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009546TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009547 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009548 std::shared_ptr<FakePointerController> fakePointerController =
9549 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009550 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009551 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009552 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009553
Garfield Tan888a6a42020-01-09 11:39:16 -08009554 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009555 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009556
Michael Wrighta9cf4192022-12-01 23:46:39 +00009557 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009558 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009559 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009560
Josep del Río2d8c79a2023-01-23 19:33:50 +00009561 // Check source is mouse that would obtain the PointerController.
9562 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009563
9564 NotifyMotionArgs motionArgs;
9565 processPosition(mapper, 100, 100);
9566 processSync(mapper);
9567
9568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9569 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9570 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9571}
9572
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009573/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009574 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9575 */
9576TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9577 addConfigurationProperty("touch.deviceType", "touchScreen");
9578 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009579 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009580
Michael Wrighta9cf4192022-12-01 23:46:39 +00009581 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009582 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9583 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9584 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9585 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009586
9587 NotifyMotionArgs args;
9588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9589 ASSERT_EQ(26, args.readTime);
9590
Harry Cutts33476232023-01-30 19:57:29 +00009591 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9592 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9593 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009594
9595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9596 ASSERT_EQ(33, args.readTime);
9597}
9598
9599/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009600 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9601 * events should not be delivered to the listener.
9602 */
9603TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9604 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009605 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009606 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009607 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009608 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009609 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009610 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009611
9612 NotifyMotionArgs motionArgs;
9613 processPosition(mapper, 100, 100);
9614 processSync(mapper);
9615
9616 mFakeListener->assertNotifyMotionWasNotCalled();
9617}
9618
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009619/**
9620 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9621 * the touch mapper can process the events and the events can be delivered to the listener.
9622 */
9623TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9624 addConfigurationProperty("touch.deviceType", "touchScreen");
9625 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009626 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009627 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009628 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009629 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009630 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009631
9632 NotifyMotionArgs motionArgs;
9633 processPosition(mapper, 100, 100);
9634 processSync(mapper);
9635
9636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9637 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9638}
9639
Josh Thielene986aed2023-06-01 14:17:30 +00009640/**
9641 * When the viewport is deactivated (isActive transitions from true to false),
9642 * and touch.enableForInactiveViewport is false, touches prior to the transition
9643 * should be cancelled.
9644 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08009645TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9646 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009647 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009648 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009649 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009650 std::optional<DisplayViewport> optionalDisplayViewport =
9651 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9652 ASSERT_TRUE(optionalDisplayViewport.has_value());
9653 DisplayViewport displayViewport = *optionalDisplayViewport;
9654
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009655 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009656 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009657 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08009658
9659 // Finger down
9660 int32_t x = 100, y = 100;
9661 processPosition(mapper, x, y);
9662 processSync(mapper);
9663
9664 NotifyMotionArgs motionArgs;
9665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9666 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9667
9668 // Deactivate display viewport
9669 displayViewport.isActive = false;
9670 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009671 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009672
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009673 // The ongoing touch should be canceled immediately
9674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9675 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9676
9677 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009678 x += 10, y += 10;
9679 processPosition(mapper, x, y);
9680 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009682
9683 // Reactivate display viewport
9684 displayViewport.isActive = true;
9685 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009686 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009687
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009688 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009689 x += 10, y += 10;
9690 processPosition(mapper, x, y);
9691 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9693 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009694}
9695
Josh Thielene986aed2023-06-01 14:17:30 +00009696/**
9697 * When the viewport is deactivated (isActive transitions from true to false),
9698 * and touch.enableForInactiveViewport is true, touches prior to the transition
9699 * should not be cancelled.
9700 */
9701TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
9702 addConfigurationProperty("touch.deviceType", "touchScreen");
9703 addConfigurationProperty("touch.enableForInactiveViewport", "1");
9704 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
9705 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
9706 std::optional<DisplayViewport> optionalDisplayViewport =
9707 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9708 ASSERT_TRUE(optionalDisplayViewport.has_value());
9709 DisplayViewport displayViewport = *optionalDisplayViewport;
9710
9711 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9712 prepareAxes(POSITION);
9713 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
9714
9715 // Finger down
9716 int32_t x = 100, y = 100;
9717 processPosition(mapper, x, y);
9718 processSync(mapper);
9719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9720 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9721
9722 // Deactivate display viewport
9723 displayViewport.isActive = false;
9724 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9725 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9726
9727 // The ongoing touch should not be canceled
9728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9729
9730 // Finger move is not ignored
9731 x += 10, y += 10;
9732 processPosition(mapper, x, y);
9733 processSync(mapper);
9734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9735 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
9736
9737 // Reactivate display viewport
9738 displayViewport.isActive = true;
9739 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9740 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9741
9742 // Finger move continues and does not start new gesture
9743 x += 10, y += 10;
9744 processPosition(mapper, x, y);
9745 processSync(mapper);
9746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9747 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
9748}
9749
Arthur Hung7c645402019-01-25 17:45:42 +08009750TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9751 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009752 prepareAxes(POSITION | ID | SLOT);
9753 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009754 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009755
9756 // Create the second touch screen device, and enable multi fingers.
9757 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009758 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009759 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009760 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009761 std::shared_ptr<InputDevice> device2 =
9762 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009763 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009764
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009765 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009766 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009767 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009768 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009769 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009770 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009771 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009772 /*flat=*/0, /*fuzz=*/0);
9773 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009774 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9775 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009776
9777 // Setup the second touch screen device.
Arpit Singha8c236b2023-04-25 13:56:05 +00009778 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
9779 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
9780 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009781 std::list<NotifyArgs> unused =
9782 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009783 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009784 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009785
9786 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009787 std::shared_ptr<FakePointerController> fakePointerController =
9788 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009789 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009790
9791 // Setup policy for associated displays and show touches.
9792 const uint8_t hdmi1 = 0;
9793 const uint8_t hdmi2 = 1;
9794 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9795 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9796 mFakePolicy->setShowTouches(true);
9797
9798 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009799 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009800 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009801
9802 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009803 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009804 InputReaderConfiguration::Change::DISPLAY_INFO |
9805 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009806
9807 // Two fingers down at default display.
9808 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9809 processPosition(mapper, x1, y1);
9810 processId(mapper, 1);
9811 processSlot(mapper, 1);
9812 processPosition(mapper, x2, y2);
9813 processId(mapper, 2);
9814 processSync(mapper);
9815
9816 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9817 fakePointerController->getSpots().find(DISPLAY_ID);
9818 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9819 ASSERT_EQ(size_t(2), iter->second.size());
9820
9821 // Two fingers down at second display.
9822 processPosition(mapper2, x1, y1);
9823 processId(mapper2, 1);
9824 processSlot(mapper2, 1);
9825 processPosition(mapper2, x2, y2);
9826 processId(mapper2, 2);
9827 processSync(mapper2);
9828
9829 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9830 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9831 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009832
9833 // Disable the show touches configuration and ensure the spots are cleared.
9834 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009835 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009836 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009837
9838 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009839}
9840
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009841TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009842 prepareAxes(POSITION);
9843 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009844 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009845 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009846
9847 NotifyMotionArgs motionArgs;
9848 // Unrotated video frame
9849 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9850 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009851 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009852 processPosition(mapper, 100, 200);
9853 processSync(mapper);
9854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9855 ASSERT_EQ(frames, motionArgs.videoFrames);
9856
9857 // Subsequent touch events should not have any videoframes
9858 // This is implemented separately in FakeEventHub,
9859 // but that should match the behaviour of TouchVideoDevice.
9860 processPosition(mapper, 200, 200);
9861 processSync(mapper);
9862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9863 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9864}
9865
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009866TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009867 prepareAxes(POSITION);
9868 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009869 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009870 // Unrotated video frame
9871 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9872 NotifyMotionArgs motionArgs;
9873
9874 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009875 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009876 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9877 clearViewports();
9878 prepareDisplay(orientation);
9879 std::vector<TouchVideoFrame> frames{frame};
9880 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9881 processPosition(mapper, 100, 200);
9882 processSync(mapper);
9883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9884 ASSERT_EQ(frames, motionArgs.videoFrames);
9885 }
9886}
9887
9888TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9889 prepareAxes(POSITION);
9890 addConfigurationProperty("touch.deviceType", "touchScreen");
9891 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9892 // orientation-aware are affected by display rotation.
9893 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009894 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009895 // Unrotated video frame
9896 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9897 NotifyMotionArgs motionArgs;
9898
9899 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009900 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009901 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9902 clearViewports();
9903 prepareDisplay(orientation);
9904 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009905 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009906 processPosition(mapper, 100, 200);
9907 processSync(mapper);
9908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009909 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9910 // compared to the display. This is so that when the window transform (which contains the
9911 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9912 // window's coordinate space.
9913 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009914 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009915
9916 // Release finger.
9917 processSync(mapper);
9918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009919 }
9920}
9921
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009922TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009923 prepareAxes(POSITION);
9924 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009925 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009926 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9927 // so mix these.
9928 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9929 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9930 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9931 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9932 NotifyMotionArgs motionArgs;
9933
Michael Wrighta9cf4192022-12-01 23:46:39 +00009934 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009935 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009936 processPosition(mapper, 100, 200);
9937 processSync(mapper);
9938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009939 ASSERT_EQ(frames, motionArgs.videoFrames);
9940}
9941
9942TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9943 prepareAxes(POSITION);
9944 addConfigurationProperty("touch.deviceType", "touchScreen");
9945 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9946 // orientation-aware are affected by display rotation.
9947 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009948 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009949 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9950 // so mix these.
9951 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9952 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9953 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9954 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9955 NotifyMotionArgs motionArgs;
9956
Michael Wrighta9cf4192022-12-01 23:46:39 +00009957 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009958 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9959 processPosition(mapper, 100, 200);
9960 processSync(mapper);
9961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9962 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9963 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9964 // compared to the display. This is so that when the window transform (which contains the
9965 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9966 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009967 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009968 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009969 ASSERT_EQ(frames, motionArgs.videoFrames);
9970}
9971
Arthur Hung9da14732019-09-02 16:16:58 +08009972/**
9973 * If we had defined port associations, but the viewport is not ready, the touch device would be
9974 * expected to be disabled, and it should be enabled after the viewport has found.
9975 */
9976TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009977 constexpr uint8_t hdmi2 = 1;
9978 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009979 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009980
9981 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9982
9983 addConfigurationProperty("touch.deviceType", "touchScreen");
9984 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009985 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009986
9987 ASSERT_EQ(mDevice->isEnabled(), false);
9988
9989 // Add display on hdmi2, the device should be enabled and can receive touch event.
9990 prepareSecondaryDisplay(type, hdmi2);
9991 ASSERT_EQ(mDevice->isEnabled(), true);
9992
9993 // Send a touch event.
9994 processPosition(mapper, 100, 100);
9995 processSync(mapper);
9996
9997 NotifyMotionArgs args;
9998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9999 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
10000}
10001
Arthur Hung421eb1c2020-01-16 00:09:42 +080010002TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010003 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010004 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010005 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010006 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010007
10008 NotifyMotionArgs motionArgs;
10009
10010 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10011 // finger down
10012 processId(mapper, 1);
10013 processPosition(mapper, x1, y1);
10014 processSync(mapper);
10015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10016 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010017 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010018
10019 // finger move
10020 processId(mapper, 1);
10021 processPosition(mapper, x2, y2);
10022 processSync(mapper);
10023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010025 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010026
10027 // finger up.
10028 processId(mapper, -1);
10029 processSync(mapper);
10030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10031 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010032 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010033
10034 // new finger down
10035 processId(mapper, 1);
10036 processPosition(mapper, x3, y3);
10037 processSync(mapper);
10038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10039 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010040 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010041}
10042
10043/**
arthurhungcc7f9802020-04-30 17:55:40 +080010044 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
10045 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +080010046 */
arthurhungcc7f9802020-04-30 17:55:40 +080010047TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010048 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010049 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010050 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010051 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010052
10053 NotifyMotionArgs motionArgs;
10054
10055 // default tool type is finger
10056 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +080010057 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010058 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);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010063
10064 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
10065 processToolType(mapper, MT_TOOL_PALM);
10066 processSync(mapper);
10067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10068 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10069
10070 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +080010071 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010072 processPosition(mapper, x2, y2);
10073 processSync(mapper);
10074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10075
10076 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +080010077 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010078 processSync(mapper);
10079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10080
10081 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +080010082 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010083 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010084 processPosition(mapper, x3, y3);
10085 processSync(mapper);
10086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10087 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010088 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010089}
10090
arthurhungbf89a482020-04-17 17:37:55 +080010091/**
arthurhungcc7f9802020-04-30 17:55:40 +080010092 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10093 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +080010094 */
arthurhungcc7f9802020-04-30 17:55:40 +080010095TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +080010096 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010097 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +080010098 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010099 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +080010100
10101 NotifyMotionArgs motionArgs;
10102
10103 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +080010104 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10105 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010106 processPosition(mapper, x1, y1);
10107 processSync(mapper);
10108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10109 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010110 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +080010111
10112 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +080010113 processSlot(mapper, SECOND_SLOT);
10114 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010115 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +080010116 processSync(mapper);
10117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010118 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010119 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010120
10121 // If the tool type of the first finger changes to MT_TOOL_PALM,
10122 // we expect to receive ACTION_POINTER_UP with cancel flag.
10123 processSlot(mapper, FIRST_SLOT);
10124 processId(mapper, FIRST_TRACKING_ID);
10125 processToolType(mapper, MT_TOOL_PALM);
10126 processSync(mapper);
10127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010128 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010129 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10130
10131 // The following MOVE events of second finger should be processed.
10132 processSlot(mapper, SECOND_SLOT);
10133 processId(mapper, SECOND_TRACKING_ID);
10134 processPosition(mapper, x2 + 1, y2 + 1);
10135 processSync(mapper);
10136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010138 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010139
10140 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
10141 // it. Second finger receive move.
10142 processSlot(mapper, FIRST_SLOT);
10143 processId(mapper, INVALID_TRACKING_ID);
10144 processSync(mapper);
10145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10146 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010147 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010148
10149 // Second finger keeps moving.
10150 processSlot(mapper, SECOND_SLOT);
10151 processId(mapper, SECOND_TRACKING_ID);
10152 processPosition(mapper, x2 + 2, y2 + 2);
10153 processSync(mapper);
10154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010156 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010157
10158 // Second finger up.
10159 processId(mapper, INVALID_TRACKING_ID);
10160 processSync(mapper);
10161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10162 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10163 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10164}
10165
10166/**
10167 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
10168 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
10169 */
10170TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
10171 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010172 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010173 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010174 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010175
10176 NotifyMotionArgs motionArgs;
10177
10178 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10179 // First finger down.
10180 processId(mapper, FIRST_TRACKING_ID);
10181 processPosition(mapper, x1, y1);
10182 processSync(mapper);
10183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10184 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010185 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010186
10187 // Second finger down.
10188 processSlot(mapper, SECOND_SLOT);
10189 processId(mapper, SECOND_TRACKING_ID);
10190 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +080010191 processSync(mapper);
10192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010193 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010194 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +080010195
arthurhungcc7f9802020-04-30 17:55:40 +080010196 // If the tool type of the first finger changes to MT_TOOL_PALM,
10197 // we expect to receive ACTION_POINTER_UP with cancel flag.
10198 processSlot(mapper, FIRST_SLOT);
10199 processId(mapper, FIRST_TRACKING_ID);
10200 processToolType(mapper, MT_TOOL_PALM);
10201 processSync(mapper);
10202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010203 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010204 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10205
10206 // Second finger keeps moving.
10207 processSlot(mapper, SECOND_SLOT);
10208 processId(mapper, SECOND_TRACKING_ID);
10209 processPosition(mapper, x2 + 1, y2 + 1);
10210 processSync(mapper);
10211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10213
10214 // second finger becomes palm, receive cancel due to only 1 finger is active.
10215 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010216 processToolType(mapper, MT_TOOL_PALM);
10217 processSync(mapper);
10218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10219 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10220
arthurhungcc7f9802020-04-30 17:55:40 +080010221 // third finger down.
10222 processSlot(mapper, THIRD_SLOT);
10223 processId(mapper, THIRD_TRACKING_ID);
10224 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +080010225 processPosition(mapper, x3, y3);
10226 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +080010227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10228 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010229 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010230 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010231
10232 // third finger move
10233 processId(mapper, THIRD_TRACKING_ID);
10234 processPosition(mapper, x3 + 1, y3 + 1);
10235 processSync(mapper);
10236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10238
10239 // first finger up, third finger receive move.
10240 processSlot(mapper, FIRST_SLOT);
10241 processId(mapper, INVALID_TRACKING_ID);
10242 processSync(mapper);
10243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010245 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010246
10247 // second finger up, third finger receive move.
10248 processSlot(mapper, SECOND_SLOT);
10249 processId(mapper, INVALID_TRACKING_ID);
10250 processSync(mapper);
10251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010253 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010254
10255 // third finger up.
10256 processSlot(mapper, THIRD_SLOT);
10257 processId(mapper, INVALID_TRACKING_ID);
10258 processSync(mapper);
10259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10260 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10261 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10262}
10263
10264/**
10265 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10266 * and the active finger could still be allowed to receive the events
10267 */
10268TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10269 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010270 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010271 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010272 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010273
10274 NotifyMotionArgs motionArgs;
10275
10276 // default tool type is finger
10277 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10278 processId(mapper, FIRST_TRACKING_ID);
10279 processPosition(mapper, x1, y1);
10280 processSync(mapper);
10281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10282 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010283 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010284
10285 // Second finger down.
10286 processSlot(mapper, SECOND_SLOT);
10287 processId(mapper, SECOND_TRACKING_ID);
10288 processPosition(mapper, x2, y2);
10289 processSync(mapper);
10290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010291 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010292 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010293
10294 // If the tool type of the second finger changes to MT_TOOL_PALM,
10295 // we expect to receive ACTION_POINTER_UP with cancel flag.
10296 processId(mapper, SECOND_TRACKING_ID);
10297 processToolType(mapper, MT_TOOL_PALM);
10298 processSync(mapper);
10299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010300 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010301 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10302
10303 // The following MOVE event should be processed.
10304 processSlot(mapper, FIRST_SLOT);
10305 processId(mapper, FIRST_TRACKING_ID);
10306 processPosition(mapper, x1 + 1, y1 + 1);
10307 processSync(mapper);
10308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010310 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010311
10312 // second finger up.
10313 processSlot(mapper, SECOND_SLOT);
10314 processId(mapper, INVALID_TRACKING_ID);
10315 processSync(mapper);
10316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10318
10319 // first finger keep moving
10320 processSlot(mapper, FIRST_SLOT);
10321 processId(mapper, FIRST_TRACKING_ID);
10322 processPosition(mapper, x1 + 2, y1 + 2);
10323 processSync(mapper);
10324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10326
10327 // first finger up.
10328 processId(mapper, INVALID_TRACKING_ID);
10329 processSync(mapper);
10330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10331 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10332 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010333}
10334
Arthur Hung9ad18942021-06-19 02:04:46 +000010335/**
10336 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10337 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10338 * cause slot be valid again.
10339 */
10340TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10341 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010342 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +000010343 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010344 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +000010345
10346 NotifyMotionArgs motionArgs;
10347
10348 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10349 // First finger down.
10350 processId(mapper, FIRST_TRACKING_ID);
10351 processPosition(mapper, x1, y1);
10352 processPressure(mapper, RAW_PRESSURE_MAX);
10353 processSync(mapper);
10354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10355 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010356 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010357
10358 // First finger move.
10359 processId(mapper, FIRST_TRACKING_ID);
10360 processPosition(mapper, x1 + 1, y1 + 1);
10361 processPressure(mapper, RAW_PRESSURE_MAX);
10362 processSync(mapper);
10363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010365 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010366
10367 // Second finger down.
10368 processSlot(mapper, SECOND_SLOT);
10369 processId(mapper, SECOND_TRACKING_ID);
10370 processPosition(mapper, x2, y2);
10371 processPressure(mapper, RAW_PRESSURE_MAX);
10372 processSync(mapper);
10373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010374 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010375 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010376
10377 // second finger up with some unexpected data.
10378 processSlot(mapper, SECOND_SLOT);
10379 processId(mapper, INVALID_TRACKING_ID);
10380 processPosition(mapper, x2, y2);
10381 processSync(mapper);
10382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010383 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010384 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010385
10386 // first finger up with some unexpected data.
10387 processSlot(mapper, FIRST_SLOT);
10388 processId(mapper, INVALID_TRACKING_ID);
10389 processPosition(mapper, x2, y2);
10390 processPressure(mapper, RAW_PRESSURE_MAX);
10391 processSync(mapper);
10392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10393 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010394 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010395}
10396
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010397TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10398 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010399 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010400 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010401 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010402
10403 // First finger down.
10404 processId(mapper, FIRST_TRACKING_ID);
10405 processPosition(mapper, 100, 200);
10406 processPressure(mapper, RAW_PRESSURE_MAX);
10407 processSync(mapper);
10408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10409 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10410
10411 // Second finger down.
10412 processSlot(mapper, SECOND_SLOT);
10413 processId(mapper, SECOND_TRACKING_ID);
10414 processPosition(mapper, 300, 400);
10415 processPressure(mapper, RAW_PRESSURE_MAX);
10416 processSync(mapper);
10417 ASSERT_NO_FATAL_FAILURE(
10418 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10419
10420 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010421 // preserved. Resetting should cancel the ongoing gesture.
10422 resetMapper(mapper, ARBITRARY_TIME);
10423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10424 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010425
10426 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10427 // the existing touch state to generate a down event.
10428 processPosition(mapper, 301, 302);
10429 processSync(mapper);
10430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10431 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10433 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10434
10435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10436}
10437
10438TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10439 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010440 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010441 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010442 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010443
10444 // First finger touches down and releases.
10445 processId(mapper, FIRST_TRACKING_ID);
10446 processPosition(mapper, 100, 200);
10447 processPressure(mapper, RAW_PRESSURE_MAX);
10448 processSync(mapper);
10449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10450 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10451 processId(mapper, INVALID_TRACKING_ID);
10452 processSync(mapper);
10453 ASSERT_NO_FATAL_FAILURE(
10454 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10455
10456 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10457 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010458 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10460
10461 // Send an empty sync frame. Since there are no pointers, no events are generated.
10462 processSync(mapper);
10463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10464}
10465
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010466TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010467 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010468 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010469 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010470 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010472
10473 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10474 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10475 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10476 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10477 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10478
10479 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010480 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010481 processId(mapper, FIRST_TRACKING_ID);
10482 processToolType(mapper, MT_TOOL_PEN);
10483 processPosition(mapper, 100, 200);
10484 processPressure(mapper, RAW_PRESSURE_MAX);
10485 processSync(mapper);
10486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10487 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10488 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010489 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010490
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010491 // Now that we know the device supports styluses, ensure that the device is re-configured with
10492 // the stylus source.
10493 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10494 {
10495 const auto& devices = mReader->getInputDevices();
10496 auto deviceInfo =
10497 std::find_if(devices.begin(), devices.end(),
10498 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10499 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10500 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10501 }
10502
10503 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10505
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010506 processId(mapper, INVALID_TRACKING_ID);
10507 processSync(mapper);
10508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10509 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10510 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010511 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010512}
10513
Seunghwan Choi356026c2023-02-01 14:37:25 +090010514TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10515 addConfigurationProperty("touch.deviceType", "touchScreen");
10516 prepareDisplay(ui::ROTATION_0);
10517 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10518 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10519 // indicate stylus presence dynamically.
10520 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10521 std::shared_ptr<FakePointerController> fakePointerController =
10522 std::make_shared<FakePointerController>();
10523 mFakePolicy->setPointerController(fakePointerController);
10524 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +000010525 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010526
10527 processId(mapper, FIRST_TRACKING_ID);
10528 processPressure(mapper, RAW_PRESSURE_MIN);
10529 processPosition(mapper, 100, 200);
10530 processToolType(mapper, MT_TOOL_PEN);
10531 processSync(mapper);
10532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10533 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010534 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010535 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10536 ASSERT_TRUE(fakePointerController->isPointerShown());
10537 ASSERT_NO_FATAL_FAILURE(
10538 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10539}
10540
10541TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10542 addConfigurationProperty("touch.deviceType", "touchScreen");
10543 prepareDisplay(ui::ROTATION_0);
10544 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10545 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10546 // indicate stylus presence dynamically.
10547 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10548 std::shared_ptr<FakePointerController> fakePointerController =
10549 std::make_shared<FakePointerController>();
10550 mFakePolicy->setPointerController(fakePointerController);
10551 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010552 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010553
10554 processId(mapper, FIRST_TRACKING_ID);
10555 processPressure(mapper, RAW_PRESSURE_MIN);
10556 processPosition(mapper, 100, 200);
10557 processToolType(mapper, MT_TOOL_PEN);
10558 processSync(mapper);
10559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10560 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010561 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010562 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10563 ASSERT_FALSE(fakePointerController->isPointerShown());
10564}
10565
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010566// --- MultiTouchInputMapperTest_ExternalDevice ---
10567
10568class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10569protected:
Chris Yea52ade12020-08-27 16:49:20 -070010570 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010571};
10572
10573/**
10574 * Expect fallback to internal viewport if device is external and external viewport is not present.
10575 */
10576TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10577 prepareAxes(POSITION);
10578 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010579 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010580 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010581
10582 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10583
10584 NotifyMotionArgs motionArgs;
10585
10586 // Expect the event to be sent to the internal viewport,
10587 // because an external viewport is not present.
10588 processPosition(mapper, 100, 100);
10589 processSync(mapper);
10590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10591 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10592
10593 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010594 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010595 processPosition(mapper, 100, 100);
10596 processSync(mapper);
10597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10598 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10599}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010600
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010601TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10602 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10603 std::shared_ptr<FakePointerController> fakePointerController =
10604 std::make_shared<FakePointerController>();
10605 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10606 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010607
10608 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010609 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010610 prepareAxes(POSITION | ID | SLOT);
10611 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10612 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10613 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010614 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010615 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010616
10617 // captured touchpad should be a touchpad source
10618 NotifyDeviceResetArgs resetArgs;
10619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10620 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10621
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010622 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010623
10624 const InputDeviceInfo::MotionRange* relRangeX =
10625 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10626 ASSERT_NE(relRangeX, nullptr);
10627 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10628 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10629 const InputDeviceInfo::MotionRange* relRangeY =
10630 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10631 ASSERT_NE(relRangeY, nullptr);
10632 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10633 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10634
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010635 // run captured pointer tests - note that this is unscaled, so input listener events should be
10636 // identical to what the hardware sends (accounting for any
10637 // calibration).
10638 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010639 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010640 processId(mapper, 1);
10641 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10642 processKey(mapper, BTN_TOUCH, 1);
10643 processSync(mapper);
10644
10645 // expect coord[0] to contain initial location of touch 0
10646 NotifyMotionArgs args;
10647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10648 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010649 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010650 ASSERT_EQ(0, args.pointerProperties[0].id);
10651 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10652 ASSERT_NO_FATAL_FAILURE(
10653 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10654
10655 // FINGER 1 DOWN
10656 processSlot(mapper, 1);
10657 processId(mapper, 2);
10658 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10659 processSync(mapper);
10660
10661 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010663 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010664 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010665 ASSERT_EQ(0, args.pointerProperties[0].id);
10666 ASSERT_EQ(1, args.pointerProperties[1].id);
10667 ASSERT_NO_FATAL_FAILURE(
10668 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10669 ASSERT_NO_FATAL_FAILURE(
10670 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10671
10672 // FINGER 1 MOVE
10673 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10674 processSync(mapper);
10675
10676 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10677 // from move
10678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10679 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10680 ASSERT_NO_FATAL_FAILURE(
10681 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10682 ASSERT_NO_FATAL_FAILURE(
10683 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10684
10685 // FINGER 0 MOVE
10686 processSlot(mapper, 0);
10687 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10688 processSync(mapper);
10689
10690 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10692 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10693 ASSERT_NO_FATAL_FAILURE(
10694 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10695 ASSERT_NO_FATAL_FAILURE(
10696 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10697
10698 // BUTTON DOWN
10699 processKey(mapper, BTN_LEFT, 1);
10700 processSync(mapper);
10701
10702 // touchinputmapper design sends a move before button press
10703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10704 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10706 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10707
10708 // BUTTON UP
10709 processKey(mapper, BTN_LEFT, 0);
10710 processSync(mapper);
10711
10712 // touchinputmapper design sends a move after button release
10713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10714 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10717
10718 // FINGER 0 UP
10719 processId(mapper, -1);
10720 processSync(mapper);
10721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10722 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10723
10724 // FINGER 1 MOVE
10725 processSlot(mapper, 1);
10726 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10727 processSync(mapper);
10728
10729 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10731 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010732 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010733 ASSERT_EQ(1, args.pointerProperties[0].id);
10734 ASSERT_NO_FATAL_FAILURE(
10735 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10736
10737 // FINGER 1 UP
10738 processId(mapper, -1);
10739 processKey(mapper, BTN_TOUCH, 0);
10740 processSync(mapper);
10741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10742 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10743
Josep del Río2d8c79a2023-01-23 19:33:50 +000010744 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010745 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010746 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010748 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010749}
10750
10751TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10752 std::shared_ptr<FakePointerController> fakePointerController =
10753 std::make_shared<FakePointerController>();
10754 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10755 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010756
10757 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010758 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010759 prepareAxes(POSITION | ID | SLOT);
10760 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10761 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010762 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010763 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010764 // run uncaptured pointer tests - pushes out generic events
10765 // FINGER 0 DOWN
10766 processId(mapper, 3);
10767 processPosition(mapper, 100, 100);
10768 processKey(mapper, BTN_TOUCH, 1);
10769 processSync(mapper);
10770
10771 // start at (100,100), cursor should be at (0,0) * scale
10772 NotifyMotionArgs args;
10773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10774 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10775 ASSERT_NO_FATAL_FAILURE(
10776 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10777
10778 // FINGER 0 MOVE
10779 processPosition(mapper, 200, 200);
10780 processSync(mapper);
10781
10782 // compute scaling to help with touch position checking
10783 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10784 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10785 float scale =
10786 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10787
10788 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10790 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10792 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010793
10794 // BUTTON DOWN
10795 processKey(mapper, BTN_LEFT, 1);
10796 processSync(mapper);
10797
10798 // touchinputmapper design sends a move before button press
10799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10800 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10802 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10803
10804 // BUTTON UP
10805 processKey(mapper, BTN_LEFT, 0);
10806 processSync(mapper);
10807
10808 // touchinputmapper design sends a move after button release
10809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10810 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10812 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010813}
10814
10815TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10816 std::shared_ptr<FakePointerController> fakePointerController =
10817 std::make_shared<FakePointerController>();
10818
Michael Wrighta9cf4192022-12-01 23:46:39 +000010819 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010820 prepareAxes(POSITION | ID | SLOT);
10821 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010822 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010823 mFakePolicy->setPointerCapture(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010824 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010825
Josep del Río2d8c79a2023-01-23 19:33:50 +000010826 // uncaptured touchpad should be a pointer device
10827 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010828
Josep del Río2d8c79a2023-01-23 19:33:50 +000010829 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010830 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010831 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010832 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10833}
10834
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010835// --- BluetoothMultiTouchInputMapperTest ---
10836
10837class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10838protected:
10839 void SetUp() override {
10840 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10841 }
10842};
10843
10844TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10845 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010846 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010847 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010848 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010849
10850 nsecs_t kernelEventTime = ARBITRARY_TIME;
10851 nsecs_t expectedEventTime = ARBITRARY_TIME;
10852 // Touch down.
10853 processId(mapper, FIRST_TRACKING_ID);
10854 processPosition(mapper, 100, 200);
10855 processPressure(mapper, RAW_PRESSURE_MAX);
10856 processSync(mapper, ARBITRARY_TIME);
10857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10858 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10859
10860 // Process several events that come in quick succession, according to their timestamps.
10861 for (int i = 0; i < 3; i++) {
10862 constexpr static nsecs_t delta = ms2ns(1);
10863 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10864 kernelEventTime += delta;
10865 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10866
10867 processPosition(mapper, 101 + i, 201 + i);
10868 processSync(mapper, kernelEventTime);
10869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10870 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10871 WithEventTime(expectedEventTime))));
10872 }
10873
10874 // Release the touch.
10875 processId(mapper, INVALID_TRACKING_ID);
10876 processPressure(mapper, RAW_PRESSURE_MIN);
10877 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10879 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10880 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10881}
10882
10883// --- MultiTouchPointerModeTest ---
10884
HQ Liue6983c72022-04-19 22:14:56 +000010885class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10886protected:
10887 float mPointerMovementScale;
10888 float mPointerXZoomScale;
10889 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10890 addConfigurationProperty("touch.deviceType", "pointer");
10891 std::shared_ptr<FakePointerController> fakePointerController =
10892 std::make_shared<FakePointerController>();
10893 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10894 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010895 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010896
10897 prepareAxes(POSITION);
10898 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10899 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10900 // needs to be disabled, and the pointer gesture needs to be enabled.
10901 mFakePolicy->setPointerCapture(false);
10902 mFakePolicy->setPointerGestureEnabled(true);
10903 mFakePolicy->setPointerController(fakePointerController);
10904
10905 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10906 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10907 mPointerMovementScale =
10908 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10909 mPointerXZoomScale =
10910 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10911 }
10912
10913 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10914 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10915 /*flat*/ 0,
10916 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10917 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10918 /*flat*/ 0,
10919 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10920 }
10921};
10922
10923/**
10924 * Two fingers down on a pointer mode touch pad. The width
10925 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10926 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10927 * be greater than the both value to be freeform gesture, so that after two
10928 * fingers start to move downwards, the gesture should be swipe.
10929 */
10930TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10931 // The min freeform gesture width is 25units/mm x 30mm = 750
10932 // which is greater than fraction of the diagnal length of the touchpad (349).
10933 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010934 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010935 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010936 NotifyMotionArgs motionArgs;
10937
10938 // Two fingers down at once.
10939 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10940 // Pointer's initial position is used the [0,0] coordinate.
10941 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10942
10943 processId(mapper, FIRST_TRACKING_ID);
10944 processPosition(mapper, x1, y1);
10945 processMTSync(mapper);
10946 processId(mapper, SECOND_TRACKING_ID);
10947 processPosition(mapper, x2, y2);
10948 processMTSync(mapper);
10949 processSync(mapper);
10950
10951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010952 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010953 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010954 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010955 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010956 ASSERT_NO_FATAL_FAILURE(
10957 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10958
10959 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10960 // that there should be 1 pointer.
10961 int32_t movingDistance = 200;
10962 y1 += movingDistance;
10963 y2 += movingDistance;
10964
10965 processId(mapper, FIRST_TRACKING_ID);
10966 processPosition(mapper, x1, y1);
10967 processMTSync(mapper);
10968 processId(mapper, SECOND_TRACKING_ID);
10969 processPosition(mapper, x2, y2);
10970 processMTSync(mapper);
10971 processSync(mapper);
10972
10973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
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_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010976 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010977 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010978 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10979 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10980 0, 0, 0, 0));
10981}
10982
10983/**
10984 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10985 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10986 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10987 * value to be freeform gesture, so that after two fingers start to move downwards,
10988 * the gesture should be swipe.
10989 */
10990TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10991 // The min freeform gesture width is 5units/mm x 30mm = 150
10992 // which is greater than fraction of the diagnal length of the touchpad (349).
10993 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010994 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +000010995 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010996 NotifyMotionArgs motionArgs;
10997
10998 // Two fingers down at once.
10999 // The two fingers are 250 units apart, expects the current gesture to be PRESS
11000 // Pointer's initial position is used the [0,0] coordinate.
11001 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
11002
11003 processId(mapper, FIRST_TRACKING_ID);
11004 processPosition(mapper, x1, y1);
11005 processMTSync(mapper);
11006 processId(mapper, SECOND_TRACKING_ID);
11007 processPosition(mapper, x2, y2);
11008 processMTSync(mapper);
11009 processSync(mapper);
11010
11011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011012 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011013 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011014 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011015 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011016 ASSERT_NO_FATAL_FAILURE(
11017 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11018
11019 // It should be recognized as a SWIPE gesture when two fingers start to move down,
11020 // and there should be 1 pointer.
11021 int32_t movingDistance = 200;
11022 y1 += movingDistance;
11023 y2 += movingDistance;
11024
11025 processId(mapper, FIRST_TRACKING_ID);
11026 processPosition(mapper, x1, y1);
11027 processMTSync(mapper);
11028 processId(mapper, SECOND_TRACKING_ID);
11029 processPosition(mapper, x2, y2);
11030 processMTSync(mapper);
11031 processSync(mapper);
11032
11033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011034 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011035 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011036 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011037 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011038 // New coordinate is the scaled relative coordinate from the initial coordinate.
11039 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
11040 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11041 0, 0, 0, 0));
11042}
11043
11044/**
11045 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
11046 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
11047 * freeform gestures after two fingers start to move downwards.
11048 */
11049TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000011050 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000011051 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000011052
11053 NotifyMotionArgs motionArgs;
11054
11055 // Two fingers down at once. Wider than the max swipe width.
11056 // The gesture is expected to be PRESS, then transformed to FREEFORM
11057 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
11058
11059 processId(mapper, FIRST_TRACKING_ID);
11060 processPosition(mapper, x1, y1);
11061 processMTSync(mapper);
11062 processId(mapper, SECOND_TRACKING_ID);
11063 processPosition(mapper, x2, y2);
11064 processMTSync(mapper);
11065 processSync(mapper);
11066
11067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011068 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011069 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011070 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011071 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011072 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
11073 ASSERT_NO_FATAL_FAILURE(
11074 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11075
11076 int32_t movingDistance = 200;
11077
11078 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
11079 // then two down events for two pointers.
11080 y1 += movingDistance;
11081 y2 += movingDistance;
11082
11083 processId(mapper, FIRST_TRACKING_ID);
11084 processPosition(mapper, x1, y1);
11085 processMTSync(mapper);
11086 processId(mapper, SECOND_TRACKING_ID);
11087 processPosition(mapper, x2, y2);
11088 processMTSync(mapper);
11089 processSync(mapper);
11090
11091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11092 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011093 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011094 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
11095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011096 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011097 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011098 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011100 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011101 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011102 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011103 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011104 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011105 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011106 // Two pointers' scaled relative coordinates from their initial centroid.
11107 // Initial y coordinates are 0 as y1 and y2 have the same value.
11108 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
11109 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
11110 // When pointers move, the new coordinates equal to the initial coordinates plus
11111 // scaled moving distance.
11112 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11113 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11114 0, 0, 0, 0));
11115 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11116 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11117 0, 0, 0, 0));
11118
11119 // Move two fingers down again, expect one MOVE motion event.
11120 y1 += movingDistance;
11121 y2 += movingDistance;
11122
11123 processId(mapper, FIRST_TRACKING_ID);
11124 processPosition(mapper, x1, y1);
11125 processMTSync(mapper);
11126 processId(mapper, SECOND_TRACKING_ID);
11127 processPosition(mapper, x2, y2);
11128 processMTSync(mapper);
11129 processSync(mapper);
11130
11131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011132 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011134 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011135 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011136 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11137 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11138 0, 0, 0, 0, 0));
11139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11140 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11141 0, 0, 0, 0, 0));
11142}
11143
Harry Cutts39b7ca22022-10-05 15:55:48 +000011144TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000011145 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000011146 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000011147 NotifyMotionArgs motionArgs;
11148
11149 // Place two fingers down.
11150 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
11151
11152 processId(mapper, FIRST_TRACKING_ID);
11153 processPosition(mapper, x1, y1);
11154 processMTSync(mapper);
11155 processId(mapper, SECOND_TRACKING_ID);
11156 processPosition(mapper, x2, y2);
11157 processMTSync(mapper);
11158 processSync(mapper);
11159
11160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011161 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000011162 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11163 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
11164 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
11165 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
11166
11167 // Move the two fingers down and to the left.
11168 int32_t movingDistance = 200;
11169 x1 -= movingDistance;
11170 y1 += movingDistance;
11171 x2 -= movingDistance;
11172 y2 += movingDistance;
11173
11174 processId(mapper, FIRST_TRACKING_ID);
11175 processPosition(mapper, x1, y1);
11176 processMTSync(mapper);
11177 processId(mapper, SECOND_TRACKING_ID);
11178 processPosition(mapper, x2, y2);
11179 processMTSync(mapper);
11180 processSync(mapper);
11181
11182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011183 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000011184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11185 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
11186 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
11187 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
11188}
11189
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011190TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000011191 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011192 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000011193 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
11195
11196 // Start a stylus gesture.
11197 processKey(mapper, BTN_TOOL_PEN, 1);
11198 processId(mapper, FIRST_TRACKING_ID);
11199 processPosition(mapper, 100, 200);
11200 processSync(mapper);
11201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11202 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
11203 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011204 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011205 // TODO(b/257078296): Pointer mode generates extra event.
11206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11207 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11208 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011209 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11211
11212 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
11213 // gesture should be disabled.
11214 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
11215 viewport->isActive = false;
11216 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000011217 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11219 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11220 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011221 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011222 // TODO(b/257078296): Pointer mode generates extra event.
11223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11224 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11225 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011226 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11228}
11229
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011230// --- JoystickInputMapperTest ---
11231
11232class JoystickInputMapperTest : public InputMapperTest {
11233protected:
11234 static const int32_t RAW_X_MIN;
11235 static const int32_t RAW_X_MAX;
11236 static const int32_t RAW_Y_MIN;
11237 static const int32_t RAW_Y_MAX;
11238
11239 void SetUp() override {
11240 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
11241 }
11242 void prepareAxes() {
11243 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
11244 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
11245 }
11246
11247 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
11248 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
11249 }
11250
11251 void processSync(JoystickInputMapper& mapper) {
11252 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
11253 }
11254
Michael Wrighta9cf4192022-12-01 23:46:39 +000011255 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011256 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
11257 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
11258 NO_PORT, ViewportType::VIRTUAL);
11259 }
11260};
11261
11262const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
11263const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
11264const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11265const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11266
11267TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11268 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000011269 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011270
11271 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11272
Michael Wrighta9cf4192022-12-01 23:46:39 +000011273 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011274
11275 // Send an axis event
11276 processAxis(mapper, ABS_X, 100);
11277 processSync(mapper);
11278
11279 NotifyMotionArgs args;
11280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11281 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11282
11283 // Send another axis event
11284 processAxis(mapper, ABS_Y, 100);
11285 processSync(mapper);
11286
11287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11288 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11289}
11290
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011291// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011292
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011293class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011294protected:
11295 static const char* DEVICE_NAME;
11296 static const char* DEVICE_LOCATION;
11297 static const int32_t DEVICE_ID;
11298 static const int32_t DEVICE_GENERATION;
11299 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011300 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011301 static const int32_t EVENTHUB_ID;
11302
11303 std::shared_ptr<FakeEventHub> mFakeEventHub;
11304 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011305 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011306 std::unique_ptr<InstrumentedInputReader> mReader;
11307 std::shared_ptr<InputDevice> mDevice;
11308
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011309 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011310 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011311 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011312 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011313 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011314 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011315 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11316 }
11317
11318 void SetUp() override { SetUp(DEVICE_CLASSES); }
11319
11320 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011321 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011322 mFakePolicy.clear();
11323 }
11324
Chris Yee2b1e5c2021-03-10 22:45:12 -080011325 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11326 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011327 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011328 InputDeviceIdentifier identifier;
11329 identifier.name = name;
11330 identifier.location = location;
11331 std::shared_ptr<InputDevice> device =
11332 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11333 identifier);
11334 mReader->pushNextDevice(device);
11335 mFakeEventHub->addDevice(eventHubId, name, classes);
11336 mReader->loopOnce();
11337 return device;
11338 }
11339
11340 template <class T, typename... Args>
11341 T& addControllerAndConfigure(Args... args) {
11342 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11343
11344 return controller;
11345 }
11346};
11347
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011348const char* PeripheralControllerTest::DEVICE_NAME = "device";
11349const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11350const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11351const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11352const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011353const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11354 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011355const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011356
11357// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011358class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011359protected:
11360 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011361 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011362 }
11363};
11364
11365TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011366 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011367
Harry Cuttsa5b71292022-11-28 12:56:17 +000011368 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11369 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11370 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011371}
11372
11373TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011374 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011375
Harry Cuttsa5b71292022-11-28 12:56:17 +000011376 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11377 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11378 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011379}
11380
11381// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011382class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011383protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011384 void SetUp() override {
11385 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11386 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011387};
11388
Chris Ye85758332021-05-16 23:05:17 -070011389TEST_F(LightControllerTest, MonoLight) {
11390 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011391 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011392 .maxBrightness = 255,
11393 .flags = InputLightClass::BRIGHTNESS,
11394 .path = ""};
11395 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011396
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011397 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011398 InputDeviceInfo info;
11399 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011400 std::vector<InputDeviceLightInfo> lights = info.getLights();
11401 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011402 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11403 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11404
11405 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11406 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11407}
11408
11409TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11410 RawLightInfo infoMono = {.id = 1,
11411 .name = "mono_keyboard_backlight",
11412 .maxBrightness = 255,
11413 .flags = InputLightClass::BRIGHTNESS |
11414 InputLightClass::KEYBOARD_BACKLIGHT,
11415 .path = ""};
11416 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11417
11418 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11419 InputDeviceInfo info;
11420 controller.populateDeviceInfo(&info);
11421 std::vector<InputDeviceLightInfo> lights = info.getLights();
11422 ASSERT_EQ(1U, lights.size());
11423 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11424 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011425
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011426 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11427 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011428}
11429
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000011430TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
11431 RawLightInfo infoMono = {.id = 1,
11432 .name = "mono_light",
11433 .maxBrightness = 255,
11434 .flags = InputLightClass::BRIGHTNESS,
11435 .path = ""};
11436 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11437 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11438 "0,100,200");
11439
11440 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11441 std::list<NotifyArgs> unused =
11442 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11443 /*changes=*/{});
11444
11445 InputDeviceInfo info;
11446 controller.populateDeviceInfo(&info);
11447 std::vector<InputDeviceLightInfo> lights = info.getLights();
11448 ASSERT_EQ(1U, lights.size());
11449 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11450}
11451
11452TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
11453 RawLightInfo infoMono = {.id = 1,
11454 .name = "mono_keyboard_backlight",
11455 .maxBrightness = 255,
11456 .flags = InputLightClass::BRIGHTNESS |
11457 InputLightClass::KEYBOARD_BACKLIGHT,
11458 .path = ""};
11459 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11460
11461 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11462 std::list<NotifyArgs> unused =
11463 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11464 /*changes=*/{});
11465
11466 InputDeviceInfo info;
11467 controller.populateDeviceInfo(&info);
11468 std::vector<InputDeviceLightInfo> lights = info.getLights();
11469 ASSERT_EQ(1U, lights.size());
11470 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11471}
11472
11473TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
11474 RawLightInfo infoMono = {.id = 1,
11475 .name = "mono_keyboard_backlight",
11476 .maxBrightness = 255,
11477 .flags = InputLightClass::BRIGHTNESS |
11478 InputLightClass::KEYBOARD_BACKLIGHT,
11479 .path = ""};
11480 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11481 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11482 "0,100,200");
11483
11484 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11485 std::list<NotifyArgs> unused =
11486 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11487 /*changes=*/{});
11488
11489 InputDeviceInfo info;
11490 controller.populateDeviceInfo(&info);
11491 std::vector<InputDeviceLightInfo> lights = info.getLights();
11492 ASSERT_EQ(1U, lights.size());
11493 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
11494 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
11495 ASSERT_EQ(BrightnessLevel(0), *it);
11496 std::advance(it, 1);
11497 ASSERT_EQ(BrightnessLevel(100), *it);
11498 std::advance(it, 1);
11499 ASSERT_EQ(BrightnessLevel(200), *it);
11500}
11501
11502TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
11503 RawLightInfo infoMono = {.id = 1,
11504 .name = "mono_keyboard_backlight",
11505 .maxBrightness = 255,
11506 .flags = InputLightClass::BRIGHTNESS |
11507 InputLightClass::KEYBOARD_BACKLIGHT,
11508 .path = ""};
11509 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11510 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11511 "0,100,200,300,400,500");
11512
11513 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11514 std::list<NotifyArgs> unused =
11515 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11516 /*changes=*/{});
11517
11518 InputDeviceInfo info;
11519 controller.populateDeviceInfo(&info);
11520 std::vector<InputDeviceLightInfo> lights = info.getLights();
11521 ASSERT_EQ(1U, lights.size());
11522 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11523}
11524
Chris Yee2b1e5c2021-03-10 22:45:12 -080011525TEST_F(LightControllerTest, RGBLight) {
11526 RawLightInfo infoRed = {.id = 1,
11527 .name = "red",
11528 .maxBrightness = 255,
11529 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11530 .path = ""};
11531 RawLightInfo infoGreen = {.id = 2,
11532 .name = "green",
11533 .maxBrightness = 255,
11534 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11535 .path = ""};
11536 RawLightInfo infoBlue = {.id = 3,
11537 .name = "blue",
11538 .maxBrightness = 255,
11539 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11540 .path = ""};
11541 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11542 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11543 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11544
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011545 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011546 InputDeviceInfo info;
11547 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011548 std::vector<InputDeviceLightInfo> lights = info.getLights();
11549 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011550 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11551 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11552 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11553
11554 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11555 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11556}
11557
11558TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11559 RawLightInfo infoRed = {.id = 1,
11560 .name = "red_keyboard_backlight",
11561 .maxBrightness = 255,
11562 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11563 InputLightClass::KEYBOARD_BACKLIGHT,
11564 .path = ""};
11565 RawLightInfo infoGreen = {.id = 2,
11566 .name = "green_keyboard_backlight",
11567 .maxBrightness = 255,
11568 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11569 InputLightClass::KEYBOARD_BACKLIGHT,
11570 .path = ""};
11571 RawLightInfo infoBlue = {.id = 3,
11572 .name = "blue_keyboard_backlight",
11573 .maxBrightness = 255,
11574 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11575 InputLightClass::KEYBOARD_BACKLIGHT,
11576 .path = ""};
11577 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11578 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11579 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11580
11581 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11582 InputDeviceInfo info;
11583 controller.populateDeviceInfo(&info);
11584 std::vector<InputDeviceLightInfo> lights = info.getLights();
11585 ASSERT_EQ(1U, lights.size());
11586 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11587 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11588 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11589
11590 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11591 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11592}
11593
11594TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11595 RawLightInfo infoRed = {.id = 1,
11596 .name = "red",
11597 .maxBrightness = 255,
11598 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11599 .path = ""};
11600 RawLightInfo infoGreen = {.id = 2,
11601 .name = "green",
11602 .maxBrightness = 255,
11603 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11604 .path = ""};
11605 RawLightInfo infoBlue = {.id = 3,
11606 .name = "blue",
11607 .maxBrightness = 255,
11608 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11609 .path = ""};
11610 RawLightInfo infoGlobal = {.id = 3,
11611 .name = "global_keyboard_backlight",
11612 .maxBrightness = 255,
11613 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11614 InputLightClass::KEYBOARD_BACKLIGHT,
11615 .path = ""};
11616 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11617 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11618 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11619 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11620
11621 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11622 InputDeviceInfo info;
11623 controller.populateDeviceInfo(&info);
11624 std::vector<InputDeviceLightInfo> lights = info.getLights();
11625 ASSERT_EQ(1U, lights.size());
11626 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11627 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11628 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011629
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011630 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11631 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011632}
11633
11634TEST_F(LightControllerTest, MultiColorRGBLight) {
11635 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011636 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011637 .maxBrightness = 255,
11638 .flags = InputLightClass::BRIGHTNESS |
11639 InputLightClass::MULTI_INTENSITY |
11640 InputLightClass::MULTI_INDEX,
11641 .path = ""};
11642
11643 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11644
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011645 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011646 InputDeviceInfo info;
11647 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011648 std::vector<InputDeviceLightInfo> lights = info.getLights();
11649 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011650 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11651 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11652 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11653
11654 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11655 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11656}
11657
11658TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11659 RawLightInfo infoColor = {.id = 1,
11660 .name = "multi_color_keyboard_backlight",
11661 .maxBrightness = 255,
11662 .flags = InputLightClass::BRIGHTNESS |
11663 InputLightClass::MULTI_INTENSITY |
11664 InputLightClass::MULTI_INDEX |
11665 InputLightClass::KEYBOARD_BACKLIGHT,
11666 .path = ""};
11667
11668 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11669
11670 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11671 InputDeviceInfo info;
11672 controller.populateDeviceInfo(&info);
11673 std::vector<InputDeviceLightInfo> lights = info.getLights();
11674 ASSERT_EQ(1U, lights.size());
11675 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11676 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11677 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011678
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011679 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11680 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011681}
11682
11683TEST_F(LightControllerTest, PlayerIdLight) {
11684 RawLightInfo info1 = {.id = 1,
11685 .name = "player1",
11686 .maxBrightness = 255,
11687 .flags = InputLightClass::BRIGHTNESS,
11688 .path = ""};
11689 RawLightInfo info2 = {.id = 2,
11690 .name = "player2",
11691 .maxBrightness = 255,
11692 .flags = InputLightClass::BRIGHTNESS,
11693 .path = ""};
11694 RawLightInfo info3 = {.id = 3,
11695 .name = "player3",
11696 .maxBrightness = 255,
11697 .flags = InputLightClass::BRIGHTNESS,
11698 .path = ""};
11699 RawLightInfo info4 = {.id = 4,
11700 .name = "player4",
11701 .maxBrightness = 255,
11702 .flags = InputLightClass::BRIGHTNESS,
11703 .path = ""};
11704 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11705 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11706 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11707 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11708
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011709 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011710 InputDeviceInfo info;
11711 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011712 std::vector<InputDeviceLightInfo> lights = info.getLights();
11713 ASSERT_EQ(1U, lights.size());
11714 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011715 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11716 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011717
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011718 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11719 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11720 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011721}
11722
Michael Wrightd02c5b62014-02-10 15:10:22 -080011723} // namespace android