blob: 70005f830a09e7451ef04877815d87497de024c1 [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
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001511/**
1512 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1513 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1514 * are passed to the listener.
1515 */
1516static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1517TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1518 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1519 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1520 NotifyKeyArgs keyArgs;
1521
1522 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1523 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1524 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1525 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1526
1527 controller->pressAndReleaseKey(BTN_GEAR_UP);
1528 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1529 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1530 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1531}
1532
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001533// --- TouchIntegrationTest ---
1534
Arpit Singh440bf652023-08-09 09:23:43 +00001535class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
Arthur Hungaab25622020-01-16 11:22:11 +08001536protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001537 const std::string UNIQUE_ID = "local:0";
1538
Chris Yea52ade12020-08-27 16:49:20 -07001539 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001540#if !defined(__ANDROID__)
1541 GTEST_SKIP();
1542#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001543 InputReaderIntegrationTest::SetUp();
1544 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001545 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1546 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001547
1548 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1549 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1550 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001551 const auto info = findDeviceByName(mDevice->getName());
1552 ASSERT_TRUE(info);
1553 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001554 }
1555
1556 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001557 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001558 std::optional<uint8_t> physicalPort,
1559 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001560 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001561 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001562 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001563 }
1564
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001565 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1566 NotifyMotionArgs args;
1567 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1568 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001569 ASSERT_EQ(points.size(), args.getPointerCount());
1570 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001571 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1572 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1573 }
1574 }
1575
Arthur Hungaab25622020-01-16 11:22:11 +08001576 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001577 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001578};
1579
Arpit Singh440bf652023-08-09 09:23:43 +00001580enum class TouchIntegrationTestDisplays { DISPLAY_INTERNAL, DISPLAY_INPUT_PORT, DISPLAY_UNIQUE_ID };
1581
1582class TouchIntegrationTest : public BaseTouchIntegrationTest,
1583 public testing::WithParamInterface<TouchIntegrationTestDisplays> {
1584protected:
1585 static constexpr std::optional<uint8_t> DISPLAY_PORT = 0;
1586 const std::string INPUT_PORT = "uinput_touch/input0";
1587
1588 void SetUp() override {
1589#if !defined(__ANDROID__)
1590 GTEST_SKIP();
1591#endif
1592 if (GetParam() == TouchIntegrationTestDisplays::DISPLAY_INTERNAL) {
1593 BaseTouchIntegrationTest::SetUp();
1594 return;
1595 }
1596
1597 // setup policy with a input-port or UniqueId association to the display
1598 bool isInputPortAssociation =
1599 GetParam() == TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT;
1600
1601 mFakePolicy = sp<FakeInputReaderPolicy>::make();
1602 if (isInputPortAssociation) {
1603 mFakePolicy->addInputPortAssociation(INPUT_PORT, DISPLAY_PORT.value());
1604 } else {
1605 mFakePolicy->addInputUniqueIdAssociation(INPUT_PORT, UNIQUE_ID);
1606 }
1607 mFakePointerController = std::make_shared<FakePointerController>();
1608 mFakePolicy->setPointerController(mFakePointerController);
1609
1610 InputReaderIntegrationTest::setupInputReader();
1611
1612 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
1613 INPUT_PORT);
1614 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1615
1616 // Add a display linked to a physical port or UniqueId.
1617 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1618 UNIQUE_ID, isInputPortAssociation ? DISPLAY_PORT : NO_PORT,
1619 ViewportType::INTERNAL);
1620 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1621 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1622 const auto info = findDeviceByName(mDevice->getName());
1623 ASSERT_TRUE(info);
1624 mDeviceInfo = *info;
1625 }
1626};
1627
1628TEST_P(TouchIntegrationTest, MultiTouchDeviceSource) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001629 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1630 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1631 // presses).
1632 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1633 mDeviceInfo.getSources());
1634}
1635
Arpit Singh440bf652023-08-09 09:23:43 +00001636TEST_P(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001637 NotifyMotionArgs args;
1638 const Point centerPoint = mDevice->getCenterPoint();
1639
1640 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001641 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001642 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001643 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001644 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1645 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1646
1647 // ACTION_MOVE
1648 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001649 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001650 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1651 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1652
1653 // ACTION_UP
1654 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001655 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001656 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1657 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1658}
1659
Arpit Singh440bf652023-08-09 09:23:43 +00001660TEST_P(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001661 NotifyMotionArgs args;
1662 const Point centerPoint = mDevice->getCenterPoint();
1663
1664 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001665 mDevice->sendSlot(FIRST_SLOT);
1666 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001667 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001668 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001669 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1670 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1671
1672 // ACTION_POINTER_DOWN (Second slot)
1673 const Point secondPoint = centerPoint + Point(100, 100);
1674 mDevice->sendSlot(SECOND_SLOT);
1675 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001676 mDevice->sendDown(secondPoint);
1677 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001678 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001679 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001680
1681 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001682 mDevice->sendMove(secondPoint + Point(1, 1));
1683 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001684 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1685 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1686
1687 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001688 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001689 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001690 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001691 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001692
1693 // ACTION_UP
1694 mDevice->sendSlot(FIRST_SLOT);
1695 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001696 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001697 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1698 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1699}
1700
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001701/**
1702 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1703 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1704 * data?
1705 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1706 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1707 * for Pointer 0 only is generated after.
1708 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1709 * events, we will not miss any information.
1710 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1711 * event generated afterwards that contains the newest movement of pointer 0.
1712 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1713 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1714 * losing information about non-palm pointers.
1715 */
Arpit Singh440bf652023-08-09 09:23:43 +00001716TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001717 NotifyMotionArgs args;
1718 const Point centerPoint = mDevice->getCenterPoint();
1719
1720 // ACTION_DOWN
1721 mDevice->sendSlot(FIRST_SLOT);
1722 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1723 mDevice->sendDown(centerPoint);
1724 mDevice->sendSync();
1725 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1726
1727 // ACTION_POINTER_DOWN (Second slot)
1728 const Point secondPoint = centerPoint + Point(100, 100);
1729 mDevice->sendSlot(SECOND_SLOT);
1730 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1731 mDevice->sendDown(secondPoint);
1732 mDevice->sendSync();
1733 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1734
1735 // ACTION_MOVE (First slot)
1736 mDevice->sendSlot(FIRST_SLOT);
1737 mDevice->sendMove(centerPoint + Point(5, 5));
1738 // ACTION_POINTER_UP (Second slot)
1739 mDevice->sendSlot(SECOND_SLOT);
1740 mDevice->sendPointerUp();
1741 // Send a single sync for the above 2 pointer updates
1742 mDevice->sendSync();
1743
1744 // First, we should get POINTER_UP for the second pointer
1745 assertReceivedMotion(ACTION_POINTER_1_UP,
1746 {/*first pointer */ centerPoint + Point(5, 5),
1747 /*second pointer*/ secondPoint});
1748
1749 // Next, the MOVE event for the first pointer
1750 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1751}
1752
1753/**
1754 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1755 * move, and then it will go up, all in the same frame.
1756 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1757 * gets sent to the listener.
1758 */
Arpit Singh440bf652023-08-09 09:23:43 +00001759TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001760 NotifyMotionArgs args;
1761 const Point centerPoint = mDevice->getCenterPoint();
1762
1763 // ACTION_DOWN
1764 mDevice->sendSlot(FIRST_SLOT);
1765 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1766 mDevice->sendDown(centerPoint);
1767 mDevice->sendSync();
1768 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1769
1770 // ACTION_POINTER_DOWN (Second slot)
1771 const Point secondPoint = centerPoint + Point(100, 100);
1772 mDevice->sendSlot(SECOND_SLOT);
1773 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1774 mDevice->sendDown(secondPoint);
1775 mDevice->sendSync();
1776 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1777
1778 // ACTION_MOVE (First slot)
1779 mDevice->sendSlot(FIRST_SLOT);
1780 mDevice->sendMove(centerPoint + Point(5, 5));
1781 // ACTION_POINTER_UP (Second slot)
1782 mDevice->sendSlot(SECOND_SLOT);
1783 mDevice->sendMove(secondPoint + Point(6, 6));
1784 mDevice->sendPointerUp();
1785 // Send a single sync for the above 2 pointer updates
1786 mDevice->sendSync();
1787
1788 // First, we should get POINTER_UP for the second pointer
1789 // The movement of the second pointer during the liftoff frame is ignored.
1790 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1791 assertReceivedMotion(ACTION_POINTER_1_UP,
1792 {/*first pointer */ centerPoint + Point(5, 5),
1793 /*second pointer*/ secondPoint});
1794
1795 // Next, the MOVE event for the first pointer
1796 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1797}
1798
Arpit Singh440bf652023-08-09 09:23:43 +00001799TEST_P(TouchIntegrationTest, InputEvent_ProcessPalm) {
Arthur Hungaab25622020-01-16 11:22:11 +08001800 NotifyMotionArgs args;
1801 const Point centerPoint = mDevice->getCenterPoint();
1802
1803 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001804 mDevice->sendSlot(FIRST_SLOT);
1805 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001806 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001807 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001808 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1809 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1810
arthurhungcc7f9802020-04-30 17:55:40 +08001811 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001812 const Point secondPoint = centerPoint + Point(100, 100);
1813 mDevice->sendSlot(SECOND_SLOT);
1814 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1815 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001816 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001817 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001818 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001819
arthurhungcc7f9802020-04-30 17:55:40 +08001820 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001821 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001822 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001823 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1824 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1825
arthurhungcc7f9802020-04-30 17:55:40 +08001826 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1827 // a palm event.
1828 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001829 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001830 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001831 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001832 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001833 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001834
arthurhungcc7f9802020-04-30 17:55:40 +08001835 // Send up to second slot, expect first slot send moving.
1836 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001837 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001838 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1839 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001840
arthurhungcc7f9802020-04-30 17:55:40 +08001841 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001842 mDevice->sendSlot(FIRST_SLOT);
1843 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001844 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001845
arthurhungcc7f9802020-04-30 17:55:40 +08001846 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1847 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001848}
1849
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +00001850/**
1851 * Some drivers historically have reported axis values outside of the range specified in the
1852 * evdev axis info. Ensure we don't crash when this happens. For example, a driver may report a
1853 * pressure value greater than the reported maximum, since it unclear what specific meaning the
1854 * maximum value for pressure has (beyond the maximum value that can be produced by a sensor),
1855 * and no units for pressure (resolution) is specified by the evdev documentation.
1856 */
1857TEST_P(TouchIntegrationTest, AcceptsAxisValuesOutsideReportedRange) {
1858 const Point centerPoint = mDevice->getCenterPoint();
1859
1860 // Down with pressure outside the reported range
1861 mDevice->sendSlot(FIRST_SLOT);
1862 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1863 mDevice->sendDown(centerPoint);
1864 mDevice->sendPressure(UinputTouchScreen::RAW_PRESSURE_MAX + 2);
1865 mDevice->sendSync();
1866 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1867 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1868
1869 // Move to a point outside the reported range
1870 mDevice->sendMove(Point(DISPLAY_WIDTH, DISPLAY_HEIGHT) + Point(1, 1));
1871 mDevice->sendSync();
1872 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1873 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1874
1875 // Up
1876 mDevice->sendUp();
1877 mDevice->sendSync();
1878 ASSERT_NO_FATAL_FAILURE(
1879 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1880}
1881
Arpit Singh440bf652023-08-09 09:23:43 +00001882TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00001883 const Point centerPoint = mDevice->getCenterPoint();
1884
1885 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1886 mDevice->sendSlot(FIRST_SLOT);
1887 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1888 mDevice->sendToolType(MT_TOOL_PEN);
1889 mDevice->sendDown(centerPoint);
1890 mDevice->sendSync();
1891 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1892 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001893 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001894
1895 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1896
1897 // Release the stylus touch.
1898 mDevice->sendUp();
1899 mDevice->sendSync();
1900 ASSERT_NO_FATAL_FAILURE(
1901 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1902
1903 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1904
1905 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1906 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1907 mDevice->sendToolType(MT_TOOL_FINGER);
1908 mDevice->sendDown(centerPoint);
1909 mDevice->sendSync();
1910 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1911 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001912 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001913
1914 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1915
1916 mDevice->sendUp();
1917 mDevice->sendSync();
1918 ASSERT_NO_FATAL_FAILURE(
1919 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1920
1921 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1922 // The policy should be notified of the stylus presence.
1923 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1924 mDevice->sendToolType(MT_TOOL_PEN);
1925 mDevice->sendMove(centerPoint);
1926 mDevice->sendSync();
1927 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1928 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001929 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001930
1931 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1932}
1933
Arpit Singh440bf652023-08-09 09:23:43 +00001934TEST_P(TouchIntegrationTest, ExternalStylusConnectedDuringTouchGesture) {
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001935 const Point centerPoint = mDevice->getCenterPoint();
1936
1937 // Down
1938 mDevice->sendSlot(FIRST_SLOT);
1939 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1940 mDevice->sendDown(centerPoint);
1941 mDevice->sendSync();
1942 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1943 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1944
1945 // Move
1946 mDevice->sendMove(centerPoint + Point(1, 1));
1947 mDevice->sendSync();
1948 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1949 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1950
1951 // Connecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
1952 auto externalStylus = createUinputDevice<UinputExternalStylus>();
1953 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1954 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1955 const auto stylusInfo = findDeviceByName(externalStylus->getName());
1956 ASSERT_TRUE(stylusInfo);
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001957
1958 // Move
1959 mDevice->sendMove(centerPoint + Point(2, 2));
1960 mDevice->sendSync();
1961 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1962 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1963
1964 // Disconnecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
1965 externalStylus.reset();
1966 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1967 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1968 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
1969
1970 // Up
1971 mDevice->sendUp();
1972 mDevice->sendSync();
1973 ASSERT_NO_FATAL_FAILURE(
1974 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1975
1976 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
1977}
1978
Arpit Singh440bf652023-08-09 09:23:43 +00001979INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
1980 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
1981 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
1982 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
1983
Prabir Pradhan124ea442022-10-28 20:27:44 +00001984// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001985
Prabir Pradhan124ea442022-10-28 20:27:44 +00001986// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1987// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1988// stylus.
1989template <typename UinputStylusDevice>
Arpit Singh440bf652023-08-09 09:23:43 +00001990class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001991protected:
1992 void SetUp() override {
1993#if !defined(__ANDROID__)
1994 GTEST_SKIP();
1995#endif
Arpit Singh440bf652023-08-09 09:23:43 +00001996 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00001997 mTouchscreen = mDevice.get();
1998 mTouchscreenInfo = mDeviceInfo;
1999
2000 setUpStylusDevice();
2001 }
2002
2003 UinputStylusDevice* mStylus{nullptr};
2004 InputDeviceInfo mStylusInfo{};
2005
2006 UinputTouchScreen* mTouchscreen{nullptr};
2007 InputDeviceInfo mTouchscreenInfo{};
2008
2009private:
2010 // When we are attempting to test stylus button events that are sent from the touchscreen,
2011 // use the same Uinput device for the touchscreen and the stylus.
2012 template <typename T = UinputStylusDevice>
2013 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2014 mStylus = mDevice.get();
2015 mStylusInfo = mDeviceInfo;
2016 }
2017
2018 // When we are attempting to stylus buttons from an external stylus being merged with touches
2019 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2020 template <typename T = UinputStylusDevice>
2021 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2022 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2023 mStylus = mStylusDeviceLifecycleTracker.get();
2024 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2025 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2026 const auto info = findDeviceByName(mStylus->getName());
2027 ASSERT_TRUE(info);
2028 mStylusInfo = *info;
2029 }
2030
2031 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2032
2033 // Hide the base class's device to expose it with a different name for readability.
Arpit Singh440bf652023-08-09 09:23:43 +00002034 using BaseTouchIntegrationTest::mDevice;
2035 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00002036};
2037
2038using StylusButtonIntegrationTestTypes =
2039 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2040TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2041
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002042TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002043 const auto stylusId = TestFixture::mStylusInfo.getId();
2044
2045 TestFixture::mStylus->pressKey(BTN_STYLUS);
2046 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2047 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2048 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2049
2050 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2051 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002052 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002053 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002054}
2055
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002056TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002057 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2058 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2059 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002060
2061 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002062 TestFixture::mStylus->pressKey(BTN_STYLUS);
2063 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002064 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002065 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002066
2067 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002068 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2069 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2070 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2071 TestFixture::mTouchscreen->sendDown(centerPoint);
2072 TestFixture::mTouchscreen->sendSync();
2073 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002074 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002075 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002076 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2077 WithDeviceId(touchscreenId))));
2078 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002079 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002080 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002081 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2082 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002083
Prabir Pradhan124ea442022-10-28 20:27:44 +00002084 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2085 TestFixture::mTouchscreen->sendSync();
2086 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002087 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002088 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002089 WithDeviceId(touchscreenId))));
2090 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002091 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002092 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002093 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002094
2095 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002096 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2097 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002098 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002099 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002100}
2101
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002102TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002103 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2104 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2105 const auto stylusId = TestFixture::mStylusInfo.getId();
2106 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002107 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002108
2109 // Press the stylus button.
2110 TestFixture::mStylus->pressKey(BTN_STYLUS);
2111 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2112 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2113 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2114
2115 // Start hovering with the stylus.
2116 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2117 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2118 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2119 TestFixture::mTouchscreen->sendMove(centerPoint);
2120 TestFixture::mTouchscreen->sendSync();
2121 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2122 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2123 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2124 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2125 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2126 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2127 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2128 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2129 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2130
2131 // Touch down with the stylus.
2132 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2133 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2134 TestFixture::mTouchscreen->sendDown(centerPoint);
2135 TestFixture::mTouchscreen->sendSync();
2136 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2137 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2138 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2139
2140 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2141 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2142 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2143
2144 // Stop touching with the stylus, and start hovering.
2145 TestFixture::mTouchscreen->sendUp();
2146 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2147 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2148 TestFixture::mTouchscreen->sendMove(centerPoint);
2149 TestFixture::mTouchscreen->sendSync();
2150 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2151 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2152 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2153 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2154 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2155 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2156 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2157 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2158 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2159
2160 // Stop hovering.
2161 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2162 TestFixture::mTouchscreen->sendSync();
2163 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2164 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2165 WithButtonState(0))));
2166 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2167 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2168 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2169 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2170
2171 // Release the stylus button.
2172 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2173 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2174 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2175 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2176}
2177
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002178TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002179 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2180 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2181 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002182
2183 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002184 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2185 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2186 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2187 TestFixture::mTouchscreen->sendDown(centerPoint);
2188 TestFixture::mTouchscreen->sendSync();
2189 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002190 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002191 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002192 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002193
2194 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002195 TestFixture::mStylus->pressKey(BTN_STYLUS);
2196 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002197 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002198 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2199 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002200 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002201 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002202 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2203 WithDeviceId(touchscreenId))));
2204 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002205 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002206 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002207 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2208 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002209
Prabir Pradhan124ea442022-10-28 20:27:44 +00002210 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2211 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002212 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002213 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2214 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002215 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002216 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002217 WithDeviceId(touchscreenId))));
2218 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002219 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002220 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002221 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002222
2223 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002224 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2225 TestFixture::mTouchscreen->sendSync();
2226 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002227 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002228 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002229 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002230}
2231
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002232TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002233 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2234 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002235 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002236
2237 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2238 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2239 const auto stylusId = TestFixture::mStylusInfo.getId();
2240
2241 // Start a stylus gesture. By the time this event is processed, the configuration change that
2242 // was requested is guaranteed to be completed.
2243 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2244 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2245 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2246 TestFixture::mTouchscreen->sendDown(centerPoint);
2247 TestFixture::mTouchscreen->sendSync();
2248 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2249 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002250 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002251 WithDeviceId(touchscreenId))));
2252
2253 // Press and release a stylus button. Each change only generates a MOVE motion event.
2254 // Key events are unaffected.
2255 TestFixture::mStylus->pressKey(BTN_STYLUS);
2256 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2257 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2258 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2259 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2260 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002261 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002262 WithDeviceId(touchscreenId))));
2263
2264 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2265 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2266 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2267 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2268 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2269 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002270 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002271 WithDeviceId(touchscreenId))));
2272
2273 // Finish the stylus gesture.
2274 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2275 TestFixture::mTouchscreen->sendSync();
2276 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2277 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002278 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002279 WithDeviceId(touchscreenId))));
2280}
2281
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002282// --- ExternalStylusIntegrationTest ---
2283
2284// Verify the behavior of an external stylus. An external stylus can report pressure or button
2285// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2286// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singh440bf652023-08-09 09:23:43 +00002287using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002288
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002289TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002290 const Point centerPoint = mDevice->getCenterPoint();
2291
2292 // Create an external stylus capable of reporting pressure data that
2293 // should be fused with a touch pointer.
2294 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2295 createUinputDevice<UinputExternalStylusWithPressure>();
2296 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2297 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2298 const auto stylusInfo = findDeviceByName(stylus->getName());
2299 ASSERT_TRUE(stylusInfo);
2300
2301 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2302
2303 const auto touchscreenId = mDeviceInfo.getId();
2304
2305 // Set a pressure value on the stylus. It doesn't generate any events.
2306 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2307 stylus->setPressure(100);
2308 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2309
2310 // Start a finger gesture, and ensure it shows up as stylus gesture
2311 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002312 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002313 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002314 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002315 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002316 mDevice->sendSync();
2317 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2318 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002319 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002320 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002321
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002322 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2323 // event with the updated pressure.
2324 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002325 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2326 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002327 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002328 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002329
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002330 // The external stylus did not generate any events.
2331 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2332 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2333}
2334
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002335TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002336 const Point centerPoint = mDevice->getCenterPoint();
2337
2338 // Create an external stylus capable of reporting pressure data that
2339 // should be fused with a touch pointer.
2340 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2341 createUinputDevice<UinputExternalStylusWithPressure>();
2342 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2343 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2344 const auto stylusInfo = findDeviceByName(stylus->getName());
2345 ASSERT_TRUE(stylusInfo);
2346
2347 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2348
2349 const auto touchscreenId = mDeviceInfo.getId();
2350
2351 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2352 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002353 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2354 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002355 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002356 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002357
2358 // Start a finger gesture. The touch device will withhold generating any touches for
2359 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2360 mDevice->sendSlot(FIRST_SLOT);
2361 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2362 mDevice->sendToolType(MT_TOOL_FINGER);
2363 mDevice->sendDown(centerPoint);
2364 auto waitUntil = std::chrono::system_clock::now() +
2365 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002366 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002367 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002368
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002369 // Since the external stylus did not report a pressure value within the timeout,
2370 // it shows up as a finger pointer.
2371 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2372 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002373 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002374 WithPressure(1.f))));
2375
2376 // Change the pressure on the external stylus. Since the pressure was not present at the start
2377 // of the gesture, it is ignored for now.
2378 stylus->setPressure(200);
2379 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2380
2381 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002382 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2383 mDevice->sendSync();
2384 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2385 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002386 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002387
2388 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2389 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2390 mDevice->sendToolType(MT_TOOL_FINGER);
2391 mDevice->sendDown(centerPoint);
2392 mDevice->sendSync();
2393 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2394 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002395 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002396 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2397
2398 // The external stylus did not generate any events.
2399 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2400 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002401}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002402
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002403TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002404 const Point centerPoint = mDevice->getCenterPoint();
2405
2406 // Create an external stylus device that does not support pressure. It should not affect any
2407 // touch pointers.
2408 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2409 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2410 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2411 const auto stylusInfo = findDeviceByName(stylus->getName());
2412 ASSERT_TRUE(stylusInfo);
2413
2414 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2415
2416 const auto touchscreenId = mDeviceInfo.getId();
2417
2418 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2419 // pressure data from the external stylus.
2420 mDevice->sendSlot(FIRST_SLOT);
2421 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2422 mDevice->sendToolType(MT_TOOL_FINGER);
2423 mDevice->sendDown(centerPoint);
2424 auto waitUntil = std::chrono::system_clock::now() +
2425 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2426 mDevice->sendSync();
2427 ASSERT_NO_FATAL_FAILURE(
2428 mTestListener
2429 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2430 WithToolType(
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002431 ToolType::FINGER),
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002432 WithButtonState(0),
2433 WithDeviceId(touchscreenId),
2434 WithPressure(1.f)),
2435 waitUntil));
2436
2437 // The external stylus did not generate any events.
2438 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2439 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2440}
2441
Michael Wrightd02c5b62014-02-10 15:10:22 -08002442// --- InputDeviceTest ---
2443class InputDeviceTest : public testing::Test {
2444protected:
2445 static const char* DEVICE_NAME;
2446 static const char* DEVICE_LOCATION;
2447 static const int32_t DEVICE_ID;
2448 static const int32_t DEVICE_GENERATION;
2449 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002450 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002451 static const int32_t EVENTHUB_ID;
2452 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2453
2454 std::shared_ptr<FakeEventHub> mFakeEventHub;
2455 sp<FakeInputReaderPolicy> mFakePolicy;
2456 std::unique_ptr<TestInputListener> mFakeListener;
2457 std::unique_ptr<InstrumentedInputReader> mReader;
2458 std::shared_ptr<InputDevice> mDevice;
2459
2460 void SetUp() override {
2461 mFakeEventHub = std::make_unique<FakeEventHub>();
2462 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2463 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002464 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002465 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002466 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002467 identifier.name = DEVICE_NAME;
2468 identifier.location = DEVICE_LOCATION;
2469 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2470 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2471 identifier);
2472 mReader->pushNextDevice(mDevice);
2473 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002474 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002475 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002476
2477 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002478 mFakeListener.reset();
2479 mFakePolicy.clear();
2480 }
2481};
2482
2483const char* InputDeviceTest::DEVICE_NAME = "device";
2484const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2485const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2486const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002487const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002488const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2489 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002490const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002491const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2492
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002493TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002494 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002495 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2496 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002497}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002498
Michael Wrightd02c5b62014-02-10 15:10:22 -08002499TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2500 ASSERT_EQ(mDevice->isEnabled(), false);
2501}
2502
2503TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2504 // Configuration.
2505 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002506 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002507
2508 // Reset.
2509 unused += mDevice->reset(ARBITRARY_TIME);
2510
2511 NotifyDeviceResetArgs resetArgs;
2512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2513 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2514 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2515
2516 // Metadata.
2517 ASSERT_TRUE(mDevice->isIgnored());
2518 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2519
2520 InputDeviceInfo info = mDevice->getDeviceInfo();
2521 ASSERT_EQ(DEVICE_ID, info.getId());
2522 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2523 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2524 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2525
2526 // State queries.
2527 ASSERT_EQ(0, mDevice->getMetaState());
2528
2529 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2530 << "Ignored device should return unknown key code state.";
2531 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2532 << "Ignored device should return unknown scan code state.";
2533 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2534 << "Ignored device should return unknown switch state.";
2535
2536 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2537 uint8_t flags[2] = { 0, 1 };
2538 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2539 << "Ignored device should never mark any key codes.";
2540 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2541 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2542}
2543
2544TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2545 // Configuration.
2546 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2547
2548 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002549 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2550 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002551 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2552 mapper1.setMetaState(AMETA_ALT_ON);
2553 mapper1.addSupportedKeyCode(AKEYCODE_A);
2554 mapper1.addSupportedKeyCode(AKEYCODE_B);
2555 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2556 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2557 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2558 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2559 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2560
2561 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002562 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2563 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002564 mapper2.setMetaState(AMETA_SHIFT_ON);
2565
2566 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002567 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002568
Harry Cuttsf13161a2023-03-08 14:15:49 +00002569 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2570 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002571 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002572 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002573
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002574 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2575 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002576
2577 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002578 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002579 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2580 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002581
2582 NotifyDeviceResetArgs resetArgs;
2583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2584 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2585 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2586
2587 // Metadata.
2588 ASSERT_FALSE(mDevice->isIgnored());
2589 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2590
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002591 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002592 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002593 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002594 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2595 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2596
2597 // State queries.
2598 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2599 << "Should query mappers and combine meta states.";
2600
2601 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2602 << "Should return unknown key code state when source not supported.";
2603 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2604 << "Should return unknown scan code state when source not supported.";
2605 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2606 << "Should return unknown switch state when source not supported.";
2607
2608 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2609 << "Should query mapper when source is supported.";
2610 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2611 << "Should query mapper when source is supported.";
2612 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2613 << "Should query mapper when source is supported.";
2614
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002615 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002617 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002618 << "Should do nothing when source is unsupported.";
2619 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2620 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2621 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2622 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2623
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002624 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002625 << "Should query mapper when source is supported.";
2626 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2627 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2628 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2629 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2630
2631 // Event handling.
2632 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002633 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002634 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002636 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2637 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002638}
2639
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -07002640TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2641 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2642 FakeInputMapper& mapper =
2643 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2644 AINPUT_SOURCE_KEYBOARD);
2645 NotifyMotionArgs args1;
2646 NotifySwitchArgs args2;
2647 NotifyKeyArgs args3;
2648 mapper.setProcessResult({args1, args2, args3});
2649
2650 InputReaderConfiguration config;
2651 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2652
2653 RawEvent event;
2654 event.deviceId = EVENTHUB_ID;
2655 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2656
2657 for (auto& arg : notifyArgs) {
2658 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2659 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2660 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2661 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2662 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2663 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2664 }
2665 }
2666}
2667
2668TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2669 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2670 FakeInputMapper& mapper =
2671 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2672 AINPUT_SOURCE_KEYBOARD);
2673 NotifyMotionArgs args;
2674 mapper.setProcessResult({args});
2675
2676 InputReaderConfiguration config;
2677 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2678
2679 RawEvent event;
2680 event.deviceId = EVENTHUB_ID;
2681 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2682
2683 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2684 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2685}
2686
2687TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2688 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2689 FakeInputMapper& mapper =
2690 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2691 AINPUT_SOURCE_KEYBOARD);
2692 NotifyMotionArgs args;
2693 args.policyFlags = POLICY_FLAG_WAKE;
2694 mapper.setProcessResult({args});
2695
2696 InputReaderConfiguration config;
2697 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2698
2699 RawEvent event;
2700 event.deviceId = EVENTHUB_ID;
2701 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2702
2703 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2704 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2705}
2706
Arthur Hung2c9a3342019-07-23 14:18:59 +08002707// A single input device is associated with a specific display. Check that:
2708// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh3e56f7e2023-07-07 13:12:37 +00002709// 2. Device is disabled when setEnabled API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002710TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002711 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2712 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002713
2714 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002715 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002716 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2717 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002718
2719 // Device should be enabled by default.
2720 ASSERT_TRUE(mDevice->isEnabled());
2721
2722 // Prepare associated info.
2723 constexpr uint8_t hdmi = 1;
2724 const std::string UNIQUE_ID = "local:1";
2725
2726 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002727 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002728 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002729 // Device should be disabled because it is associated with a specific display via
2730 // input port <-> display port association, but the corresponding display is not found
2731 ASSERT_FALSE(mDevice->isEnabled());
2732
2733 // Prepare displays.
2734 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002735 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002736 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002737 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002738 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002739 ASSERT_TRUE(mDevice->isEnabled());
2740
2741 // Device should be disabled after set disable.
2742 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002743 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002744 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002745 ASSERT_FALSE(mDevice->isEnabled());
2746
2747 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002748 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002749 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002750 ASSERT_FALSE(mDevice->isEnabled());
2751}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002752
Christine Franks1ba71cc2021-04-07 14:37:42 -07002753TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2754 // Device should be enabled by default.
2755 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002756 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2757 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002758 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002759 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2760 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002761 ASSERT_TRUE(mDevice->isEnabled());
2762
2763 // Device should be disabled because it is associated with a specific display, but the
2764 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002765 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002766 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002767 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002768 ASSERT_FALSE(mDevice->isEnabled());
2769
2770 // Device should be enabled when a display is found.
2771 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002772 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002773 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002774 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002775 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002776 ASSERT_TRUE(mDevice->isEnabled());
2777
2778 // Device should be disabled after set disable.
2779 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002780 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002781 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002782 ASSERT_FALSE(mDevice->isEnabled());
2783
2784 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002785 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002786 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002787 ASSERT_FALSE(mDevice->isEnabled());
2788}
2789
Christine Franks2a2293c2022-01-18 11:51:16 -08002790TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2791 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002792 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2793 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002794 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002795 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2796 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002797
Christine Franks2a2293c2022-01-18 11:51:16 -08002798 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2799 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002800 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002801 NO_PORT, ViewportType::INTERNAL);
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::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002804 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2805}
2806
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002807/**
2808 * This test reproduces a crash caused by a dangling reference that remains after device is added
2809 * and removed. The reference is accessed in InputDevice::dump(..);
2810 */
2811TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2812 constexpr int32_t TEST_EVENTHUB_ID = 10;
2813 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2814
Harry Cutts33476232023-01-30 19:57:29 +00002815 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh7f1765e2023-07-07 13:12:37 +00002816 device.addEventHubDevice(TEST_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002817 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2818 std::string dumpStr, eventHubDevStr;
2819 device.dump(dumpStr, eventHubDevStr);
2820}
2821
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002822TEST_F(InputDeviceTest, GetBluetoothAddress) {
2823 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2824 ASSERT_TRUE(address);
2825 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2826}
2827
Michael Wrightd02c5b62014-02-10 15:10:22 -08002828// --- SwitchInputMapperTest ---
2829
2830class SwitchInputMapperTest : public InputMapperTest {
2831protected:
2832};
2833
2834TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002835 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002836
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002837 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002838}
2839
2840TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002841 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002842
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002843 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002844 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002845
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002846 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002847 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002848}
2849
2850TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002851 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002852 std::list<NotifyArgs> out;
2853 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2854 ASSERT_TRUE(out.empty());
2855 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2856 ASSERT_TRUE(out.empty());
2857 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2858 ASSERT_TRUE(out.empty());
2859 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002860
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002861 ASSERT_EQ(1u, out.size());
2862 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002863 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002864 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2865 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002866 args.switchMask);
2867 ASSERT_EQ(uint32_t(0), args.policyFlags);
2868}
2869
Chris Ye87143712020-11-10 05:05:58 +00002870// --- VibratorInputMapperTest ---
2871class VibratorInputMapperTest : public InputMapperTest {
2872protected:
2873 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2874};
2875
2876TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002877 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002878
2879 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2880}
2881
2882TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002883 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002884
2885 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2886}
2887
2888TEST_F(VibratorInputMapperTest, Vibrate) {
2889 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002890 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00002891 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002892
2893 VibrationElement pattern(2);
2894 VibrationSequence sequence(2);
2895 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002896 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2897 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002898 sequence.addElement(pattern);
2899 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002900 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2901 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002902 sequence.addElement(pattern);
2903
2904 std::vector<int64_t> timings = {0, 1};
2905 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2906
2907 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002908 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002909 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002910 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002911 // Verify vibrator state listener was notified.
2912 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002913 ASSERT_EQ(1u, out.size());
2914 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2915 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2916 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002917 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002918 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002919 ASSERT_FALSE(mapper.isVibrating());
2920 // Verify vibrator state listener was notified.
2921 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002922 ASSERT_EQ(1u, out.size());
2923 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2924 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2925 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002926}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002927
Chris Yef59a2f42020-10-16 12:55:26 -07002928// --- SensorInputMapperTest ---
2929
2930class SensorInputMapperTest : public InputMapperTest {
2931protected:
2932 static const int32_t ACCEL_RAW_MIN;
2933 static const int32_t ACCEL_RAW_MAX;
2934 static const int32_t ACCEL_RAW_FUZZ;
2935 static const int32_t ACCEL_RAW_FLAT;
2936 static const int32_t ACCEL_RAW_RESOLUTION;
2937
2938 static const int32_t GYRO_RAW_MIN;
2939 static const int32_t GYRO_RAW_MAX;
2940 static const int32_t GYRO_RAW_FUZZ;
2941 static const int32_t GYRO_RAW_FLAT;
2942 static const int32_t GYRO_RAW_RESOLUTION;
2943
2944 static const float GRAVITY_MS2_UNIT;
2945 static const float DEGREE_RADIAN_UNIT;
2946
2947 void prepareAccelAxes();
2948 void prepareGyroAxes();
2949 void setAccelProperties();
2950 void setGyroProperties();
2951 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2952};
2953
2954const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2955const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2956const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2957const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2958const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2959
2960const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2961const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2962const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2963const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2964const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2965
2966const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2967const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2968
2969void SensorInputMapperTest::prepareAccelAxes() {
2970 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2971 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2972 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2973 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2974 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2975 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2976}
2977
2978void SensorInputMapperTest::prepareGyroAxes() {
2979 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2980 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2981 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2982 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2983 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2984 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2985}
2986
2987void SensorInputMapperTest::setAccelProperties() {
2988 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2989 /* sensorDataIndex */ 0);
2990 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2991 /* sensorDataIndex */ 1);
2992 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2993 /* sensorDataIndex */ 2);
2994 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2995 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2996 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2997 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2998 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2999}
3000
3001void SensorInputMapperTest::setGyroProperties() {
3002 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3003 /* sensorDataIndex */ 0);
3004 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3005 /* sensorDataIndex */ 1);
3006 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3007 /* sensorDataIndex */ 2);
3008 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3009 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3010 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3011 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3012 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3013}
3014
3015TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00003016 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003017
3018 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3019}
3020
3021TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3022 setAccelProperties();
3023 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003024 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003025
3026 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3027 std::chrono::microseconds(10000),
3028 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003029 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003030 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3032 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3033 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003035
3036 NotifySensorArgs args;
3037 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3038 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3039 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3040
3041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3042 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3043 ASSERT_EQ(args.deviceId, DEVICE_ID);
3044 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3045 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3046 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3047 ASSERT_EQ(args.values, values);
3048 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3049}
3050
3051TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3052 setGyroProperties();
3053 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003054 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003055
3056 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3057 std::chrono::microseconds(10000),
3058 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003059 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003060 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3062 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3063 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3064 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003065
3066 NotifySensorArgs args;
3067 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3068 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3069 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3070
3071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3072 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3073 ASSERT_EQ(args.deviceId, DEVICE_ID);
3074 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3075 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3076 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3077 ASSERT_EQ(args.values, values);
3078 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3079}
3080
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081// --- KeyboardInputMapperTest ---
3082
3083class KeyboardInputMapperTest : public InputMapperTest {
3084protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003085 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003086 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003087 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003088
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003090 int32_t originalKeyCode, int32_t rotatedKeyCode,
3091 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092};
3093
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003094/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3095 * orientation.
3096 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003097void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003098 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3099 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003100}
3101
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003102void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003103 int32_t originalScanCode, int32_t originalKeyCode,
3104 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105 NotifyKeyArgs args;
3106
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3109 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3110 ASSERT_EQ(originalScanCode, args.scanCode);
3111 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003112 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003113
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003114 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3116 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3117 ASSERT_EQ(originalScanCode, args.scanCode);
3118 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003119 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003120}
3121
Michael Wrightd02c5b62014-02-10 15:10:22 -08003122TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003123 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003124 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003125 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003126
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003127 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128}
3129
3130TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3131 const int32_t USAGE_A = 0x070004;
3132 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003133 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3134 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003135 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3136 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3137 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003138
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003139 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003140 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003141 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003142 // Initial metastate is AMETA_NONE.
3143 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003144
3145 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003147 NotifyKeyArgs args;
3148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3149 ASSERT_EQ(DEVICE_ID, args.deviceId);
3150 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3151 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3152 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3153 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3154 ASSERT_EQ(KEY_HOME, args.scanCode);
3155 ASSERT_EQ(AMETA_NONE, args.metaState);
3156 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3157 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3158 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3159
3160 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003161 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3163 ASSERT_EQ(DEVICE_ID, args.deviceId);
3164 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3165 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3166 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3167 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3168 ASSERT_EQ(KEY_HOME, args.scanCode);
3169 ASSERT_EQ(AMETA_NONE, args.metaState);
3170 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3171 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3172 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3173
3174 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003175 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3178 ASSERT_EQ(DEVICE_ID, args.deviceId);
3179 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3180 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3181 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3182 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3183 ASSERT_EQ(0, args.scanCode);
3184 ASSERT_EQ(AMETA_NONE, args.metaState);
3185 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3186 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3187 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3188
3189 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003190 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3191 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3193 ASSERT_EQ(DEVICE_ID, args.deviceId);
3194 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3195 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3196 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3197 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3198 ASSERT_EQ(0, args.scanCode);
3199 ASSERT_EQ(AMETA_NONE, args.metaState);
3200 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3201 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3202 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3203
3204 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3208 ASSERT_EQ(DEVICE_ID, args.deviceId);
3209 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3210 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3211 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3212 ASSERT_EQ(0, args.keyCode);
3213 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3214 ASSERT_EQ(AMETA_NONE, args.metaState);
3215 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3216 ASSERT_EQ(0U, args.policyFlags);
3217 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3218
3219 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003220 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3221 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3223 ASSERT_EQ(DEVICE_ID, args.deviceId);
3224 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3225 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3226 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3227 ASSERT_EQ(0, args.keyCode);
3228 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3229 ASSERT_EQ(AMETA_NONE, args.metaState);
3230 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3231 ASSERT_EQ(0U, args.policyFlags);
3232 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3233}
3234
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003235TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3236 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3237 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3238 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3239
3240 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003241 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003242 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3243
3244 // Key down by scan code.
3245 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3246 NotifyKeyArgs args;
3247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3248 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3249
3250 // Key up by scan code.
3251 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3253 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3254}
3255
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003256/**
3257 * Ensure that the readTime is set to the time when the EV_KEY is received.
3258 */
3259TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3260 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3261
3262 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003263 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003264 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3265 NotifyKeyArgs args;
3266
3267 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003268 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3270 ASSERT_EQ(12, args.readTime);
3271
3272 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003273 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3275 ASSERT_EQ(15, args.readTime);
3276}
3277
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003279 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3280 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003281 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3282 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3283 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003284
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003285 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003286 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003287 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288
Arthur Hung95f68612022-04-07 14:08:22 +08003289 // Initial metastate is AMETA_NONE.
3290 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291
3292 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003294 NotifyKeyArgs args;
3295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3296 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003297 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003298 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003299
3300 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003301 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3303 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003304 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003305
3306 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003307 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3309 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003310 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003311
3312 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003313 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3315 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003316 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003317 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003318}
3319
3320TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003321 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3322 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3323 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3324 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003326 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003327 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003328 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003329
Michael Wrighta9cf4192022-12-01 23:46:39 +00003330 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003331 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3332 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3333 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3334 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3335 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3336 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3337 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3338 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3339}
3340
3341TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003342 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3343 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3344 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3345 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346
Michael Wrightd02c5b62014-02-10 15:10:22 -08003347 addConfigurationProperty("keyboard.orientationAware", "1");
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_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003353 ASSERT_NO_FATAL_FAILURE(
3354 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3355 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3356 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3357 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3358 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3359 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3360 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003361
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003362 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003363 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003364 ASSERT_NO_FATAL_FAILURE(
3365 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3366 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3367 AKEYCODE_DPAD_UP, DISPLAY_ID));
3368 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3369 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3370 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3371 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003372
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003373 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003374 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003375 ASSERT_NO_FATAL_FAILURE(
3376 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3377 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3378 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3379 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3380 AKEYCODE_DPAD_UP, DISPLAY_ID));
3381 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3382 AKEYCODE_DPAD_RIGHT, 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_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003386 ASSERT_NO_FATAL_FAILURE(
3387 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3388 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3389 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3390 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3391 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3392 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3393 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003394
3395 // Special case: if orientation changes while key is down, we still emit the same keycode
3396 // in the key up as we did in the key down.
3397 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003398 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003399 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3402 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3403 ASSERT_EQ(KEY_UP, args.scanCode);
3404 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3405
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003406 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003407 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3410 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3411 ASSERT_EQ(KEY_UP, args.scanCode);
3412 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3413}
3414
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003415TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3416 // If the keyboard is not orientation aware,
3417 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003418 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003419
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003420 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003421 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003422 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003423 NotifyKeyArgs args;
3424
3425 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3430 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3431
Michael Wrighta9cf4192022-12-01 23:46:39 +00003432 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003433 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003435 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3437 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3438}
3439
3440TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3441 // If the keyboard is orientation aware,
3442 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003443 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003444
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003445 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003446 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003447 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003448 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003449 NotifyKeyArgs args;
3450
3451 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3452 // ^--- already checked by the previous test
3453
Michael Wrighta9cf4192022-12-01 23:46:39 +00003454 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003455 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3460 ASSERT_EQ(DISPLAY_ID, args.displayId);
3461
3462 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003463 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003464 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003465 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003466 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3470 ASSERT_EQ(newDisplayId, args.displayId);
3471}
3472
Michael Wrightd02c5b62014-02-10 15:10:22 -08003473TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003474 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003475 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003476 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003477
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003478 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003479 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003481 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003482 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483}
3484
Philip Junker4af3b3d2021-12-14 10:36:55 +01003485TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3486 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003487 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003488 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3489
3490 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3491 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3492 << "If a mapping is available, the result is equal to the mapping";
3493
3494 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3495 << "If no mapping is available, the result is the key location";
3496}
3497
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003499 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003500 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003501 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003502
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003503 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003504 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003505
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003506 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003507 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003508}
3509
3510TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003511 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003512 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003513 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003515 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003516
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003518 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003519 ASSERT_TRUE(flags[0]);
3520 ASSERT_FALSE(flags[1]);
3521}
3522
3523TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003524 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3525 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3526 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3527 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3528 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3529 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003530
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003531 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003532 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003533 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003534 // Initial metastate is AMETA_NONE.
3535 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003536
3537 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003538 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3539 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3540 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003541
3542 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003545 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3546 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3547 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003548 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003549
3550 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003551 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3552 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003553 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3554 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3555 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003556 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557
3558 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003559 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3560 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003561 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3562 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3563 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003564 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003565
3566 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003569 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3570 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3571 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003572 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003573
3574 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003575 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3576 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003577 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3578 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3579 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003580 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003581
3582 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003583 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3584 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003585 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3586 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3587 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003588 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003589}
3590
Chris Yea52ade12020-08-27 16:49:20 -07003591TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3592 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3593 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3594 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3595 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3596
3597 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003598 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003599 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3600
Chris Yea52ade12020-08-27 16:49:20 -07003601 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003602 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003603 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3604 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3605 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3606 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3607
3608 NotifyKeyArgs args;
3609 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003610 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3612 ASSERT_EQ(AMETA_NONE, args.metaState);
3613 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3614 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3615 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3616
3617 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003618 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3620 ASSERT_EQ(AMETA_NONE, args.metaState);
3621 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3622 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3623 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3624}
3625
Arthur Hung2c9a3342019-07-23 14:18:59 +08003626TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3627 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003628 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3629 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3630 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3631 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003632
3633 // keyboard 2.
3634 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003635 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003636 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003637 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003638 std::shared_ptr<InputDevice> device2 =
3639 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003640 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003641
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003642 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3643 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3644 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3645 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003646
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003647 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003648 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003649 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003650
Arpit Singh67ca6842023-04-26 14:43:16 +00003651 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003652 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003653 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3654 mFakePolicy
3655 ->getReaderConfiguration(),
3656 AINPUT_SOURCE_KEYBOARD,
3657 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003658 std::list<NotifyArgs> unused =
3659 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003660 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003661 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003662
3663 // Prepared displays and associated info.
3664 constexpr uint8_t hdmi1 = 0;
3665 constexpr uint8_t hdmi2 = 1;
3666 const std::string SECONDARY_UNIQUE_ID = "local:1";
3667
3668 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3669 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3670
3671 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003672 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003673 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003674 ASSERT_FALSE(device2->isEnabled());
3675
3676 // Prepare second display.
3677 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003678 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003679 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003680 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003681 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003682 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003683 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003684 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003685
3686 // Device should be enabled after the associated display is found.
3687 ASSERT_TRUE(mDevice->isEnabled());
3688 ASSERT_TRUE(device2->isEnabled());
3689
3690 // Test pad key events
3691 ASSERT_NO_FATAL_FAILURE(
3692 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3693 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3694 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3695 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3696 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3697 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3698 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3699
3700 ASSERT_NO_FATAL_FAILURE(
3701 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3702 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3703 AKEYCODE_DPAD_RIGHT, newDisplayId));
3704 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3705 AKEYCODE_DPAD_DOWN, newDisplayId));
3706 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3707 AKEYCODE_DPAD_LEFT, newDisplayId));
3708}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709
arthurhungc903df12020-08-11 15:08:42 +08003710TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3711 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3712 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3713 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3714 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3715 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3716 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3717
3718 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003719 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003720 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003721 // Initial metastate is AMETA_NONE.
3722 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003723
3724 // Initialization should have turned all of the lights off.
3725 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3726 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3727 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3728
3729 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003730 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3731 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003732 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3733 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3734
3735 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003736 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3737 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003738 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3739 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3740
3741 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003742 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3743 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003744 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3745 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3746
3747 mFakeEventHub->removeDevice(EVENTHUB_ID);
3748 mReader->loopOnce();
3749
3750 // keyboard 2 should default toggle keys.
3751 const std::string USB2 = "USB2";
3752 const std::string DEVICE_NAME2 = "KEYBOARD2";
3753 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3754 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3755 std::shared_ptr<InputDevice> device2 =
3756 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003757 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003758 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3759 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3760 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3761 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3762 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3763 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3764
Arpit Singh67ca6842023-04-26 14:43:16 +00003765 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003766 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003767 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3768 mFakePolicy
3769 ->getReaderConfiguration(),
3770 AINPUT_SOURCE_KEYBOARD,
3771 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003772 std::list<NotifyArgs> unused =
3773 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003774 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003775 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003776
3777 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3778 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3779 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003780 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3781 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003782}
3783
Arthur Hungcb40a002021-08-03 14:31:01 +00003784TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3785 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3786 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3787 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3788
3789 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh67ca6842023-04-26 14:43:16 +00003790 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003791 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3792 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003793 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003794 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003795 // Initial metastate is AMETA_NONE.
3796 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003797
3798 mReader->toggleCapsLockState(DEVICE_ID);
3799 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3800}
3801
Arthur Hungfb3cc112022-04-13 07:39:50 +00003802TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3803 // keyboard 1.
3804 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3805 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3806 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
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 KeyboardInputMapper& mapper1 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003812 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003813 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3814
3815 // keyboard 2.
3816 const std::string USB2 = "USB2";
3817 const std::string DEVICE_NAME2 = "KEYBOARD2";
3818 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3819 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3820 std::shared_ptr<InputDevice> device2 =
3821 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3822 ftl::Flags<InputDeviceClass>(0));
3823 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3824 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3825 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3826 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3827 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3828 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3829
Arpit Singh67ca6842023-04-26 14:43:16 +00003830 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003831 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003832 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3833 mFakePolicy
3834 ->getReaderConfiguration(),
3835 AINPUT_SOURCE_KEYBOARD,
3836 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003837 std::list<NotifyArgs> unused =
3838 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003839 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003840 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003841
Arthur Hung95f68612022-04-07 14:08:22 +08003842 // Initial metastate is AMETA_NONE.
3843 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3844 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3845
3846 // Toggle num lock on and off.
3847 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3848 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003849 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3850 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3851 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3852
3853 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3854 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3855 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3856 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3857 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3858
3859 // Toggle caps lock on and off.
3860 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3861 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3862 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3863 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3864 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3865
3866 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3867 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3868 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3869 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3870 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3871
3872 // Toggle scroll lock on and off.
3873 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3874 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3875 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3876 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3877 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3878
3879 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3880 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3881 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3882 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3883 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3884}
3885
Arthur Hung2141d542022-08-23 07:45:21 +00003886TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3887 const int32_t USAGE_A = 0x070004;
3888 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3889 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3890
3891 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003892 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00003893 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3894 // Key down by scan code.
3895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3896 NotifyKeyArgs args;
3897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3898 ASSERT_EQ(DEVICE_ID, args.deviceId);
3899 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3900 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3901 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3902 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3903 ASSERT_EQ(KEY_HOME, args.scanCode);
3904 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3905
3906 // Disable device, it should synthesize cancellation events for down events.
3907 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003908 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00003909
3910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3911 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3912 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3913 ASSERT_EQ(KEY_HOME, args.scanCode);
3914 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3915}
3916
Zixuan Qufecb6062022-11-12 04:44:31 +00003917TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh67ca6842023-04-26 14:43:16 +00003918 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3919 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00003920 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003921 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3922 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00003923
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003924 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00003925 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3926
3927 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003928 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00003929
3930 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3931 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3932 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3933 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3934 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003935 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
3936
3937 // Call change layout association with the same values: Generation shouldn't change
3938 generation = mReader->getContext()->getGeneration();
3939 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3940 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3941 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
3942 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00003943}
3944
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003945TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3946 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3947 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3948
3949 // Configuration
Arpit Singh67ca6842023-04-26 14:43:16 +00003950 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003951 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3952 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003953 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003954
3955 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3956 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3957}
3958
Justin Chung71ddb432023-03-27 04:29:07 +00003959TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
3960 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
3961 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003962 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Justin Chung71ddb432023-03-27 04:29:07 +00003963 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3964 NotifyKeyArgs args;
3965
3966 // Key down
3967 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
3968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3969 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
3970}
3971
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003972// --- KeyboardInputMapperTest_ExternalDevice ---
3973
3974class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3975protected:
Chris Yea52ade12020-08-27 16:49:20 -07003976 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003977};
3978
3979TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003980 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3981 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003982
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003983 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3984 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3985 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3986 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003987
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003988 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003989 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003990 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003991
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003993 NotifyKeyArgs args;
3994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3995 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3996
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003997 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3999 ASSERT_EQ(uint32_t(0), args.policyFlags);
4000
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004003 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07004004
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004005 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4007 ASSERT_EQ(uint32_t(0), args.policyFlags);
4008
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004009 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4011 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4012
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004013 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4015 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4016}
4017
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004018TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004019 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004020
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004021 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4022 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4023 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004024
Powei Fengd041c5d2019-05-03 17:11:33 -07004025 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004026 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004027 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004028 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004029
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004030 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004031 NotifyKeyArgs args;
4032 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_HOME, 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
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4041 ASSERT_EQ(uint32_t(0), args.policyFlags);
4042
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004043 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4045 ASSERT_EQ(uint32_t(0), args.policyFlags);
4046
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4049 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4050
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004051 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4053 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4054}
4055
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056// --- CursorInputMapperTest ---
4057
4058class CursorInputMapperTest : public InputMapperTest {
4059protected:
4060 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4061
Michael Wright17db18e2020-06-26 20:51:44 +01004062 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004063
Chris Yea52ade12020-08-27 16:49:20 -07004064 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004065 InputMapperTest::SetUp();
4066
Michael Wright17db18e2020-06-26 20:51:44 +01004067 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004068 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004069 }
4070
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004071 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4072 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004073
Michael Wrighta9cf4192022-12-01 23:46:39 +00004074 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004075 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4076 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4077 }
4078
4079 void prepareSecondaryDisplay() {
4080 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004081 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004082 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004083 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004084
4085 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4086 float pressure) {
4087 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4088 0.0f, 0.0f, 0.0f, EPSILON));
4089 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004090};
4091
4092const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4093
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004094void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4095 int32_t originalY, int32_t rotatedX,
4096 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004097 NotifyMotionArgs args;
4098
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004099 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4100 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4101 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4103 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004104 ASSERT_NO_FATAL_FAILURE(
4105 assertCursorPointerCoords(args.pointerCoords[0],
4106 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4107 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004108}
4109
4110TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004111 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004112 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004113
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004114 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004115}
4116
4117TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004118 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004119 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004120
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004121 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004122}
4123
4124TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004126 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004127
4128 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004129 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130
4131 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004132 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4133 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004134 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4135 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4136
4137 // When the bounds are set, then there should be a valid motion range.
4138 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4139
4140 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004141 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142
4143 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4144 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4145 1, 800 - 1, 0.0f, 0.0f));
4146 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4147 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4148 2, 480 - 1, 0.0f, 0.0f));
4149 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4150 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4151 0.0f, 1.0f, 0.0f, 0.0f));
4152}
4153
4154TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004156 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157
4158 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004159 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160
4161 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4162 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4163 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4164 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4165 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4166 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4167 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4168 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4169 0.0f, 1.0f, 0.0f, 0.0f));
4170}
4171
4172TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004173 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004174 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175
arthurhungdcef2dc2020-08-11 14:47:50 +08004176 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177
4178 NotifyMotionArgs args;
4179
4180 // Button press.
4181 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004182 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4185 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4186 ASSERT_EQ(DEVICE_ID, args.deviceId);
4187 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4188 ASSERT_EQ(uint32_t(0), args.policyFlags);
4189 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4190 ASSERT_EQ(0, args.flags);
4191 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4192 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4193 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004194 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004196 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004197 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004198 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4199 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4200 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4201
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4203 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4204 ASSERT_EQ(DEVICE_ID, args.deviceId);
4205 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4206 ASSERT_EQ(uint32_t(0), args.policyFlags);
4207 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4208 ASSERT_EQ(0, args.flags);
4209 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4210 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4211 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004212 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004213 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004214 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004215 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004216 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4217 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4218 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4219
Michael Wrightd02c5b62014-02-10 15:10:22 -08004220 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004221 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4222 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4224 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4225 ASSERT_EQ(DEVICE_ID, args.deviceId);
4226 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4227 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004228 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4229 ASSERT_EQ(0, args.flags);
4230 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4231 ASSERT_EQ(0, args.buttonState);
4232 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004233 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004234 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004235 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004236 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004237 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4238 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4239 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4240
4241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4242 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4243 ASSERT_EQ(DEVICE_ID, args.deviceId);
4244 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4245 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004246 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4247 ASSERT_EQ(0, args.flags);
4248 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4249 ASSERT_EQ(0, args.buttonState);
4250 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004251 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004253 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004254 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004255 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4256 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4257 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4258}
4259
4260TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004262 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263
4264 NotifyMotionArgs args;
4265
4266 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4268 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4270 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004271 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4272 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4273 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274
4275 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4277 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4279 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004280 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4281 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282}
4283
4284TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004285 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004286 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004287
4288 NotifyMotionArgs args;
4289
4290 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004291 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4294 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004295 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4298 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004299 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004300
Michael Wrightd02c5b62014-02-10 15:10:22 -08004301 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004305 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004306 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004307
4308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004310 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311}
4312
4313TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004314 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004315 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316
4317 NotifyMotionArgs args;
4318
4319 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004320 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4321 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4322 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4325 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004326 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4327 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4328 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004329
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4331 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004332 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4333 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4334 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004335
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004337 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4338 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004342 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4343 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4344 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004345
4346 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4348 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004350 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004351 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004352
4353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004354 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004355 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356}
4357
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004358TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004359 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004361 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4362 // need to be rotated.
4363 addConfigurationProperty("cursor.orientationAware", "1");
Arpit Singhe036ad72023-04-27 12:48:15 +00004364 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365
Michael Wrighta9cf4192022-12-01 23:46:39 +00004366 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004367 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4368 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4369 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4370 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4371 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4372 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4373 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4374 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4375}
4376
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004377TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004378 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004380 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4381 // orientation-aware are affected by display rotation.
Arpit Singhe036ad72023-04-27 12:48:15 +00004382 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004383
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004384 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004385 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004386 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4387 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4388 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4389 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4390 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4391 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4392 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4393 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4394
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004395 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004396 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004397 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4398 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4399 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4400 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4401 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4402 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4403 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4404 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
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_180);
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_270);
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}
4428
4429TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004431 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432
4433 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4434 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435
4436 NotifyMotionArgs motionArgs;
4437 NotifyKeyArgs keyArgs;
4438
4439 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4441 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4443 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4444 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004445 ASSERT_NO_FATAL_FAILURE(
4446 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4449 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4450 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004451 ASSERT_NO_FATAL_FAILURE(
4452 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004453
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004454 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004457 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004458 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004459 ASSERT_NO_FATAL_FAILURE(
4460 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461
4462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004463 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004465 ASSERT_NO_FATAL_FAILURE(
4466 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004467
4468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004470 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004471 ASSERT_NO_FATAL_FAILURE(
4472 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473
4474 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
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));
4479 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4480 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004481 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004482 ASSERT_NO_FATAL_FAILURE(
4483 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004484
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4486 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4487 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004488 ASSERT_NO_FATAL_FAILURE(
4489 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004490
4491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4492 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4493 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004494 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004495 ASSERT_NO_FATAL_FAILURE(
4496 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004497
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004498 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4499 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004501 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004502 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004503 ASSERT_NO_FATAL_FAILURE(
4504 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004505
4506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004507 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004508 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004509 ASSERT_NO_FATAL_FAILURE(
4510 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004512 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4513 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004515 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4516 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004517 ASSERT_NO_FATAL_FAILURE(
4518 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004521
4522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004524 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004525 ASSERT_NO_FATAL_FAILURE(
4526 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004527
Michael Wrightd02c5b62014-02-10 15:10:22 -08004528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4529 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004530 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004531 ASSERT_NO_FATAL_FAILURE(
4532 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533
4534 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4538 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4539 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004540
Michael Wrightd02c5b62014-02-10 15:10:22 -08004541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004542 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004543 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004544 ASSERT_NO_FATAL_FAILURE(
4545 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004546
4547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4548 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4549 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004550 ASSERT_NO_FATAL_FAILURE(
4551 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004552
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4554 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004556 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004557 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004558 ASSERT_NO_FATAL_FAILURE(
4559 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004560
4561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004562 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004563 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004564
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004565 ASSERT_NO_FATAL_FAILURE(
4566 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4568 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4569 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4570
4571 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004572 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4573 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4575 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4576 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004577
Michael Wrightd02c5b62014-02-10 15:10:22 -08004578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004579 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004580 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004581 ASSERT_NO_FATAL_FAILURE(
4582 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004583
4584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4585 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4586 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
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
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004590 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4591 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004593 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004594 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004595 ASSERT_NO_FATAL_FAILURE(
4596 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004597
4598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4599 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4600 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004601 ASSERT_NO_FATAL_FAILURE(
4602 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004603
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4605 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4606 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4607
4608 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004609 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4610 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4612 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4613 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004614
Michael Wrightd02c5b62014-02-10 15:10:22 -08004615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004616 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004617 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004618 ASSERT_NO_FATAL_FAILURE(
4619 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004620
4621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4622 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4623 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004624 ASSERT_NO_FATAL_FAILURE(
4625 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4628 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004630 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004631 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004632 ASSERT_NO_FATAL_FAILURE(
4633 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004634
4635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4636 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4637 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004638 ASSERT_NO_FATAL_FAILURE(
4639 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004640
Michael Wrightd02c5b62014-02-10 15:10:22 -08004641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4642 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4643 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4644
4645 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004646 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4649 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4650 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004651
Michael Wrightd02c5b62014-02-10 15:10:22 -08004652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004653 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004654 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004655 ASSERT_NO_FATAL_FAILURE(
4656 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004657
4658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4659 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4660 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004661 ASSERT_NO_FATAL_FAILURE(
4662 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4665 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004667 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004669 ASSERT_NO_FATAL_FAILURE(
4670 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004671
4672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4673 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4674 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004675 ASSERT_NO_FATAL_FAILURE(
4676 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004677
Michael Wrightd02c5b62014-02-10 15:10:22 -08004678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4679 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4680 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4681}
4682
4683TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004684 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004685 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004686
4687 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4688 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004689
4690 NotifyMotionArgs args;
4691
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4693 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4694 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004696 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4697 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4699 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 +00004700 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004701}
4702
4703TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004704 addConfigurationProperty("cursor.mode", "pointer");
4705 mFakePolicy->setPointerCapture(true);
Arpit Singhe036ad72023-04-27 12:48:15 +00004706 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004707
4708 NotifyDeviceResetArgs resetArgs;
4709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4710 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4711 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4712
4713 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4714 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004715
4716 NotifyMotionArgs args;
4717
4718 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004719 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4720 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4721 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4723 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4726 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 +00004727 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004728
4729 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004730 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4731 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4733 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4734 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4736 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4738 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4739 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4741 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4742
4743 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004744 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4745 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4747 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4748 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4750 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4752 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4753 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4755 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4756
4757 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004758 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4760 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4762 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4763 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4764 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4765 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 +00004766 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004767
4768 // Disable pointer capture and check that the device generation got bumped
4769 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004770 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004771 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004772 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004773 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004774
4775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004776 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4777
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4782 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004783 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4785 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 +00004786 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004787}
4788
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004789/**
4790 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4791 * pointer acceleration or speed processing should not be applied.
4792 */
4793TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4794 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004795 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4796 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004797 mFakePolicy->setVelocityControlParams(testParams);
Arpit Singhe036ad72023-04-27 12:48:15 +00004798 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004799
4800 NotifyDeviceResetArgs resetArgs;
4801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4802 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4803 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4804
4805 NotifyMotionArgs args;
4806
4807 // Move and verify scale is applied.
4808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4810 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4812 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4813 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4814 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4815 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4816 ASSERT_GT(relX, 10);
4817 ASSERT_GT(relY, 20);
4818
4819 // Enable Pointer Capture
4820 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004821 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004822 NotifyPointerCaptureChangedArgs captureArgs;
4823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4824 ASSERT_TRUE(captureArgs.request.enable);
4825
4826 // Move and verify scale is not applied.
4827 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4831 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4832 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4833 ASSERT_EQ(10, args.pointerCoords[0].getX());
4834 ASSERT_EQ(20, args.pointerCoords[0].getY());
4835}
4836
Prabir Pradhan208360b2022-06-24 18:37:04 +00004837TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4838 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004839 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan208360b2022-06-24 18:37:04 +00004840
4841 NotifyDeviceResetArgs resetArgs;
4842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4843 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4844 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4845
4846 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004847 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004848
4849 NotifyMotionArgs args;
4850
4851 // Verify that the coordinates are rotated.
4852 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4853 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4854 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4856 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4857 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4858 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4859 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4860
4861 // Enable Pointer Capture.
4862 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004863 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004864 NotifyPointerCaptureChangedArgs captureArgs;
4865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4866 ASSERT_TRUE(captureArgs.request.enable);
4867
4868 // Move and verify rotation is not applied.
4869 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4870 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4871 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4873 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4875 ASSERT_EQ(10, args.pointerCoords[0].getX());
4876 ASSERT_EQ(20, args.pointerCoords[0].getY());
4877}
4878
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004879TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004880 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004881
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004882 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004883 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004884
4885 // Set up the secondary display as the display on which the pointer should be shown.
4886 // The InputDevice is not associated with any display.
4887 prepareSecondaryDisplay();
4888 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004889 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004890
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004891 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004892 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004893
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004894 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4896 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4897 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004899 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4900 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4901 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004902 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004903}
4904
4905TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004906 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004907
4908 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004909 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004910
4911 // Set up the secondary display as the display on which the pointer should be shown,
4912 // and associate the InputDevice with the secondary display.
4913 prepareSecondaryDisplay();
4914 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4915 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004916 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004917
4918 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4919 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004920
4921 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4922 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4923 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004925 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4926 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4927 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004928 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004929}
4930
4931TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004932 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004933
4934 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004935 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004936 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4937
4938 // Associate the InputDevice with the secondary display.
4939 prepareSecondaryDisplay();
4940 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004941 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004942
4943 // The mapper should not generate any events because it is associated with a display that is
4944 // different from the pointer display.
4945 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004949}
4950
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004951// --- BluetoothCursorInputMapperTest ---
4952
4953class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4954protected:
4955 void SetUp() override {
4956 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4957
4958 mFakePointerController = std::make_shared<FakePointerController>();
4959 mFakePolicy->setPointerController(mFakePointerController);
4960 }
4961};
4962
4963TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4964 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004965 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004966
4967 nsecs_t kernelEventTime = ARBITRARY_TIME;
4968 nsecs_t expectedEventTime = ARBITRARY_TIME;
4969 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4970 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4972 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4973 WithEventTime(expectedEventTime))));
4974
4975 // Process several events that come in quick succession, according to their timestamps.
4976 for (int i = 0; i < 3; i++) {
4977 constexpr static nsecs_t delta = ms2ns(1);
4978 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4979 kernelEventTime += delta;
4980 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4981
4982 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4983 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4985 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4986 WithEventTime(expectedEventTime))));
4987 }
4988}
4989
4990TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4991 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004992 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004993
4994 nsecs_t expectedEventTime = ARBITRARY_TIME;
4995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4996 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4998 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4999 WithEventTime(expectedEventTime))));
5000
5001 // Process several events with the same timestamp from the kernel.
5002 // Ensure that we do not generate events too far into the future.
5003 constexpr static int32_t numEvents =
5004 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
5005 for (int i = 0; i < numEvents; i++) {
5006 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5007
5008 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5009 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5011 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5012 WithEventTime(expectedEventTime))));
5013 }
5014
5015 // By processing more events with the same timestamp, we should not generate events with a
5016 // timestamp that is more than the specified max time delta from the timestamp at its injection.
5017 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
5018 for (int i = 0; i < 3; i++) {
5019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5022 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5023 WithEventTime(cappedEventTime))));
5024 }
5025}
5026
5027TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
5028 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00005029 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005030
5031 nsecs_t kernelEventTime = ARBITRARY_TIME;
5032 nsecs_t expectedEventTime = ARBITRARY_TIME;
5033 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5034 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5036 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5037 WithEventTime(expectedEventTime))));
5038
5039 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
5040 // smoothening is not needed, its timestamp is not affected.
5041 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
5042 expectedEventTime = kernelEventTime;
5043
5044 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5045 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5047 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5048 WithEventTime(expectedEventTime))));
5049}
5050
Michael Wrightd02c5b62014-02-10 15:10:22 -08005051// --- TouchInputMapperTest ---
5052
5053class TouchInputMapperTest : public InputMapperTest {
5054protected:
5055 static const int32_t RAW_X_MIN;
5056 static const int32_t RAW_X_MAX;
5057 static const int32_t RAW_Y_MIN;
5058 static const int32_t RAW_Y_MAX;
5059 static const int32_t RAW_TOUCH_MIN;
5060 static const int32_t RAW_TOUCH_MAX;
5061 static const int32_t RAW_TOOL_MIN;
5062 static const int32_t RAW_TOOL_MAX;
5063 static const int32_t RAW_PRESSURE_MIN;
5064 static const int32_t RAW_PRESSURE_MAX;
5065 static const int32_t RAW_ORIENTATION_MIN;
5066 static const int32_t RAW_ORIENTATION_MAX;
5067 static const int32_t RAW_DISTANCE_MIN;
5068 static const int32_t RAW_DISTANCE_MAX;
5069 static const int32_t RAW_TILT_MIN;
5070 static const int32_t RAW_TILT_MAX;
5071 static const int32_t RAW_ID_MIN;
5072 static const int32_t RAW_ID_MAX;
5073 static const int32_t RAW_SLOT_MIN;
5074 static const int32_t RAW_SLOT_MAX;
5075 static const float X_PRECISION;
5076 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005077 static const float X_PRECISION_VIRTUAL;
5078 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005079
5080 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005081 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005082
5083 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5084
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005085 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005086 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005087
Michael Wrightd02c5b62014-02-10 15:10:22 -08005088 enum Axes {
5089 POSITION = 1 << 0,
5090 TOUCH = 1 << 1,
5091 TOOL = 1 << 2,
5092 PRESSURE = 1 << 3,
5093 ORIENTATION = 1 << 4,
5094 MINOR = 1 << 5,
5095 ID = 1 << 6,
5096 DISTANCE = 1 << 7,
5097 TILT = 1 << 8,
5098 SLOT = 1 << 9,
5099 TOOL_TYPE = 1 << 10,
5100 };
5101
Michael Wrighta9cf4192022-12-01 23:46:39 +00005102 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005103 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00005104 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005106 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005107 int32_t toRawX(float displayX);
5108 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005109 int32_t toRotatedRawX(float displayX);
5110 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005111 float toCookedX(float rawX, float rawY);
5112 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005114 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005115 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005116 float toDisplayY(int32_t rawY, int32_t displayHeight);
5117
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118};
5119
5120const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5121const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5122const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5123const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5124const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5125const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5126const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5127const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005128const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5129const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5131const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5132const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5133const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5134const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5135const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5136const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5137const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5138const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5139const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5140const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5141const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005142const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5143 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5144const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5145 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005146const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5147 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005148
5149const float TouchInputMapperTest::GEOMETRIC_SCALE =
5150 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5151 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5152
5153const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5154 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5155 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5156};
5157
Michael Wrighta9cf4192022-12-01 23:46:39 +00005158void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005159 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5160 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005161}
5162
5163void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5164 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00005165 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005166}
5167
Michael Wrighta9cf4192022-12-01 23:46:39 +00005168void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005169 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5170 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5171 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005172}
5173
Michael Wrightd02c5b62014-02-10 15:10:22 -08005174void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005175 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5176 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5177 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5178 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005179}
5180
Jason Gerecke489fda82012-09-07 17:19:40 -07005181void TouchInputMapperTest::prepareLocationCalibration() {
5182 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5183}
5184
Michael Wrightd02c5b62014-02-10 15:10:22 -08005185int32_t TouchInputMapperTest::toRawX(float displayX) {
5186 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5187}
5188
5189int32_t TouchInputMapperTest::toRawY(float displayY) {
5190 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5191}
5192
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005193int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5194 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5195}
5196
5197int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5198 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5199}
5200
Jason Gerecke489fda82012-09-07 17:19:40 -07005201float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5202 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5203 return rawX;
5204}
5205
5206float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5207 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5208 return rawY;
5209}
5210
Michael Wrightd02c5b62014-02-10 15:10:22 -08005211float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005212 return toDisplayX(rawX, DISPLAY_WIDTH);
5213}
5214
5215float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5216 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005217}
5218
5219float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005220 return toDisplayY(rawY, DISPLAY_HEIGHT);
5221}
5222
5223float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5224 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005225}
5226
5227
5228// --- SingleTouchInputMapperTest ---
5229
5230class SingleTouchInputMapperTest : public TouchInputMapperTest {
5231protected:
5232 void prepareButtons();
5233 void prepareAxes(int axes);
5234
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005235 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5236 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5237 void processUp(SingleTouchInputMapper& mappery);
5238 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5239 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5240 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5241 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5242 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5243 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005244};
5245
5246void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005247 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005248}
5249
5250void SingleTouchInputMapperTest::prepareAxes(int axes) {
5251 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005252 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5253 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005254 }
5255 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005256 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5257 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005258 }
5259 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005260 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5261 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005262 }
5263 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005264 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5265 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005266 }
5267 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005268 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5269 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005270 }
5271}
5272
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005273void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005277}
5278
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005279void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005282}
5283
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005284void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005285 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005286}
5287
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005288void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005290}
5291
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005292void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5293 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005295}
5296
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005297void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005299}
5300
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005301void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5302 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5304 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005305}
5306
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005307void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5308 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005310}
5311
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005312void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314}
5315
Michael Wrightd02c5b62014-02-10 15:10:22 -08005316TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005317 prepareButtons();
5318 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005319 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005320
Josep del Río2d8c79a2023-01-23 19:33:50 +00005321 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322}
5323
Michael Wrightd02c5b62014-02-10 15:10:22 -08005324TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005325 prepareButtons();
5326 prepareAxes(POSITION);
5327 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00005328 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005329
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005330 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005331}
5332
5333TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005334 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005335 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005336 prepareButtons();
5337 prepareAxes(POSITION);
5338 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005339 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005340
5341 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005342 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005343
5344 // Virtual key is down.
5345 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5346 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5347 processDown(mapper, x, y);
5348 processSync(mapper);
5349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5350
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005351 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005352
5353 // Virtual key is up.
5354 processUp(mapper);
5355 processSync(mapper);
5356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5357
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005358 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005359}
5360
5361TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005362 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005363 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005364 prepareButtons();
5365 prepareAxes(POSITION);
5366 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005367 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005368
5369 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005370 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005371
5372 // Virtual key is down.
5373 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5374 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5375 processDown(mapper, x, y);
5376 processSync(mapper);
5377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5378
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005379 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005380
5381 // Virtual key is up.
5382 processUp(mapper);
5383 processSync(mapper);
5384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5385
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005386 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005387}
5388
5389TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005390 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005391 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005392 prepareButtons();
5393 prepareAxes(POSITION);
5394 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005395 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005396
Michael Wrightd02c5b62014-02-10 15:10:22 -08005397 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005398 ASSERT_TRUE(
5399 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005400 ASSERT_TRUE(flags[0]);
5401 ASSERT_FALSE(flags[1]);
5402}
5403
5404TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005405 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005406 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005407 prepareButtons();
5408 prepareAxes(POSITION);
5409 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005410 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411
arthurhungdcef2dc2020-08-11 14:47:50 +08005412 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413
5414 NotifyKeyArgs args;
5415
5416 // Press virtual key.
5417 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5418 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5419 processDown(mapper, x, y);
5420 processSync(mapper);
5421
5422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5423 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5424 ASSERT_EQ(DEVICE_ID, args.deviceId);
5425 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5426 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5427 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5428 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5429 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5430 ASSERT_EQ(KEY_HOME, args.scanCode);
5431 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5432 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5433
5434 // Release virtual key.
5435 processUp(mapper);
5436 processSync(mapper);
5437
5438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5439 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5440 ASSERT_EQ(DEVICE_ID, args.deviceId);
5441 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5442 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5443 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5444 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5445 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5446 ASSERT_EQ(KEY_HOME, args.scanCode);
5447 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5448 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5449
5450 // Should not have sent any motions.
5451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5452}
5453
5454TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005455 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005456 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005457 prepareButtons();
5458 prepareAxes(POSITION);
5459 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005460 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005461
arthurhungdcef2dc2020-08-11 14:47:50 +08005462 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005463
5464 NotifyKeyArgs keyArgs;
5465
5466 // Press virtual key.
5467 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5468 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5469 processDown(mapper, x, y);
5470 processSync(mapper);
5471
5472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5473 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5474 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5475 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5476 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5477 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5478 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5479 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5480 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5481 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5482 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5483
5484 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5485 // into the display area.
5486 y -= 100;
5487 processMove(mapper, x, y);
5488 processSync(mapper);
5489
5490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5491 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5492 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5493 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5494 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5495 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5496 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5497 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5498 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5499 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5500 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5501 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5502
5503 NotifyMotionArgs motionArgs;
5504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5505 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5506 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5507 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5508 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5509 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5510 ASSERT_EQ(0, motionArgs.flags);
5511 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5512 ASSERT_EQ(0, motionArgs.buttonState);
5513 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005514 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005515 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005516 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5518 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5519 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5520 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5521 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5522
5523 // Keep moving out of bounds. Should generate a pointer move.
5524 y -= 50;
5525 processMove(mapper, x, y);
5526 processSync(mapper);
5527
5528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5529 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5530 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5531 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5532 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5534 ASSERT_EQ(0, motionArgs.flags);
5535 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5536 ASSERT_EQ(0, motionArgs.buttonState);
5537 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005538 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005539 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005540 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5542 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5543 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5544 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5545 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5546
5547 // Release out of bounds. Should generate a pointer up.
5548 processUp(mapper);
5549 processSync(mapper);
5550
5551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5552 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5553 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5554 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5555 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5556 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5557 ASSERT_EQ(0, motionArgs.flags);
5558 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5559 ASSERT_EQ(0, motionArgs.buttonState);
5560 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005561 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005562 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005563 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005564 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5565 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5566 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5567 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5568 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5569
5570 // Should not have sent any more keys or motions.
5571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5573}
5574
5575TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005576 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005577 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005578 prepareButtons();
5579 prepareAxes(POSITION);
5580 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005581 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005582
arthurhungdcef2dc2020-08-11 14:47:50 +08005583 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005584
5585 NotifyMotionArgs motionArgs;
5586
5587 // Initially go down out of bounds.
5588 int32_t x = -10;
5589 int32_t y = -10;
5590 processDown(mapper, x, y);
5591 processSync(mapper);
5592
5593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5594
5595 // Move into the display area. Should generate a pointer down.
5596 x = 50;
5597 y = 75;
5598 processMove(mapper, x, y);
5599 processSync(mapper);
5600
5601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5602 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5603 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5604 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5605 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5606 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5607 ASSERT_EQ(0, motionArgs.flags);
5608 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5609 ASSERT_EQ(0, motionArgs.buttonState);
5610 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005611 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005612 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005613 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5615 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5616 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5617 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5618 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5619
5620 // Release. Should generate a pointer up.
5621 processUp(mapper);
5622 processSync(mapper);
5623
5624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5625 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5626 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5627 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5628 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5629 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5630 ASSERT_EQ(0, motionArgs.flags);
5631 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5632 ASSERT_EQ(0, motionArgs.buttonState);
5633 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005634 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005635 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005636 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5638 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5639 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5640 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5641 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5642
5643 // Should not have sent any more keys or motions.
5644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5646}
5647
Santos Cordonfa5cf462017-04-05 10:37:00 -07005648TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005649 addConfigurationProperty("touch.deviceType", "touchScreen");
5650 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5651
Michael Wrighta9cf4192022-12-01 23:46:39 +00005652 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005653 prepareButtons();
5654 prepareAxes(POSITION);
5655 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005656 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005657
arthurhungdcef2dc2020-08-11 14:47:50 +08005658 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005659
5660 NotifyMotionArgs motionArgs;
5661
5662 // Down.
5663 int32_t x = 100;
5664 int32_t y = 125;
5665 processDown(mapper, x, y);
5666 processSync(mapper);
5667
5668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5669 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5670 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5671 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5672 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5673 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5674 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5675 ASSERT_EQ(0, motionArgs.flags);
5676 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5677 ASSERT_EQ(0, motionArgs.buttonState);
5678 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005679 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005680 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005681 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5683 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5684 1, 0, 0, 0, 0, 0, 0, 0));
5685 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5686 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5687 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5688
5689 // Move.
5690 x += 50;
5691 y += 75;
5692 processMove(mapper, x, y);
5693 processSync(mapper);
5694
5695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5696 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5697 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5698 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5699 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5700 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5701 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5702 ASSERT_EQ(0, motionArgs.flags);
5703 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5704 ASSERT_EQ(0, motionArgs.buttonState);
5705 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005706 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005707 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005708 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5710 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5711 1, 0, 0, 0, 0, 0, 0, 0));
5712 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5713 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5714 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5715
5716 // Up.
5717 processUp(mapper);
5718 processSync(mapper);
5719
5720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5721 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5722 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5723 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5724 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5725 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5726 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5727 ASSERT_EQ(0, motionArgs.flags);
5728 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5729 ASSERT_EQ(0, motionArgs.buttonState);
5730 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005731 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005732 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005733 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5735 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5736 1, 0, 0, 0, 0, 0, 0, 0));
5737 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5738 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5739 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5740
5741 // Should not have sent any more keys or motions.
5742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5744}
5745
Michael Wrightd02c5b62014-02-10 15:10:22 -08005746TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005747 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005748 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005749 prepareButtons();
5750 prepareAxes(POSITION);
5751 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005752 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005753
arthurhungdcef2dc2020-08-11 14:47:50 +08005754 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005755
5756 NotifyMotionArgs motionArgs;
5757
5758 // Down.
5759 int32_t x = 100;
5760 int32_t y = 125;
5761 processDown(mapper, x, y);
5762 processSync(mapper);
5763
5764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5765 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5766 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5767 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5768 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5769 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5770 ASSERT_EQ(0, motionArgs.flags);
5771 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5772 ASSERT_EQ(0, motionArgs.buttonState);
5773 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005774 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005775 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005776 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5778 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5779 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5780 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5781 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5782
5783 // Move.
5784 x += 50;
5785 y += 75;
5786 processMove(mapper, x, y);
5787 processSync(mapper);
5788
5789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5790 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5791 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5792 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5793 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5794 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5795 ASSERT_EQ(0, motionArgs.flags);
5796 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5797 ASSERT_EQ(0, motionArgs.buttonState);
5798 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005799 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005800 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005801 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5803 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5804 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5805 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5806 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5807
5808 // Up.
5809 processUp(mapper);
5810 processSync(mapper);
5811
5812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5813 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5814 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5815 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5816 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5817 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5818 ASSERT_EQ(0, motionArgs.flags);
5819 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5820 ASSERT_EQ(0, motionArgs.buttonState);
5821 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005822 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005823 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005824 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5826 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5827 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5828 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5829 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5830
5831 // Should not have sent any more keys or motions.
5832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5834}
5835
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005836TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005837 addConfigurationProperty("touch.deviceType", "touchScreen");
5838 prepareButtons();
5839 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005840 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5841 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005842 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005843
5844 NotifyMotionArgs args;
5845
5846 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005847 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005848 processDown(mapper, toRawX(50), toRawY(75));
5849 processSync(mapper);
5850
5851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5852 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5853 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5854
5855 processUp(mapper);
5856 processSync(mapper);
5857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5858}
5859
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005860TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005861 addConfigurationProperty("touch.deviceType", "touchScreen");
5862 prepareButtons();
5863 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005864 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5865 // orientation-aware are affected by display rotation.
5866 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005867 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005868
5869 NotifyMotionArgs args;
5870
5871 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005872 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005873 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005874 processDown(mapper, toRawX(50), toRawY(75));
5875 processSync(mapper);
5876
5877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5878 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5879 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5880
5881 processUp(mapper);
5882 processSync(mapper);
5883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5884
5885 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005886 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005887 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005888 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005889 processSync(mapper);
5890
5891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5892 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5893 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5894
5895 processUp(mapper);
5896 processSync(mapper);
5897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5898
5899 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005900 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005901 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005902 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5903 processSync(mapper);
5904
5905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5906 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5907 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5908
5909 processUp(mapper);
5910 processSync(mapper);
5911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5912
5913 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005914 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005915 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005916 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005917 processSync(mapper);
5918
5919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5920 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5921 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5922
5923 processUp(mapper);
5924 processSync(mapper);
5925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5926}
5927
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005928TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5929 addConfigurationProperty("touch.deviceType", "touchScreen");
5930 prepareButtons();
5931 prepareAxes(POSITION);
5932 addConfigurationProperty("touch.orientationAware", "1");
5933 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5934 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005935 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005936 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005937 NotifyMotionArgs args;
5938
5939 // Orientation 0.
5940 processDown(mapper, toRawX(50), toRawY(75));
5941 processSync(mapper);
5942
5943 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5944 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5945 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5946
5947 processUp(mapper);
5948 processSync(mapper);
5949 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5950}
5951
5952TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5953 addConfigurationProperty("touch.deviceType", "touchScreen");
5954 prepareButtons();
5955 prepareAxes(POSITION);
5956 addConfigurationProperty("touch.orientationAware", "1");
5957 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5958 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005959 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005960 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005961 NotifyMotionArgs args;
5962
5963 // Orientation 90.
5964 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5965 processSync(mapper);
5966
5967 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5968 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5969 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5970
5971 processUp(mapper);
5972 processSync(mapper);
5973 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5974}
5975
5976TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5977 addConfigurationProperty("touch.deviceType", "touchScreen");
5978 prepareButtons();
5979 prepareAxes(POSITION);
5980 addConfigurationProperty("touch.orientationAware", "1");
5981 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5982 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005983 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005984 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005985 NotifyMotionArgs args;
5986
5987 // Orientation 180.
5988 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5989 processSync(mapper);
5990
5991 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5992 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5993 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5994
5995 processUp(mapper);
5996 processSync(mapper);
5997 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5998}
5999
6000TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6001 addConfigurationProperty("touch.deviceType", "touchScreen");
6002 prepareButtons();
6003 prepareAxes(POSITION);
6004 addConfigurationProperty("touch.orientationAware", "1");
6005 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6006 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006007 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006008 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006009 NotifyMotionArgs args;
6010
6011 // Orientation 270.
6012 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6013 processSync(mapper);
6014
6015 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6016 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6017 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6018
6019 processUp(mapper);
6020 processSync(mapper);
6021 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6022}
6023
6024TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6025 addConfigurationProperty("touch.deviceType", "touchScreen");
6026 prepareButtons();
6027 prepareAxes(POSITION);
6028 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6029 // orientation-aware are affected by display rotation.
6030 addConfigurationProperty("touch.orientationAware", "0");
6031 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00006032 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006033
6034 NotifyMotionArgs args;
6035
6036 // Orientation 90, Rotation 0.
6037 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006038 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006039 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6040 processSync(mapper);
6041
6042 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6043 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6044 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6045
6046 processUp(mapper);
6047 processSync(mapper);
6048 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6049
6050 // Orientation 90, Rotation 90.
6051 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006052 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006053 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006054 processSync(mapper);
6055
6056 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6057 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6058 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6059
6060 processUp(mapper);
6061 processSync(mapper);
6062 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6063
6064 // Orientation 90, Rotation 180.
6065 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006066 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006067 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6068 processSync(mapper);
6069
6070 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6071 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6072 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6073
6074 processUp(mapper);
6075 processSync(mapper);
6076 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6077
6078 // Orientation 90, Rotation 270.
6079 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006080 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006081 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 -07006082 processSync(mapper);
6083
6084 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6085 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6086 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6087
6088 processUp(mapper);
6089 processSync(mapper);
6090 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6091}
6092
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006093TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
6094 addConfigurationProperty("touch.deviceType", "touchScreen");
6095 prepareButtons();
6096 prepareAxes(POSITION);
6097 addConfigurationProperty("touch.orientationAware", "1");
6098 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006099 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006100
6101 // Set a physical frame in the display viewport.
6102 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6103 viewport->physicalLeft = 20;
6104 viewport->physicalTop = 600;
6105 viewport->physicalRight = 30;
6106 viewport->physicalBottom = 610;
6107 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006108 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006109
6110 // Start the touch.
6111 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6112 processSync(mapper);
6113
6114 // Expect all input starting outside the physical frame to be ignored.
6115 const std::array<Point, 6> outsidePoints = {
6116 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6117 for (const auto& p : outsidePoints) {
6118 processMove(mapper, toRawX(p.x), toRawY(p.y));
6119 processSync(mapper);
6120 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6121 }
6122
6123 // Move the touch into the physical frame.
6124 processMove(mapper, toRawX(25), toRawY(605));
6125 processSync(mapper);
6126 NotifyMotionArgs args;
6127 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6128 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6129 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6130 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6131
6132 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
6133 for (const auto& p : outsidePoints) {
6134 processMove(mapper, toRawX(p.x), toRawY(p.y));
6135 processSync(mapper);
6136 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6137 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
6138 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6139 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6140 }
6141
6142 processUp(mapper);
6143 processSync(mapper);
6144 EXPECT_NO_FATAL_FAILURE(
6145 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
6146}
6147
Harry Cutts1db43992023-06-19 17:05:07 +00006148TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
6149 std::shared_ptr<FakePointerController> fakePointerController =
6150 std::make_shared<FakePointerController>();
6151 mFakePolicy->setPointerController(fakePointerController);
6152
6153 addConfigurationProperty("touch.deviceType", "pointer");
6154 prepareAxes(POSITION);
6155 prepareDisplay(ui::ROTATION_0);
6156 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
6157
6158 // Set a physical frame in the display viewport.
6159 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6160 viewport->physicalLeft = 20;
6161 viewport->physicalTop = 600;
6162 viewport->physicalRight = 30;
6163 viewport->physicalBottom = 610;
6164 mFakePolicy->updateViewport(*viewport);
6165 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
6166
6167 // Start the touch.
6168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6169 processSync(mapper);
6170
6171 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
6172 // produced.
6173 const std::array<Point, 6> outsidePoints = {
6174 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6175 for (const auto& p : outsidePoints) {
6176 processMove(mapper, toRawX(p.x), toRawY(p.y));
6177 processSync(mapper);
6178 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6179 }
6180}
6181
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006183 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006184 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006185 prepareButtons();
6186 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00006187 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006188
6189 // These calculations are based on the input device calibration documentation.
6190 int32_t rawX = 100;
6191 int32_t rawY = 200;
6192 int32_t rawPressure = 10;
6193 int32_t rawToolMajor = 12;
6194 int32_t rawDistance = 2;
6195 int32_t rawTiltX = 30;
6196 int32_t rawTiltY = 110;
6197
6198 float x = toDisplayX(rawX);
6199 float y = toDisplayY(rawY);
6200 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6201 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6202 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6203 float distance = float(rawDistance);
6204
6205 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6206 float tiltScale = M_PI / 180;
6207 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6208 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6209 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6210 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6211
6212 processDown(mapper, rawX, rawY);
6213 processPressure(mapper, rawPressure);
6214 processToolMajor(mapper, rawToolMajor);
6215 processDistance(mapper, rawDistance);
6216 processTilt(mapper, rawTiltX, rawTiltY);
6217 processSync(mapper);
6218
6219 NotifyMotionArgs args;
6220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6221 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6222 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6223 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6224}
6225
Jason Gerecke489fda82012-09-07 17:19:40 -07006226TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006227 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006228 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07006229 prepareLocationCalibration();
6230 prepareButtons();
6231 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006232 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006233
6234 int32_t rawX = 100;
6235 int32_t rawY = 200;
6236
6237 float x = toDisplayX(toCookedX(rawX, rawY));
6238 float y = toDisplayY(toCookedY(rawX, rawY));
6239
6240 processDown(mapper, rawX, rawY);
6241 processSync(mapper);
6242
6243 NotifyMotionArgs args;
6244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6246 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6247}
6248
Michael Wrightd02c5b62014-02-10 15:10:22 -08006249TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006250 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006251 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252 prepareButtons();
6253 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006254 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006255
6256 NotifyMotionArgs motionArgs;
6257 NotifyKeyArgs keyArgs;
6258
6259 processDown(mapper, 100, 200);
6260 processSync(mapper);
6261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6262 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6263 ASSERT_EQ(0, motionArgs.buttonState);
6264
6265 // press BTN_LEFT, release BTN_LEFT
6266 processKey(mapper, BTN_LEFT, 1);
6267 processSync(mapper);
6268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6270 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6271
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6273 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6274 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6275
Michael Wrightd02c5b62014-02-10 15:10:22 -08006276 processKey(mapper, BTN_LEFT, 0);
6277 processSync(mapper);
6278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006279 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006280 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006281
6282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006284 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006285
6286 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6287 processKey(mapper, BTN_RIGHT, 1);
6288 processKey(mapper, BTN_MIDDLE, 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_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6293 motionArgs.buttonState);
6294
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6296 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6297 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6298
6299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6301 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6302 motionArgs.buttonState);
6303
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304 processKey(mapper, BTN_RIGHT, 0);
6305 processSync(mapper);
6306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006307 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006309
6310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006312 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006313
6314 processKey(mapper, BTN_MIDDLE, 0);
6315 processSync(mapper);
6316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006317 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006319
6320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006321 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006322 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006323
6324 // press BTN_BACK, release BTN_BACK
6325 processKey(mapper, BTN_BACK, 1);
6326 processSync(mapper);
6327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6328 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6329 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006330
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006333 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6334
6335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6336 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6337 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338
6339 processKey(mapper, BTN_BACK, 0);
6340 processSync(mapper);
6341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006342 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006343 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006344
6345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006347 ASSERT_EQ(0, motionArgs.buttonState);
6348
Michael Wrightd02c5b62014-02-10 15:10:22 -08006349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6350 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6351 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6352
6353 // press BTN_SIDE, release BTN_SIDE
6354 processKey(mapper, BTN_SIDE, 1);
6355 processSync(mapper);
6356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6357 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6358 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006359
Michael Wrightd02c5b62014-02-10 15:10:22 -08006360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006362 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6363
6364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6365 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6366 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006367
6368 processKey(mapper, BTN_SIDE, 0);
6369 processSync(mapper);
6370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006371 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006372 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006373
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006376 ASSERT_EQ(0, motionArgs.buttonState);
6377
Michael Wrightd02c5b62014-02-10 15:10:22 -08006378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6379 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6380 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6381
6382 // press BTN_FORWARD, release BTN_FORWARD
6383 processKey(mapper, BTN_FORWARD, 1);
6384 processSync(mapper);
6385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6386 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6387 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006388
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006391 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6392
6393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6394 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6395 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006396
6397 processKey(mapper, BTN_FORWARD, 0);
6398 processSync(mapper);
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006400 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006401 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006402
6403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006405 ASSERT_EQ(0, motionArgs.buttonState);
6406
Michael Wrightd02c5b62014-02-10 15:10:22 -08006407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6408 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6409 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6410
6411 // press BTN_EXTRA, release BTN_EXTRA
6412 processKey(mapper, BTN_EXTRA, 1);
6413 processSync(mapper);
6414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6415 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6416 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006417
Michael Wrightd02c5b62014-02-10 15:10:22 -08006418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006420 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6421
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6423 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6424 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006425
6426 processKey(mapper, BTN_EXTRA, 0);
6427 processSync(mapper);
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006429 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006430 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006431
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006433 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006434 ASSERT_EQ(0, motionArgs.buttonState);
6435
Michael Wrightd02c5b62014-02-10 15:10:22 -08006436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6437 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6438 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6439
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6441
Michael Wrightd02c5b62014-02-10 15:10:22 -08006442 // press BTN_STYLUS, release BTN_STYLUS
6443 processKey(mapper, BTN_STYLUS, 1);
6444 processSync(mapper);
6445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6446 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006447 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6448
6449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6450 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6451 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006452
6453 processKey(mapper, BTN_STYLUS, 0);
6454 processSync(mapper);
6455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006456 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006457 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006458
6459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006461 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006462
6463 // press BTN_STYLUS2, release BTN_STYLUS2
6464 processKey(mapper, BTN_STYLUS2, 1);
6465 processSync(mapper);
6466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006468 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6469
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6471 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6472 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006473
6474 processKey(mapper, BTN_STYLUS2, 0);
6475 processSync(mapper);
6476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006477 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006478 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006479
6480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006482 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006483
6484 // release touch
6485 processUp(mapper);
6486 processSync(mapper);
6487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6488 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6489 ASSERT_EQ(0, motionArgs.buttonState);
6490}
6491
6492TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006493 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006494 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006495 prepareButtons();
6496 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006497 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006498
6499 NotifyMotionArgs motionArgs;
6500
6501 // default tool type is finger
6502 processDown(mapper, 100, 200);
6503 processSync(mapper);
6504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6505 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006506 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006507
6508 // eraser
6509 processKey(mapper, BTN_TOOL_RUBBER, 1);
6510 processSync(mapper);
6511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006513 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006514
6515 // stylus
6516 processKey(mapper, BTN_TOOL_RUBBER, 0);
6517 processKey(mapper, BTN_TOOL_PEN, 1);
6518 processSync(mapper);
6519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006521 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006522
6523 // brush
6524 processKey(mapper, BTN_TOOL_PEN, 0);
6525 processKey(mapper, BTN_TOOL_BRUSH, 1);
6526 processSync(mapper);
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006529 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006530
6531 // pencil
6532 processKey(mapper, BTN_TOOL_BRUSH, 0);
6533 processKey(mapper, BTN_TOOL_PENCIL, 1);
6534 processSync(mapper);
6535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6536 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006537 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006538
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006539 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006540 processKey(mapper, BTN_TOOL_PENCIL, 0);
6541 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6542 processSync(mapper);
6543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6544 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006545 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006546
6547 // mouse
6548 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6549 processKey(mapper, BTN_TOOL_MOUSE, 1);
6550 processSync(mapper);
6551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006553 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006554
6555 // lens
6556 processKey(mapper, BTN_TOOL_MOUSE, 0);
6557 processKey(mapper, BTN_TOOL_LENS, 1);
6558 processSync(mapper);
6559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6560 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006561 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006562
6563 // double-tap
6564 processKey(mapper, BTN_TOOL_LENS, 0);
6565 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6566 processSync(mapper);
6567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6568 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006569 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006570
6571 // triple-tap
6572 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6573 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6574 processSync(mapper);
6575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6576 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006577 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006578
6579 // quad-tap
6580 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6581 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6582 processSync(mapper);
6583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6584 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006585 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006586
6587 // finger
6588 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6589 processKey(mapper, BTN_TOOL_FINGER, 1);
6590 processSync(mapper);
6591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6592 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006593 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006594
6595 // stylus trumps finger
6596 processKey(mapper, BTN_TOOL_PEN, 1);
6597 processSync(mapper);
6598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006600 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006601
6602 // eraser trumps stylus
6603 processKey(mapper, BTN_TOOL_RUBBER, 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::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006608
6609 // mouse trumps eraser
6610 processKey(mapper, BTN_TOOL_MOUSE, 1);
6611 processSync(mapper);
6612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6613 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006614 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006615
6616 // back to default tool type
6617 processKey(mapper, BTN_TOOL_MOUSE, 0);
6618 processKey(mapper, BTN_TOOL_RUBBER, 0);
6619 processKey(mapper, BTN_TOOL_PEN, 0);
6620 processKey(mapper, BTN_TOOL_FINGER, 0);
6621 processSync(mapper);
6622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006624 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006625}
6626
6627TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006628 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006629 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006630 prepareButtons();
6631 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006632 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006633 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006634
6635 NotifyMotionArgs motionArgs;
6636
6637 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6638 processKey(mapper, BTN_TOOL_FINGER, 1);
6639 processMove(mapper, 100, 200);
6640 processSync(mapper);
6641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6642 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6644 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6645
6646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6647 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6649 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6650
6651 // move a little
6652 processMove(mapper, 150, 250);
6653 processSync(mapper);
6654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6655 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6657 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6658
6659 // down when BTN_TOUCH is pressed, pressure defaults to 1
6660 processKey(mapper, BTN_TOUCH, 1);
6661 processSync(mapper);
6662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6663 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6665 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6666
6667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6668 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6670 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6671
6672 // up when BTN_TOUCH is released, hover restored
6673 processKey(mapper, BTN_TOUCH, 0);
6674 processSync(mapper);
6675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6676 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6678 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6679
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6681 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6683 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6684
6685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6686 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6688 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6689
6690 // exit hover when pointer goes away
6691 processKey(mapper, BTN_TOOL_FINGER, 0);
6692 processSync(mapper);
6693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6694 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6696 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6697}
6698
6699TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006700 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006701 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006702 prepareButtons();
6703 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006704 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006705
6706 NotifyMotionArgs motionArgs;
6707
6708 // initially hovering because pressure is 0
6709 processDown(mapper, 100, 200);
6710 processPressure(mapper, 0);
6711 processSync(mapper);
6712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6713 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6714 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6715 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6716
6717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6718 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6719 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6720 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6721
6722 // move a little
6723 processMove(mapper, 150, 250);
6724 processSync(mapper);
6725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6726 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6728 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6729
6730 // down when pressure is non-zero
6731 processPressure(mapper, RAW_PRESSURE_MAX);
6732 processSync(mapper);
6733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6734 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6736 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6737
6738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6739 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6741 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6742
6743 // up when pressure becomes 0, hover restored
6744 processPressure(mapper, 0);
6745 processSync(mapper);
6746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6747 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6749 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6750
6751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6752 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6754 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6755
6756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6757 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6759 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6760
6761 // exit hover when pointer goes away
6762 processUp(mapper);
6763 processSync(mapper);
6764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6765 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6767 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6768}
6769
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006770TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6771 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006772 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006773 prepareButtons();
6774 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006775 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006776
6777 // Touch down.
6778 processDown(mapper, 100, 200);
6779 processPressure(mapper, 1);
6780 processSync(mapper);
6781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6782 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6783
6784 // Reset the mapper. This should cancel the ongoing gesture.
6785 resetMapper(mapper, ARBITRARY_TIME);
6786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6787 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6788
6789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6790}
6791
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006792TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6793 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006794 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006795 prepareButtons();
6796 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006797 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006798
6799 // Set the initial state for the touch pointer.
6800 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6801 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6802 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6803 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6804
6805 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006806 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6807 // does not generate any events.
6808 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006809
6810 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6811 // the recreated touch state to generate a down event.
6812 processSync(mapper);
6813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6814 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6815
6816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6817}
6818
lilinnan687e58f2022-07-19 16:00:50 +08006819TEST_F(SingleTouchInputMapperTest,
6820 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6821 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006822 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006823 prepareButtons();
6824 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006825 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006826 NotifyMotionArgs motionArgs;
6827
6828 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006829 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006830 processSync(mapper);
6831
6832 // We should receive a down event
6833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6834 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6835
6836 // Change display id
6837 clearViewports();
6838 prepareSecondaryDisplay(ViewportType::INTERNAL);
6839
6840 // We should receive a cancel event
6841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6842 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6843 // Then receive reset called
6844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6845}
6846
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006847TEST_F(SingleTouchInputMapperTest,
6848 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6849 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006850 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006851 prepareButtons();
6852 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006853 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6855 NotifyMotionArgs motionArgs;
6856
6857 // Start a new gesture.
6858 processDown(mapper, 100, 200);
6859 processSync(mapper);
6860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6861 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6862
6863 // Make the viewport inactive. This will put the device in disabled mode.
6864 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6865 viewport->isActive = false;
6866 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006867 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006868
6869 // We should receive a cancel event for the ongoing gesture.
6870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6871 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6872 // Then we should be notified that the device was reset.
6873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6874
6875 // No events are generated while the viewport is inactive.
6876 processMove(mapper, 101, 201);
6877 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006878 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006879 processSync(mapper);
6880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6881
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006882 // Start a new gesture while the viewport is still inactive.
6883 processDown(mapper, 300, 400);
6884 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6885 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6886 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6887 processSync(mapper);
6888
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006889 // Make the viewport active again. The device should resume processing events.
6890 viewport->isActive = true;
6891 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006892 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006893
6894 // The device is reset because it changes back to direct mode, without generating any events.
6895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6897
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006898 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006899 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6901 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006902
6903 // No more events.
6904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6906}
6907
Prabir Pradhan211ba622022-10-31 21:09:21 +00006908TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6909 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006910 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006911 prepareButtons();
6912 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006913 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6915
6916 // Press a stylus button.
6917 processKey(mapper, BTN_STYLUS, 1);
6918 processSync(mapper);
6919
6920 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6921 processDown(mapper, 100, 200);
6922 processSync(mapper);
6923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6924 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6925 WithCoords(toDisplayX(100), toDisplayY(200)),
6926 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6928 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6929 WithCoords(toDisplayX(100), toDisplayY(200)),
6930 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6931
6932 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6933 // the button has not actually been released, since there will be no pointers through which the
6934 // button state can be reported. The event is generated at the location of the pointer before
6935 // it went up.
6936 processUp(mapper);
6937 processSync(mapper);
6938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6939 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6940 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6942 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6943 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6944}
6945
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006946TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6947 addConfigurationProperty("touch.deviceType", "touchScreen");
6948 prepareDisplay(ui::ROTATION_0);
6949 prepareButtons();
6950 prepareAxes(POSITION);
6951
6952 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6953
Arpit Singha8c236b2023-04-25 13:56:05 +00006954 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6956
6957 // Press a stylus button.
6958 processKey(mapper, BTN_STYLUS, 1);
6959 processSync(mapper);
6960
6961 // Start a touch gesture and ensure that the stylus button is not reported.
6962 processDown(mapper, 100, 200);
6963 processSync(mapper);
6964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6965 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6966
6967 // Release and press the stylus button again.
6968 processKey(mapper, BTN_STYLUS, 0);
6969 processSync(mapper);
6970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6971 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6972 processKey(mapper, BTN_STYLUS, 1);
6973 processSync(mapper);
6974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6975 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6976
6977 // Release the touch gesture.
6978 processUp(mapper);
6979 processSync(mapper);
6980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6981 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6982
6983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6984}
6985
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006986TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6987 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6988 prepareDisplay(ui::ROTATION_0);
6989 prepareButtons();
6990 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006991 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6993
6994 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6995}
6996
Seunghwan Choi356026c2023-02-01 14:37:25 +09006997TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6998 std::shared_ptr<FakePointerController> fakePointerController =
6999 std::make_shared<FakePointerController>();
7000 addConfigurationProperty("touch.deviceType", "touchScreen");
7001 prepareDisplay(ui::ROTATION_0);
7002 prepareButtons();
7003 prepareAxes(POSITION);
7004 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7005 mFakePolicy->setPointerController(fakePointerController);
7006 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00007007 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09007008
7009 processKey(mapper, BTN_TOOL_PEN, 1);
7010 processMove(mapper, 100, 200);
7011 processSync(mapper);
7012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7013 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007014 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09007015 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
7016 ASSERT_TRUE(fakePointerController->isPointerShown());
7017 ASSERT_NO_FATAL_FAILURE(
7018 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
7019}
7020
7021TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
7022 std::shared_ptr<FakePointerController> fakePointerController =
7023 std::make_shared<FakePointerController>();
7024 addConfigurationProperty("touch.deviceType", "touchScreen");
7025 prepareDisplay(ui::ROTATION_0);
7026 prepareButtons();
7027 prepareAxes(POSITION);
7028 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7029 mFakePolicy->setPointerController(fakePointerController);
7030 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00007031 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09007032
7033 processKey(mapper, BTN_TOOL_PEN, 1);
7034 processMove(mapper, 100, 200);
7035 processSync(mapper);
7036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7037 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007038 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09007039 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
7040 ASSERT_FALSE(fakePointerController->isPointerShown());
7041}
7042
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007043TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
7044 // Initialize the device without setting device source to touch navigation.
7045 addConfigurationProperty("touch.deviceType", "touchScreen");
7046 prepareDisplay(ui::ROTATION_0);
7047 prepareButtons();
7048 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007049 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007050
7051 // Ensure that the device is created as a touchscreen, not touch navigation.
7052 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7053
7054 // Add device type association after the device was created.
7055 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
7056
7057 // Send update to the mapper.
7058 std::list<NotifyArgs> unused2 =
7059 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007060 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007061
7062 // Check whether device type update was successful.
7063 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
7064}
7065
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007066TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
7067 // Initialize the device without setting device source to touch navigation.
7068 addConfigurationProperty("touch.deviceType", "touchScreen");
7069 prepareDisplay(ui::ROTATION_0);
7070 prepareButtons();
7071 prepareAxes(POSITION);
7072 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7073
7074 // Set a physical frame in the display viewport.
7075 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7076 viewport->physicalLeft = 0;
7077 viewport->physicalTop = 0;
7078 viewport->physicalRight = DISPLAY_WIDTH / 2;
7079 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
7080 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007081 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007082
Arpit Singha8c236b2023-04-25 13:56:05 +00007083 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007084
7085 // Hovering inside the physical frame produces events.
7086 processKey(mapper, BTN_TOOL_PEN, 1);
7087 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
7088 processSync(mapper);
7089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7090 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
7091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7092 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
7093
7094 // Leaving the physical frame ends the hovering gesture.
7095 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
7096 processSync(mapper);
7097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7098 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
7099
7100 // Moving outside the physical frame does not produce events.
7101 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
7102 processSync(mapper);
7103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7104
7105 // Re-entering the physical frame produces events.
7106 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
7107 processSync(mapper);
7108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7109 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
7110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7111 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
7112}
7113
Prabir Pradhan5632d622021-09-06 07:57:20 -07007114// --- TouchDisplayProjectionTest ---
7115
7116class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7117public:
7118 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7119 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7120 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00007121 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
7122 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
7123 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007124 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007125 auto rotatedWidth = naturalDisplayWidth;
7126 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007127 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00007128 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007129 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007130 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007131 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007132 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007133 inverseRotationFlags = ui::Transform::ROT_180;
7134 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007135 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007136 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007137 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007138 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007139 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007140 inverseRotationFlags = ui::Transform::ROT_0;
7141 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007142 }
7143
Prabir Pradhana9df3162022-12-05 23:57:27 +00007144 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007145 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7146
7147 std::optional<DisplayViewport> internalViewport =
7148 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7149 DisplayViewport& v = *internalViewport;
7150 v.displayId = DISPLAY_ID;
7151 v.orientation = orientation;
7152
7153 v.logicalLeft = 0;
7154 v.logicalTop = 0;
7155 v.logicalRight = 100;
7156 v.logicalBottom = 100;
7157
7158 v.physicalLeft = rotatedPhysicalDisplay.left;
7159 v.physicalTop = rotatedPhysicalDisplay.top;
7160 v.physicalRight = rotatedPhysicalDisplay.right;
7161 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7162
Prabir Pradhana9df3162022-12-05 23:57:27 +00007163 v.deviceWidth = rotatedWidth;
7164 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007165
7166 v.isActive = true;
7167 v.uniqueId = UNIQUE_ID;
7168 v.type = ViewportType::INTERNAL;
7169 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007170 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007171 }
7172
7173 void assertReceivedMove(const Point& point) {
7174 NotifyMotionArgs motionArgs;
7175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7176 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007177 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007178 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7179 1, 0, 0, 0, 0, 0, 0, 0));
7180 }
7181};
7182
7183TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7184 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007185 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007186
7187 prepareButtons();
7188 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007189 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007190
7191 NotifyMotionArgs motionArgs;
7192
7193 // Configure the DisplayViewport such that the logical display maps to a subsection of
7194 // the display panel called the physical display. Here, the physical display is bounded by the
7195 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7196 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7197 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7198 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7199
Michael Wrighta9cf4192022-12-01 23:46:39 +00007200 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007201 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7202
7203 // Touches outside the physical display should be ignored, and should not generate any
7204 // events. Ensure touches at the following points that lie outside of the physical display
7205 // area do not generate any events.
7206 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7207 processDown(mapper, toRawX(point.x), toRawY(point.y));
7208 processSync(mapper);
7209 processUp(mapper);
7210 processSync(mapper);
7211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7212 << "Unexpected event generated for touch outside physical display at point: "
7213 << point.x << ", " << point.y;
7214 }
7215 }
7216}
7217
7218TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7219 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007220 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007221
7222 prepareButtons();
7223 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007224 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007225
7226 NotifyMotionArgs motionArgs;
7227
7228 // Configure the DisplayViewport such that the logical display maps to a subsection of
7229 // the display panel called the physical display. Here, the physical display is bounded by the
7230 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7231 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7232
Michael Wrighta9cf4192022-12-01 23:46:39 +00007233 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007234 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7235
7236 // Touches that start outside the physical display should be ignored until it enters the
7237 // physical display bounds, at which point it should generate a down event. Start a touch at
7238 // the point (5, 100), which is outside the physical display bounds.
7239 static const Point kOutsidePoint{5, 100};
7240 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7241 processSync(mapper);
7242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7243
7244 // Move the touch into the physical display area. This should generate a pointer down.
7245 processMove(mapper, toRawX(11), toRawY(21));
7246 processSync(mapper);
7247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7248 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007249 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007250 ASSERT_NO_FATAL_FAILURE(
7251 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7252
7253 // Move the touch inside the physical display area. This should generate a pointer move.
7254 processMove(mapper, toRawX(69), toRawY(159));
7255 processSync(mapper);
7256 assertReceivedMove({69, 159});
7257
7258 // Move outside the physical display area. Since the pointer is already down, this should
7259 // now continue generating events.
7260 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7261 processSync(mapper);
7262 assertReceivedMove(kOutsidePoint);
7263
7264 // Release. This should generate a pointer up.
7265 processUp(mapper);
7266 processSync(mapper);
7267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7268 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7270 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7271
7272 // Ensure no more events were generated.
7273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7275 }
7276}
7277
Prabir Pradhana9df3162022-12-05 23:57:27 +00007278// --- TouchscreenPrecisionTests ---
7279
7280// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
7281// in various orientations and with different display rotations. We configure the touchscreen to
7282// have a higher resolution than that of the display by an integer scale factor in each axis so that
7283// we can enforce that coordinates match precisely as expected.
7284class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
7285 public ::testing::WithParamInterface<ui::Rotation> {
7286public:
7287 void SetUp() override {
7288 SingleTouchInputMapperTest::SetUp();
7289
7290 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7291 // four times the resolution of the display in the Y axis.
7292 prepareButtons();
7293 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007294 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7295 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007296 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007297 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7298 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007299 }
7300
7301 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7302 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7303 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7304 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7305
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007306 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7307 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7308
7309 static const int32_t PRECISION_RAW_X_FLAT = 16;
7310 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7311
7312 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7313 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7314
Prabir Pradhana9df3162022-12-05 23:57:27 +00007315 static const std::array<Point, 4> kRawCorners;
7316};
7317
7318const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7319 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7320 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7321 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7322 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7323}};
7324
7325// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7326// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7327// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7328TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7329 enum class Orientation {
7330 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7331 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7332 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7333 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7334 ftl_last = ORIENTATION_270,
7335 };
7336 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7337 Orientation::ORIENTATION_270;
7338 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7339 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7340 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7341 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7342 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7343 };
7344
7345 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7346
7347 // Configure the touchscreen as being installed in the one of the four different orientations
7348 // relative to the display.
7349 addConfigurationProperty("touch.deviceType", "touchScreen");
7350 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7351 prepareDisplay(ui::ROTATION_0);
7352
Arpit Singha8c236b2023-04-25 13:56:05 +00007353 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00007354
7355 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7356 // orientations of either 90 or 270) this means the display's natural resolution will be
7357 // flipped.
7358 const bool displayRotated =
7359 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7360 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7361 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7362 const Rect physicalFrame{0, 0, width, height};
7363 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7364
7365 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7366 const float expectedPrecisionX = displayRotated ? 4 : 2;
7367 const float expectedPrecisionY = displayRotated ? 2 : 4;
7368
7369 // Test all four corners.
7370 for (int i = 0; i < 4; i++) {
7371 const auto& raw = kRawCorners[i];
7372 processDown(mapper, raw.x, raw.y);
7373 processSync(mapper);
7374 const auto& expected = expectedPoints[i];
7375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7376 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7377 WithCoords(expected.x, expected.y),
7378 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7379 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7380 << "with touchscreen orientation "
7381 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7382 << expected.x << ", " << expected.y << ").";
7383 processUp(mapper);
7384 processSync(mapper);
7385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7386 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7387 WithCoords(expected.x, expected.y))));
7388 }
7389}
7390
Prabir Pradhan82687402022-12-06 01:32:53 +00007391TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7392 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7393 kMappedCorners = {
7394 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7395 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7396 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7397 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7398 };
7399
7400 const ui::Rotation displayRotation = GetParam();
7401
7402 addConfigurationProperty("touch.deviceType", "touchScreen");
7403 prepareDisplay(displayRotation);
7404
Arpit Singha8c236b2023-04-25 13:56:05 +00007405 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00007406
7407 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7408
7409 // Test all four corners.
7410 for (int i = 0; i < 4; i++) {
7411 const auto& expected = expectedPoints[i];
7412 const auto& raw = kRawCorners[i];
7413 processDown(mapper, raw.x, raw.y);
7414 processSync(mapper);
7415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7416 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7417 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7418 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7419 << "with display rotation " << ui::toCString(displayRotation)
7420 << ", expected point (" << expected.x << ", " << expected.y << ").";
7421 processUp(mapper);
7422 processSync(mapper);
7423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7424 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7425 WithCoords(expected.x, expected.y))));
7426 }
7427}
7428
Prabir Pradhan3e798762022-12-02 21:02:11 +00007429TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7430 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7431 kMappedCorners = {
7432 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7433 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7434 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7435 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7436 };
7437
7438 const ui::Rotation displayRotation = GetParam();
7439
7440 addConfigurationProperty("touch.deviceType", "touchScreen");
7441 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7442
Arpit Singha8c236b2023-04-25 13:56:05 +00007443 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00007444
7445 // Ori 270, so width and height swapped
7446 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7447 prepareDisplay(displayRotation);
7448 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7449
7450 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7451
7452 // Test all four corners.
7453 for (int i = 0; i < 4; i++) {
7454 const auto& expected = expectedPoints[i];
7455 const auto& raw = kRawCorners[i];
7456 processDown(mapper, raw.x, raw.y);
7457 processSync(mapper);
7458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7459 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7460 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7461 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7462 << "with display rotation " << ui::toCString(displayRotation)
7463 << ", expected point (" << expected.x << ", " << expected.y << ").";
7464 processUp(mapper);
7465 processSync(mapper);
7466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7467 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7468 WithCoords(expected.x, expected.y))));
7469 }
7470}
7471
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007472TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7473 const ui::Rotation displayRotation = GetParam();
7474
7475 addConfigurationProperty("touch.deviceType", "touchScreen");
7476 prepareDisplay(displayRotation);
7477
7478 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00007479 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007480
7481 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7482 // MotionRanges use display pixels as their units
7483 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7484 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7485
7486 // The MotionRanges should be oriented in the rotated display's coordinate space
7487 const bool displayRotated =
7488 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7489
7490 constexpr float MAX_X = 479.5;
7491 constexpr float MAX_Y = 799.75;
7492 EXPECT_EQ(xRange->min, 0.f);
7493 EXPECT_EQ(yRange->min, 0.f);
7494 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7495 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7496
7497 EXPECT_EQ(xRange->flat, 8.f);
7498 EXPECT_EQ(yRange->flat, 8.f);
7499
7500 EXPECT_EQ(xRange->fuzz, 2.f);
7501 EXPECT_EQ(yRange->fuzz, 2.f);
7502
7503 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7504 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7505}
7506
Prabir Pradhana9df3162022-12-05 23:57:27 +00007507// Run the precision tests for all rotations.
7508INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7509 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7510 ui::ROTATION_270),
7511 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7512 return ftl::enum_string(testParamInfo.param);
7513 });
7514
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007515// --- ExternalStylusFusionTest ---
7516
7517class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7518public:
7519 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7520 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007521 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007522 prepareButtons();
7523 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007524 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007525
7526 mStylusState.when = ARBITRARY_TIME;
7527 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007528 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007529 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007530 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007531 processExternalStylusState(mapper);
7532 return mapper;
7533 }
7534
7535 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7536 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7537 for (const NotifyArgs& args : generatedArgs) {
7538 mFakeListener->notify(args);
7539 }
7540 // Loop the reader to flush the input listener queue.
7541 mReader->loopOnce();
7542 return generatedArgs;
7543 }
7544
7545protected:
7546 StylusState mStylusState{};
7547 static constexpr uint32_t EXPECTED_SOURCE =
7548 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7549
7550 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7551 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007552 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007553
7554 // The first pointer is withheld.
7555 processDown(mapper, 100, 200);
7556 processSync(mapper);
7557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7558 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7559 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7560
7561 // The external stylus reports pressure. The withheld finger pointer is released as a
7562 // stylus.
7563 mStylusState.pressure = 1.f;
7564 processExternalStylusState(mapper);
7565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7566 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7567 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7568
7569 // Subsequent pointer events are not withheld.
7570 processMove(mapper, 101, 201);
7571 processSync(mapper);
7572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7573 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7574
7575 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7577 }
7578
7579 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7580 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7581
7582 // Releasing the touch pointer ends the gesture.
7583 processUp(mapper);
7584 processSync(mapper);
7585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7586 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007587 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007588
7589 mStylusState.pressure = 0.f;
7590 processExternalStylusState(mapper);
7591 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7593 }
7594
7595 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7596 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007597 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007598
7599 // The first pointer is withheld when an external stylus is connected,
7600 // and a timeout is requested.
7601 processDown(mapper, 100, 200);
7602 processSync(mapper);
7603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7604 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7605 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7606
7607 // If the timeout expires early, it is requested again.
7608 handleTimeout(mapper, ARBITRARY_TIME + 1);
7609 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7610 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7611
7612 // When the timeout expires, the withheld touch is released as a finger pointer.
7613 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7615 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7616
7617 // Subsequent pointer events are not withheld.
7618 processMove(mapper, 101, 201);
7619 processSync(mapper);
7620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7621 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7622 processUp(mapper);
7623 processSync(mapper);
7624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7625 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7626
7627 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7629 }
7630
7631private:
7632 InputDeviceInfo mExternalStylusDeviceInfo{};
7633};
7634
7635TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7636 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7637 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7638}
7639
7640TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7641 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7642 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7643}
7644
7645TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7646 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7647 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7648}
7649
7650// Test a successful stylus fusion gesture where the pressure is reported by the external
7651// before the touch is reported by the touchscreen.
7652TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7653 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7654 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007655 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007656
7657 // The external stylus reports pressure first. It is ignored for now.
7658 mStylusState.pressure = 1.f;
7659 processExternalStylusState(mapper);
7660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7661 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7662
7663 // When the touch goes down afterwards, it is reported as a stylus pointer.
7664 processDown(mapper, 100, 200);
7665 processSync(mapper);
7666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7667 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7668 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7669
7670 processMove(mapper, 101, 201);
7671 processSync(mapper);
7672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7673 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7674 processUp(mapper);
7675 processSync(mapper);
7676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7677 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7678
7679 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7681}
7682
7683TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7684 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7685
7686 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7687 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7688
7689 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7690 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7691 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7692 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7693}
7694
7695TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7696 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7697 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007698 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007699
7700 mStylusState.pressure = 0.8f;
7701 processExternalStylusState(mapper);
7702 processDown(mapper, 100, 200);
7703 processSync(mapper);
7704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7705 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7706 WithPressure(0.8f))));
7707 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7708
7709 // The external stylus reports a pressure change. We wait for some time for a touch event.
7710 mStylusState.pressure = 0.6f;
7711 processExternalStylusState(mapper);
7712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7713 ASSERT_NO_FATAL_FAILURE(
7714 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7715
7716 // If a touch is reported within the timeout, it reports the updated pressure.
7717 processMove(mapper, 101, 201);
7718 processSync(mapper);
7719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7720 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7721 WithPressure(0.6f))));
7722 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7723
7724 // There is another pressure change.
7725 mStylusState.pressure = 0.5f;
7726 processExternalStylusState(mapper);
7727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7728 ASSERT_NO_FATAL_FAILURE(
7729 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7730
7731 // If a touch is not reported within the timeout, a move event is generated to report
7732 // the new pressure.
7733 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7735 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7736 WithPressure(0.5f))));
7737
7738 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7739 // repeated indefinitely.
7740 mStylusState.pressure = 0.0f;
7741 processExternalStylusState(mapper);
7742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7743 ASSERT_NO_FATAL_FAILURE(
7744 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7745 processMove(mapper, 102, 202);
7746 processSync(mapper);
7747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7748 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7749 WithPressure(0.5f))));
7750 processMove(mapper, 103, 203);
7751 processSync(mapper);
7752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7753 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7754 WithPressure(0.5f))));
7755
7756 processUp(mapper);
7757 processSync(mapper);
7758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7759 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007760 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007761
7762 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7764}
7765
7766TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7767 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7768 auto source = WithSource(EXPECTED_SOURCE);
7769
7770 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007771 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007772 processExternalStylusState(mapper);
7773 processDown(mapper, 100, 200);
7774 processSync(mapper);
7775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7776 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007777 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007778 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7779
7780 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007781 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007782 processExternalStylusState(mapper);
7783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7784 ASSERT_NO_FATAL_FAILURE(
7785 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7786
7787 // If a touch is reported within the timeout, it reports the updated pressure.
7788 processMove(mapper, 101, 201);
7789 processSync(mapper);
7790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7791 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007792 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007793 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7794
7795 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007796 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007797 processExternalStylusState(mapper);
7798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7799 ASSERT_NO_FATAL_FAILURE(
7800 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7801
7802 // If a touch is not reported within the timeout, a move event is generated to report
7803 // the new tool type.
7804 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7806 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007807 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007808
7809 processUp(mapper);
7810 processSync(mapper);
7811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7812 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007813 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007814
7815 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7817}
7818
7819TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7820 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7821 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007822 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007823
7824 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7825
7826 // The external stylus reports a button change. We wait for some time for a touch event.
7827 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7828 processExternalStylusState(mapper);
7829 ASSERT_NO_FATAL_FAILURE(
7830 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7831
7832 // If a touch is reported within the timeout, it reports the updated button state.
7833 processMove(mapper, 101, 201);
7834 processSync(mapper);
7835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7836 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7837 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7839 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7840 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7841 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7842
7843 // The button is now released.
7844 mStylusState.buttons = 0;
7845 processExternalStylusState(mapper);
7846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7847 ASSERT_NO_FATAL_FAILURE(
7848 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7849
7850 // If a touch is not reported within the timeout, a move event is generated to report
7851 // the new button state.
7852 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7854 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7855 WithButtonState(0))));
7856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007857 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7858 WithButtonState(0))));
7859
7860 processUp(mapper);
7861 processSync(mapper);
7862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007863 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7864
7865 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7867}
7868
Michael Wrightd02c5b62014-02-10 15:10:22 -08007869// --- MultiTouchInputMapperTest ---
7870
7871class MultiTouchInputMapperTest : public TouchInputMapperTest {
7872protected:
7873 void prepareAxes(int axes);
7874
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007875 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7876 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7877 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7878 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7879 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7880 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7881 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7882 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7883 void processId(MultiTouchInputMapper& mapper, int32_t id);
7884 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7885 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7886 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007887 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007888 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007889 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7890 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007891};
7892
7893void MultiTouchInputMapperTest::prepareAxes(int axes) {
7894 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007895 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7896 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007897 }
7898 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007899 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7900 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007901 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007902 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7903 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007904 }
7905 }
7906 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007907 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7908 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007909 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007910 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007911 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007912 }
7913 }
7914 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007915 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7916 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007917 }
7918 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007919 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7920 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007921 }
7922 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007923 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7924 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007925 }
7926 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007927 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7928 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007929 }
7930 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007931 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7932 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007933 }
7934 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007935 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007936 }
7937}
7938
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007939void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7940 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007941 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007943}
7944
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007945void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7946 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007948}
7949
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007950void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7951 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007952 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007953}
7954
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007955void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007956 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007957}
7958
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007959void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007960 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007961}
7962
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007963void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7964 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007965 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007966}
7967
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007968void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007970}
7971
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007972void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007974}
7975
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007976void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007977 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007978}
7979
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007980void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007981 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007982}
7983
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007984void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007985 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007986}
7987
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007988void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7989 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007990 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007991}
7992
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007993void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7994 int32_t value) {
7995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7996 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7997}
7998
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007999void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008001}
8002
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00008003void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
8004 nsecs_t readTime) {
8005 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008006}
8007
Michael Wrightd02c5b62014-02-10 15:10:22 -08008008TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008009 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008010 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008011 prepareAxes(POSITION);
8012 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008013 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008014
arthurhungdcef2dc2020-08-11 14:47:50 +08008015 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008016
8017 NotifyMotionArgs motionArgs;
8018
8019 // Two fingers down at once.
8020 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8021 processPosition(mapper, x1, y1);
8022 processMTSync(mapper);
8023 processPosition(mapper, x2, y2);
8024 processMTSync(mapper);
8025 processSync(mapper);
8026
8027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8028 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8029 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8030 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8031 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8032 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8033 ASSERT_EQ(0, motionArgs.flags);
8034 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8035 ASSERT_EQ(0, motionArgs.buttonState);
8036 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008037 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008038 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008039 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008040 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8041 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8042 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8043 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8044 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8045
8046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8047 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8048 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8049 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8050 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008051 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008052 ASSERT_EQ(0, motionArgs.flags);
8053 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8054 ASSERT_EQ(0, motionArgs.buttonState);
8055 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008056 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008057 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008058 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008059 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008060 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8062 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8064 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8065 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8066 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8067 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8068
8069 // Move.
8070 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8071 processPosition(mapper, x1, y1);
8072 processMTSync(mapper);
8073 processPosition(mapper, x2, y2);
8074 processMTSync(mapper);
8075 processSync(mapper);
8076
8077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8078 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8079 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8080 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8081 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8082 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8083 ASSERT_EQ(0, motionArgs.flags);
8084 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8085 ASSERT_EQ(0, motionArgs.buttonState);
8086 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008087 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008088 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008089 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008090 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008091 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8093 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8095 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8096 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8097 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8098 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8099
8100 // First finger up.
8101 x2 += 15; y2 -= 20;
8102 processPosition(mapper, x2, y2);
8103 processMTSync(mapper);
8104 processSync(mapper);
8105
8106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8107 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8108 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8109 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8110 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008111 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008112 ASSERT_EQ(0, motionArgs.flags);
8113 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8114 ASSERT_EQ(0, motionArgs.buttonState);
8115 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008116 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008117 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008118 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008119 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008120 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008121 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8122 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8123 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8124 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8125 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8126 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8127 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8128
8129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8130 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8131 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8132 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8133 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8135 ASSERT_EQ(0, motionArgs.flags);
8136 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8137 ASSERT_EQ(0, motionArgs.buttonState);
8138 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008139 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008140 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008141 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008142 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8143 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8144 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8145 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8146 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8147
8148 // Move.
8149 x2 += 20; y2 -= 25;
8150 processPosition(mapper, x2, y2);
8151 processMTSync(mapper);
8152 processSync(mapper);
8153
8154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8155 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8156 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8157 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8158 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8159 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8160 ASSERT_EQ(0, motionArgs.flags);
8161 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8162 ASSERT_EQ(0, motionArgs.buttonState);
8163 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008164 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008165 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008166 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008167 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8168 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8169 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8170 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8171 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8172
8173 // New finger down.
8174 int32_t x3 = 700, y3 = 300;
8175 processPosition(mapper, x2, y2);
8176 processMTSync(mapper);
8177 processPosition(mapper, x3, y3);
8178 processMTSync(mapper);
8179 processSync(mapper);
8180
8181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8182 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8183 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8184 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8185 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008186 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008187 ASSERT_EQ(0, motionArgs.flags);
8188 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8189 ASSERT_EQ(0, motionArgs.buttonState);
8190 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008191 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008192 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008193 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008194 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008195 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008196 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8197 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8198 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8199 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8200 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8201 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8202 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8203
8204 // Second finger up.
8205 x3 += 30; y3 -= 20;
8206 processPosition(mapper, x3, y3);
8207 processMTSync(mapper);
8208 processSync(mapper);
8209
8210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8211 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8212 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8213 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8214 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008215 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008216 ASSERT_EQ(0, motionArgs.flags);
8217 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8218 ASSERT_EQ(0, motionArgs.buttonState);
8219 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008220 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008221 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008222 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008223 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008224 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008225 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8226 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8228 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8229 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8230 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8231 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8232
8233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8234 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8235 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8236 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8237 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8238 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8239 ASSERT_EQ(0, motionArgs.flags);
8240 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8241 ASSERT_EQ(0, motionArgs.buttonState);
8242 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008243 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008244 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008245 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8247 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8248 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8249 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8250 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8251
8252 // Last finger up.
8253 processMTSync(mapper);
8254 processSync(mapper);
8255
8256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8257 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8258 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8259 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8260 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8261 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8262 ASSERT_EQ(0, motionArgs.flags);
8263 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8264 ASSERT_EQ(0, motionArgs.buttonState);
8265 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008266 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008267 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008268 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8270 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8271 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8272 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8273 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8274
8275 // Should not have sent any more keys or motions.
8276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8278}
8279
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008280TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8281 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008282 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008283
8284 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8285 /*fuzz*/ 0, /*resolution*/ 10);
8286 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8287 /*fuzz*/ 0, /*resolution*/ 11);
8288 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8289 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8290 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8291 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8292 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8293 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8294 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8295 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8296
Arpit Singha8c236b2023-04-25 13:56:05 +00008297 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008298
8299 // X and Y axes
8300 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8301 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8302 // Touch major and minor
8303 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8304 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8305 // Tool major and minor
8306 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8307 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8308}
8309
8310TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8311 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008312 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008313
8314 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8315 /*fuzz*/ 0, /*resolution*/ 10);
8316 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8317 /*fuzz*/ 0, /*resolution*/ 11);
8318
8319 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8320
Arpit Singha8c236b2023-04-25 13:56:05 +00008321 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008322
8323 // Touch major and minor
8324 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8325 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8326 // Tool major and minor
8327 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8328 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8329}
8330
Michael Wrightd02c5b62014-02-10 15:10:22 -08008331TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008332 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008333 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008334 prepareAxes(POSITION | ID);
8335 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008336 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008337
arthurhungdcef2dc2020-08-11 14:47:50 +08008338 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008339
8340 NotifyMotionArgs motionArgs;
8341
8342 // Two fingers down at once.
8343 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8344 processPosition(mapper, x1, y1);
8345 processId(mapper, 1);
8346 processMTSync(mapper);
8347 processPosition(mapper, x2, y2);
8348 processId(mapper, 2);
8349 processMTSync(mapper);
8350 processSync(mapper);
8351
8352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8353 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008354 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008355 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008356 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8358 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8359
8360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008361 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008362 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008363 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008364 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008365 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008366 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8368 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8370 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8371
8372 // Move.
8373 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8374 processPosition(mapper, x1, y1);
8375 processId(mapper, 1);
8376 processMTSync(mapper);
8377 processPosition(mapper, x2, y2);
8378 processId(mapper, 2);
8379 processMTSync(mapper);
8380 processSync(mapper);
8381
8382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, 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 // First finger up.
8395 x2 += 15; y2 -= 20;
8396 processPosition(mapper, x2, y2);
8397 processId(mapper, 2);
8398 processMTSync(mapper);
8399 processSync(mapper);
8400
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008402 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008403 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008404 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008405 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008406 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008407 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008408 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8409 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8411 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8412
8413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8414 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008415 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008416 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008417 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8419 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8420
8421 // Move.
8422 x2 += 20; y2 -= 25;
8423 processPosition(mapper, x2, y2);
8424 processId(mapper, 2);
8425 processMTSync(mapper);
8426 processSync(mapper);
8427
8428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008430 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008431 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008432 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8434 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8435
8436 // New finger down.
8437 int32_t x3 = 700, y3 = 300;
8438 processPosition(mapper, x2, y2);
8439 processId(mapper, 2);
8440 processMTSync(mapper);
8441 processPosition(mapper, x3, y3);
8442 processId(mapper, 3);
8443 processMTSync(mapper);
8444 processSync(mapper);
8445
8446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008447 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008448 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008449 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008450 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008451 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008452 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8454 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8456 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8457
8458 // Second finger up.
8459 x3 += 30; y3 -= 20;
8460 processPosition(mapper, x3, y3);
8461 processId(mapper, 3);
8462 processMTSync(mapper);
8463 processSync(mapper);
8464
8465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008466 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008467 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008468 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008469 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008470 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008471 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8473 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8475 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8476
8477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8478 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008479 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008480 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008481 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008482 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8483 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8484
8485 // Last finger up.
8486 processMTSync(mapper);
8487 processSync(mapper);
8488
8489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8490 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008491 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008492 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008493 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].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
8497 // Should not have sent any more keys or motions.
8498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8500}
8501
8502TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008503 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008504 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008505 prepareAxes(POSITION | ID | SLOT);
8506 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008507 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008508
arthurhungdcef2dc2020-08-11 14:47:50 +08008509 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008510
8511 NotifyMotionArgs motionArgs;
8512
8513 // Two fingers down at once.
8514 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8515 processPosition(mapper, x1, y1);
8516 processId(mapper, 1);
8517 processSlot(mapper, 1);
8518 processPosition(mapper, x2, y2);
8519 processId(mapper, 2);
8520 processSync(mapper);
8521
8522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8523 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008524 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008525 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008526 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8528 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8529
8530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008531 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008532 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008533 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008534 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008535 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008536 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8538 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8540 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8541
8542 // Move.
8543 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8544 processSlot(mapper, 0);
8545 processPosition(mapper, x1, y1);
8546 processSlot(mapper, 1);
8547 processPosition(mapper, x2, y2);
8548 processSync(mapper);
8549
8550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008552 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008553 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008554 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008555 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008556 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8558 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8560 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8561
8562 // First finger up.
8563 x2 += 15; y2 -= 20;
8564 processSlot(mapper, 0);
8565 processId(mapper, -1);
8566 processSlot(mapper, 1);
8567 processPosition(mapper, x2, y2);
8568 processSync(mapper);
8569
8570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008571 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008572 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008573 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008574 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008575 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008576 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8578 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8580 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8581
8582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8583 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008584 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008585 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008586 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8588 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8589
8590 // Move.
8591 x2 += 20; y2 -= 25;
8592 processPosition(mapper, x2, y2);
8593 processSync(mapper);
8594
8595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8596 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008597 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008598 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008599 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8601 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8602
8603 // New finger down.
8604 int32_t x3 = 700, y3 = 300;
8605 processPosition(mapper, x2, y2);
8606 processSlot(mapper, 0);
8607 processId(mapper, 3);
8608 processPosition(mapper, x3, y3);
8609 processSync(mapper);
8610
8611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008612 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008613 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008614 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008615 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008616 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008617 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8619 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8621 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8622
8623 // Second finger up.
8624 x3 += 30; y3 -= 20;
8625 processSlot(mapper, 1);
8626 processId(mapper, -1);
8627 processSlot(mapper, 0);
8628 processPosition(mapper, x3, y3);
8629 processSync(mapper);
8630
8631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008632 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008633 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008634 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008635 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008636 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008637 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8639 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8641 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8642
8643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008645 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008646 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008647 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8649 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8650
8651 // Last finger up.
8652 processId(mapper, -1);
8653 processSync(mapper);
8654
8655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8656 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008657 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008658 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008659 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].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
8663 // Should not have sent any more keys or motions.
8664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8666}
8667
8668TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008669 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008670 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008671 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008672 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008673
8674 // These calculations are based on the input device calibration documentation.
8675 int32_t rawX = 100;
8676 int32_t rawY = 200;
8677 int32_t rawTouchMajor = 7;
8678 int32_t rawTouchMinor = 6;
8679 int32_t rawToolMajor = 9;
8680 int32_t rawToolMinor = 8;
8681 int32_t rawPressure = 11;
8682 int32_t rawDistance = 0;
8683 int32_t rawOrientation = 3;
8684 int32_t id = 5;
8685
8686 float x = toDisplayX(rawX);
8687 float y = toDisplayY(rawY);
8688 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8689 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8690 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8691 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8692 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8693 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8694 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8695 float distance = float(rawDistance);
8696
8697 processPosition(mapper, rawX, rawY);
8698 processTouchMajor(mapper, rawTouchMajor);
8699 processTouchMinor(mapper, rawTouchMinor);
8700 processToolMajor(mapper, rawToolMajor);
8701 processToolMinor(mapper, rawToolMinor);
8702 processPressure(mapper, rawPressure);
8703 processOrientation(mapper, rawOrientation);
8704 processDistance(mapper, rawDistance);
8705 processId(mapper, id);
8706 processMTSync(mapper);
8707 processSync(mapper);
8708
8709 NotifyMotionArgs args;
8710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8711 ASSERT_EQ(0, args.pointerProperties[0].id);
8712 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8713 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8714 orientation, distance));
8715}
8716
8717TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008718 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008719 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008720 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8721 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00008722 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008723
8724 // These calculations are based on the input device calibration documentation.
8725 int32_t rawX = 100;
8726 int32_t rawY = 200;
8727 int32_t rawTouchMajor = 140;
8728 int32_t rawTouchMinor = 120;
8729 int32_t rawToolMajor = 180;
8730 int32_t rawToolMinor = 160;
8731
8732 float x = toDisplayX(rawX);
8733 float y = toDisplayY(rawY);
8734 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8735 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8736 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8737 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8738 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8739
8740 processPosition(mapper, rawX, rawY);
8741 processTouchMajor(mapper, rawTouchMajor);
8742 processTouchMinor(mapper, rawTouchMinor);
8743 processToolMajor(mapper, rawToolMajor);
8744 processToolMinor(mapper, rawToolMinor);
8745 processMTSync(mapper);
8746 processSync(mapper);
8747
8748 NotifyMotionArgs args;
8749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8750 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8751 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8752}
8753
8754TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008755 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008756 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008757 prepareAxes(POSITION | TOUCH | TOOL);
8758 addConfigurationProperty("touch.size.calibration", "diameter");
8759 addConfigurationProperty("touch.size.scale", "10");
8760 addConfigurationProperty("touch.size.bias", "160");
8761 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00008762 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008763
8764 // These calculations are based on the input device calibration documentation.
8765 // Note: We only provide a single common touch/tool value because the device is assumed
8766 // not to emit separate values for each pointer (isSummed = 1).
8767 int32_t rawX = 100;
8768 int32_t rawY = 200;
8769 int32_t rawX2 = 150;
8770 int32_t rawY2 = 250;
8771 int32_t rawTouchMajor = 5;
8772 int32_t rawToolMajor = 8;
8773
8774 float x = toDisplayX(rawX);
8775 float y = toDisplayY(rawY);
8776 float x2 = toDisplayX(rawX2);
8777 float y2 = toDisplayY(rawY2);
8778 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8779 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8780 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8781
8782 processPosition(mapper, rawX, rawY);
8783 processTouchMajor(mapper, rawTouchMajor);
8784 processToolMajor(mapper, rawToolMajor);
8785 processMTSync(mapper);
8786 processPosition(mapper, rawX2, rawY2);
8787 processTouchMajor(mapper, rawTouchMajor);
8788 processToolMajor(mapper, rawToolMajor);
8789 processMTSync(mapper);
8790 processSync(mapper);
8791
8792 NotifyMotionArgs args;
8793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8794 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8795
8796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008797 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008798 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008799 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8800 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8802 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8803}
8804
8805TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008806 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008807 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008808 prepareAxes(POSITION | TOUCH | TOOL);
8809 addConfigurationProperty("touch.size.calibration", "area");
8810 addConfigurationProperty("touch.size.scale", "43");
8811 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00008812 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008813
8814 // These calculations are based on the input device calibration documentation.
8815 int32_t rawX = 100;
8816 int32_t rawY = 200;
8817 int32_t rawTouchMajor = 5;
8818 int32_t rawToolMajor = 8;
8819
8820 float x = toDisplayX(rawX);
8821 float y = toDisplayY(rawY);
8822 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8823 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8824 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8825
8826 processPosition(mapper, rawX, rawY);
8827 processTouchMajor(mapper, rawTouchMajor);
8828 processToolMajor(mapper, rawToolMajor);
8829 processMTSync(mapper);
8830 processSync(mapper);
8831
8832 NotifyMotionArgs args;
8833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8835 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8836}
8837
8838TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008839 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008840 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008841 prepareAxes(POSITION | PRESSURE);
8842 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8843 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00008844 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008845
Michael Wrightaa449c92017-12-13 21:21:43 +00008846 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008847 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008848 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8849 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8850 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8851
Michael Wrightd02c5b62014-02-10 15:10:22 -08008852 // These calculations are based on the input device calibration documentation.
8853 int32_t rawX = 100;
8854 int32_t rawY = 200;
8855 int32_t rawPressure = 60;
8856
8857 float x = toDisplayX(rawX);
8858 float y = toDisplayY(rawY);
8859 float pressure = float(rawPressure) * 0.01f;
8860
8861 processPosition(mapper, rawX, rawY);
8862 processPressure(mapper, rawPressure);
8863 processMTSync(mapper);
8864 processSync(mapper);
8865
8866 NotifyMotionArgs args;
8867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8869 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8870}
8871
8872TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008873 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008874 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008875 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008876 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008877
8878 NotifyMotionArgs motionArgs;
8879 NotifyKeyArgs keyArgs;
8880
8881 processId(mapper, 1);
8882 processPosition(mapper, 100, 200);
8883 processSync(mapper);
8884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8885 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8886 ASSERT_EQ(0, motionArgs.buttonState);
8887
8888 // press BTN_LEFT, release BTN_LEFT
8889 processKey(mapper, BTN_LEFT, 1);
8890 processSync(mapper);
8891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8892 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8893 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8894
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8896 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8897 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8898
Michael Wrightd02c5b62014-02-10 15:10:22 -08008899 processKey(mapper, BTN_LEFT, 0);
8900 processSync(mapper);
8901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008902 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008903 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008904
8905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008906 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008907 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008908
8909 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8910 processKey(mapper, BTN_RIGHT, 1);
8911 processKey(mapper, BTN_MIDDLE, 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_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8916 motionArgs.buttonState);
8917
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8919 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8920 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8921
8922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8923 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8924 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8925 motionArgs.buttonState);
8926
Michael Wrightd02c5b62014-02-10 15:10:22 -08008927 processKey(mapper, BTN_RIGHT, 0);
8928 processSync(mapper);
8929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008930 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008931 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008932
8933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008934 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008935 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008936
8937 processKey(mapper, BTN_MIDDLE, 0);
8938 processSync(mapper);
8939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008940 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008941 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008942
8943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008944 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008945 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008946
8947 // press BTN_BACK, release BTN_BACK
8948 processKey(mapper, BTN_BACK, 1);
8949 processSync(mapper);
8950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8951 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8952 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008953
Michael Wrightd02c5b62014-02-10 15:10:22 -08008954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008955 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008956 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8957
8958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8959 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8960 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008961
8962 processKey(mapper, BTN_BACK, 0);
8963 processSync(mapper);
8964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008965 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008966 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008967
8968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008969 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008970 ASSERT_EQ(0, motionArgs.buttonState);
8971
Michael Wrightd02c5b62014-02-10 15:10:22 -08008972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8973 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8974 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8975
8976 // press BTN_SIDE, release BTN_SIDE
8977 processKey(mapper, BTN_SIDE, 1);
8978 processSync(mapper);
8979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8980 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8981 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008982
Michael Wrightd02c5b62014-02-10 15:10:22 -08008983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008984 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008985 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8986
8987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8988 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8989 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008990
8991 processKey(mapper, BTN_SIDE, 0);
8992 processSync(mapper);
8993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008994 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008995 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008996
8997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008998 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008999 ASSERT_EQ(0, motionArgs.buttonState);
9000
Michael Wrightd02c5b62014-02-10 15:10:22 -08009001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9002 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9003 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9004
9005 // press BTN_FORWARD, release BTN_FORWARD
9006 processKey(mapper, BTN_FORWARD, 1);
9007 processSync(mapper);
9008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9009 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9010 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009011
Michael Wrightd02c5b62014-02-10 15:10:22 -08009012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009013 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009014 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9015
9016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9017 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9018 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009019
9020 processKey(mapper, BTN_FORWARD, 0);
9021 processSync(mapper);
9022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009023 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009024 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009025
9026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009027 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009028 ASSERT_EQ(0, motionArgs.buttonState);
9029
Michael Wrightd02c5b62014-02-10 15:10:22 -08009030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9031 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9032 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9033
9034 // press BTN_EXTRA, release BTN_EXTRA
9035 processKey(mapper, BTN_EXTRA, 1);
9036 processSync(mapper);
9037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9038 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9039 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009040
Michael Wrightd02c5b62014-02-10 15:10:22 -08009041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009042 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009043 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9044
9045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9046 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9047 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009048
9049 processKey(mapper, BTN_EXTRA, 0);
9050 processSync(mapper);
9051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009052 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009053 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009054
9055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009056 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009057 ASSERT_EQ(0, motionArgs.buttonState);
9058
Michael Wrightd02c5b62014-02-10 15:10:22 -08009059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9060 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9061 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9062
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9064
Michael Wrightd02c5b62014-02-10 15:10:22 -08009065 // press BTN_STYLUS, release BTN_STYLUS
9066 processKey(mapper, BTN_STYLUS, 1);
9067 processSync(mapper);
9068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9069 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009070 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
9071
9072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9073 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9074 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009075
9076 processKey(mapper, BTN_STYLUS, 0);
9077 processSync(mapper);
9078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009079 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009080 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009081
9082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009084 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009085
9086 // press BTN_STYLUS2, release BTN_STYLUS2
9087 processKey(mapper, BTN_STYLUS2, 1);
9088 processSync(mapper);
9089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9090 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009091 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
9092
9093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9094 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9095 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009096
9097 processKey(mapper, BTN_STYLUS2, 0);
9098 processSync(mapper);
9099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009100 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009101 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009102
9103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009105 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009106
9107 // release touch
9108 processId(mapper, -1);
9109 processSync(mapper);
9110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9111 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9112 ASSERT_EQ(0, motionArgs.buttonState);
9113}
9114
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009115TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
9116 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009117 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009118 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00009119 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009120
9121 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
9122 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
9123
9124 // Touch down.
9125 processId(mapper, 1);
9126 processPosition(mapper, 100, 200);
9127 processSync(mapper);
9128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9129 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
9130
9131 // Press and release button mapped to the primary stylus button.
9132 processKey(mapper, BTN_A, 1);
9133 processSync(mapper);
9134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9135 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9136 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9138 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9139 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9140
9141 processKey(mapper, BTN_A, 0);
9142 processSync(mapper);
9143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9144 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9146 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9147
9148 // Press and release the HID usage mapped to the secondary stylus button.
9149 processHidUsage(mapper, 0xabcd, 1);
9150 processSync(mapper);
9151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9152 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9153 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9155 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9156 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9157
9158 processHidUsage(mapper, 0xabcd, 0);
9159 processSync(mapper);
9160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9161 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9163 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9164
9165 // Release touch.
9166 processId(mapper, -1);
9167 processSync(mapper);
9168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9169 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
9170}
9171
Michael Wrightd02c5b62014-02-10 15:10:22 -08009172TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009173 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009174 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009175 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009176 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009177
9178 NotifyMotionArgs motionArgs;
9179
9180 // default tool type is finger
9181 processId(mapper, 1);
9182 processPosition(mapper, 100, 200);
9183 processSync(mapper);
9184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9185 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009186 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009187
9188 // eraser
9189 processKey(mapper, BTN_TOOL_RUBBER, 1);
9190 processSync(mapper);
9191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009193 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009194
9195 // stylus
9196 processKey(mapper, BTN_TOOL_RUBBER, 0);
9197 processKey(mapper, BTN_TOOL_PEN, 1);
9198 processSync(mapper);
9199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009201 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009202
9203 // brush
9204 processKey(mapper, BTN_TOOL_PEN, 0);
9205 processKey(mapper, BTN_TOOL_BRUSH, 1);
9206 processSync(mapper);
9207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009209 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009210
9211 // pencil
9212 processKey(mapper, BTN_TOOL_BRUSH, 0);
9213 processKey(mapper, BTN_TOOL_PENCIL, 1);
9214 processSync(mapper);
9215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009217 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009218
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009219 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009220 processKey(mapper, BTN_TOOL_PENCIL, 0);
9221 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
9222 processSync(mapper);
9223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9224 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009225 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009226
9227 // mouse
9228 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9229 processKey(mapper, BTN_TOOL_MOUSE, 1);
9230 processSync(mapper);
9231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009233 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009234
9235 // lens
9236 processKey(mapper, BTN_TOOL_MOUSE, 0);
9237 processKey(mapper, BTN_TOOL_LENS, 1);
9238 processSync(mapper);
9239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009241 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009242
9243 // double-tap
9244 processKey(mapper, BTN_TOOL_LENS, 0);
9245 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9246 processSync(mapper);
9247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009249 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009250
9251 // triple-tap
9252 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9253 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9254 processSync(mapper);
9255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9256 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009257 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009258
9259 // quad-tap
9260 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9261 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9262 processSync(mapper);
9263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9264 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009265 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009266
9267 // finger
9268 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9269 processKey(mapper, BTN_TOOL_FINGER, 1);
9270 processSync(mapper);
9271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9272 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009273 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009274
9275 // stylus trumps finger
9276 processKey(mapper, BTN_TOOL_PEN, 1);
9277 processSync(mapper);
9278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9279 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009280 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009281
9282 // eraser trumps stylus
9283 processKey(mapper, BTN_TOOL_RUBBER, 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::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009288
9289 // mouse trumps eraser
9290 processKey(mapper, BTN_TOOL_MOUSE, 1);
9291 processSync(mapper);
9292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009294 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009295
9296 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9297 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9298 processSync(mapper);
9299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009301 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009302
9303 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9304 processToolType(mapper, MT_TOOL_PEN);
9305 processSync(mapper);
9306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9307 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009308 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009309
9310 // back to default tool type
9311 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9312 processKey(mapper, BTN_TOOL_MOUSE, 0);
9313 processKey(mapper, BTN_TOOL_RUBBER, 0);
9314 processKey(mapper, BTN_TOOL_PEN, 0);
9315 processKey(mapper, BTN_TOOL_FINGER, 0);
9316 processSync(mapper);
9317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009319 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009320}
9321
9322TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009323 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009324 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009325 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009326 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009327 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009328
9329 NotifyMotionArgs motionArgs;
9330
9331 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9332 processId(mapper, 1);
9333 processPosition(mapper, 100, 200);
9334 processSync(mapper);
9335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9336 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9338 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9339
9340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9341 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9342 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9343 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9344
9345 // move a little
9346 processPosition(mapper, 150, 250);
9347 processSync(mapper);
9348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9349 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9350 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9351 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9352
9353 // down when BTN_TOUCH is pressed, pressure defaults to 1
9354 processKey(mapper, BTN_TOUCH, 1);
9355 processSync(mapper);
9356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9357 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9359 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9360
9361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9362 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9364 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9365
9366 // up when BTN_TOUCH is released, hover restored
9367 processKey(mapper, BTN_TOUCH, 0);
9368 processSync(mapper);
9369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9370 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9372 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9373
9374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9375 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9377 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9378
9379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9380 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9381 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9382 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9383
9384 // exit hover when pointer goes away
9385 processId(mapper, -1);
9386 processSync(mapper);
9387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9388 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9390 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9391}
9392
9393TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009394 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009395 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009396 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009397 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009398
9399 NotifyMotionArgs motionArgs;
9400
9401 // initially hovering because pressure is 0
9402 processId(mapper, 1);
9403 processPosition(mapper, 100, 200);
9404 processPressure(mapper, 0);
9405 processSync(mapper);
9406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9407 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9408 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9409 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9410
9411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9412 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9414 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9415
9416 // move a little
9417 processPosition(mapper, 150, 250);
9418 processSync(mapper);
9419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9420 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9421 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9422 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9423
9424 // down when pressure becomes non-zero
9425 processPressure(mapper, RAW_PRESSURE_MAX);
9426 processSync(mapper);
9427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9428 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9429 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9430 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9431
9432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9433 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9435 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9436
9437 // up when pressure becomes 0, hover restored
9438 processPressure(mapper, 0);
9439 processSync(mapper);
9440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9441 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9443 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9444
9445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9446 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9448 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9449
9450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9451 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9453 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9454
9455 // exit hover when pointer goes away
9456 processId(mapper, -1);
9457 processSync(mapper);
9458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9459 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9460 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9461 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9462}
9463
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009464/**
9465 * Set the input device port <--> display port associations, and check that the
9466 * events are routed to the display that matches the display port.
9467 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9468 */
9469TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009470 const std::string usb2 = "USB2";
9471 const uint8_t hdmi1 = 0;
9472 const uint8_t hdmi2 = 1;
9473 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009474 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009475
9476 addConfigurationProperty("touch.deviceType", "touchScreen");
9477 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009478 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009479
9480 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9481 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9482
9483 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9484 // for this input device is specified, and the matching viewport is not present,
9485 // the input device should be disabled (at the mapper level).
9486
9487 // Add viewport for display 2 on hdmi2
9488 prepareSecondaryDisplay(type, hdmi2);
9489 // Send a touch event
9490 processPosition(mapper, 100, 100);
9491 processSync(mapper);
9492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9493
9494 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009495 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009496 // Send a touch event again
9497 processPosition(mapper, 100, 100);
9498 processSync(mapper);
9499
9500 NotifyMotionArgs args;
9501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9502 ASSERT_EQ(DISPLAY_ID, args.displayId);
9503}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009504
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009505TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9506 addConfigurationProperty("touch.deviceType", "touchScreen");
9507 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009508 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009509
9510 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9511
Michael Wrighta9cf4192022-12-01 23:46:39 +00009512 prepareDisplay(ui::ROTATION_0);
9513 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009514
9515 // Send a touch event
9516 processPosition(mapper, 100, 100);
9517 processSync(mapper);
9518
9519 NotifyMotionArgs args;
9520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9521 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9522}
9523
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009524TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009525 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009526 std::shared_ptr<FakePointerController> fakePointerController =
9527 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009528 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009529 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009530 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009531
Garfield Tan888a6a42020-01-09 11:39:16 -08009532 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009533 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009534
Michael Wrighta9cf4192022-12-01 23:46:39 +00009535 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009536 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009537 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009538
Josep del Río2d8c79a2023-01-23 19:33:50 +00009539 // Check source is mouse that would obtain the PointerController.
9540 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009541
9542 NotifyMotionArgs motionArgs;
9543 processPosition(mapper, 100, 100);
9544 processSync(mapper);
9545
9546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9547 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9548 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9549}
9550
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009551/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009552 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9553 */
9554TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9555 addConfigurationProperty("touch.deviceType", "touchScreen");
9556 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009557 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009558
Michael Wrighta9cf4192022-12-01 23:46:39 +00009559 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009560 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9561 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9562 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9563 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009564
9565 NotifyMotionArgs args;
9566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9567 ASSERT_EQ(26, args.readTime);
9568
Harry Cutts33476232023-01-30 19:57:29 +00009569 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9570 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9571 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009572
9573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9574 ASSERT_EQ(33, args.readTime);
9575}
9576
9577/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009578 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9579 * events should not be delivered to the listener.
9580 */
9581TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9582 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009583 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009584 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009585 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009586 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009587 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009588 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009589
9590 NotifyMotionArgs motionArgs;
9591 processPosition(mapper, 100, 100);
9592 processSync(mapper);
9593
9594 mFakeListener->assertNotifyMotionWasNotCalled();
9595}
9596
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009597/**
9598 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9599 * the touch mapper can process the events and the events can be delivered to the listener.
9600 */
9601TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9602 addConfigurationProperty("touch.deviceType", "touchScreen");
9603 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009604 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009605 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009606 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009607 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009608 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009609
9610 NotifyMotionArgs motionArgs;
9611 processPosition(mapper, 100, 100);
9612 processSync(mapper);
9613
9614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9615 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9616}
9617
Josh Thielene986aed2023-06-01 14:17:30 +00009618/**
9619 * When the viewport is deactivated (isActive transitions from true to false),
9620 * and touch.enableForInactiveViewport is false, touches prior to the transition
9621 * should be cancelled.
9622 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08009623TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9624 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009625 addConfigurationProperty("touch.enableForInactiveViewport", "0");
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=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009628 std::optional<DisplayViewport> optionalDisplayViewport =
9629 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9630 ASSERT_TRUE(optionalDisplayViewport.has_value());
9631 DisplayViewport displayViewport = *optionalDisplayViewport;
9632
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009633 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009634 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009635 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08009636
9637 // Finger down
9638 int32_t x = 100, y = 100;
9639 processPosition(mapper, x, y);
9640 processSync(mapper);
9641
9642 NotifyMotionArgs motionArgs;
9643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9644 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9645
9646 // Deactivate display viewport
9647 displayViewport.isActive = false;
9648 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009649 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009650
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009651 // The ongoing touch should be canceled immediately
9652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9653 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9654
9655 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009656 x += 10, y += 10;
9657 processPosition(mapper, x, y);
9658 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009660
9661 // Reactivate display viewport
9662 displayViewport.isActive = true;
9663 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009664 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009665
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009666 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009667 x += 10, y += 10;
9668 processPosition(mapper, x, y);
9669 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9671 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009672}
9673
Josh Thielene986aed2023-06-01 14:17:30 +00009674/**
9675 * When the viewport is deactivated (isActive transitions from true to false),
9676 * and touch.enableForInactiveViewport is true, touches prior to the transition
9677 * should not be cancelled.
9678 */
9679TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
9680 addConfigurationProperty("touch.deviceType", "touchScreen");
9681 addConfigurationProperty("touch.enableForInactiveViewport", "1");
9682 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
9683 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
9684 std::optional<DisplayViewport> optionalDisplayViewport =
9685 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9686 ASSERT_TRUE(optionalDisplayViewport.has_value());
9687 DisplayViewport displayViewport = *optionalDisplayViewport;
9688
9689 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9690 prepareAxes(POSITION);
9691 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
9692
9693 // Finger down
9694 int32_t x = 100, y = 100;
9695 processPosition(mapper, x, y);
9696 processSync(mapper);
9697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9698 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9699
9700 // Deactivate display viewport
9701 displayViewport.isActive = false;
9702 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9703 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9704
9705 // The ongoing touch should not be canceled
9706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9707
9708 // Finger move is not ignored
9709 x += 10, y += 10;
9710 processPosition(mapper, x, y);
9711 processSync(mapper);
9712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9713 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
9714
9715 // Reactivate display viewport
9716 displayViewport.isActive = true;
9717 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9718 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
9719
9720 // Finger move continues and does not start new gesture
9721 x += 10, y += 10;
9722 processPosition(mapper, x, y);
9723 processSync(mapper);
9724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9725 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
9726}
9727
Arthur Hung7c645402019-01-25 17:45:42 +08009728TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9729 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009730 prepareAxes(POSITION | ID | SLOT);
9731 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009732 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009733
9734 // Create the second touch screen device, and enable multi fingers.
9735 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009736 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009737 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009738 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009739 std::shared_ptr<InputDevice> device2 =
9740 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009741 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009742
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009743 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009744 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009745 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009746 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009747 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009748 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009749 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009750 /*flat=*/0, /*fuzz=*/0);
9751 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009752 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9753 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009754
9755 // Setup the second touch screen device.
Arpit Singha8c236b2023-04-25 13:56:05 +00009756 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
9757 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
9758 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009759 std::list<NotifyArgs> unused =
9760 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009761 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009762 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009763
9764 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009765 std::shared_ptr<FakePointerController> fakePointerController =
9766 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009767 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009768
9769 // Setup policy for associated displays and show touches.
9770 const uint8_t hdmi1 = 0;
9771 const uint8_t hdmi2 = 1;
9772 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9773 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9774 mFakePolicy->setShowTouches(true);
9775
9776 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009777 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009778 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009779
9780 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009781 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009782 InputReaderConfiguration::Change::DISPLAY_INFO |
9783 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009784
9785 // Two fingers down at default display.
9786 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9787 processPosition(mapper, x1, y1);
9788 processId(mapper, 1);
9789 processSlot(mapper, 1);
9790 processPosition(mapper, x2, y2);
9791 processId(mapper, 2);
9792 processSync(mapper);
9793
9794 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9795 fakePointerController->getSpots().find(DISPLAY_ID);
9796 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9797 ASSERT_EQ(size_t(2), iter->second.size());
9798
9799 // Two fingers down at second display.
9800 processPosition(mapper2, x1, y1);
9801 processId(mapper2, 1);
9802 processSlot(mapper2, 1);
9803 processPosition(mapper2, x2, y2);
9804 processId(mapper2, 2);
9805 processSync(mapper2);
9806
9807 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9808 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9809 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009810
9811 // Disable the show touches configuration and ensure the spots are cleared.
9812 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009813 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009814 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009815
9816 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009817}
9818
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009819TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009820 prepareAxes(POSITION);
9821 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009822 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009823 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009824
9825 NotifyMotionArgs motionArgs;
9826 // Unrotated video frame
9827 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9828 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009829 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009830 processPosition(mapper, 100, 200);
9831 processSync(mapper);
9832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9833 ASSERT_EQ(frames, motionArgs.videoFrames);
9834
9835 // Subsequent touch events should not have any videoframes
9836 // This is implemented separately in FakeEventHub,
9837 // but that should match the behaviour of TouchVideoDevice.
9838 processPosition(mapper, 200, 200);
9839 processSync(mapper);
9840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9841 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9842}
9843
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009844TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009845 prepareAxes(POSITION);
9846 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009847 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009848 // Unrotated video frame
9849 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9850 NotifyMotionArgs motionArgs;
9851
9852 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009853 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009854 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9855 clearViewports();
9856 prepareDisplay(orientation);
9857 std::vector<TouchVideoFrame> frames{frame};
9858 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9859 processPosition(mapper, 100, 200);
9860 processSync(mapper);
9861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9862 ASSERT_EQ(frames, motionArgs.videoFrames);
9863 }
9864}
9865
9866TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9867 prepareAxes(POSITION);
9868 addConfigurationProperty("touch.deviceType", "touchScreen");
9869 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9870 // orientation-aware are affected by display rotation.
9871 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009872 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009873 // Unrotated video frame
9874 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9875 NotifyMotionArgs motionArgs;
9876
9877 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009878 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009879 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9880 clearViewports();
9881 prepareDisplay(orientation);
9882 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009883 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009884 processPosition(mapper, 100, 200);
9885 processSync(mapper);
9886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009887 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9888 // compared to the display. This is so that when the window transform (which contains the
9889 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9890 // window's coordinate space.
9891 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009892 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009893
9894 // Release finger.
9895 processSync(mapper);
9896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009897 }
9898}
9899
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009900TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009901 prepareAxes(POSITION);
9902 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009903 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009904 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9905 // so mix these.
9906 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9907 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9908 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9909 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9910 NotifyMotionArgs motionArgs;
9911
Michael Wrighta9cf4192022-12-01 23:46:39 +00009912 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009913 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009914 processPosition(mapper, 100, 200);
9915 processSync(mapper);
9916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009917 ASSERT_EQ(frames, motionArgs.videoFrames);
9918}
9919
9920TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9921 prepareAxes(POSITION);
9922 addConfigurationProperty("touch.deviceType", "touchScreen");
9923 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9924 // orientation-aware are affected by display rotation.
9925 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009926 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009927 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9928 // so mix these.
9929 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9930 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9931 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9932 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9933 NotifyMotionArgs motionArgs;
9934
Michael Wrighta9cf4192022-12-01 23:46:39 +00009935 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009936 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9937 processPosition(mapper, 100, 200);
9938 processSync(mapper);
9939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9940 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9941 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9942 // compared to the display. This is so that when the window transform (which contains the
9943 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9944 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009945 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009946 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009947 ASSERT_EQ(frames, motionArgs.videoFrames);
9948}
9949
Arthur Hung9da14732019-09-02 16:16:58 +08009950/**
9951 * If we had defined port associations, but the viewport is not ready, the touch device would be
9952 * expected to be disabled, and it should be enabled after the viewport has found.
9953 */
9954TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009955 constexpr uint8_t hdmi2 = 1;
9956 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009957 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009958
9959 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9960
9961 addConfigurationProperty("touch.deviceType", "touchScreen");
9962 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009963 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009964
9965 ASSERT_EQ(mDevice->isEnabled(), false);
9966
9967 // Add display on hdmi2, the device should be enabled and can receive touch event.
9968 prepareSecondaryDisplay(type, hdmi2);
9969 ASSERT_EQ(mDevice->isEnabled(), true);
9970
9971 // Send a touch event.
9972 processPosition(mapper, 100, 100);
9973 processSync(mapper);
9974
9975 NotifyMotionArgs args;
9976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9977 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9978}
9979
Arthur Hung421eb1c2020-01-16 00:09:42 +08009980TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009981 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009982 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009983 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009984 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009985
9986 NotifyMotionArgs motionArgs;
9987
9988 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9989 // finger down
9990 processId(mapper, 1);
9991 processPosition(mapper, x1, y1);
9992 processSync(mapper);
9993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9994 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009995 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009996
9997 // finger move
9998 processId(mapper, 1);
9999 processPosition(mapper, x2, y2);
10000 processSync(mapper);
10001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10002 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010003 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010004
10005 // finger up.
10006 processId(mapper, -1);
10007 processSync(mapper);
10008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10009 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010010 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010011
10012 // new finger down
10013 processId(mapper, 1);
10014 processPosition(mapper, x3, y3);
10015 processSync(mapper);
10016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10017 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010018 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010019}
10020
10021/**
arthurhungcc7f9802020-04-30 17:55:40 +080010022 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
10023 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +080010024 */
arthurhungcc7f9802020-04-30 17:55:40 +080010025TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010026 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010027 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010028 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010029 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010030
10031 NotifyMotionArgs motionArgs;
10032
10033 // default tool type is finger
10034 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +080010035 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010036 processPosition(mapper, x1, y1);
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 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
10043 processToolType(mapper, MT_TOOL_PALM);
10044 processSync(mapper);
10045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10046 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10047
10048 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +080010049 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010050 processPosition(mapper, x2, y2);
10051 processSync(mapper);
10052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10053
10054 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +080010055 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010056 processSync(mapper);
10057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10058
10059 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +080010060 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010061 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010062 processPosition(mapper, x3, y3);
10063 processSync(mapper);
10064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10065 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010066 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010067}
10068
arthurhungbf89a482020-04-17 17:37:55 +080010069/**
arthurhungcc7f9802020-04-30 17:55:40 +080010070 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10071 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +080010072 */
arthurhungcc7f9802020-04-30 17:55:40 +080010073TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +080010074 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010075 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +080010076 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010077 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +080010078
10079 NotifyMotionArgs motionArgs;
10080
10081 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +080010082 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10083 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010084 processPosition(mapper, x1, y1);
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);
arthurhungbf89a482020-04-17 17:37:55 +080010089
10090 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +080010091 processSlot(mapper, SECOND_SLOT);
10092 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010093 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +080010094 processSync(mapper);
10095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010096 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010097 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010098
10099 // If the tool type of the first finger changes to MT_TOOL_PALM,
10100 // we expect to receive ACTION_POINTER_UP with cancel flag.
10101 processSlot(mapper, FIRST_SLOT);
10102 processId(mapper, FIRST_TRACKING_ID);
10103 processToolType(mapper, MT_TOOL_PALM);
10104 processSync(mapper);
10105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010106 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010107 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10108
10109 // The following MOVE events of second finger should be processed.
10110 processSlot(mapper, SECOND_SLOT);
10111 processId(mapper, SECOND_TRACKING_ID);
10112 processPosition(mapper, x2 + 1, y2 + 1);
10113 processSync(mapper);
10114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010116 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010117
10118 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
10119 // it. Second finger receive move.
10120 processSlot(mapper, FIRST_SLOT);
10121 processId(mapper, INVALID_TRACKING_ID);
10122 processSync(mapper);
10123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010125 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010126
10127 // Second finger keeps moving.
10128 processSlot(mapper, SECOND_SLOT);
10129 processId(mapper, SECOND_TRACKING_ID);
10130 processPosition(mapper, x2 + 2, y2 + 2);
10131 processSync(mapper);
10132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010134 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010135
10136 // Second finger up.
10137 processId(mapper, INVALID_TRACKING_ID);
10138 processSync(mapper);
10139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10140 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10141 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10142}
10143
10144/**
10145 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
10146 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
10147 */
10148TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
10149 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010150 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010151 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010152 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010153
10154 NotifyMotionArgs motionArgs;
10155
10156 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10157 // First finger down.
10158 processId(mapper, FIRST_TRACKING_ID);
10159 processPosition(mapper, x1, y1);
10160 processSync(mapper);
10161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10162 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010163 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010164
10165 // Second finger down.
10166 processSlot(mapper, SECOND_SLOT);
10167 processId(mapper, SECOND_TRACKING_ID);
10168 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +080010169 processSync(mapper);
10170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010171 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010172 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +080010173
arthurhungcc7f9802020-04-30 17:55:40 +080010174 // If the tool type of the first finger changes to MT_TOOL_PALM,
10175 // we expect to receive ACTION_POINTER_UP with cancel flag.
10176 processSlot(mapper, FIRST_SLOT);
10177 processId(mapper, FIRST_TRACKING_ID);
10178 processToolType(mapper, MT_TOOL_PALM);
10179 processSync(mapper);
10180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010181 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010182 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10183
10184 // Second finger keeps moving.
10185 processSlot(mapper, SECOND_SLOT);
10186 processId(mapper, SECOND_TRACKING_ID);
10187 processPosition(mapper, x2 + 1, y2 + 1);
10188 processSync(mapper);
10189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10191
10192 // second finger becomes palm, receive cancel due to only 1 finger is active.
10193 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010194 processToolType(mapper, MT_TOOL_PALM);
10195 processSync(mapper);
10196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10197 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10198
arthurhungcc7f9802020-04-30 17:55:40 +080010199 // third finger down.
10200 processSlot(mapper, THIRD_SLOT);
10201 processId(mapper, THIRD_TRACKING_ID);
10202 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +080010203 processPosition(mapper, x3, y3);
10204 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +080010205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10206 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010207 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010208 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010209
10210 // third finger move
10211 processId(mapper, THIRD_TRACKING_ID);
10212 processPosition(mapper, x3 + 1, y3 + 1);
10213 processSync(mapper);
10214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10215 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10216
10217 // first finger up, third finger receive move.
10218 processSlot(mapper, FIRST_SLOT);
10219 processId(mapper, INVALID_TRACKING_ID);
10220 processSync(mapper);
10221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010223 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010224
10225 // second finger up, third finger receive move.
10226 processSlot(mapper, SECOND_SLOT);
10227 processId(mapper, INVALID_TRACKING_ID);
10228 processSync(mapper);
10229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10230 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010231 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010232
10233 // third finger up.
10234 processSlot(mapper, THIRD_SLOT);
10235 processId(mapper, INVALID_TRACKING_ID);
10236 processSync(mapper);
10237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10238 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10239 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10240}
10241
10242/**
10243 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10244 * and the active finger could still be allowed to receive the events
10245 */
10246TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10247 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010248 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010249 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010250 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010251
10252 NotifyMotionArgs motionArgs;
10253
10254 // default tool type is finger
10255 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10256 processId(mapper, FIRST_TRACKING_ID);
10257 processPosition(mapper, x1, y1);
10258 processSync(mapper);
10259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10260 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010261 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010262
10263 // Second finger down.
10264 processSlot(mapper, SECOND_SLOT);
10265 processId(mapper, SECOND_TRACKING_ID);
10266 processPosition(mapper, x2, y2);
10267 processSync(mapper);
10268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010269 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010270 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010271
10272 // If the tool type of the second finger changes to MT_TOOL_PALM,
10273 // we expect to receive ACTION_POINTER_UP with cancel flag.
10274 processId(mapper, SECOND_TRACKING_ID);
10275 processToolType(mapper, MT_TOOL_PALM);
10276 processSync(mapper);
10277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010278 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010279 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10280
10281 // The following MOVE event should be processed.
10282 processSlot(mapper, FIRST_SLOT);
10283 processId(mapper, FIRST_TRACKING_ID);
10284 processPosition(mapper, x1 + 1, y1 + 1);
10285 processSync(mapper);
10286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010288 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010289
10290 // second finger up.
10291 processSlot(mapper, SECOND_SLOT);
10292 processId(mapper, INVALID_TRACKING_ID);
10293 processSync(mapper);
10294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10295 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10296
10297 // first finger keep moving
10298 processSlot(mapper, FIRST_SLOT);
10299 processId(mapper, FIRST_TRACKING_ID);
10300 processPosition(mapper, x1 + 2, y1 + 2);
10301 processSync(mapper);
10302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10304
10305 // first finger up.
10306 processId(mapper, INVALID_TRACKING_ID);
10307 processSync(mapper);
10308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10309 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10310 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010311}
10312
Arthur Hung9ad18942021-06-19 02:04:46 +000010313/**
10314 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10315 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10316 * cause slot be valid again.
10317 */
10318TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10319 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010320 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +000010321 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010322 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +000010323
10324 NotifyMotionArgs motionArgs;
10325
10326 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10327 // First finger down.
10328 processId(mapper, FIRST_TRACKING_ID);
10329 processPosition(mapper, x1, y1);
10330 processPressure(mapper, RAW_PRESSURE_MAX);
10331 processSync(mapper);
10332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10333 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010334 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010335
10336 // First finger move.
10337 processId(mapper, FIRST_TRACKING_ID);
10338 processPosition(mapper, x1 + 1, y1 + 1);
10339 processPressure(mapper, RAW_PRESSURE_MAX);
10340 processSync(mapper);
10341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010343 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010344
10345 // Second finger down.
10346 processSlot(mapper, SECOND_SLOT);
10347 processId(mapper, SECOND_TRACKING_ID);
10348 processPosition(mapper, x2, y2);
10349 processPressure(mapper, RAW_PRESSURE_MAX);
10350 processSync(mapper);
10351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010352 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010353 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010354
10355 // second finger up with some unexpected data.
10356 processSlot(mapper, SECOND_SLOT);
10357 processId(mapper, INVALID_TRACKING_ID);
10358 processPosition(mapper, x2, y2);
10359 processSync(mapper);
10360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010361 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010362 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010363
10364 // first finger up with some unexpected data.
10365 processSlot(mapper, FIRST_SLOT);
10366 processId(mapper, INVALID_TRACKING_ID);
10367 processPosition(mapper, x2, y2);
10368 processPressure(mapper, RAW_PRESSURE_MAX);
10369 processSync(mapper);
10370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10371 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010372 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010373}
10374
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010375TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10376 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010377 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010378 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010379 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010380
10381 // First finger down.
10382 processId(mapper, FIRST_TRACKING_ID);
10383 processPosition(mapper, 100, 200);
10384 processPressure(mapper, RAW_PRESSURE_MAX);
10385 processSync(mapper);
10386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10387 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10388
10389 // Second finger down.
10390 processSlot(mapper, SECOND_SLOT);
10391 processId(mapper, SECOND_TRACKING_ID);
10392 processPosition(mapper, 300, 400);
10393 processPressure(mapper, RAW_PRESSURE_MAX);
10394 processSync(mapper);
10395 ASSERT_NO_FATAL_FAILURE(
10396 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10397
10398 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010399 // preserved. Resetting should cancel the ongoing gesture.
10400 resetMapper(mapper, ARBITRARY_TIME);
10401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10402 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010403
10404 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10405 // the existing touch state to generate a down event.
10406 processPosition(mapper, 301, 302);
10407 processSync(mapper);
10408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10409 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10411 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10412
10413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10414}
10415
10416TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10417 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010418 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010419 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010420 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010421
10422 // First finger touches down and releases.
10423 processId(mapper, FIRST_TRACKING_ID);
10424 processPosition(mapper, 100, 200);
10425 processPressure(mapper, RAW_PRESSURE_MAX);
10426 processSync(mapper);
10427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10428 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10429 processId(mapper, INVALID_TRACKING_ID);
10430 processSync(mapper);
10431 ASSERT_NO_FATAL_FAILURE(
10432 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10433
10434 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10435 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010436 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10438
10439 // Send an empty sync frame. Since there are no pointers, no events are generated.
10440 processSync(mapper);
10441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10442}
10443
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010444TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010445 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010446 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010447 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010448 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010450
10451 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10452 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10453 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10454 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10455 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10456
10457 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010458 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010459 processId(mapper, FIRST_TRACKING_ID);
10460 processToolType(mapper, MT_TOOL_PEN);
10461 processPosition(mapper, 100, 200);
10462 processPressure(mapper, RAW_PRESSURE_MAX);
10463 processSync(mapper);
10464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10465 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10466 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010467 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010468
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010469 // Now that we know the device supports styluses, ensure that the device is re-configured with
10470 // the stylus source.
10471 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10472 {
10473 const auto& devices = mReader->getInputDevices();
10474 auto deviceInfo =
10475 std::find_if(devices.begin(), devices.end(),
10476 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10477 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10478 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10479 }
10480
10481 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10483
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010484 processId(mapper, INVALID_TRACKING_ID);
10485 processSync(mapper);
10486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10487 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
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}
10491
Seunghwan Choi356026c2023-02-01 14:37:25 +090010492TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10493 addConfigurationProperty("touch.deviceType", "touchScreen");
10494 prepareDisplay(ui::ROTATION_0);
10495 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10496 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10497 // indicate stylus presence dynamically.
10498 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10499 std::shared_ptr<FakePointerController> fakePointerController =
10500 std::make_shared<FakePointerController>();
10501 mFakePolicy->setPointerController(fakePointerController);
10502 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +000010503 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010504
10505 processId(mapper, FIRST_TRACKING_ID);
10506 processPressure(mapper, RAW_PRESSURE_MIN);
10507 processPosition(mapper, 100, 200);
10508 processToolType(mapper, MT_TOOL_PEN);
10509 processSync(mapper);
10510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10511 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010512 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010513 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10514 ASSERT_TRUE(fakePointerController->isPointerShown());
10515 ASSERT_NO_FATAL_FAILURE(
10516 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10517}
10518
10519TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10520 addConfigurationProperty("touch.deviceType", "touchScreen");
10521 prepareDisplay(ui::ROTATION_0);
10522 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10523 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10524 // indicate stylus presence dynamically.
10525 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10526 std::shared_ptr<FakePointerController> fakePointerController =
10527 std::make_shared<FakePointerController>();
10528 mFakePolicy->setPointerController(fakePointerController);
10529 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010530 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010531
10532 processId(mapper, FIRST_TRACKING_ID);
10533 processPressure(mapper, RAW_PRESSURE_MIN);
10534 processPosition(mapper, 100, 200);
10535 processToolType(mapper, MT_TOOL_PEN);
10536 processSync(mapper);
10537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10538 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010539 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010540 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10541 ASSERT_FALSE(fakePointerController->isPointerShown());
10542}
10543
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010544// --- MultiTouchInputMapperTest_ExternalDevice ---
10545
10546class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10547protected:
Chris Yea52ade12020-08-27 16:49:20 -070010548 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010549};
10550
10551/**
10552 * Expect fallback to internal viewport if device is external and external viewport is not present.
10553 */
10554TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10555 prepareAxes(POSITION);
10556 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010557 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010558 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010559
10560 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10561
10562 NotifyMotionArgs motionArgs;
10563
10564 // Expect the event to be sent to the internal viewport,
10565 // because an external viewport is not present.
10566 processPosition(mapper, 100, 100);
10567 processSync(mapper);
10568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10569 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10570
10571 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010572 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010573 processPosition(mapper, 100, 100);
10574 processSync(mapper);
10575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10576 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10577}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010578
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010579TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10580 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10581 std::shared_ptr<FakePointerController> fakePointerController =
10582 std::make_shared<FakePointerController>();
10583 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10584 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010585
10586 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010587 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010588 prepareAxes(POSITION | ID | SLOT);
10589 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10590 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10591 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010592 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010593 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010594
10595 // captured touchpad should be a touchpad source
10596 NotifyDeviceResetArgs resetArgs;
10597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10598 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10599
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010600 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010601
10602 const InputDeviceInfo::MotionRange* relRangeX =
10603 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10604 ASSERT_NE(relRangeX, nullptr);
10605 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10606 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10607 const InputDeviceInfo::MotionRange* relRangeY =
10608 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10609 ASSERT_NE(relRangeY, nullptr);
10610 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10611 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10612
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010613 // run captured pointer tests - note that this is unscaled, so input listener events should be
10614 // identical to what the hardware sends (accounting for any
10615 // calibration).
10616 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010617 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010618 processId(mapper, 1);
10619 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10620 processKey(mapper, BTN_TOUCH, 1);
10621 processSync(mapper);
10622
10623 // expect coord[0] to contain initial location of touch 0
10624 NotifyMotionArgs args;
10625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10626 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010627 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010628 ASSERT_EQ(0, args.pointerProperties[0].id);
10629 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10630 ASSERT_NO_FATAL_FAILURE(
10631 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10632
10633 // FINGER 1 DOWN
10634 processSlot(mapper, 1);
10635 processId(mapper, 2);
10636 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10637 processSync(mapper);
10638
10639 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010641 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010642 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010643 ASSERT_EQ(0, args.pointerProperties[0].id);
10644 ASSERT_EQ(1, args.pointerProperties[1].id);
10645 ASSERT_NO_FATAL_FAILURE(
10646 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10647 ASSERT_NO_FATAL_FAILURE(
10648 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10649
10650 // FINGER 1 MOVE
10651 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10652 processSync(mapper);
10653
10654 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10655 // from move
10656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10658 ASSERT_NO_FATAL_FAILURE(
10659 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10660 ASSERT_NO_FATAL_FAILURE(
10661 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10662
10663 // FINGER 0 MOVE
10664 processSlot(mapper, 0);
10665 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10666 processSync(mapper);
10667
10668 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10671 ASSERT_NO_FATAL_FAILURE(
10672 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10673 ASSERT_NO_FATAL_FAILURE(
10674 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10675
10676 // BUTTON DOWN
10677 processKey(mapper, BTN_LEFT, 1);
10678 processSync(mapper);
10679
10680 // touchinputmapper design sends a move before button press
10681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10682 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10684 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10685
10686 // BUTTON UP
10687 processKey(mapper, BTN_LEFT, 0);
10688 processSync(mapper);
10689
10690 // touchinputmapper design sends a move after button release
10691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10692 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10694 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10695
10696 // FINGER 0 UP
10697 processId(mapper, -1);
10698 processSync(mapper);
10699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10700 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10701
10702 // FINGER 1 MOVE
10703 processSlot(mapper, 1);
10704 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10705 processSync(mapper);
10706
10707 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10709 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010710 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010711 ASSERT_EQ(1, args.pointerProperties[0].id);
10712 ASSERT_NO_FATAL_FAILURE(
10713 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10714
10715 // FINGER 1 UP
10716 processId(mapper, -1);
10717 processKey(mapper, BTN_TOUCH, 0);
10718 processSync(mapper);
10719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10720 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10721
Josep del Río2d8c79a2023-01-23 19:33:50 +000010722 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010723 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010724 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010726 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010727}
10728
10729TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10730 std::shared_ptr<FakePointerController> fakePointerController =
10731 std::make_shared<FakePointerController>();
10732 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10733 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010734
10735 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010736 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010737 prepareAxes(POSITION | ID | SLOT);
10738 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10739 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010740 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010741 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010742 // run uncaptured pointer tests - pushes out generic events
10743 // FINGER 0 DOWN
10744 processId(mapper, 3);
10745 processPosition(mapper, 100, 100);
10746 processKey(mapper, BTN_TOUCH, 1);
10747 processSync(mapper);
10748
10749 // start at (100,100), cursor should be at (0,0) * scale
10750 NotifyMotionArgs args;
10751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10752 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10753 ASSERT_NO_FATAL_FAILURE(
10754 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10755
10756 // FINGER 0 MOVE
10757 processPosition(mapper, 200, 200);
10758 processSync(mapper);
10759
10760 // compute scaling to help with touch position checking
10761 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10762 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10763 float scale =
10764 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10765
10766 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10768 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10770 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010771
10772 // BUTTON DOWN
10773 processKey(mapper, BTN_LEFT, 1);
10774 processSync(mapper);
10775
10776 // touchinputmapper design sends a move before button press
10777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10778 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10780 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10781
10782 // BUTTON UP
10783 processKey(mapper, BTN_LEFT, 0);
10784 processSync(mapper);
10785
10786 // touchinputmapper design sends a move after button release
10787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10788 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10790 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010791}
10792
10793TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10794 std::shared_ptr<FakePointerController> fakePointerController =
10795 std::make_shared<FakePointerController>();
10796
Michael Wrighta9cf4192022-12-01 23:46:39 +000010797 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010798 prepareAxes(POSITION | ID | SLOT);
10799 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010800 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010801 mFakePolicy->setPointerCapture(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010802 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010803
Josep del Río2d8c79a2023-01-23 19:33:50 +000010804 // uncaptured touchpad should be a pointer device
10805 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010806
Josep del Río2d8c79a2023-01-23 19:33:50 +000010807 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010808 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010809 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010810 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10811}
10812
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010813// --- BluetoothMultiTouchInputMapperTest ---
10814
10815class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10816protected:
10817 void SetUp() override {
10818 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10819 }
10820};
10821
10822TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10823 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010824 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010825 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010826 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010827
10828 nsecs_t kernelEventTime = ARBITRARY_TIME;
10829 nsecs_t expectedEventTime = ARBITRARY_TIME;
10830 // Touch down.
10831 processId(mapper, FIRST_TRACKING_ID);
10832 processPosition(mapper, 100, 200);
10833 processPressure(mapper, RAW_PRESSURE_MAX);
10834 processSync(mapper, ARBITRARY_TIME);
10835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10836 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10837
10838 // Process several events that come in quick succession, according to their timestamps.
10839 for (int i = 0; i < 3; i++) {
10840 constexpr static nsecs_t delta = ms2ns(1);
10841 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10842 kernelEventTime += delta;
10843 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10844
10845 processPosition(mapper, 101 + i, 201 + i);
10846 processSync(mapper, kernelEventTime);
10847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10848 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10849 WithEventTime(expectedEventTime))));
10850 }
10851
10852 // Release the touch.
10853 processId(mapper, INVALID_TRACKING_ID);
10854 processPressure(mapper, RAW_PRESSURE_MIN);
10855 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10857 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10858 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10859}
10860
10861// --- MultiTouchPointerModeTest ---
10862
HQ Liue6983c72022-04-19 22:14:56 +000010863class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10864protected:
10865 float mPointerMovementScale;
10866 float mPointerXZoomScale;
10867 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10868 addConfigurationProperty("touch.deviceType", "pointer");
10869 std::shared_ptr<FakePointerController> fakePointerController =
10870 std::make_shared<FakePointerController>();
10871 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10872 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010873 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010874
10875 prepareAxes(POSITION);
10876 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10877 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10878 // needs to be disabled, and the pointer gesture needs to be enabled.
10879 mFakePolicy->setPointerCapture(false);
10880 mFakePolicy->setPointerGestureEnabled(true);
10881 mFakePolicy->setPointerController(fakePointerController);
10882
10883 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10884 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10885 mPointerMovementScale =
10886 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10887 mPointerXZoomScale =
10888 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10889 }
10890
10891 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10892 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10893 /*flat*/ 0,
10894 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10895 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10896 /*flat*/ 0,
10897 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10898 }
10899};
10900
10901/**
10902 * Two fingers down on a pointer mode touch pad. The width
10903 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10904 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10905 * be greater than the both value to be freeform gesture, so that after two
10906 * fingers start to move downwards, the gesture should be swipe.
10907 */
10908TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10909 // The min freeform gesture width is 25units/mm x 30mm = 750
10910 // which is greater than fraction of the diagnal length of the touchpad (349).
10911 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010912 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010913 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010914 NotifyMotionArgs motionArgs;
10915
10916 // Two fingers down at once.
10917 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10918 // Pointer's initial position is used the [0,0] coordinate.
10919 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10920
10921 processId(mapper, FIRST_TRACKING_ID);
10922 processPosition(mapper, x1, y1);
10923 processMTSync(mapper);
10924 processId(mapper, SECOND_TRACKING_ID);
10925 processPosition(mapper, x2, y2);
10926 processMTSync(mapper);
10927 processSync(mapper);
10928
10929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010930 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010931 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010932 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010933 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010934 ASSERT_NO_FATAL_FAILURE(
10935 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10936
10937 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10938 // that there should be 1 pointer.
10939 int32_t movingDistance = 200;
10940 y1 += movingDistance;
10941 y2 += movingDistance;
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_MOVE, 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::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10957 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10958 0, 0, 0, 0));
10959}
10960
10961/**
10962 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10963 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10964 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10965 * value to be freeform gesture, so that after two fingers start to move downwards,
10966 * the gesture should be swipe.
10967 */
10968TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10969 // The min freeform gesture width is 5units/mm x 30mm = 150
10970 // which is greater than fraction of the diagnal length of the touchpad (349).
10971 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010972 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +000010973 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010974 NotifyMotionArgs motionArgs;
10975
10976 // Two fingers down at once.
10977 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10978 // Pointer's initial position is used the [0,0] coordinate.
10979 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10980
10981 processId(mapper, FIRST_TRACKING_ID);
10982 processPosition(mapper, x1, y1);
10983 processMTSync(mapper);
10984 processId(mapper, SECOND_TRACKING_ID);
10985 processPosition(mapper, x2, y2);
10986 processMTSync(mapper);
10987 processSync(mapper);
10988
10989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010990 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010991 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010992 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010993 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010994 ASSERT_NO_FATAL_FAILURE(
10995 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10996
10997 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10998 // and there should be 1 pointer.
10999 int32_t movingDistance = 200;
11000 y1 += movingDistance;
11001 y2 += movingDistance;
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_MOVE, 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::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011016 // New coordinate is the scaled relative coordinate from the initial coordinate.
11017 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
11018 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11019 0, 0, 0, 0));
11020}
11021
11022/**
11023 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
11024 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
11025 * freeform gestures after two fingers start to move downwards.
11026 */
11027TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000011028 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000011029 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000011030
11031 NotifyMotionArgs motionArgs;
11032
11033 // Two fingers down at once. Wider than the max swipe width.
11034 // The gesture is expected to be PRESS, then transformed to FREEFORM
11035 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
11036
11037 processId(mapper, FIRST_TRACKING_ID);
11038 processPosition(mapper, x1, y1);
11039 processMTSync(mapper);
11040 processId(mapper, SECOND_TRACKING_ID);
11041 processPosition(mapper, x2, y2);
11042 processMTSync(mapper);
11043 processSync(mapper);
11044
11045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011046 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011047 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011048 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011049 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011050 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
11051 ASSERT_NO_FATAL_FAILURE(
11052 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11053
11054 int32_t movingDistance = 200;
11055
11056 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
11057 // then two down events for two pointers.
11058 y1 += movingDistance;
11059 y2 += movingDistance;
11060
11061 processId(mapper, FIRST_TRACKING_ID);
11062 processPosition(mapper, x1, y1);
11063 processMTSync(mapper);
11064 processId(mapper, SECOND_TRACKING_ID);
11065 processPosition(mapper, x2, y2);
11066 processMTSync(mapper);
11067 processSync(mapper);
11068
11069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11070 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011071 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011072 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
11073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011074 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011075 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011076 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011078 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011079 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011080 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011081 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011082 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011083 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011084 // Two pointers' scaled relative coordinates from their initial centroid.
11085 // Initial y coordinates are 0 as y1 and y2 have the same value.
11086 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
11087 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
11088 // When pointers move, the new coordinates equal to the initial coordinates plus
11089 // scaled moving distance.
11090 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11091 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11092 0, 0, 0, 0));
11093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11094 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11095 0, 0, 0, 0));
11096
11097 // Move two fingers down again, expect one MOVE motion event.
11098 y1 += movingDistance;
11099 y2 += movingDistance;
11100
11101 processId(mapper, FIRST_TRACKING_ID);
11102 processPosition(mapper, x1, y1);
11103 processMTSync(mapper);
11104 processId(mapper, SECOND_TRACKING_ID);
11105 processPosition(mapper, x2, y2);
11106 processMTSync(mapper);
11107 processSync(mapper);
11108
11109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011110 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011112 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011113 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011114 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11115 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11116 0, 0, 0, 0, 0));
11117 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11118 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11119 0, 0, 0, 0, 0));
11120}
11121
Harry Cutts39b7ca22022-10-05 15:55:48 +000011122TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000011123 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000011124 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000011125 NotifyMotionArgs motionArgs;
11126
11127 // Place two fingers down.
11128 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
11129
11130 processId(mapper, FIRST_TRACKING_ID);
11131 processPosition(mapper, x1, y1);
11132 processMTSync(mapper);
11133 processId(mapper, SECOND_TRACKING_ID);
11134 processPosition(mapper, x2, y2);
11135 processMTSync(mapper);
11136 processSync(mapper);
11137
11138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011139 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000011140 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11141 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
11142 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
11143 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
11144
11145 // Move the two fingers down and to the left.
11146 int32_t movingDistance = 200;
11147 x1 -= movingDistance;
11148 y1 += movingDistance;
11149 x2 -= movingDistance;
11150 y2 += movingDistance;
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_MOVE, motionArgs.action);
11163 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
11164 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
11165 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
11166}
11167
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011168TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000011169 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011170 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000011171 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
11173
11174 // Start a stylus gesture.
11175 processKey(mapper, BTN_TOOL_PEN, 1);
11176 processId(mapper, FIRST_TRACKING_ID);
11177 processPosition(mapper, 100, 200);
11178 processSync(mapper);
11179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11180 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
11181 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011182 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011183 // TODO(b/257078296): Pointer mode generates extra event.
11184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11185 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11186 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011187 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11189
11190 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
11191 // gesture should be disabled.
11192 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
11193 viewport->isActive = false;
11194 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000011195 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11197 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11198 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011199 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011200 // TODO(b/257078296): Pointer mode generates extra event.
11201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11202 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11206}
11207
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011208// --- JoystickInputMapperTest ---
11209
11210class JoystickInputMapperTest : public InputMapperTest {
11211protected:
11212 static const int32_t RAW_X_MIN;
11213 static const int32_t RAW_X_MAX;
11214 static const int32_t RAW_Y_MIN;
11215 static const int32_t RAW_Y_MAX;
11216
11217 void SetUp() override {
11218 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
11219 }
11220 void prepareAxes() {
11221 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
11222 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
11223 }
11224
11225 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
11226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
11227 }
11228
11229 void processSync(JoystickInputMapper& mapper) {
11230 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
11231 }
11232
Michael Wrighta9cf4192022-12-01 23:46:39 +000011233 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011234 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
11235 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
11236 NO_PORT, ViewportType::VIRTUAL);
11237 }
11238};
11239
11240const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
11241const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
11242const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11243const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11244
11245TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11246 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000011247 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011248
11249 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11250
Michael Wrighta9cf4192022-12-01 23:46:39 +000011251 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011252
11253 // Send an axis event
11254 processAxis(mapper, ABS_X, 100);
11255 processSync(mapper);
11256
11257 NotifyMotionArgs args;
11258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11259 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11260
11261 // Send another axis event
11262 processAxis(mapper, ABS_Y, 100);
11263 processSync(mapper);
11264
11265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11266 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11267}
11268
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011269// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011270
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011271class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011272protected:
11273 static const char* DEVICE_NAME;
11274 static const char* DEVICE_LOCATION;
11275 static const int32_t DEVICE_ID;
11276 static const int32_t DEVICE_GENERATION;
11277 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011278 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011279 static const int32_t EVENTHUB_ID;
11280
11281 std::shared_ptr<FakeEventHub> mFakeEventHub;
11282 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011283 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011284 std::unique_ptr<InstrumentedInputReader> mReader;
11285 std::shared_ptr<InputDevice> mDevice;
11286
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011287 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011288 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011289 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011290 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011291 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011292 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011293 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11294 }
11295
11296 void SetUp() override { SetUp(DEVICE_CLASSES); }
11297
11298 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011299 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011300 mFakePolicy.clear();
11301 }
11302
Chris Yee2b1e5c2021-03-10 22:45:12 -080011303 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11304 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011305 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011306 InputDeviceIdentifier identifier;
11307 identifier.name = name;
11308 identifier.location = location;
11309 std::shared_ptr<InputDevice> device =
11310 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11311 identifier);
11312 mReader->pushNextDevice(device);
11313 mFakeEventHub->addDevice(eventHubId, name, classes);
11314 mReader->loopOnce();
11315 return device;
11316 }
11317
11318 template <class T, typename... Args>
11319 T& addControllerAndConfigure(Args... args) {
11320 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11321
11322 return controller;
11323 }
11324};
11325
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011326const char* PeripheralControllerTest::DEVICE_NAME = "device";
11327const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11328const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11329const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11330const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011331const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11332 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011333const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011334
11335// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011336class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011337protected:
11338 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011339 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011340 }
11341};
11342
11343TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011344 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011345
Harry Cuttsa5b71292022-11-28 12:56:17 +000011346 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11347 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11348 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011349}
11350
11351TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011352 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011353
Harry Cuttsa5b71292022-11-28 12:56:17 +000011354 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11355 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11356 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011357}
11358
11359// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011360class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011361protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011362 void SetUp() override {
11363 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11364 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011365};
11366
Chris Ye85758332021-05-16 23:05:17 -070011367TEST_F(LightControllerTest, MonoLight) {
11368 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011369 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011370 .maxBrightness = 255,
11371 .flags = InputLightClass::BRIGHTNESS,
11372 .path = ""};
11373 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011374
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011375 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011376 InputDeviceInfo info;
11377 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011378 std::vector<InputDeviceLightInfo> lights = info.getLights();
11379 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011380 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11381 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11382
11383 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11384 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11385}
11386
11387TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11388 RawLightInfo infoMono = {.id = 1,
11389 .name = "mono_keyboard_backlight",
11390 .maxBrightness = 255,
11391 .flags = InputLightClass::BRIGHTNESS |
11392 InputLightClass::KEYBOARD_BACKLIGHT,
11393 .path = ""};
11394 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11395
11396 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11397 InputDeviceInfo info;
11398 controller.populateDeviceInfo(&info);
11399 std::vector<InputDeviceLightInfo> lights = info.getLights();
11400 ASSERT_EQ(1U, lights.size());
11401 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11402 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011403
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011404 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11405 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011406}
11407
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000011408TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
11409 RawLightInfo infoMono = {.id = 1,
11410 .name = "mono_light",
11411 .maxBrightness = 255,
11412 .flags = InputLightClass::BRIGHTNESS,
11413 .path = ""};
11414 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11415 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11416 "0,100,200");
11417
11418 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11419 std::list<NotifyArgs> unused =
11420 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11421 /*changes=*/{});
11422
11423 InputDeviceInfo info;
11424 controller.populateDeviceInfo(&info);
11425 std::vector<InputDeviceLightInfo> lights = info.getLights();
11426 ASSERT_EQ(1U, lights.size());
11427 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11428}
11429
11430TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
11431 RawLightInfo infoMono = {.id = 1,
11432 .name = "mono_keyboard_backlight",
11433 .maxBrightness = 255,
11434 .flags = InputLightClass::BRIGHTNESS |
11435 InputLightClass::KEYBOARD_BACKLIGHT,
11436 .path = ""};
11437 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11438
11439 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11440 std::list<NotifyArgs> unused =
11441 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11442 /*changes=*/{});
11443
11444 InputDeviceInfo info;
11445 controller.populateDeviceInfo(&info);
11446 std::vector<InputDeviceLightInfo> lights = info.getLights();
11447 ASSERT_EQ(1U, lights.size());
11448 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11449}
11450
11451TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
11452 RawLightInfo infoMono = {.id = 1,
11453 .name = "mono_keyboard_backlight",
11454 .maxBrightness = 255,
11455 .flags = InputLightClass::BRIGHTNESS |
11456 InputLightClass::KEYBOARD_BACKLIGHT,
11457 .path = ""};
11458 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11459 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11460 "0,100,200");
11461
11462 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11463 std::list<NotifyArgs> unused =
11464 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11465 /*changes=*/{});
11466
11467 InputDeviceInfo info;
11468 controller.populateDeviceInfo(&info);
11469 std::vector<InputDeviceLightInfo> lights = info.getLights();
11470 ASSERT_EQ(1U, lights.size());
11471 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
11472 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
11473 ASSERT_EQ(BrightnessLevel(0), *it);
11474 std::advance(it, 1);
11475 ASSERT_EQ(BrightnessLevel(100), *it);
11476 std::advance(it, 1);
11477 ASSERT_EQ(BrightnessLevel(200), *it);
11478}
11479
11480TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
11481 RawLightInfo infoMono = {.id = 1,
11482 .name = "mono_keyboard_backlight",
11483 .maxBrightness = 255,
11484 .flags = InputLightClass::BRIGHTNESS |
11485 InputLightClass::KEYBOARD_BACKLIGHT,
11486 .path = ""};
11487 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11488 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11489 "0,100,200,300,400,500");
11490
11491 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11492 std::list<NotifyArgs> unused =
11493 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11494 /*changes=*/{});
11495
11496 InputDeviceInfo info;
11497 controller.populateDeviceInfo(&info);
11498 std::vector<InputDeviceLightInfo> lights = info.getLights();
11499 ASSERT_EQ(1U, lights.size());
11500 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11501}
11502
Chris Yee2b1e5c2021-03-10 22:45:12 -080011503TEST_F(LightControllerTest, RGBLight) {
11504 RawLightInfo infoRed = {.id = 1,
11505 .name = "red",
11506 .maxBrightness = 255,
11507 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11508 .path = ""};
11509 RawLightInfo infoGreen = {.id = 2,
11510 .name = "green",
11511 .maxBrightness = 255,
11512 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11513 .path = ""};
11514 RawLightInfo infoBlue = {.id = 3,
11515 .name = "blue",
11516 .maxBrightness = 255,
11517 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11518 .path = ""};
11519 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11520 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11521 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11522
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011523 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011524 InputDeviceInfo info;
11525 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011526 std::vector<InputDeviceLightInfo> lights = info.getLights();
11527 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011528 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11529 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11530 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11531
11532 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11533 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11534}
11535
11536TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11537 RawLightInfo infoRed = {.id = 1,
11538 .name = "red_keyboard_backlight",
11539 .maxBrightness = 255,
11540 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11541 InputLightClass::KEYBOARD_BACKLIGHT,
11542 .path = ""};
11543 RawLightInfo infoGreen = {.id = 2,
11544 .name = "green_keyboard_backlight",
11545 .maxBrightness = 255,
11546 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11547 InputLightClass::KEYBOARD_BACKLIGHT,
11548 .path = ""};
11549 RawLightInfo infoBlue = {.id = 3,
11550 .name = "blue_keyboard_backlight",
11551 .maxBrightness = 255,
11552 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11553 InputLightClass::KEYBOARD_BACKLIGHT,
11554 .path = ""};
11555 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11556 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11557 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11558
11559 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11560 InputDeviceInfo info;
11561 controller.populateDeviceInfo(&info);
11562 std::vector<InputDeviceLightInfo> lights = info.getLights();
11563 ASSERT_EQ(1U, lights.size());
11564 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11565 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11566 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11567
11568 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11569 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11570}
11571
11572TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11573 RawLightInfo infoRed = {.id = 1,
11574 .name = "red",
11575 .maxBrightness = 255,
11576 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11577 .path = ""};
11578 RawLightInfo infoGreen = {.id = 2,
11579 .name = "green",
11580 .maxBrightness = 255,
11581 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11582 .path = ""};
11583 RawLightInfo infoBlue = {.id = 3,
11584 .name = "blue",
11585 .maxBrightness = 255,
11586 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11587 .path = ""};
11588 RawLightInfo infoGlobal = {.id = 3,
11589 .name = "global_keyboard_backlight",
11590 .maxBrightness = 255,
11591 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11592 InputLightClass::KEYBOARD_BACKLIGHT,
11593 .path = ""};
11594 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11595 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11596 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11597 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11598
11599 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11600 InputDeviceInfo info;
11601 controller.populateDeviceInfo(&info);
11602 std::vector<InputDeviceLightInfo> lights = info.getLights();
11603 ASSERT_EQ(1U, lights.size());
11604 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11605 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11606 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011607
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011608 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11609 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011610}
11611
11612TEST_F(LightControllerTest, MultiColorRGBLight) {
11613 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011614 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011615 .maxBrightness = 255,
11616 .flags = InputLightClass::BRIGHTNESS |
11617 InputLightClass::MULTI_INTENSITY |
11618 InputLightClass::MULTI_INDEX,
11619 .path = ""};
11620
11621 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11622
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011623 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011624 InputDeviceInfo info;
11625 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011626 std::vector<InputDeviceLightInfo> lights = info.getLights();
11627 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011628 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11629 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11630 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11631
11632 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11633 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11634}
11635
11636TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11637 RawLightInfo infoColor = {.id = 1,
11638 .name = "multi_color_keyboard_backlight",
11639 .maxBrightness = 255,
11640 .flags = InputLightClass::BRIGHTNESS |
11641 InputLightClass::MULTI_INTENSITY |
11642 InputLightClass::MULTI_INDEX |
11643 InputLightClass::KEYBOARD_BACKLIGHT,
11644 .path = ""};
11645
11646 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11647
11648 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11649 InputDeviceInfo info;
11650 controller.populateDeviceInfo(&info);
11651 std::vector<InputDeviceLightInfo> lights = info.getLights();
11652 ASSERT_EQ(1U, lights.size());
11653 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11654 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11655 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011656
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011657 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11658 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011659}
11660
11661TEST_F(LightControllerTest, PlayerIdLight) {
11662 RawLightInfo info1 = {.id = 1,
11663 .name = "player1",
11664 .maxBrightness = 255,
11665 .flags = InputLightClass::BRIGHTNESS,
11666 .path = ""};
11667 RawLightInfo info2 = {.id = 2,
11668 .name = "player2",
11669 .maxBrightness = 255,
11670 .flags = InputLightClass::BRIGHTNESS,
11671 .path = ""};
11672 RawLightInfo info3 = {.id = 3,
11673 .name = "player3",
11674 .maxBrightness = 255,
11675 .flags = InputLightClass::BRIGHTNESS,
11676 .path = ""};
11677 RawLightInfo info4 = {.id = 4,
11678 .name = "player4",
11679 .maxBrightness = 255,
11680 .flags = InputLightClass::BRIGHTNESS,
11681 .path = ""};
11682 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11683 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11684 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11685 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11686
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011687 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011688 InputDeviceInfo info;
11689 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011690 std::vector<InputDeviceLightInfo> lights = info.getLights();
11691 ASSERT_EQ(1U, lights.size());
11692 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011693 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11694 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011695
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011696 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11697 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11698 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011699}
11700
Michael Wrightd02c5b62014-02-10 15:10:22 -080011701} // namespace android