blob: 01539671d838ce00b7f5a33350fcf4b238e12447 [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>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
Prabir Pradhan739dca42022-09-09 20:12:01 +000034#include <TestInputListenerMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070035#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080036#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000037#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070038#include <android-base/thread_annotations.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000039#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050041#include <gui/constants.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000042#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080043
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070044#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000045#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000046#include "FakeInputReaderPolicy.h"
Harry Cuttsb57f1702022-11-28 15:34:22 +000047#include "FakePointerController.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000048#include "TestConstants.h"
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000049#include "android/hardware/input/InputDeviceCountryCode.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000050#include "input/DisplayViewport.h"
51#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010052
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000053using android::hardware::input::InputDeviceCountryCode;
54
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
Michael Wrightd02c5b62014-02-10 15:10:22 -080094// Error tolerance for floating point assertions.
95static const float EPSILON = 0.001f;
96
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000097// Minimum timestamp separation between subsequent input events from a Bluetooth device.
98static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
99// Maximum smoothing time delta so that we don't generate events too far into the future.
100constexpr static nsecs_t MAX_BLUETOOTH_SMOOTHING_DELTA = ms2ns(32);
101
Michael Wrightd02c5b62014-02-10 15:10:22 -0800102template<typename T>
103static inline T min(T a, T b) {
104 return a < b ? a : b;
105}
106
107static inline float avg(float x, float y) {
108 return (x + y) / 2;
109}
110
Chris Ye3fdbfef2021-01-06 18:45:18 -0800111// Mapping for light color name and the light color
112const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
113 {"green", LightColor::GREEN},
114 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115
Michael Wrighta9cf4192022-12-01 23:46:39 +0000116static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700117 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000118 case ui::ROTATION_90:
119 return ui::ROTATION_270;
120 case ui::ROTATION_270:
121 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700122 default:
123 return orientation;
124 }
125}
126
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800127static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
128 InputDeviceInfo info;
129 mapper.populateDeviceInfo(&info);
130
131 const InputDeviceInfo::MotionRange* motionRange =
132 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
133 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
134}
135
136static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
137 InputDeviceInfo info;
138 mapper.populateDeviceInfo(&info);
139
140 const InputDeviceInfo::MotionRange* motionRange =
141 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
142 ASSERT_EQ(nullptr, motionRange);
143}
144
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700145[[maybe_unused]] static void dumpReader(InputReader& reader) {
146 std::string dump;
147 reader.dump(dump);
148 std::istringstream iss(dump);
149 for (std::string line; std::getline(iss, line);) {
150 ALOGE("%s", line.c_str());
151 std::this_thread::sleep_for(std::chrono::milliseconds(1));
152 }
153}
154
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155// --- FakeInputMapper ---
156
157class FakeInputMapper : public InputMapper {
158 uint32_t mSources;
159 int32_t mKeyboardType;
160 int32_t mMetaState;
161 KeyedVector<int32_t, int32_t> mKeyCodeStates;
162 KeyedVector<int32_t, int32_t> mScanCodeStates;
163 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100164 // fake mapping which would normally come from keyCharacterMap
165 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800166 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800167
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700168 std::mutex mLock;
169 std::condition_variable mStateChangedCondition;
170 bool mConfigureWasCalled GUARDED_BY(mLock);
171 bool mResetWasCalled GUARDED_BY(mLock);
172 bool mProcessWasCalled GUARDED_BY(mLock);
173 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800174
Arthur Hungc23540e2018-11-29 20:42:11 +0800175 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800177 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
178 : InputMapper(deviceContext),
179 mSources(sources),
180 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800182 mConfigureWasCalled(false),
183 mResetWasCalled(false),
184 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185
Chris Yea52ade12020-08-27 16:49:20 -0700186 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800187
188 void setKeyboardType(int32_t keyboardType) {
189 mKeyboardType = keyboardType;
190 }
191
192 void setMetaState(int32_t metaState) {
193 mMetaState = metaState;
194 }
195
196 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700197 std::unique_lock<std::mutex> lock(mLock);
198 base::ScopedLockAssertion assumeLocked(mLock);
199 const bool configureCalled =
200 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
201 return mConfigureWasCalled;
202 });
203 if (!configureCalled) {
204 FAIL() << "Expected configure() to have been called.";
205 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206 mConfigureWasCalled = false;
207 }
208
209 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700210 std::unique_lock<std::mutex> lock(mLock);
211 base::ScopedLockAssertion assumeLocked(mLock);
212 const bool resetCalled =
213 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
214 return mResetWasCalled;
215 });
216 if (!resetCalled) {
217 FAIL() << "Expected reset() to have been called.";
218 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800219 mResetWasCalled = false;
220 }
221
Yi Kong9b14ac62018-07-17 13:48:38 -0700222 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700223 std::unique_lock<std::mutex> lock(mLock);
224 base::ScopedLockAssertion assumeLocked(mLock);
225 const bool processCalled =
226 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
227 return mProcessWasCalled;
228 });
229 if (!processCalled) {
230 FAIL() << "Expected process() to have been called.";
231 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800232 if (outLastEvent) {
233 *outLastEvent = mLastEvent;
234 }
235 mProcessWasCalled = false;
236 }
237
238 void setKeyCodeState(int32_t keyCode, int32_t state) {
239 mKeyCodeStates.replaceValueFor(keyCode, state);
240 }
241
242 void setScanCodeState(int32_t scanCode, int32_t state) {
243 mScanCodeStates.replaceValueFor(scanCode, state);
244 }
245
246 void setSwitchState(int32_t switchCode, int32_t state) {
247 mSwitchStates.replaceValueFor(switchCode, state);
248 }
249
250 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800251 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800252 }
253
Philip Junker4af3b3d2021-12-14 10:36:55 +0100254 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
255 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
256 }
257
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100259 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260
Chris Yea52ade12020-08-27 16:49:20 -0700261 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800262 InputMapper::populateDeviceInfo(deviceInfo);
263
264 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
265 deviceInfo->setKeyboardType(mKeyboardType);
266 }
267 }
268
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700269 std::list<NotifyArgs> configure(nsecs_t, const InputReaderConfiguration* config,
270 uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700271 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800272 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800273
274 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800275 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +0800276 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
277 mViewport = config->getDisplayViewportByPort(*displayPort);
278 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700279
280 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700281 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800282 }
283
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700284 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700285 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800286 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700287 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> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700292 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800293 mLastEvent = *rawEvent;
294 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700295 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700296 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800297 }
298
Chris Yea52ade12020-08-27 16:49:20 -0700299 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800300 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
301 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
302 }
303
Philip Junker4af3b3d2021-12-14 10:36:55 +0100304 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
305 auto it = mKeyCodeMapping.find(locationKeyCode);
306 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
307 }
308
Chris Yea52ade12020-08-27 16:49:20 -0700309 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800310 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
311 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
312 }
313
Chris Yea52ade12020-08-27 16:49:20 -0700314 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800315 ssize_t index = mSwitchStates.indexOfKey(switchCode);
316 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
317 }
318
Chris Yea52ade12020-08-27 16:49:20 -0700319 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700320 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700321 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700322 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800323 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
324 if (keyCodes[i] == mSupportedKeyCodes[j]) {
325 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800326 }
327 }
328 }
Chris Yea52ade12020-08-27 16:49:20 -0700329 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800330 return result;
331 }
332
333 virtual int32_t getMetaState() {
334 return mMetaState;
335 }
336
337 virtual void fadePointer() {
338 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800339
340 virtual std::optional<int32_t> getAssociatedDisplay() {
341 if (mViewport) {
342 return std::make_optional(mViewport->displayId);
343 }
344 return std::nullopt;
345 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800346};
347
348
349// --- InstrumentedInputReader ---
350
351class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800352 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353
354public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700355 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
356 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700357 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +0800358 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800359
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000360 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800361
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800362 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800363
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800364 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000365 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800366 InputDeviceIdentifier identifier;
367 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +0800368 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800369 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +0800370 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800371 }
372
Prabir Pradhan28efc192019-11-05 01:10:04 +0000373 // Make the protected loopOnce method accessible to tests.
374 using InputReader::loopOnce;
375
Michael Wrightd02c5b62014-02-10 15:10:22 -0800376protected:
Chris Ye1c2e0892020-11-30 21:41:44 -0800377 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
378 const InputDeviceIdentifier& identifier)
379 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800380 if (!mNextDevices.empty()) {
381 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
382 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800383 return device;
384 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800385 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800386 }
387
arthurhungdcef2dc2020-08-11 14:47:50 +0800388 // --- FakeInputReaderContext ---
389 class FakeInputReaderContext : public ContextImpl {
390 int32_t mGlobalMetaState;
391 bool mUpdateGlobalMetaStateWasCalled;
392 int32_t mGeneration;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +0000393 std::optional<nsecs_t> mRequestedTimeout;
394 std::vector<InputDeviceInfo> mExternalStylusDevices;
arthurhungdcef2dc2020-08-11 14:47:50 +0800395
396 public:
397 FakeInputReaderContext(InputReader* reader)
398 : ContextImpl(reader),
399 mGlobalMetaState(0),
400 mUpdateGlobalMetaStateWasCalled(false),
401 mGeneration(1) {}
402
403 virtual ~FakeInputReaderContext() {}
404
405 void assertUpdateGlobalMetaStateWasCalled() {
406 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
407 << "Expected updateGlobalMetaState() to have been called.";
408 mUpdateGlobalMetaStateWasCalled = false;
409 }
410
411 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
412
413 uint32_t getGeneration() { return mGeneration; }
414
415 void updateGlobalMetaState() override {
416 mUpdateGlobalMetaStateWasCalled = true;
417 ContextImpl::updateGlobalMetaState();
418 }
419
420 int32_t getGlobalMetaState() override {
421 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
422 }
423
424 int32_t bumpGeneration() override {
425 mGeneration = ContextImpl::bumpGeneration();
426 return mGeneration;
427 }
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +0000428
429 void requestTimeoutAtTime(nsecs_t when) override { mRequestedTimeout = when; }
430
431 void assertTimeoutWasRequested(nsecs_t when) {
432 ASSERT_TRUE(mRequestedTimeout) << "Expected timeout at time " << when
433 << " but there was no timeout requested.";
434 ASSERT_EQ(when, *mRequestedTimeout);
435 mRequestedTimeout.reset();
436 }
437
438 void assertTimeoutWasNotRequested() {
439 ASSERT_FALSE(mRequestedTimeout) << "Expected no timeout to have been requested,"
440 " but one was requested at time "
441 << *mRequestedTimeout;
442 }
443
444 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override {
445 outDevices = mExternalStylusDevices;
446 }
447
448 void setExternalStylusDevices(std::vector<InputDeviceInfo>&& devices) {
449 mExternalStylusDevices = devices;
450 }
arthurhungdcef2dc2020-08-11 14:47:50 +0800451 } mFakeContext;
452
Michael Wrightd02c5b62014-02-10 15:10:22 -0800453 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +0800454
455public:
456 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800457};
458
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700459// --- InputReaderPolicyTest ---
460class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700461protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700462 sp<FakeInputReaderPolicy> mFakePolicy;
463
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700464 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700465 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700466};
467
468/**
469 * Check that empty set of viewports is an acceptable configuration.
470 * Also try to get internal viewport two different ways - by type and by uniqueId.
471 *
472 * There will be confusion if two viewports with empty uniqueId and identical type are present.
473 * Such configuration is not currently allowed.
474 */
475TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700476 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700477
478 // We didn't add any viewports yet, so there shouldn't be any.
479 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100480 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700481 ASSERT_FALSE(internalViewport);
482
483 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000484 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
485 true /*isActive*/, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700486
487 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700488 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700489 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100490 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700491
492 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100493 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700494 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700495 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700496
497 mFakePolicy->clearViewports();
498 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700499 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700500 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100501 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700502 ASSERT_FALSE(internalViewport);
503}
504
505TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
506 const std::string internalUniqueId = "local:0";
507 const std::string externalUniqueId = "local:1";
508 const std::string virtualUniqueId1 = "virtual:2";
509 const std::string virtualUniqueId2 = "virtual:3";
510 constexpr int32_t virtualDisplayId1 = 2;
511 constexpr int32_t virtualDisplayId2 = 3;
512
513 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000514 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
515 true /*isActive*/, internalUniqueId, NO_PORT,
516 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700517 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000518 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
519 true /*isActive*/, externalUniqueId, NO_PORT,
520 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700521 // Add an virtual viewport
522 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000523 ui::ROTATION_0, true /*isActive*/, virtualUniqueId1, NO_PORT,
524 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700525 // Add another virtual viewport
526 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000527 ui::ROTATION_0, true /*isActive*/, virtualUniqueId2, NO_PORT,
528 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700529
530 // Check matching by type for internal
531 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100532 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700533 ASSERT_TRUE(internalViewport);
534 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
535
536 // Check matching by type for external
537 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100538 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700539 ASSERT_TRUE(externalViewport);
540 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
541
542 // Check matching by uniqueId for virtual viewport #1
543 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700544 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700545 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100546 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700547 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
548 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
549
550 // Check matching by uniqueId for virtual viewport #2
551 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700552 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700553 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100554 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700555 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
556 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
557}
558
559
560/**
561 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
562 * that lookup works by checking display id.
563 * Check that 2 viewports of each kind is possible, for all existing viewport types.
564 */
565TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
566 const std::string uniqueId1 = "uniqueId1";
567 const std::string uniqueId2 = "uniqueId2";
568 constexpr int32_t displayId1 = 2;
569 constexpr int32_t displayId2 = 3;
570
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100571 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
572 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700573 for (const ViewportType& type : types) {
574 mFakePolicy->clearViewports();
575 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000576 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
577 true /*isActive*/, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700578 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000579 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
580 true /*isActive*/, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700581
582 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700583 std::optional<DisplayViewport> viewport1 =
584 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700585 ASSERT_TRUE(viewport1);
586 ASSERT_EQ(displayId1, viewport1->displayId);
587 ASSERT_EQ(type, viewport1->type);
588
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700589 std::optional<DisplayViewport> viewport2 =
590 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700591 ASSERT_TRUE(viewport2);
592 ASSERT_EQ(displayId2, viewport2->displayId);
593 ASSERT_EQ(type, viewport2->type);
594
595 // When there are multiple viewports of the same kind, and uniqueId is not specified
596 // in the call to getDisplayViewport, then that situation is not supported.
597 // The viewports can be stored in any order, so we cannot rely on the order, since that
598 // is just implementation detail.
599 // However, we can check that it still returns *a* viewport, we just cannot assert
600 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700601 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700602 ASSERT_TRUE(someViewport);
603 }
604}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800605
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700606/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000607 * When we have multiple internal displays make sure we always return the default display when
608 * querying by type.
609 */
610TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
611 const std::string uniqueId1 = "uniqueId1";
612 const std::string uniqueId2 = "uniqueId2";
613 constexpr int32_t nonDefaultDisplayId = 2;
614 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
615 "Test display ID should not be ADISPLAY_ID_DEFAULT");
616
617 // Add the default display first and ensure it gets returned.
618 mFakePolicy->clearViewports();
619 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000620 ui::ROTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000621 ViewportType::INTERNAL);
622 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000623 ui::ROTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000624 ViewportType::INTERNAL);
625
626 std::optional<DisplayViewport> viewport =
627 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
628 ASSERT_TRUE(viewport);
629 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
630 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
631
632 // Add the default display second to make sure order doesn't matter.
633 mFakePolicy->clearViewports();
634 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000635 ui::ROTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000636 ViewportType::INTERNAL);
637 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000638 ui::ROTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000639 ViewportType::INTERNAL);
640
641 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
642 ASSERT_TRUE(viewport);
643 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
644 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
645}
646
647/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700648 * Check getDisplayViewportByPort
649 */
650TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100651 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700652 const std::string uniqueId1 = "uniqueId1";
653 const std::string uniqueId2 = "uniqueId2";
654 constexpr int32_t displayId1 = 1;
655 constexpr int32_t displayId2 = 2;
656 const uint8_t hdmi1 = 0;
657 const uint8_t hdmi2 = 1;
658 const uint8_t hdmi3 = 2;
659
660 mFakePolicy->clearViewports();
661 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000662 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
663 true /*isActive*/, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700664 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000665 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
666 true /*isActive*/, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700667
668 // Check that correct display viewport was returned by comparing the display ports.
669 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
670 ASSERT_TRUE(hdmi1Viewport);
671 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
672 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
673
674 // Check that we can still get the same viewport using the uniqueId
675 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
676 ASSERT_TRUE(hdmi1Viewport);
677 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
678 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
679 ASSERT_EQ(type, hdmi1Viewport->type);
680
681 // Check that we cannot find a port with "HDMI2", because we never added one
682 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
683 ASSERT_FALSE(hdmi2Viewport);
684}
685
Michael Wrightd02c5b62014-02-10 15:10:22 -0800686// --- InputReaderTest ---
687
688class InputReaderTest : public testing::Test {
689protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700690 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700692 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000693 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800694
Chris Yea52ade12020-08-27 16:49:20 -0700695 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700696 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700697 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700698 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800699
Prabir Pradhan28efc192019-11-05 01:10:04 +0000700 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700701 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800702 }
703
Chris Yea52ade12020-08-27 16:49:20 -0700704 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700705 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800706 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800707 }
708
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700709 void addDevice(int32_t eventHubId, const std::string& name,
710 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800711 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800712
713 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800714 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800715 }
716 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000717 mReader->loopOnce();
718 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700719 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
720 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800721 }
722
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800723 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700724 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000725 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700726 }
727
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800728 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700729 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000730 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700731 }
732
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800733 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700734 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700735 ftl::Flags<InputDeviceClass> classes,
736 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800737 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800738 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
739 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800740 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800741 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 return mapper;
743 }
744};
745
Chris Ye98d3f532020-10-01 21:48:59 -0700746TEST_F(InputReaderTest, PolicyGetInputDevices) {
747 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700748 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700749 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750
751 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700752 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800753 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800754 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100755 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
757 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000758 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800759}
760
Chris Yee7310032020-09-22 15:36:28 -0700761TEST_F(InputReaderTest, GetMergedInputDevices) {
762 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
763 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
764 // Add two subdevices to device
765 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
766 // Must add at least one mapper or the device will be ignored!
767 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
768 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
769
770 // Push same device instance for next device to be added, so they'll have same identifier.
771 mReader->pushNextDevice(device);
772 mReader->pushNextDevice(device);
773 ASSERT_NO_FATAL_FAILURE(
774 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
775 ASSERT_NO_FATAL_FAILURE(
776 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
777
778 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000779 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700780}
781
Chris Yee14523a2020-12-19 13:46:00 -0800782TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
783 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
784 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
785 // Add two subdevices to device
786 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
787 // Must add at least one mapper or the device will be ignored!
788 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
789 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
790
791 // Push same device instance for next device to be added, so they'll have same identifier.
792 mReader->pushNextDevice(device);
793 mReader->pushNextDevice(device);
794 // Sensor device is initially disabled
795 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
796 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
797 nullptr));
798 // Device is disabled because the only sub device is a sensor device and disabled initially.
799 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
800 ASSERT_FALSE(device->isEnabled());
801 ASSERT_NO_FATAL_FAILURE(
802 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
803 // The merged device is enabled if any sub device is enabled
804 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
805 ASSERT_TRUE(device->isEnabled());
806}
807
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700808TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800809 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700810 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800811 constexpr int32_t eventHubId = 1;
812 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700813 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800814 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800815 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800816 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700817
Yi Kong9b14ac62018-07-17 13:48:38 -0700818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700819
820 NotifyDeviceResetArgs resetArgs;
821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700822 ASSERT_EQ(deviceId, resetArgs.deviceId);
823
824 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800825 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000826 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700827
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700829 ASSERT_EQ(deviceId, resetArgs.deviceId);
830 ASSERT_EQ(device->isEnabled(), false);
831
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800832 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000833 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700836 ASSERT_EQ(device->isEnabled(), false);
837
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800838 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000839 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700841 ASSERT_EQ(deviceId, resetArgs.deviceId);
842 ASSERT_EQ(device->isEnabled(), true);
843}
844
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800846 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700847 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800848 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800849 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800850 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800851 AINPUT_SOURCE_KEYBOARD, nullptr);
852 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800853
854 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
855 AINPUT_SOURCE_ANY, AKEYCODE_A))
856 << "Should return unknown when the device id is >= 0 but unknown.";
857
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800858 ASSERT_EQ(AKEY_STATE_UNKNOWN,
859 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
860 << "Should return unknown when the device id is valid but the sources are not "
861 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800862
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800863 ASSERT_EQ(AKEY_STATE_DOWN,
864 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
865 AKEYCODE_A))
866 << "Should return value provided by mapper when device id is valid and the device "
867 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868
869 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
870 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
871 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
872
873 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
874 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
875 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
876}
877
Philip Junker4af3b3d2021-12-14 10:36:55 +0100878TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
879 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
880 constexpr int32_t eventHubId = 1;
881 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
882 InputDeviceClass::KEYBOARD,
883 AINPUT_SOURCE_KEYBOARD, nullptr);
884 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
885
886 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
887 << "Should return unknown when the device with the specified id is not found.";
888
889 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
890 << "Should return correct mapping when device id is valid and mapping exists.";
891
892 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
893 << "Should return the location key code when device id is valid and there's no "
894 "mapping.";
895}
896
897TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
898 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
899 constexpr int32_t eventHubId = 1;
900 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
901 InputDeviceClass::JOYSTICK,
902 AINPUT_SOURCE_GAMEPAD, nullptr);
903 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
904
905 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
906 << "Should return unknown when the device id is valid but there is no keyboard mapper";
907}
908
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800910 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700911 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800912 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800913 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800914 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800915 AINPUT_SOURCE_KEYBOARD, nullptr);
916 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800917
918 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
919 AINPUT_SOURCE_ANY, KEY_A))
920 << "Should return unknown when the device id is >= 0 but unknown.";
921
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800922 ASSERT_EQ(AKEY_STATE_UNKNOWN,
923 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
924 << "Should return unknown when the device id is valid but the sources are not "
925 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800926
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800927 ASSERT_EQ(AKEY_STATE_DOWN,
928 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
929 KEY_A))
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
933 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
934 AINPUT_SOURCE_TRACKBALL, KEY_A))
935 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
936
937 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
938 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
939 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
940}
941
942TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800943 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700944 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800945 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800946 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800947 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800948 AINPUT_SOURCE_KEYBOARD, nullptr);
949 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800950
951 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
952 AINPUT_SOURCE_ANY, SW_LID))
953 << "Should return unknown when the device id is >= 0 but unknown.";
954
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800955 ASSERT_EQ(AKEY_STATE_UNKNOWN,
956 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
957 << "Should return unknown when the device id is valid but the sources are not "
958 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800959
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800960 ASSERT_EQ(AKEY_STATE_DOWN,
961 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
962 SW_LID))
963 << "Should return value provided by mapper when device id is valid and the device "
964 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965
966 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
967 AINPUT_SOURCE_TRACKBALL, SW_LID))
968 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
969
970 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
971 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
972 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
973}
974
975TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800976 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700977 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800978 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800979 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800980 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800981 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100982
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800983 mapper.addSupportedKeyCode(AKEYCODE_A);
984 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700986 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800987 uint8_t flags[4] = { 0, 0, 0, 1 };
988
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700989 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800990 << "Should return false when device id is >= 0 but unknown.";
991 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
992
993 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700994 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800995 << "Should return false when device id is valid but the sources are not supported by "
996 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800997 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
998
999 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001000 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001001 keyCodes, flags))
1002 << "Should return value provided by mapper when device id is valid and the device "
1003 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001004 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1005
1006 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001007 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1008 << "Should return false when the device id is < 0 but the sources are not supported by "
1009 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001010 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1011
1012 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001013 ASSERT_TRUE(
1014 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1015 << "Should return value provided by mapper when device id is < 0 and one of the "
1016 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001017 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1018}
1019
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001020TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001021 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001022 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001023
1024 NotifyConfigurationChangedArgs args;
1025
1026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1027 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1028}
1029
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001030TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001031 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001032 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001033 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001034 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001035 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001036 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001037 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001038 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001039
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001040 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001041 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001042 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1043
1044 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001045 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001046 ASSERT_EQ(when, event.when);
1047 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001048 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001049 ASSERT_EQ(EV_KEY, event.type);
1050 ASSERT_EQ(KEY_A, event.code);
1051 ASSERT_EQ(1, event.value);
1052}
1053
Garfield Tan1c7bc862020-01-28 13:24:04 -08001054TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001055 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001056 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001057 constexpr int32_t eventHubId = 1;
1058 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001059 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001060 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001061 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001062 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001063
1064 NotifyDeviceResetArgs resetArgs;
1065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001066 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001067
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001068 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001069 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001071 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001072 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001073
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001074 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001075 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001077 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001078 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001079
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001080 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001081 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001083 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001084 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001085}
1086
Garfield Tan1c7bc862020-01-28 13:24:04 -08001087TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1088 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001089 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001090 constexpr int32_t eventHubId = 1;
1091 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1092 // Must add at least one mapper or the device will be ignored!
1093 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001094 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001095 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1096
1097 NotifyDeviceResetArgs resetArgs;
1098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1099 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1100}
1101
Arthur Hungc23540e2018-11-29 20:42:11 +08001102TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001103 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001104 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001105 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001106 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001107 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1108 FakeInputMapper& mapper =
1109 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001110 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001111
1112 const uint8_t hdmi1 = 1;
1113
1114 // Associated touch screen with second display.
1115 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1116
1117 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001118 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001119 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1120 true /*isActive*/, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001121 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001122 ui::ROTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001123 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001124 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001125 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001126
1127 // Add the device, and make sure all of the callbacks are triggered.
1128 // The device is added after the input port associations are processed since
1129 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001130 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001133 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001134
Arthur Hung2c9a3342019-07-23 14:18:59 +08001135 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001136 ASSERT_EQ(deviceId, device->getId());
1137 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1138 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001139
1140 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001141 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001142 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001143 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001144}
1145
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001146TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1147 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001148 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001149 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1150 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1151 // Must add at least one mapper or the device will be ignored!
1152 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1153 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1154 mReader->pushNextDevice(device);
1155 mReader->pushNextDevice(device);
1156 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1157 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1158
1159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1160
1161 NotifyDeviceResetArgs resetArgs;
1162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1163 ASSERT_EQ(deviceId, resetArgs.deviceId);
1164 ASSERT_TRUE(device->isEnabled());
1165 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1166 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1167
1168 disableDevice(deviceId);
1169 mReader->loopOnce();
1170
1171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1172 ASSERT_EQ(deviceId, resetArgs.deviceId);
1173 ASSERT_FALSE(device->isEnabled());
1174 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1175 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1176
1177 enableDevice(deviceId);
1178 mReader->loopOnce();
1179
1180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1181 ASSERT_EQ(deviceId, resetArgs.deviceId);
1182 ASSERT_TRUE(device->isEnabled());
1183 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1184 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1185}
1186
1187TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1188 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001189 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001190 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1191 // Add two subdevices to device
1192 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1193 FakeInputMapper& mapperDevice1 =
1194 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1195 FakeInputMapper& mapperDevice2 =
1196 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1197 mReader->pushNextDevice(device);
1198 mReader->pushNextDevice(device);
1199 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1200 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1201
1202 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1203 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1204
1205 ASSERT_EQ(AKEY_STATE_DOWN,
1206 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1207 ASSERT_EQ(AKEY_STATE_DOWN,
1208 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1209 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1210 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1211}
1212
Prabir Pradhan7e186182020-11-10 13:56:45 -08001213TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1214 NotifyPointerCaptureChangedArgs args;
1215
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001216 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001217 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1218 mReader->loopOnce();
1219 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001220 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1221 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001222
1223 mFakePolicy->setPointerCapture(false);
1224 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1225 mReader->loopOnce();
1226 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001227 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001228
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001229 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001230 // does not change.
1231 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1232 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001233 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001234}
1235
Chris Ye87143712020-11-10 05:05:58 +00001236class FakeVibratorInputMapper : public FakeInputMapper {
1237public:
1238 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1239 : FakeInputMapper(deviceContext, sources) {}
1240
1241 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1242};
1243
1244TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1245 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001246 ftl::Flags<InputDeviceClass> deviceClass =
1247 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001248 constexpr int32_t eventHubId = 1;
1249 const char* DEVICE_LOCATION = "BLUETOOTH";
1250 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1251 FakeVibratorInputMapper& mapper =
1252 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1253 mReader->pushNextDevice(device);
1254
1255 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1256 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1257
1258 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1259 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1260}
1261
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001262// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001263
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001264class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001265public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001266 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001267
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001268 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001269
Andy Chenf9f1a022022-08-29 20:07:10 -04001270 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1271
Chris Yee2b1e5c2021-03-10 22:45:12 -08001272 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1273
1274 void dump(std::string& dump) override {}
1275
1276 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1277 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001278 }
1279
Chris Yee2b1e5c2021-03-10 22:45:12 -08001280 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1281 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001282 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001283
1284 bool setLightColor(int32_t lightId, int32_t color) override {
1285 getDeviceContext().setLightBrightness(lightId, color >> 24);
1286 return true;
1287 }
1288
1289 std::optional<int32_t> getLightColor(int32_t lightId) override {
1290 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1291 if (!result.has_value()) {
1292 return std::nullopt;
1293 }
1294 return result.value() << 24;
1295 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001296
1297 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1298
1299 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1300
1301private:
1302 InputDeviceContext& mDeviceContext;
1303 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1304 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001305 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001306};
1307
Chris Yee2b1e5c2021-03-10 22:45:12 -08001308TEST_F(InputReaderTest, BatteryGetCapacity) {
1309 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001310 ftl::Flags<InputDeviceClass> deviceClass =
1311 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001312 constexpr int32_t eventHubId = 1;
1313 const char* DEVICE_LOCATION = "BLUETOOTH";
1314 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001315 FakePeripheralController& controller =
1316 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001317 mReader->pushNextDevice(device);
1318
1319 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1320
Harry Cuttsa5b71292022-11-28 12:56:17 +00001321 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1322 FakeEventHub::BATTERY_CAPACITY);
1323 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001324}
1325
1326TEST_F(InputReaderTest, BatteryGetStatus) {
1327 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001328 ftl::Flags<InputDeviceClass> deviceClass =
1329 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001330 constexpr int32_t eventHubId = 1;
1331 const char* DEVICE_LOCATION = "BLUETOOTH";
1332 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001333 FakePeripheralController& controller =
1334 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001335 mReader->pushNextDevice(device);
1336
1337 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1338
Harry Cuttsa5b71292022-11-28 12:56:17 +00001339 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1340 FakeEventHub::BATTERY_STATUS);
1341 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001342}
1343
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001344TEST_F(InputReaderTest, BatteryGetDevicePath) {
1345 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1346 ftl::Flags<InputDeviceClass> deviceClass =
1347 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1348 constexpr int32_t eventHubId = 1;
1349 const char* DEVICE_LOCATION = "BLUETOOTH";
1350 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1351 device->addController<FakePeripheralController>(eventHubId);
1352 mReader->pushNextDevice(device);
1353
1354 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1355
Harry Cuttsa5b71292022-11-28 12:56:17 +00001356 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001357}
1358
Chris Ye3fdbfef2021-01-06 18:45:18 -08001359TEST_F(InputReaderTest, LightGetColor) {
1360 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001361 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001362 constexpr int32_t eventHubId = 1;
1363 const char* DEVICE_LOCATION = "BLUETOOTH";
1364 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001365 FakePeripheralController& controller =
1366 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001367 mReader->pushNextDevice(device);
1368 RawLightInfo info = {.id = 1,
1369 .name = "Mono",
1370 .maxBrightness = 255,
1371 .flags = InputLightClass::BRIGHTNESS,
1372 .path = ""};
1373 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
1374 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
1375
1376 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001377
Chris Yee2b1e5c2021-03-10 22:45:12 -08001378 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
1379 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001380 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
1381 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
1382}
1383
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001384// --- InputReaderIntegrationTest ---
1385
1386// These tests create and interact with the InputReader only through its interface.
1387// The InputReader is started during SetUp(), which starts its processing in its own
1388// thread. The tests use linux uinput to emulate input devices.
1389// NOTE: Interacting with the physical device while these tests are running may cause
1390// the tests to fail.
1391class InputReaderIntegrationTest : public testing::Test {
1392protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001393 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001394 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001395 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001396
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001397 std::shared_ptr<FakePointerController> mFakePointerController;
1398
Chris Yea52ade12020-08-27 16:49:20 -07001399 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001400#if !defined(__ANDROID__)
1401 GTEST_SKIP();
1402#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001403 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001404 mFakePointerController = std::make_shared<FakePointerController>();
1405 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001406 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
1407 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001408
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001409 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1410 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001411 ASSERT_EQ(mReader->start(), OK);
1412
1413 // Since this test is run on a real device, all the input devices connected
1414 // to the test device will show up in mReader. We wait for those input devices to
1415 // show up before beginning the tests.
1416 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1417 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1418 }
1419
Chris Yea52ade12020-08-27 16:49:20 -07001420 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001421#if !defined(__ANDROID__)
1422 return;
1423#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001424 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001425 mReader.reset();
1426 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001427 mFakePolicy.clear();
1428 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001429
1430 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1431 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1432 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1433 [&name](const InputDeviceInfo& info) {
1434 return info.getIdentifier().name == name;
1435 });
1436 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1437 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001438};
1439
1440TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1441 // An invalid input device that is only used for this test.
1442 class InvalidUinputDevice : public UinputDevice {
1443 public:
Prabir Pradhanb7d434e2022-10-14 22:41:38 +00001444 InvalidUinputDevice() : UinputDevice("Invalid Device", 99 /*productId*/) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001445
1446 private:
1447 void configureDevice(int fd, uinput_user_dev* device) override {}
1448 };
1449
1450 const size_t numDevices = mFakePolicy->getInputDevices().size();
1451
1452 // UinputDevice does not set any event or key bits, so InputReader should not
1453 // consider it as a valid device.
1454 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1455 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1456 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1457 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1458
1459 invalidDevice.reset();
1460 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1461 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1462 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1463}
1464
1465TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1466 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1467
1468 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1469 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1470 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1471 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1472
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001473 const auto device = findDeviceByName(keyboard->getName());
1474 ASSERT_TRUE(device.has_value());
1475 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1476 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1477 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001478
1479 keyboard.reset();
1480 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1481 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1482 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1483}
1484
1485TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1486 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1487 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1488
1489 NotifyConfigurationChangedArgs configChangedArgs;
1490 ASSERT_NO_FATAL_FAILURE(
1491 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001492 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001493 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1494
1495 NotifyKeyArgs keyArgs;
1496 keyboard->pressAndReleaseHomeKey();
1497 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1498 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001499 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001500 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001501 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001502 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001503 prevTimestamp = keyArgs.eventTime;
1504
1505 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1506 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001507 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001508 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001509 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001510}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001511
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001512TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1513 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1514 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1515
1516 const auto device = findDeviceByName(stylus->getName());
1517 ASSERT_TRUE(device.has_value());
1518
Prabir Pradhana3621852022-10-14 18:57:23 +00001519 // An external stylus with buttons should also be recognized as a keyboard.
1520 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001521 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1522 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1523
1524 const auto DOWN =
1525 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1526 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1527
1528 stylus->pressAndReleaseKey(BTN_STYLUS);
1529 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1530 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1531 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1532 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1533
1534 stylus->pressAndReleaseKey(BTN_STYLUS2);
1535 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1536 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1537 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1538 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1539
1540 stylus->pressAndReleaseKey(BTN_STYLUS3);
1541 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1542 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1543 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1544 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1545}
1546
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001547/**
1548 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1549 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1550 * are passed to the listener.
1551 */
1552static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1553TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1554 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1555 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1556 NotifyKeyArgs keyArgs;
1557
1558 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1559 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1560 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1561 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1562
1563 controller->pressAndReleaseKey(BTN_GEAR_UP);
1564 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1565 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1566 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1567}
1568
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001569// --- TouchIntegrationTest ---
1570
Arthur Hungaab25622020-01-16 11:22:11 +08001571class TouchIntegrationTest : public InputReaderIntegrationTest {
1572protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001573 const std::string UNIQUE_ID = "local:0";
1574
Chris Yea52ade12020-08-27 16:49:20 -07001575 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001576#if !defined(__ANDROID__)
1577 GTEST_SKIP();
1578#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001579 InputReaderIntegrationTest::SetUp();
1580 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001581 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1582 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001583
1584 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1585 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1586 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001587 const auto info = findDeviceByName(mDevice->getName());
1588 ASSERT_TRUE(info);
1589 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001590 }
1591
1592 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001593 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001594 std::optional<uint8_t> physicalPort,
1595 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001596 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
1597 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08001598 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1599 }
1600
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001601 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1602 NotifyMotionArgs args;
1603 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1604 EXPECT_EQ(action, args.action);
1605 ASSERT_EQ(points.size(), args.pointerCount);
1606 for (size_t i = 0; i < args.pointerCount; i++) {
1607 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1608 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1609 }
1610 }
1611
Arthur Hungaab25622020-01-16 11:22:11 +08001612 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001613 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001614};
1615
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001616TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
1617 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1618 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1619 // presses).
1620 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1621 mDeviceInfo.getSources());
1622}
1623
Arthur Hungaab25622020-01-16 11:22:11 +08001624TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1625 NotifyMotionArgs args;
1626 const Point centerPoint = mDevice->getCenterPoint();
1627
1628 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001629 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001630 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001631 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001632 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1633 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1634
1635 // ACTION_MOVE
1636 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001637 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001638 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1639 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1640
1641 // ACTION_UP
1642 mDevice->sendUp();
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_UP, args.action);
1646}
1647
1648TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1649 NotifyMotionArgs args;
1650 const Point centerPoint = mDevice->getCenterPoint();
1651
1652 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001653 mDevice->sendSlot(FIRST_SLOT);
1654 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001655 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001656 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001657 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1658 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1659
1660 // ACTION_POINTER_DOWN (Second slot)
1661 const Point secondPoint = centerPoint + Point(100, 100);
1662 mDevice->sendSlot(SECOND_SLOT);
1663 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001664 mDevice->sendDown(secondPoint);
1665 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001666 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001667 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001668
1669 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001670 mDevice->sendMove(secondPoint + Point(1, 1));
1671 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001672 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1673 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1674
1675 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001676 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001677 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001678 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001679 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001680
1681 // ACTION_UP
1682 mDevice->sendSlot(FIRST_SLOT);
1683 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001684 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001685 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1686 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1687}
1688
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001689/**
1690 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1691 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1692 * data?
1693 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1694 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1695 * for Pointer 0 only is generated after.
1696 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1697 * events, we will not miss any information.
1698 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1699 * event generated afterwards that contains the newest movement of pointer 0.
1700 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1701 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1702 * losing information about non-palm pointers.
1703 */
1704TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1705 NotifyMotionArgs args;
1706 const Point centerPoint = mDevice->getCenterPoint();
1707
1708 // ACTION_DOWN
1709 mDevice->sendSlot(FIRST_SLOT);
1710 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1711 mDevice->sendDown(centerPoint);
1712 mDevice->sendSync();
1713 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1714
1715 // ACTION_POINTER_DOWN (Second slot)
1716 const Point secondPoint = centerPoint + Point(100, 100);
1717 mDevice->sendSlot(SECOND_SLOT);
1718 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1719 mDevice->sendDown(secondPoint);
1720 mDevice->sendSync();
1721 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1722
1723 // ACTION_MOVE (First slot)
1724 mDevice->sendSlot(FIRST_SLOT);
1725 mDevice->sendMove(centerPoint + Point(5, 5));
1726 // ACTION_POINTER_UP (Second slot)
1727 mDevice->sendSlot(SECOND_SLOT);
1728 mDevice->sendPointerUp();
1729 // Send a single sync for the above 2 pointer updates
1730 mDevice->sendSync();
1731
1732 // First, we should get POINTER_UP for the second pointer
1733 assertReceivedMotion(ACTION_POINTER_1_UP,
1734 {/*first pointer */ centerPoint + Point(5, 5),
1735 /*second pointer*/ secondPoint});
1736
1737 // Next, the MOVE event for the first pointer
1738 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1739}
1740
1741/**
1742 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1743 * move, and then it will go up, all in the same frame.
1744 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1745 * gets sent to the listener.
1746 */
1747TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1748 NotifyMotionArgs args;
1749 const Point centerPoint = mDevice->getCenterPoint();
1750
1751 // ACTION_DOWN
1752 mDevice->sendSlot(FIRST_SLOT);
1753 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1754 mDevice->sendDown(centerPoint);
1755 mDevice->sendSync();
1756 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1757
1758 // ACTION_POINTER_DOWN (Second slot)
1759 const Point secondPoint = centerPoint + Point(100, 100);
1760 mDevice->sendSlot(SECOND_SLOT);
1761 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1762 mDevice->sendDown(secondPoint);
1763 mDevice->sendSync();
1764 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1765
1766 // ACTION_MOVE (First slot)
1767 mDevice->sendSlot(FIRST_SLOT);
1768 mDevice->sendMove(centerPoint + Point(5, 5));
1769 // ACTION_POINTER_UP (Second slot)
1770 mDevice->sendSlot(SECOND_SLOT);
1771 mDevice->sendMove(secondPoint + Point(6, 6));
1772 mDevice->sendPointerUp();
1773 // Send a single sync for the above 2 pointer updates
1774 mDevice->sendSync();
1775
1776 // First, we should get POINTER_UP for the second pointer
1777 // The movement of the second pointer during the liftoff frame is ignored.
1778 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1779 assertReceivedMotion(ACTION_POINTER_1_UP,
1780 {/*first pointer */ centerPoint + Point(5, 5),
1781 /*second pointer*/ secondPoint});
1782
1783 // Next, the MOVE event for the first pointer
1784 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1785}
1786
Arthur Hungaab25622020-01-16 11:22:11 +08001787TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1788 NotifyMotionArgs args;
1789 const Point centerPoint = mDevice->getCenterPoint();
1790
1791 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001792 mDevice->sendSlot(FIRST_SLOT);
1793 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001794 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001795 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001796 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1797 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1798
arthurhungcc7f9802020-04-30 17:55:40 +08001799 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001800 const Point secondPoint = centerPoint + Point(100, 100);
1801 mDevice->sendSlot(SECOND_SLOT);
1802 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1803 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001804 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001805 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001806 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001807
arthurhungcc7f9802020-04-30 17:55:40 +08001808 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001809 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001810 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001811 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1812 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1813
arthurhungcc7f9802020-04-30 17:55:40 +08001814 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1815 // a palm event.
1816 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001817 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001818 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001819 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001820 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001821 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001822
arthurhungcc7f9802020-04-30 17:55:40 +08001823 // Send up to second slot, expect first slot send moving.
1824 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001825 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001826 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1827 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001828
arthurhungcc7f9802020-04-30 17:55:40 +08001829 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001830 mDevice->sendSlot(FIRST_SLOT);
1831 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001832 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001833
arthurhungcc7f9802020-04-30 17:55:40 +08001834 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1835 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001836}
1837
Prabir Pradhanda20b172022-09-26 17:01:18 +00001838TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
1839 const Point centerPoint = mDevice->getCenterPoint();
1840
1841 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1842 mDevice->sendSlot(FIRST_SLOT);
1843 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1844 mDevice->sendToolType(MT_TOOL_PEN);
1845 mDevice->sendDown(centerPoint);
1846 mDevice->sendSync();
1847 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1848 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1849 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
1850
1851 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1852
1853 // Release the stylus touch.
1854 mDevice->sendUp();
1855 mDevice->sendSync();
1856 ASSERT_NO_FATAL_FAILURE(
1857 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1858
1859 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1860
1861 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1862 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1863 mDevice->sendToolType(MT_TOOL_FINGER);
1864 mDevice->sendDown(centerPoint);
1865 mDevice->sendSync();
1866 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1867 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1868 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
1869
1870 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1871
1872 mDevice->sendUp();
1873 mDevice->sendSync();
1874 ASSERT_NO_FATAL_FAILURE(
1875 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1876
1877 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1878 // The policy should be notified of the stylus presence.
1879 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1880 mDevice->sendToolType(MT_TOOL_PEN);
1881 mDevice->sendMove(centerPoint);
1882 mDevice->sendSync();
1883 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1884 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1885 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
1886
1887 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1888}
1889
Prabir Pradhan124ea442022-10-28 20:27:44 +00001890// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001891
Prabir Pradhan124ea442022-10-28 20:27:44 +00001892// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1893// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1894// stylus.
1895template <typename UinputStylusDevice>
1896class StylusButtonIntegrationTest : public TouchIntegrationTest {
1897protected:
1898 void SetUp() override {
1899#if !defined(__ANDROID__)
1900 GTEST_SKIP();
1901#endif
1902 TouchIntegrationTest::SetUp();
1903 mTouchscreen = mDevice.get();
1904 mTouchscreenInfo = mDeviceInfo;
1905
1906 setUpStylusDevice();
1907 }
1908
1909 UinputStylusDevice* mStylus{nullptr};
1910 InputDeviceInfo mStylusInfo{};
1911
1912 UinputTouchScreen* mTouchscreen{nullptr};
1913 InputDeviceInfo mTouchscreenInfo{};
1914
1915private:
1916 // When we are attempting to test stylus button events that are sent from the touchscreen,
1917 // use the same Uinput device for the touchscreen and the stylus.
1918 template <typename T = UinputStylusDevice>
1919 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1920 mStylus = mDevice.get();
1921 mStylusInfo = mDeviceInfo;
1922 }
1923
1924 // When we are attempting to stylus buttons from an external stylus being merged with touches
1925 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1926 template <typename T = UinputStylusDevice>
1927 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1928 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1929 mStylus = mStylusDeviceLifecycleTracker.get();
1930 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1931 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1932 const auto info = findDeviceByName(mStylus->getName());
1933 ASSERT_TRUE(info);
1934 mStylusInfo = *info;
1935 }
1936
1937 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1938
1939 // Hide the base class's device to expose it with a different name for readability.
1940 using TouchIntegrationTest::mDevice;
1941 using TouchIntegrationTest::mDeviceInfo;
1942};
1943
1944using StylusButtonIntegrationTestTypes =
1945 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1946TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1947
1948TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
1949 const auto stylusId = TestFixture::mStylusInfo.getId();
1950
1951 TestFixture::mStylus->pressKey(BTN_STYLUS);
1952 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1953 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1954 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1955
1956 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1957 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001958 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001959 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001960}
1961
Prabir Pradhan124ea442022-10-28 20:27:44 +00001962TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
1963 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1964 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1965 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001966
1967 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001968 TestFixture::mStylus->pressKey(BTN_STYLUS);
1969 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001970 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001971 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001972
1973 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001974 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1975 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1976 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1977 TestFixture::mTouchscreen->sendDown(centerPoint);
1978 TestFixture::mTouchscreen->sendSync();
1979 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001980 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1981 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001982 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1983 WithDeviceId(touchscreenId))));
1984 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001985 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1986 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001987 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1988 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001989
Prabir Pradhan124ea442022-10-28 20:27:44 +00001990 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1991 TestFixture::mTouchscreen->sendSync();
1992 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001993 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001994 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1995 WithDeviceId(touchscreenId))));
1996 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001997 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001998 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1999 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002000
2001 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002002 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2003 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002004 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002005 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002006}
2007
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002008TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
2009 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2010 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2011 const auto stylusId = TestFixture::mStylusInfo.getId();
2012 auto toolTypeDevice =
2013 AllOf(WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithDeviceId(touchscreenId));
2014
2015 // Press the stylus button.
2016 TestFixture::mStylus->pressKey(BTN_STYLUS);
2017 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2018 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2019 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2020
2021 // Start hovering with the stylus.
2022 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2023 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2024 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2025 TestFixture::mTouchscreen->sendMove(centerPoint);
2026 TestFixture::mTouchscreen->sendSync();
2027 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2028 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2029 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2030 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2031 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2032 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2033 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2034 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2035 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2036
2037 // Touch down with the stylus.
2038 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2039 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2040 TestFixture::mTouchscreen->sendDown(centerPoint);
2041 TestFixture::mTouchscreen->sendSync();
2042 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2043 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2044 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2045
2046 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2047 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2048 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2049
2050 // Stop touching with the stylus, and start hovering.
2051 TestFixture::mTouchscreen->sendUp();
2052 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2053 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2054 TestFixture::mTouchscreen->sendMove(centerPoint);
2055 TestFixture::mTouchscreen->sendSync();
2056 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2057 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2058 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2059 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2060 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2061 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2062 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2063 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2064 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2065
2066 // Stop hovering.
2067 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2068 TestFixture::mTouchscreen->sendSync();
2069 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2070 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2071 WithButtonState(0))));
2072 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2073 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2074 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2075 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2076
2077 // Release the stylus button.
2078 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2079 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2080 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2081 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2082}
2083
Prabir Pradhan124ea442022-10-28 20:27:44 +00002084TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
2085 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2086 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2087 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002088
2089 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002090 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2091 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2092 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2093 TestFixture::mTouchscreen->sendDown(centerPoint);
2094 TestFixture::mTouchscreen->sendSync();
2095 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002096 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002097 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2098 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002099
2100 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002101 TestFixture::mStylus->pressKey(BTN_STYLUS);
2102 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002103 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002104 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2105 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002106 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2107 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002108 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2109 WithDeviceId(touchscreenId))));
2110 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002111 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2112 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002113 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2114 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002115
Prabir Pradhan124ea442022-10-28 20:27:44 +00002116 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2117 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002118 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002119 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2120 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002121 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002122 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2123 WithDeviceId(touchscreenId))));
2124 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002125 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002126 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2127 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002128
2129 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002130 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2131 TestFixture::mTouchscreen->sendSync();
2132 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002133 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002134 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2135 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002136}
2137
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002138// --- ExternalStylusIntegrationTest ---
2139
2140// Verify the behavior of an external stylus. An external stylus can report pressure or button
2141// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2142// ongoing stylus gesture that is being emitted by the touchscreen.
2143using ExternalStylusIntegrationTest = TouchIntegrationTest;
2144
2145TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
2146 const Point centerPoint = mDevice->getCenterPoint();
2147
2148 // Create an external stylus capable of reporting pressure data that
2149 // should be fused with a touch pointer.
2150 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2151 createUinputDevice<UinputExternalStylusWithPressure>();
2152 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2153 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2154 const auto stylusInfo = findDeviceByName(stylus->getName());
2155 ASSERT_TRUE(stylusInfo);
2156
2157 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2158
2159 const auto touchscreenId = mDeviceInfo.getId();
2160
2161 // Set a pressure value on the stylus. It doesn't generate any events.
2162 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2163 stylus->setPressure(100);
2164 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2165
2166 // Start a finger gesture, and ensure it shows up as stylus gesture
2167 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002168 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002169 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002170 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002171 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002172 mDevice->sendSync();
2173 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2174 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002175 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2176 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002177
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002178 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2179 // event with the updated pressure.
2180 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002181 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2182 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002183 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2184 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002185
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002186 // The external stylus did not generate any events.
2187 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2188 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2189}
2190
2191TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
2192 const Point centerPoint = mDevice->getCenterPoint();
2193
2194 // Create an external stylus capable of reporting pressure data that
2195 // should be fused with a touch pointer.
2196 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2197 createUinputDevice<UinputExternalStylusWithPressure>();
2198 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2199 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2200 const auto stylusInfo = findDeviceByName(stylus->getName());
2201 ASSERT_TRUE(stylusInfo);
2202
2203 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2204
2205 const auto touchscreenId = mDeviceInfo.getId();
2206
2207 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2208 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002209 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2210 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002211 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002212 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002213
2214 // Start a finger gesture. The touch device will withhold generating any touches for
2215 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2216 mDevice->sendSlot(FIRST_SLOT);
2217 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2218 mDevice->sendToolType(MT_TOOL_FINGER);
2219 mDevice->sendDown(centerPoint);
2220 auto waitUntil = std::chrono::system_clock::now() +
2221 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002222 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002223 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002224
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002225 // Since the external stylus did not report a pressure value within the timeout,
2226 // it shows up as a finger pointer.
2227 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2228 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2229 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithDeviceId(touchscreenId),
2230 WithPressure(1.f))));
2231
2232 // Change the pressure on the external stylus. Since the pressure was not present at the start
2233 // of the gesture, it is ignored for now.
2234 stylus->setPressure(200);
2235 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2236
2237 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002238 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2239 mDevice->sendSync();
2240 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2241 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002242 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2243
2244 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2245 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2246 mDevice->sendToolType(MT_TOOL_FINGER);
2247 mDevice->sendDown(centerPoint);
2248 mDevice->sendSync();
2249 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2250 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2251 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2252 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2253
2254 // The external stylus did not generate any events.
2255 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2256 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002257}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002258
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002259TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
2260 const Point centerPoint = mDevice->getCenterPoint();
2261
2262 // Create an external stylus device that does not support pressure. It should not affect any
2263 // touch pointers.
2264 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2265 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2266 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2267 const auto stylusInfo = findDeviceByName(stylus->getName());
2268 ASSERT_TRUE(stylusInfo);
2269
2270 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2271
2272 const auto touchscreenId = mDeviceInfo.getId();
2273
2274 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2275 // pressure data from the external stylus.
2276 mDevice->sendSlot(FIRST_SLOT);
2277 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2278 mDevice->sendToolType(MT_TOOL_FINGER);
2279 mDevice->sendDown(centerPoint);
2280 auto waitUntil = std::chrono::system_clock::now() +
2281 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2282 mDevice->sendSync();
2283 ASSERT_NO_FATAL_FAILURE(
2284 mTestListener
2285 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2286 WithToolType(
2287 AMOTION_EVENT_TOOL_TYPE_FINGER),
2288 WithButtonState(0),
2289 WithDeviceId(touchscreenId),
2290 WithPressure(1.f)),
2291 waitUntil));
2292
2293 // The external stylus did not generate any events.
2294 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2295 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2296}
2297
Michael Wrightd02c5b62014-02-10 15:10:22 -08002298// --- InputDeviceTest ---
2299class InputDeviceTest : public testing::Test {
2300protected:
2301 static const char* DEVICE_NAME;
2302 static const char* DEVICE_LOCATION;
2303 static const int32_t DEVICE_ID;
2304 static const int32_t DEVICE_GENERATION;
2305 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002306 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002307 static const int32_t EVENTHUB_ID;
2308 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2309
2310 std::shared_ptr<FakeEventHub> mFakeEventHub;
2311 sp<FakeInputReaderPolicy> mFakePolicy;
2312 std::unique_ptr<TestInputListener> mFakeListener;
2313 std::unique_ptr<InstrumentedInputReader> mReader;
2314 std::shared_ptr<InputDevice> mDevice;
2315
2316 void SetUp() override {
2317 mFakeEventHub = std::make_unique<FakeEventHub>();
2318 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2319 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002320 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002321 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002322 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002323 identifier.name = DEVICE_NAME;
2324 identifier.location = DEVICE_LOCATION;
2325 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2326 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2327 identifier);
2328 mReader->pushNextDevice(mDevice);
2329 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002330 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002331 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002332
2333 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002334 mFakeListener.reset();
2335 mFakePolicy.clear();
2336 }
2337};
2338
2339const char* InputDeviceTest::DEVICE_NAME = "device";
2340const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2341const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2342const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002343const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002344const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2345 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002346const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002347const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2348
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002349TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002350 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002351 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2352 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002353}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002354
Michael Wrightd02c5b62014-02-10 15:10:22 -08002355TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002356 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2357
Michael Wrightd02c5b62014-02-10 15:10:22 -08002358 // Configuration
2359 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002360 InputReaderConfiguration config;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002361 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2362
Michael Wrightd02c5b62014-02-10 15:10:22 -08002363 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2364}
2365
2366TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2367 ASSERT_EQ(mDevice->isEnabled(), false);
2368}
2369
2370TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2371 // Configuration.
2372 InputReaderConfiguration config;
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002373 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002374
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002375 // Reset.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002376 unused += mDevice->reset(ARBITRARY_TIME);
2377
2378 NotifyDeviceResetArgs resetArgs;
2379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2380 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2381 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2382
2383 // Metadata.
2384 ASSERT_TRUE(mDevice->isIgnored());
2385 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2386
2387 InputDeviceInfo info = mDevice->getDeviceInfo();
2388 ASSERT_EQ(DEVICE_ID, info.getId());
2389 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2390 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2391 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2392
2393 // State queries.
2394 ASSERT_EQ(0, mDevice->getMetaState());
2395
2396 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002397 << "Ignored device should return unknown key code state.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002398 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002399 << "Ignored device should return unknown scan code state.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002400 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2401 << "Ignored device should return unknown switch state.";
2402
2403 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2404 uint8_t flags[2] = { 0, 1 };
2405 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002406 << "Ignored device should never mark any key codes.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002407 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2408 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2409}
2410
2411TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2412 // Configuration.
2413 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2414
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002415 FakeInputMapper& mapper1 =
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002416 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002417 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002418 mapper1.setMetaState(AMETA_ALT_ON);
2419 mapper1.addSupportedKeyCode(AKEYCODE_A);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002420 mapper1.addSupportedKeyCode(AKEYCODE_B);
2421 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002422 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2423 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2424 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2425 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002426
Arthur Hung2c9a3342019-07-23 14:18:59 +08002427 FakeInputMapper& mapper2 =
2428 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002429 mapper2.setMetaState(AMETA_SHIFT_ON);
2430
Arthur Hung2c9a3342019-07-23 14:18:59 +08002431 InputReaderConfiguration config;
2432 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2433
2434 std::string propertyValue;
2435 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
2436 << "Device should have read configuration during configuration phase.";
2437 ASSERT_EQ("value", propertyValue);
2438
2439 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002440 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
2441
Arthur Hung2c9a3342019-07-23 14:18:59 +08002442 // Reset
2443 unused += mDevice->reset(ARBITRARY_TIME);
2444 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2445 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
2446
2447 NotifyDeviceResetArgs resetArgs;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2449 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002450 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2451
Arthur Hung2c9a3342019-07-23 14:18:59 +08002452 // Metadata.
2453 ASSERT_FALSE(mDevice->isIgnored());
2454 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2455
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002456 InputDeviceInfo info = mDevice->getDeviceInfo();
2457 ASSERT_EQ(DEVICE_ID, info.getId());
Arthur Hung2c9a3342019-07-23 14:18:59 +08002458 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2459 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2460 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002461
2462 // State queries.
Arthur Hung2c9a3342019-07-23 14:18:59 +08002463 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2464 << "Should query mappers and combine meta states.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002465
Christine Franks1ba71cc2021-04-07 14:37:42 -07002466 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2467 << "Should return unknown key code state when source not supported.";
2468 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2469 << "Should return unknown scan code state when source not supported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002470 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2471 << "Should return unknown switch state when source not supported.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002472
2473 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2474 << "Should query mapper when source is supported.";
2475 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
Christine Franks2a2293c2022-01-18 11:51:16 -08002476 << "Should query mapper when source is supported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002477 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2478 << "Should query mapper when source is supported.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002479
2480 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
2481 uint8_t flags[4] = { 0, 0, 0, 1 };
2482 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
2483 << "Should do nothing when source is unsupported.";
2484 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002485 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2486 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002487 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2488
2489 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2490 << "Should query mapper when source is supported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002491 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2492 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002493 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2494 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2495
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002496 // Event handling.
2497 RawEvent event;
Christine Franks1ba71cc2021-04-07 14:37:42 -07002498 event.deviceId = EVENTHUB_ID;
2499 unused += mDevice->process(&event, 1);
2500
Christine Franks2a2293c2022-01-18 11:51:16 -08002501 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2502 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
2503}
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002504
2505// A single input device is associated with a specific display. Check that:
Christine Franks2a2293c2022-01-18 11:51:16 -08002506// 1. Device is disabled if the viewport corresponding to the associated display is not found
Christine Franks2a2293c2022-01-18 11:51:16 -08002507// 2. Device is disabled when setEnabled API is called
2508TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
2509 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
2510
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002511 // First Configuration.
2512 std::list<NotifyArgs> unused =
Christine Franks2a2293c2022-01-18 11:51:16 -08002513 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2514
2515 // Device should be enabled by default.
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002516 ASSERT_TRUE(mDevice->isEnabled());
2517
2518 // Prepare associated info.
2519 constexpr uint8_t hdmi = 1;
2520 const std::string UNIQUE_ID = "local:1";
2521
2522 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2523 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2524 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2525 // Device should be disabled because it is associated with a specific display via
2526 // input port <-> display port association, but the corresponding display is not found
2527 ASSERT_FALSE(mDevice->isEnabled());
2528
2529 // Prepare displays.
2530 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002531 ui::ROTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002532 ViewportType::INTERNAL);
2533 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2534 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2535 ASSERT_TRUE(mDevice->isEnabled());
2536
2537 // Device should be disabled after set disable.
2538 mFakePolicy->addDisabledDevice(mDevice->getId());
2539 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2540 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2541 ASSERT_FALSE(mDevice->isEnabled());
2542
2543 // Device should still be disabled even found the associated display.
2544 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2545 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2546 ASSERT_FALSE(mDevice->isEnabled());
2547}
2548
2549TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2550 // Device should be enabled by default.
2551 mFakePolicy->clearViewports();
2552 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2553 std::list<NotifyArgs> unused =
2554 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2555 ASSERT_TRUE(mDevice->isEnabled());
2556
2557 // Device should be disabled because it is associated with a specific display, but the
2558 // corresponding display is not found.
2559 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2560 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2561 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2562 ASSERT_FALSE(mDevice->isEnabled());
2563
2564 // Device should be enabled when a display is found.
2565 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002566 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002567 NO_PORT, ViewportType::INTERNAL);
2568 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2569 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2570 ASSERT_TRUE(mDevice->isEnabled());
2571
2572 // Device should be disabled after set disable.
2573 mFakePolicy->addDisabledDevice(mDevice->getId());
2574 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2575 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2576 ASSERT_FALSE(mDevice->isEnabled());
2577
2578 // Device should still be disabled even found the associated display.
2579 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2580 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2581 ASSERT_FALSE(mDevice->isEnabled());
2582}
2583
2584TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2585 mFakePolicy->clearViewports();
2586 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2587 std::list<NotifyArgs> unused =
2588 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002589
Michael Wrightd02c5b62014-02-10 15:10:22 -08002590 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2591 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002592 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Chris Ye1b0c7342020-07-28 21:57:03 -07002593 NO_PORT, ViewportType::INTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002594 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002595 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002596 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002597}
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002598
arthurhungdcef2dc2020-08-11 14:47:50 +08002599/**
2600 * This test reproduces a crash caused by a dangling reference that remains after device is added
Michael Wrightd02c5b62014-02-10 15:10:22 -08002601 * and removed. The reference is accessed in InputDevice::dump(..);
Chris Ye1b0c7342020-07-28 21:57:03 -07002602 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002603TEST_F(InputDeviceTest, DumpDoesNotCrash) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002604 constexpr int32_t TEST_EVENTHUB_ID = 10;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002605 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
arthurhungdcef2dc2020-08-11 14:47:50 +08002606
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002607 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002608 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002609 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2610 std::string dumpStr, eventHubDevStr;
2611 device.dump(dumpStr, eventHubDevStr);
2612}
2613
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002614TEST_F(InputDeviceTest, GetBluetoothAddress) {
2615 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2616 ASSERT_TRUE(address);
2617 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2618}
2619
Michael Wrightd02c5b62014-02-10 15:10:22 -08002620// --- InputMapperTest ---
2621
2622class InputMapperTest : public testing::Test {
2623protected:
2624 static const char* DEVICE_NAME;
2625 static const char* DEVICE_LOCATION;
2626 static const int32_t DEVICE_ID;
2627 static const int32_t DEVICE_GENERATION;
2628 static const int32_t DEVICE_CONTROLLER_NUMBER;
2629 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
2630 static const int32_t EVENTHUB_ID;
2631
2632 std::shared_ptr<FakeEventHub> mFakeEventHub;
2633 sp<FakeInputReaderPolicy> mFakePolicy;
2634 std::unique_ptr<TestInputListener> mFakeListener;
2635 std::unique_ptr<InstrumentedInputReader> mReader;
2636 std::shared_ptr<InputDevice> mDevice;
2637
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00002638 virtual void SetUp(ftl::Flags<InputDeviceClass> classes, int bus = 0) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002639 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002640 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08002641 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002642 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2643 *mFakeListener);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00002644 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes, bus);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002645 // Consume the device reset notification generated when adding a new device.
2646 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002647 }
2648
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002649 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002650 SetUp(DEVICE_CLASSES);
2651 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002652
Chris Yea52ade12020-08-27 16:49:20 -07002653 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002654 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002655 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002656 }
2657
2658 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002659 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002660 }
2661
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002662 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002663 if (!changes ||
2664 (changes &
2665 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2666 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002667 mReader->requestRefreshConfiguration(changes);
2668 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002669 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002670 std::list<NotifyArgs> out =
2671 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002672 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002673 for (const NotifyArgs& args : out) {
2674 mFakeListener->notify(args);
2675 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002676 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002677 return out;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002678 }
2679
arthurhungdcef2dc2020-08-11 14:47:50 +08002680 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2681 const std::string& location, int32_t eventHubId,
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00002682 ftl::Flags<InputDeviceClass> classes, int bus = 0) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002683 InputDeviceIdentifier identifier;
2684 identifier.name = name;
2685 identifier.location = location;
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00002686 identifier.bus = bus;
arthurhungdcef2dc2020-08-11 14:47:50 +08002687 std::shared_ptr<InputDevice> device =
2688 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2689 identifier);
2690 mReader->pushNextDevice(device);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00002691 mFakeEventHub->addDevice(eventHubId, name, classes, bus);
arthurhungdcef2dc2020-08-11 14:47:50 +08002692 mReader->loopOnce();
2693 return device;
2694 }
2695
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002696 template <class T, typename... Args>
2697 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002698 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002699 configureDevice(0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002700 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
2701 resetArgList += mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002702 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002703 for (const NotifyArgs& loopArgs : resetArgList) {
2704 mFakeListener->notify(loopArgs);
2705 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002706 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002707 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708 }
2709
2710 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002711 ui::Rotation orientation, const std::string& uniqueId,
2712 std::optional<uint8_t> physicalPort,
2713 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002714 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2715 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002716 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2717 }
2718
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002719 void clearViewports() {
2720 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002721 }
2722
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002723 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
2724 int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002725 RawEvent event;
2726 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002727 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002728 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002729 event.type = type;
2730 event.code = code;
2731 event.value = value;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002732 std::list<NotifyArgs> processArgList = mapper.process(&event);
2733 for (const NotifyArgs& args : processArgList) {
2734 mFakeListener->notify(args);
2735 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002736 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08002737 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002738 return processArgList;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002739 }
2740
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00002741 void resetMapper(InputMapper& mapper, nsecs_t when) {
2742 const auto resetArgs = mapper.reset(when);
Michael Wrighta9cf4192022-12-01 23:46:39 +00002743 for (const auto& args : resetArgs) {
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00002744 mFakeListener->notify(args);
2745 }
2746 // Loop the reader to flush the input listener queue.
2747 mReader->loopOnce();
2748 }
2749
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00002750 std::list<NotifyArgs> handleTimeout(InputMapper& mapper, nsecs_t when) {
2751 std::list<NotifyArgs> generatedArgs = mapper.timeoutExpired(when);
2752 for (const NotifyArgs& args : generatedArgs) {
2753 mFakeListener->notify(args);
2754 }
2755 // Loop the reader to flush the input listener queue.
2756 mReader->loopOnce();
2757 return generatedArgs;
2758 }
2759
Michael Wrightd02c5b62014-02-10 15:10:22 -08002760 static void assertMotionRange(const InputDeviceInfo& info,
2761 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2762 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002763 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2765 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2766 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2767 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2768 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2769 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2770 }
2771
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002772 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2773 float size, float touchMajor, float touchMinor, float toolMajor,
2774 float toolMinor, float orientation, float distance,
2775 float scaledAxisEpsilon = 1.f) {
2776 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2777 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002778 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2779 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002780 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2781 scaledAxisEpsilon);
2782 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2783 scaledAxisEpsilon);
2784 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2785 scaledAxisEpsilon);
2786 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2787 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002788 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2789 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2790 }
2791
Michael Wright17db18e2020-06-26 20:51:44 +01002792 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002793 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002794 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002795 ASSERT_NEAR(x, actualX, 1);
2796 ASSERT_NEAR(y, actualY, 1);
2797 }
2798};
2799
2800const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002801const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002802const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002803const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2804const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002805const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2806 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002807const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808
2809// --- SwitchInputMapperTest ---
2810
2811class SwitchInputMapperTest : public InputMapperTest {
2812protected:
2813};
2814
2815TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002816 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002817
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002818 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002819}
2820
2821TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002822 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002823
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002824 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002825 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002826
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002827 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002828 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002829}
2830
2831TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002832 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002833 std::list<NotifyArgs> out;
2834 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2835 ASSERT_TRUE(out.empty());
2836 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2837 ASSERT_TRUE(out.empty());
2838 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2839 ASSERT_TRUE(out.empty());
2840 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002841
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002842 ASSERT_EQ(1u, out.size());
2843 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002844 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002845 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2846 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002847 args.switchMask);
2848 ASSERT_EQ(uint32_t(0), args.policyFlags);
2849}
2850
Chris Ye87143712020-11-10 05:05:58 +00002851// --- VibratorInputMapperTest ---
2852class VibratorInputMapperTest : public InputMapperTest {
2853protected:
2854 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2855};
2856
2857TEST_F(VibratorInputMapperTest, GetSources) {
2858 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2859
2860 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2861}
2862
2863TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2864 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2865
2866 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2867}
2868
2869TEST_F(VibratorInputMapperTest, Vibrate) {
2870 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002871 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002872 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2873
2874 VibrationElement pattern(2);
2875 VibrationSequence sequence(2);
2876 pattern.duration = std::chrono::milliseconds(200);
2877 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2878 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2879 sequence.addElement(pattern);
2880 pattern.duration = std::chrono::milliseconds(500);
2881 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2882 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2883 sequence.addElement(pattern);
2884
2885 std::vector<int64_t> timings = {0, 1};
2886 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2887
2888 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002889 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002890 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002891 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002892 // Verify vibrator state listener was notified.
2893 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002894 ASSERT_EQ(1u, out.size());
2895 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2896 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2897 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002898 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002899 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002900 ASSERT_FALSE(mapper.isVibrating());
2901 // Verify vibrator state listener was notified.
2902 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002903 ASSERT_EQ(1u, out.size());
2904 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2905 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2906 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002907}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002908
Chris Yef59a2f42020-10-16 12:55:26 -07002909// --- SensorInputMapperTest ---
2910
2911class SensorInputMapperTest : public InputMapperTest {
2912protected:
2913 static const int32_t ACCEL_RAW_MIN;
2914 static const int32_t ACCEL_RAW_MAX;
2915 static const int32_t ACCEL_RAW_FUZZ;
2916 static const int32_t ACCEL_RAW_FLAT;
2917 static const int32_t ACCEL_RAW_RESOLUTION;
2918
2919 static const int32_t GYRO_RAW_MIN;
2920 static const int32_t GYRO_RAW_MAX;
2921 static const int32_t GYRO_RAW_FUZZ;
2922 static const int32_t GYRO_RAW_FLAT;
2923 static const int32_t GYRO_RAW_RESOLUTION;
2924
2925 static const float GRAVITY_MS2_UNIT;
2926 static const float DEGREE_RADIAN_UNIT;
2927
2928 void prepareAccelAxes();
2929 void prepareGyroAxes();
2930 void setAccelProperties();
2931 void setGyroProperties();
2932 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2933};
2934
2935const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2936const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2937const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2938const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2939const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2940
2941const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2942const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2943const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2944const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2945const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2946
2947const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2948const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2949
2950void SensorInputMapperTest::prepareAccelAxes() {
2951 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2952 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2953 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2954 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2955 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2956 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2957}
2958
2959void SensorInputMapperTest::prepareGyroAxes() {
2960 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2961 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2962 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2963 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2964 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2965 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2966}
2967
2968void SensorInputMapperTest::setAccelProperties() {
2969 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2970 /* sensorDataIndex */ 0);
2971 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2972 /* sensorDataIndex */ 1);
2973 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2974 /* sensorDataIndex */ 2);
2975 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2976 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2977 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2978 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2979 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2980}
2981
2982void SensorInputMapperTest::setGyroProperties() {
2983 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2984 /* sensorDataIndex */ 0);
2985 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2986 /* sensorDataIndex */ 1);
2987 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2988 /* sensorDataIndex */ 2);
2989 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2990 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2991 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2992 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2993 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2994}
2995
2996TEST_F(SensorInputMapperTest, GetSources) {
2997 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2998
2999 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3000}
3001
3002TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3003 setAccelProperties();
3004 prepareAccelAxes();
3005 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3006
3007 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3008 std::chrono::microseconds(10000),
3009 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003010 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003011 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3013 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3015 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003016
3017 NotifySensorArgs args;
3018 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3019 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3020 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3021
3022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3023 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3024 ASSERT_EQ(args.deviceId, DEVICE_ID);
3025 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3026 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3027 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3028 ASSERT_EQ(args.values, values);
3029 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3030}
3031
3032TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3033 setGyroProperties();
3034 prepareGyroAxes();
3035 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3036
3037 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3038 std::chrono::microseconds(10000),
3039 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003040 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003041 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3042 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003046
3047 NotifySensorArgs args;
3048 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3049 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3050 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3051
3052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3053 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3054 ASSERT_EQ(args.deviceId, DEVICE_ID);
3055 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3056 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3057 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3058 ASSERT_EQ(args.values, values);
3059 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3060}
3061
Michael Wrightd02c5b62014-02-10 15:10:22 -08003062// --- KeyboardInputMapperTest ---
3063
3064class KeyboardInputMapperTest : public InputMapperTest {
3065protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003066 const std::string UNIQUE_ID = "local:0";
3067
Michael Wrighta9cf4192022-12-01 23:46:39 +00003068 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003069
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003070 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003071 int32_t originalKeyCode, int32_t rotatedKeyCode,
3072 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073};
3074
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003075/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3076 * orientation.
3077 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003078void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003079 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3080 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003081}
3082
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003083void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003084 int32_t originalScanCode, int32_t originalKeyCode,
3085 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086 NotifyKeyArgs args;
3087
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003088 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3090 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3091 ASSERT_EQ(originalScanCode, args.scanCode);
3092 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003093 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003095 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3097 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3098 ASSERT_EQ(originalScanCode, args.scanCode);
3099 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003100 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003101}
3102
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003104 KeyboardInputMapper& mapper =
3105 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3106 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003107
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003108 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003109}
3110
3111TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3112 const int32_t USAGE_A = 0x070004;
3113 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003114 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3115 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003116 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3117 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3118 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003119
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003120 KeyboardInputMapper& mapper =
3121 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3122 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003123 // Initial metastate is AMETA_NONE.
3124 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125
3126 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003127 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128 NotifyKeyArgs args;
3129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3130 ASSERT_EQ(DEVICE_ID, args.deviceId);
3131 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3132 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3133 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3134 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3135 ASSERT_EQ(KEY_HOME, args.scanCode);
3136 ASSERT_EQ(AMETA_NONE, args.metaState);
3137 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3138 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3139 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3140
3141 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003142 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3144 ASSERT_EQ(DEVICE_ID, args.deviceId);
3145 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3146 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3147 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3148 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3149 ASSERT_EQ(KEY_HOME, args.scanCode);
3150 ASSERT_EQ(AMETA_NONE, args.metaState);
3151 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3152 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3153 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3154
3155 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3159 ASSERT_EQ(DEVICE_ID, args.deviceId);
3160 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3161 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3162 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3163 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3164 ASSERT_EQ(0, args.scanCode);
3165 ASSERT_EQ(AMETA_NONE, args.metaState);
3166 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3167 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3168 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3169
3170 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3172 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3174 ASSERT_EQ(DEVICE_ID, args.deviceId);
3175 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3176 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3177 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3178 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3179 ASSERT_EQ(0, args.scanCode);
3180 ASSERT_EQ(AMETA_NONE, args.metaState);
3181 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3182 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3183 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3184
3185 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3189 ASSERT_EQ(DEVICE_ID, args.deviceId);
3190 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3191 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3192 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3193 ASSERT_EQ(0, args.keyCode);
3194 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3195 ASSERT_EQ(AMETA_NONE, args.metaState);
3196 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3197 ASSERT_EQ(0U, args.policyFlags);
3198 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3199
3200 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3202 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3204 ASSERT_EQ(DEVICE_ID, args.deviceId);
3205 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3206 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3207 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3208 ASSERT_EQ(0, args.keyCode);
3209 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3210 ASSERT_EQ(AMETA_NONE, args.metaState);
3211 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3212 ASSERT_EQ(0U, args.policyFlags);
3213 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3214}
3215
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003216/**
3217 * Ensure that the readTime is set to the time when the EV_KEY is received.
3218 */
3219TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3220 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3221
3222 KeyboardInputMapper& mapper =
3223 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3224 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3225 NotifyKeyArgs args;
3226
3227 // Key down
3228 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3230 ASSERT_EQ(12, args.readTime);
3231
3232 // Key up
3233 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3235 ASSERT_EQ(15, args.readTime);
3236}
3237
Michael Wrightd02c5b62014-02-10 15:10:22 -08003238TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003239 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3240 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003241 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3242 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3243 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003245 KeyboardInputMapper& mapper =
3246 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3247 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248
Arthur Hung95f68612022-04-07 14:08:22 +08003249 // Initial metastate is AMETA_NONE.
3250 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251
3252 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003254 NotifyKeyArgs args;
3255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3256 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003257 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003258 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259
3260 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003261 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3263 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003264 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003265
3266 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003267 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3269 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003270 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271
3272 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003273 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3275 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003276 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003277 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278}
3279
3280TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003281 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3282 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3283 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3284 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003286 KeyboardInputMapper& mapper =
3287 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3288 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003289
Michael Wrighta9cf4192022-12-01 23:46:39 +00003290 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3292 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3293 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3294 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3295 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3296 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3297 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3298 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3299}
3300
3301TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003302 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3303 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3304 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3305 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003306
Michael Wrightd02c5b62014-02-10 15:10:22 -08003307 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003308 KeyboardInputMapper& mapper =
3309 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3310 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003311
Michael Wrighta9cf4192022-12-01 23:46:39 +00003312 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003313 ASSERT_NO_FATAL_FAILURE(
3314 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3315 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3316 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3317 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3318 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3319 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3320 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003321
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003322 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003323 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003324 ASSERT_NO_FATAL_FAILURE(
3325 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3326 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3327 AKEYCODE_DPAD_UP, DISPLAY_ID));
3328 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3329 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3330 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3331 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003332
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003333 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003334 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003335 ASSERT_NO_FATAL_FAILURE(
3336 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3337 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3338 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3339 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3340 AKEYCODE_DPAD_UP, DISPLAY_ID));
3341 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3342 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003343
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003344 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003345 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003346 ASSERT_NO_FATAL_FAILURE(
3347 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3348 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3349 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3350 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3351 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3352 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3353 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003354
3355 // Special case: if orientation changes while key is down, we still emit the same keycode
3356 // in the key up as we did in the key down.
3357 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003358 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003359 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003360 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3362 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3363 ASSERT_EQ(KEY_UP, args.scanCode);
3364 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3365
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003366 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003367 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3370 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3371 ASSERT_EQ(KEY_UP, args.scanCode);
3372 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3373}
3374
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003375TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3376 // If the keyboard is not orientation aware,
3377 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003378 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003379
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003380 KeyboardInputMapper& mapper =
3381 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3382 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003383 NotifyKeyArgs args;
3384
3385 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3390 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3391
Michael Wrighta9cf4192022-12-01 23:46:39 +00003392 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003393 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003395 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3397 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3398}
3399
3400TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3401 // If the keyboard is orientation aware,
3402 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003403 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003404
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003405 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003406 KeyboardInputMapper& mapper =
3407 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3408 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003409 NotifyKeyArgs args;
3410
3411 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3412 // ^--- already checked by the previous test
3413
Michael Wrighta9cf4192022-12-01 23:46:39 +00003414 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003415 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003416 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3420 ASSERT_EQ(DISPLAY_ID, args.displayId);
3421
3422 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003423 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003424 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003425 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
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(newDisplayId, args.displayId);
3431}
3432
Michael Wrightd02c5b62014-02-10 15:10:22 -08003433TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003434 KeyboardInputMapper& mapper =
3435 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3436 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003437
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003438 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003439 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003441 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003442 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003443}
3444
Philip Junker4af3b3d2021-12-14 10:36:55 +01003445TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3446 KeyboardInputMapper& mapper =
3447 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3448 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3449
3450 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3451 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3452 << "If a mapping is available, the result is equal to the mapping";
3453
3454 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3455 << "If no mapping is available, the result is the key location";
3456}
3457
Michael Wrightd02c5b62014-02-10 15:10:22 -08003458TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003459 KeyboardInputMapper& mapper =
3460 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3461 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003463 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003464 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003465
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003466 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003467 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003468}
3469
3470TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003471 KeyboardInputMapper& mapper =
3472 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3473 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003474
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003475 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003476
Michael Wrightd02c5b62014-02-10 15:10:22 -08003477 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003478 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003479 ASSERT_TRUE(flags[0]);
3480 ASSERT_FALSE(flags[1]);
3481}
3482
3483TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003484 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3485 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3486 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3487 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3488 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3489 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003490
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003491 KeyboardInputMapper& mapper =
3492 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3493 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003494 // Initial metastate is AMETA_NONE.
3495 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003496
3497 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003498 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3499 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3500 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003501
3502 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003503 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3504 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003505 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3506 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3507 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003508 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003509
3510 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003511 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3512 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003513 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3514 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3515 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003516 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517
3518 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003521 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3522 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3523 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003524 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003525
3526 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003527 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3528 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003529 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3530 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3531 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003532 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003533
3534 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003537 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3538 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3539 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003540 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003541
3542 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003545 ASSERT_FALSE(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_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003549}
3550
Chris Yea52ade12020-08-27 16:49:20 -07003551TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3552 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3553 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3554 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3555 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3556
3557 KeyboardInputMapper& mapper =
3558 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3559 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3560
Chris Yea52ade12020-08-27 16:49:20 -07003561 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003562 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003563 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3564 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3565 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3566 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3567
3568 NotifyKeyArgs args;
3569 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003570 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3572 ASSERT_EQ(AMETA_NONE, args.metaState);
3573 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3574 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3575 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3576
3577 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003578 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3580 ASSERT_EQ(AMETA_NONE, args.metaState);
3581 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3582 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3583 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3584}
3585
Arthur Hung2c9a3342019-07-23 14:18:59 +08003586TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3587 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003588 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3589 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3590 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3591 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003592
3593 // keyboard 2.
3594 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003595 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003596 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003597 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003598 std::shared_ptr<InputDevice> device2 =
3599 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003600 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003601
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003602 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3603 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3604 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3605 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003606
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003607 KeyboardInputMapper& mapper =
3608 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3609 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003610
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003611 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003612 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003613 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003614 std::list<NotifyArgs> unused =
3615 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3616 0 /*changes*/);
3617 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003618
3619 // Prepared displays and associated info.
3620 constexpr uint8_t hdmi1 = 0;
3621 constexpr uint8_t hdmi2 = 1;
3622 const std::string SECONDARY_UNIQUE_ID = "local:1";
3623
3624 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3625 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3626
3627 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003628 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3629 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003630 ASSERT_FALSE(device2->isEnabled());
3631
3632 // Prepare second display.
3633 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003634 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003635 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003636 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003637 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003638 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003639 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3640 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003641
3642 // Device should be enabled after the associated display is found.
3643 ASSERT_TRUE(mDevice->isEnabled());
3644 ASSERT_TRUE(device2->isEnabled());
3645
3646 // Test pad key events
3647 ASSERT_NO_FATAL_FAILURE(
3648 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3649 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3650 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3651 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3652 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3653 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3654 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3655
3656 ASSERT_NO_FATAL_FAILURE(
3657 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3658 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3659 AKEYCODE_DPAD_RIGHT, newDisplayId));
3660 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3661 AKEYCODE_DPAD_DOWN, newDisplayId));
3662 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3663 AKEYCODE_DPAD_LEFT, newDisplayId));
3664}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003665
arthurhungc903df12020-08-11 15:08:42 +08003666TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3667 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3668 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3669 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3670 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3671 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3672 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3673
3674 KeyboardInputMapper& mapper =
3675 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3676 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003677 // Initial metastate is AMETA_NONE.
3678 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003679
3680 // Initialization should have turned all of the lights off.
3681 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3682 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3683 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3684
3685 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003688 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3689 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3690
3691 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3693 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003694 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3695 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3696
3697 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003698 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3699 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003700 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3701 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3702
3703 mFakeEventHub->removeDevice(EVENTHUB_ID);
3704 mReader->loopOnce();
3705
3706 // keyboard 2 should default toggle keys.
3707 const std::string USB2 = "USB2";
3708 const std::string DEVICE_NAME2 = "KEYBOARD2";
3709 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3710 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3711 std::shared_ptr<InputDevice> device2 =
3712 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003713 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003714 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3715 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3716 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3717 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3718 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3719 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3720
arthurhung6fe95782020-10-05 22:41:16 +08003721 KeyboardInputMapper& mapper2 =
3722 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3723 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003724 std::list<NotifyArgs> unused =
3725 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3726 0 /*changes*/);
3727 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003728
3729 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3730 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3731 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003732 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3733 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003734}
3735
Arthur Hungcb40a002021-08-03 14:31:01 +00003736TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3737 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3738 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3739 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3740
3741 // Suppose we have two mappers. (DPAD + KEYBOARD)
3742 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3743 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3744 KeyboardInputMapper& mapper =
3745 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3746 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003747 // Initial metastate is AMETA_NONE.
3748 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003749
3750 mReader->toggleCapsLockState(DEVICE_ID);
3751 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3752}
3753
Arthur Hungfb3cc112022-04-13 07:39:50 +00003754TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3755 // keyboard 1.
3756 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3757 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3758 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3759 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3760 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3761 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3762
3763 KeyboardInputMapper& mapper1 =
3764 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3765 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3766
3767 // keyboard 2.
3768 const std::string USB2 = "USB2";
3769 const std::string DEVICE_NAME2 = "KEYBOARD2";
3770 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3771 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3772 std::shared_ptr<InputDevice> device2 =
3773 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3774 ftl::Flags<InputDeviceClass>(0));
3775 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3776 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3777 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3778 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3779 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3780 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3781
3782 KeyboardInputMapper& mapper2 =
3783 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3784 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003785 std::list<NotifyArgs> unused =
3786 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3787 0 /*changes*/);
3788 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003789
Arthur Hung95f68612022-04-07 14:08:22 +08003790 // Initial metastate is AMETA_NONE.
3791 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3792 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3793
3794 // Toggle num lock on and off.
3795 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3796 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003797 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3798 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3799 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3800
3801 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3802 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3803 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3804 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3805 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3806
3807 // Toggle caps lock on and off.
3808 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3809 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3810 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3811 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3812 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3813
3814 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3815 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3816 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3817 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3818 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3819
3820 // Toggle scroll lock on and off.
3821 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3822 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3823 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3824 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3825 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3826
3827 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3828 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3829 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3830 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3831 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3832}
3833
Arthur Hung2141d542022-08-23 07:45:21 +00003834TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3835 const int32_t USAGE_A = 0x070004;
3836 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3837 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3838
3839 KeyboardInputMapper& mapper =
3840 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3841 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3842 // Key down by scan code.
3843 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3844 NotifyKeyArgs args;
3845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3846 ASSERT_EQ(DEVICE_ID, args.deviceId);
3847 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3848 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3849 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3850 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3851 ASSERT_EQ(KEY_HOME, args.scanCode);
3852 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3853
3854 // Disable device, it should synthesize cancellation events for down events.
3855 mFakePolicy->addDisabledDevice(DEVICE_ID);
3856 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
3857
3858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3859 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3860 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3861 ASSERT_EQ(KEY_HOME, args.scanCode);
3862 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3863}
3864
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003865// --- KeyboardInputMapperTest_ExternalDevice ---
3866
3867class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3868protected:
Chris Yea52ade12020-08-27 16:49:20 -07003869 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003870};
3871
3872TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003873 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3874 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003875
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003876 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3877 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3878 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3879 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003880
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003881 KeyboardInputMapper& mapper =
3882 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3883 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003884
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003885 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003886 NotifyKeyArgs args;
3887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3888 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3889
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003890 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3892 ASSERT_EQ(uint32_t(0), args.policyFlags);
3893
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003894 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3896 ASSERT_EQ(uint32_t(0), args.policyFlags);
3897
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003898 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3900 ASSERT_EQ(uint32_t(0), args.policyFlags);
3901
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003902 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3904 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3905
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003906 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3908 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3909}
3910
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003911TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003912 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003913
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003914 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3915 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3916 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003917
Powei Fengd041c5d2019-05-03 17:11:33 -07003918 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003919 KeyboardInputMapper& mapper =
3920 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3921 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003922
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003923 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003924 NotifyKeyArgs args;
3925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3926 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3927
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003928 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3930 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3931
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003932 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3934 ASSERT_EQ(uint32_t(0), args.policyFlags);
3935
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003936 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3938 ASSERT_EQ(uint32_t(0), args.policyFlags);
3939
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003940 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3942 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3943
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003944 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3946 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3947}
3948
Michael Wrightd02c5b62014-02-10 15:10:22 -08003949// --- CursorInputMapperTest ---
3950
3951class CursorInputMapperTest : public InputMapperTest {
3952protected:
3953 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3954
Michael Wright17db18e2020-06-26 20:51:44 +01003955 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003956
Chris Yea52ade12020-08-27 16:49:20 -07003957 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003958 InputMapperTest::SetUp();
3959
Michael Wright17db18e2020-06-26 20:51:44 +01003960 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003961 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003962 }
3963
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003964 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3965 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003966
Michael Wrighta9cf4192022-12-01 23:46:39 +00003967 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003968 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3969 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3970 }
3971
3972 void prepareSecondaryDisplay() {
3973 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003974 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003975 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003976 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003977
3978 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3979 float pressure) {
3980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3981 0.0f, 0.0f, 0.0f, EPSILON));
3982 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003983};
3984
3985const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3986
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003987void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3988 int32_t originalY, int32_t rotatedX,
3989 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003990 NotifyMotionArgs args;
3991
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003997 ASSERT_NO_FATAL_FAILURE(
3998 assertCursorPointerCoords(args.pointerCoords[0],
3999 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4000 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004001}
4002
4003TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004004 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004005 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004006
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004007 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004008}
4009
4010TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004011 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004012 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004013
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004014 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004015}
4016
4017TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004018 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004019 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004020
4021 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004022 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004023
4024 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004025 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4026 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004027 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4028 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4029
4030 // When the bounds are set, then there should be a valid motion range.
4031 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4032
4033 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004034 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004035
4036 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4037 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4038 1, 800 - 1, 0.0f, 0.0f));
4039 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4040 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4041 2, 480 - 1, 0.0f, 0.0f));
4042 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4043 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4044 0.0f, 1.0f, 0.0f, 0.0f));
4045}
4046
4047TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004048 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004049 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004050
4051 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004052 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004053
4054 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4055 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4056 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4057 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4058 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4059 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4060 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4061 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4062 0.0f, 1.0f, 0.0f, 0.0f));
4063}
4064
4065TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004066 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004067 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004068
arthurhungdcef2dc2020-08-11 14:47:50 +08004069 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004070
4071 NotifyMotionArgs args;
4072
4073 // Button press.
4074 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004075 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4076 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4078 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4079 ASSERT_EQ(DEVICE_ID, args.deviceId);
4080 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4081 ASSERT_EQ(uint32_t(0), args.policyFlags);
4082 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4083 ASSERT_EQ(0, args.flags);
4084 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4085 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4086 ASSERT_EQ(0, args.edgeFlags);
4087 ASSERT_EQ(uint32_t(1), args.pointerCount);
4088 ASSERT_EQ(0, args.pointerProperties[0].id);
4089 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004090 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4092 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4093 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4094
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4096 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4097 ASSERT_EQ(DEVICE_ID, args.deviceId);
4098 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4099 ASSERT_EQ(uint32_t(0), args.policyFlags);
4100 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4101 ASSERT_EQ(0, args.flags);
4102 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4103 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4104 ASSERT_EQ(0, args.edgeFlags);
4105 ASSERT_EQ(uint32_t(1), args.pointerCount);
4106 ASSERT_EQ(0, args.pointerProperties[0].id);
4107 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004108 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004109 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4110 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4111 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4112
Michael Wrightd02c5b62014-02-10 15:10:22 -08004113 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004114 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4115 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4117 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4118 ASSERT_EQ(DEVICE_ID, args.deviceId);
4119 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4120 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004121 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4122 ASSERT_EQ(0, args.flags);
4123 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4124 ASSERT_EQ(0, args.buttonState);
4125 ASSERT_EQ(0, args.edgeFlags);
4126 ASSERT_EQ(uint32_t(1), args.pointerCount);
4127 ASSERT_EQ(0, args.pointerProperties[0].id);
4128 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004129 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004130 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4131 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4132 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4133
4134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4135 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4136 ASSERT_EQ(DEVICE_ID, args.deviceId);
4137 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4138 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004139 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4140 ASSERT_EQ(0, args.flags);
4141 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4142 ASSERT_EQ(0, args.buttonState);
4143 ASSERT_EQ(0, args.edgeFlags);
4144 ASSERT_EQ(uint32_t(1), args.pointerCount);
4145 ASSERT_EQ(0, args.pointerProperties[0].id);
4146 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004147 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004148 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4149 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4150 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4151}
4152
4153TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004155 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004156
4157 NotifyMotionArgs args;
4158
4159 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004160 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004164 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4165 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4166 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004167
4168 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004173 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4174 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175}
4176
4177TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004179 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180
4181 NotifyMotionArgs args;
4182
4183 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4187 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004188 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004189
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4191 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004192 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004193
Michael Wrightd02c5b62014-02-10 15:10:22 -08004194 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004195 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4196 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004198 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004199 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004200
4201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004203 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004204}
4205
4206TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004207 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004208 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004209
4210 NotifyMotionArgs args;
4211
4212 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004213 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4214 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4218 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004219 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4220 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4221 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4224 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004225 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4226 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4227 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004228
Michael Wrightd02c5b62014-02-10 15:10:22 -08004229 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004230 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4231 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4232 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004235 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4236 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4237 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004238
4239 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4241 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004243 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004244 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004245
4246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004247 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004248 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249}
4250
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004251TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004252 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004254 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4255 // need to be rotated.
4256 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004257 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004258
Michael Wrighta9cf4192022-12-01 23:46:39 +00004259 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4261 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4262 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4263 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4264 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4265 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4266 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4267 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4268}
4269
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004270TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004271 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004273 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4274 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004275 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004277 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004278 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4280 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4281 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4282 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4283 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4284 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4285 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4286 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4287
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004288 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004289 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004290 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4291 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4292 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4293 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4294 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4295 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4296 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4297 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004298
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004299 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004300 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004301 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4302 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4303 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4304 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4305 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4306 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4307 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4308 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4309
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004310 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004311 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004312 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4313 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4314 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4315 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4316 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4317 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4318 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4319 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320}
4321
4322TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004324 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325
4326 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4327 mFakePointerController->setPosition(100, 200);
4328 mFakePointerController->setButtonState(0);
4329
4330 NotifyMotionArgs motionArgs;
4331 NotifyKeyArgs keyArgs;
4332
4333 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4337 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4338 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4339 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004340 ASSERT_NO_FATAL_FAILURE(
4341 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004342
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4344 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4345 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4346 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004347 ASSERT_NO_FATAL_FAILURE(
4348 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004349
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004350 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004353 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004354 ASSERT_EQ(0, motionArgs.buttonState);
4355 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004356 ASSERT_NO_FATAL_FAILURE(
4357 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358
4359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004360 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004361 ASSERT_EQ(0, motionArgs.buttonState);
4362 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004363 ASSERT_NO_FATAL_FAILURE(
4364 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004365
4366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004367 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004368 ASSERT_EQ(0, motionArgs.buttonState);
4369 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004370 ASSERT_NO_FATAL_FAILURE(
4371 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372
4373 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004374 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4375 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4378 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4379 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4380 motionArgs.buttonState);
4381 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4382 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004383 ASSERT_NO_FATAL_FAILURE(
4384 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4387 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4388 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4389 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4390 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004391 ASSERT_NO_FATAL_FAILURE(
4392 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004393
4394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4395 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4396 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4397 motionArgs.buttonState);
4398 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4399 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004400 ASSERT_NO_FATAL_FAILURE(
4401 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004402
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004403 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4404 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004406 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4408 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004409 ASSERT_NO_FATAL_FAILURE(
4410 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004411
4412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004413 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004414 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4415 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004416 ASSERT_NO_FATAL_FAILURE(
4417 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004418
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004422 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4423 ASSERT_EQ(0, motionArgs.buttonState);
4424 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004425 ASSERT_NO_FATAL_FAILURE(
4426 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004427 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004429
4430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431 ASSERT_EQ(0, motionArgs.buttonState);
4432 ASSERT_EQ(0, mFakePointerController->getButtonState());
4433 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004434 ASSERT_NO_FATAL_FAILURE(
4435 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004436
Michael Wrightd02c5b62014-02-10 15:10:22 -08004437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4438 ASSERT_EQ(0, motionArgs.buttonState);
4439 ASSERT_EQ(0, mFakePointerController->getButtonState());
4440 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004441 ASSERT_NO_FATAL_FAILURE(
4442 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004443
4444 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4448 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4449 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004450
Michael Wrightd02c5b62014-02-10 15:10:22 -08004451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004452 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4454 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004455 ASSERT_NO_FATAL_FAILURE(
4456 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004457
4458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4459 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4460 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4461 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004462 ASSERT_NO_FATAL_FAILURE(
4463 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004465 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4466 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004468 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469 ASSERT_EQ(0, motionArgs.buttonState);
4470 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004471 ASSERT_NO_FATAL_FAILURE(
4472 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004473
4474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004476 ASSERT_EQ(0, motionArgs.buttonState);
4477 ASSERT_EQ(0, mFakePointerController->getButtonState());
4478
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004479 ASSERT_NO_FATAL_FAILURE(
4480 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4482 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4483 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4484
4485 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004486 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4487 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4489 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4490 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004491
Michael Wrightd02c5b62014-02-10 15:10:22 -08004492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004493 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004494 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4495 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004496 ASSERT_NO_FATAL_FAILURE(
4497 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004498
4499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4500 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4501 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4502 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004503 ASSERT_NO_FATAL_FAILURE(
4504 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004505
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004506 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4507 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004509 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004510 ASSERT_EQ(0, motionArgs.buttonState);
4511 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004512 ASSERT_NO_FATAL_FAILURE(
4513 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004514
4515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4516 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4517 ASSERT_EQ(0, motionArgs.buttonState);
4518 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004519 ASSERT_NO_FATAL_FAILURE(
4520 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004521
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4523 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4524 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4525
4526 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004527 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4528 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4530 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4531 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004532
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004534 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004535 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4536 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004537 ASSERT_NO_FATAL_FAILURE(
4538 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004539
4540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4541 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4542 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4543 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004544 ASSERT_NO_FATAL_FAILURE(
4545 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004546
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004547 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004550 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004551 ASSERT_EQ(0, motionArgs.buttonState);
4552 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004553 ASSERT_NO_FATAL_FAILURE(
4554 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004555
4556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4557 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4558 ASSERT_EQ(0, motionArgs.buttonState);
4559 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004560 ASSERT_NO_FATAL_FAILURE(
4561 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004562
Michael Wrightd02c5b62014-02-10 15:10:22 -08004563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4564 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4565 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4566
4567 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4571 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4572 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004573
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004575 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004576 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4577 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004578 ASSERT_NO_FATAL_FAILURE(
4579 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004580
4581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4582 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4583 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4584 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004585 ASSERT_NO_FATAL_FAILURE(
4586 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004587
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004588 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004591 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004592 ASSERT_EQ(0, motionArgs.buttonState);
4593 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004594 ASSERT_NO_FATAL_FAILURE(
4595 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004596
4597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4598 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4599 ASSERT_EQ(0, motionArgs.buttonState);
4600 ASSERT_EQ(0, mFakePointerController->getButtonState());
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_FORWARD, keyArgs.keyCode);
4607}
4608
4609TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004611 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004612
4613 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4614 mFakePointerController->setPosition(100, 200);
4615 mFakePointerController->setButtonState(0);
4616
4617 NotifyMotionArgs args;
4618
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004619 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4620 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4621 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004623 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4624 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4626 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004627 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004628}
4629
4630TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004631 addConfigurationProperty("cursor.mode", "pointer");
4632 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004633 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004634
4635 NotifyDeviceResetArgs resetArgs;
4636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4637 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4638 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4639
4640 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4641 mFakePointerController->setPosition(100, 200);
4642 mFakePointerController->setButtonState(0);
4643
4644 NotifyMotionArgs args;
4645
4646 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4648 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4651 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4652 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4654 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004655 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004656
4657 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004658 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4659 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4661 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4662 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4664 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4666 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4667 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4669 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4670
4671 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004672 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4673 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4675 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4676 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4678 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4680 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4681 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4683 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4684
4685 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4688 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4690 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4691 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4692 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4693 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004694 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004695
4696 // Disable pointer capture and check that the device generation got bumped
4697 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004698 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004699 mFakePolicy->setPointerCapture(false);
4700 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004701 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004702
4703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004704 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4705
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4707 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4708 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4710 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4712 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4713 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004714 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004715}
4716
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004717/**
4718 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4719 * pointer acceleration or speed processing should not be applied.
4720 */
4721TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4722 addConfigurationProperty("cursor.mode", "pointer");
4723 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4724 100.f /*high threshold*/, 10.f /*acceleration*/);
4725 mFakePolicy->setVelocityControlParams(testParams);
4726 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4727
4728 NotifyDeviceResetArgs resetArgs;
4729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4730 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4731 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4732
4733 NotifyMotionArgs args;
4734
4735 // Move and verify scale is applied.
4736 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4737 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4740 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4741 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4742 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4743 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4744 ASSERT_GT(relX, 10);
4745 ASSERT_GT(relY, 20);
4746
4747 // Enable Pointer Capture
4748 mFakePolicy->setPointerCapture(true);
4749 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4750 NotifyPointerCaptureChangedArgs captureArgs;
4751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4752 ASSERT_TRUE(captureArgs.request.enable);
4753
4754 // Move and verify scale is not applied.
4755 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4756 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4757 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4759 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4761 ASSERT_EQ(10, args.pointerCoords[0].getX());
4762 ASSERT_EQ(20, args.pointerCoords[0].getY());
4763}
4764
Prabir Pradhan208360b2022-06-24 18:37:04 +00004765TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4766 addConfigurationProperty("cursor.mode", "pointer");
4767 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4768
4769 NotifyDeviceResetArgs resetArgs;
4770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4771 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4772 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4773
4774 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004775 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004776
4777 NotifyMotionArgs args;
4778
4779 // Verify that the coordinates are rotated.
4780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4784 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4785 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4786 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4787 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4788
4789 // Enable Pointer Capture.
4790 mFakePolicy->setPointerCapture(true);
4791 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4792 NotifyPointerCaptureChangedArgs captureArgs;
4793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4794 ASSERT_TRUE(captureArgs.request.enable);
4795
4796 // Move and verify rotation is not applied.
4797 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4798 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4801 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4802 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4803 ASSERT_EQ(10, args.pointerCoords[0].getX());
4804 ASSERT_EQ(20, args.pointerCoords[0].getY());
4805}
4806
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004807TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004808 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004809
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004810 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004811 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004812
4813 // Set up the secondary display as the display on which the pointer should be shown.
4814 // The InputDevice is not associated with any display.
4815 prepareSecondaryDisplay();
4816 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08004817 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4818
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004819 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004820 mFakePointerController->setPosition(100, 200);
4821 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004822
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004823 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4825 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4826 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004828 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4829 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4830 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01004831 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004832}
4833
4834TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
4835 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4836
4837 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004838 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004839
4840 // Set up the secondary display as the display on which the pointer should be shown,
4841 // and associate the InputDevice with the secondary display.
4842 prepareSecondaryDisplay();
4843 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4844 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4845 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4846
4847 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4848 mFakePointerController->setPosition(100, 200);
4849 mFakePointerController->setButtonState(0);
4850
4851 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4852 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4853 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004855 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4856 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4857 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004858 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
4859}
4860
4861TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
4862 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4863
4864 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004865 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004866 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4867
4868 // Associate the InputDevice with the secondary display.
4869 prepareSecondaryDisplay();
4870 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4871 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4872
4873 // The mapper should not generate any events because it is associated with a display that is
4874 // different from the pointer display.
4875 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4876 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4877 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004879}
4880
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004881// --- BluetoothCursorInputMapperTest ---
4882
4883class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4884protected:
4885 void SetUp() override {
4886 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4887
4888 mFakePointerController = std::make_shared<FakePointerController>();
4889 mFakePolicy->setPointerController(mFakePointerController);
4890 }
4891};
4892
4893TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4894 addConfigurationProperty("cursor.mode", "pointer");
4895 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4896
4897 nsecs_t kernelEventTime = ARBITRARY_TIME;
4898 nsecs_t expectedEventTime = ARBITRARY_TIME;
4899 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4900 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4902 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4903 WithEventTime(expectedEventTime))));
4904
4905 // Process several events that come in quick succession, according to their timestamps.
4906 for (int i = 0; i < 3; i++) {
4907 constexpr static nsecs_t delta = ms2ns(1);
4908 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4909 kernelEventTime += delta;
4910 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4911
4912 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4913 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4915 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4916 WithEventTime(expectedEventTime))));
4917 }
4918}
4919
4920TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4921 addConfigurationProperty("cursor.mode", "pointer");
4922 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4923
4924 nsecs_t expectedEventTime = ARBITRARY_TIME;
4925 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4926 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4928 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4929 WithEventTime(expectedEventTime))));
4930
4931 // Process several events with the same timestamp from the kernel.
4932 // Ensure that we do not generate events too far into the future.
4933 constexpr static int32_t numEvents =
4934 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4935 for (int i = 0; i < numEvents; i++) {
4936 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4937
4938 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4939 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4941 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4942 WithEventTime(expectedEventTime))));
4943 }
4944
4945 // By processing more events with the same timestamp, we should not generate events with a
4946 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4947 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4948 for (int i = 0; i < 3; i++) {
4949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4952 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4953 WithEventTime(cappedEventTime))));
4954 }
4955}
4956
4957TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4958 addConfigurationProperty("cursor.mode", "pointer");
4959 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4960
4961 nsecs_t kernelEventTime = ARBITRARY_TIME;
4962 nsecs_t expectedEventTime = ARBITRARY_TIME;
4963 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4964 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4966 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4967 WithEventTime(expectedEventTime))));
4968
4969 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4970 // smoothening is not needed, its timestamp is not affected.
4971 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4972 expectedEventTime = kernelEventTime;
4973
4974 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4975 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4977 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4978 WithEventTime(expectedEventTime))));
4979}
4980
Michael Wrightd02c5b62014-02-10 15:10:22 -08004981// --- TouchInputMapperTest ---
4982
4983class TouchInputMapperTest : public InputMapperTest {
4984protected:
4985 static const int32_t RAW_X_MIN;
4986 static const int32_t RAW_X_MAX;
4987 static const int32_t RAW_Y_MIN;
4988 static const int32_t RAW_Y_MAX;
4989 static const int32_t RAW_TOUCH_MIN;
4990 static const int32_t RAW_TOUCH_MAX;
4991 static const int32_t RAW_TOOL_MIN;
4992 static const int32_t RAW_TOOL_MAX;
4993 static const int32_t RAW_PRESSURE_MIN;
4994 static const int32_t RAW_PRESSURE_MAX;
4995 static const int32_t RAW_ORIENTATION_MIN;
4996 static const int32_t RAW_ORIENTATION_MAX;
4997 static const int32_t RAW_DISTANCE_MIN;
4998 static const int32_t RAW_DISTANCE_MAX;
4999 static const int32_t RAW_TILT_MIN;
5000 static const int32_t RAW_TILT_MAX;
5001 static const int32_t RAW_ID_MIN;
5002 static const int32_t RAW_ID_MAX;
5003 static const int32_t RAW_SLOT_MIN;
5004 static const int32_t RAW_SLOT_MAX;
5005 static const float X_PRECISION;
5006 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005007 static const float X_PRECISION_VIRTUAL;
5008 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009
5010 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005011 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005012
5013 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5014
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005015 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005016 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005017
Michael Wrightd02c5b62014-02-10 15:10:22 -08005018 enum Axes {
5019 POSITION = 1 << 0,
5020 TOUCH = 1 << 1,
5021 TOOL = 1 << 2,
5022 PRESSURE = 1 << 3,
5023 ORIENTATION = 1 << 4,
5024 MINOR = 1 << 5,
5025 ID = 1 << 6,
5026 DISTANCE = 1 << 7,
5027 TILT = 1 << 8,
5028 SLOT = 1 << 9,
5029 TOOL_TYPE = 1 << 10,
5030 };
5031
Michael Wrighta9cf4192022-12-01 23:46:39 +00005032 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005033 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00005034 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005036 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005037 int32_t toRawX(float displayX);
5038 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005039 int32_t toRotatedRawX(float displayX);
5040 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005041 float toCookedX(float rawX, float rawY);
5042 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005043 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005044 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005045 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005046 float toDisplayY(int32_t rawY, int32_t displayHeight);
5047
Michael Wrightd02c5b62014-02-10 15:10:22 -08005048};
5049
5050const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5051const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5052const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5053const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5054const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5055const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5056const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5057const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005058const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5059const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5061const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5062const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5063const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5064const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5065const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5066const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5067const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5068const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5069const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5070const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5071const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005072const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5073 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5074const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5075 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005076const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5077 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005078
5079const float TouchInputMapperTest::GEOMETRIC_SCALE =
5080 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5081 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5082
5083const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5084 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5085 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5086};
5087
Michael Wrighta9cf4192022-12-01 23:46:39 +00005088void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005089 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5090 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005091}
5092
5093void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5094 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00005095 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005096}
5097
Michael Wrighta9cf4192022-12-01 23:46:39 +00005098void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005099 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5100 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5101 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005102}
5103
Michael Wrightd02c5b62014-02-10 15:10:22 -08005104void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005105 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5106 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5107 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5108 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005109}
5110
Jason Gerecke489fda82012-09-07 17:19:40 -07005111void TouchInputMapperTest::prepareLocationCalibration() {
5112 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5113}
5114
Michael Wrightd02c5b62014-02-10 15:10:22 -08005115int32_t TouchInputMapperTest::toRawX(float displayX) {
5116 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5117}
5118
5119int32_t TouchInputMapperTest::toRawY(float displayY) {
5120 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5121}
5122
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005123int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5124 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5125}
5126
5127int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5128 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5129}
5130
Jason Gerecke489fda82012-09-07 17:19:40 -07005131float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5132 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5133 return rawX;
5134}
5135
5136float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5137 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5138 return rawY;
5139}
5140
Michael Wrightd02c5b62014-02-10 15:10:22 -08005141float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005142 return toDisplayX(rawX, DISPLAY_WIDTH);
5143}
5144
5145float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5146 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005147}
5148
5149float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005150 return toDisplayY(rawY, DISPLAY_HEIGHT);
5151}
5152
5153float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5154 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005155}
5156
5157
5158// --- SingleTouchInputMapperTest ---
5159
5160class SingleTouchInputMapperTest : public TouchInputMapperTest {
5161protected:
5162 void prepareButtons();
5163 void prepareAxes(int axes);
5164
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005165 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5166 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5167 void processUp(SingleTouchInputMapper& mappery);
5168 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5169 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5170 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5171 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5172 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5173 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005174};
5175
5176void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005177 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005178}
5179
5180void SingleTouchInputMapperTest::prepareAxes(int axes) {
5181 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005182 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5183 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005184 }
5185 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005186 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5187 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005188 }
5189 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005190 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5191 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005192 }
5193 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005194 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5195 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005196 }
5197 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005198 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5199 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005200 }
5201}
5202
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005203void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005207}
5208
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005209void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005210 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005212}
5213
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005214void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005216}
5217
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005218void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005219 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005220}
5221
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005222void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5223 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005224 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005225}
5226
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005227void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005228 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005229}
5230
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005231void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5232 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005235}
5236
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005237void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5238 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005239 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005240}
5241
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005242void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005243 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005244}
5245
Michael Wrightd02c5b62014-02-10 15:10:22 -08005246TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005247 prepareButtons();
5248 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005249 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005250
Harry Cutts16a24cc2022-10-26 15:22:19 +00005251 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005252}
5253
Michael Wrightd02c5b62014-02-10 15:10:22 -08005254TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005255 prepareButtons();
5256 prepareAxes(POSITION);
5257 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005258 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005259
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005260 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005261}
5262
5263TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005264 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005265 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005266 prepareButtons();
5267 prepareAxes(POSITION);
5268 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005269 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005270
5271 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005272 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005273
5274 // Virtual key is down.
5275 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5276 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5277 processDown(mapper, x, y);
5278 processSync(mapper);
5279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5280
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005281 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005282
5283 // Virtual key is up.
5284 processUp(mapper);
5285 processSync(mapper);
5286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5287
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005288 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005289}
5290
5291TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005292 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005293 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005294 prepareButtons();
5295 prepareAxes(POSITION);
5296 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005297 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005298
5299 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005300 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005301
5302 // Virtual key is down.
5303 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5304 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5305 processDown(mapper, x, y);
5306 processSync(mapper);
5307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5308
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005309 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005310
5311 // Virtual key is up.
5312 processUp(mapper);
5313 processSync(mapper);
5314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5315
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005316 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005317}
5318
5319TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005320 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005321 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322 prepareButtons();
5323 prepareAxes(POSITION);
5324 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005325 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005326
Michael Wrightd02c5b62014-02-10 15:10:22 -08005327 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005328 ASSERT_TRUE(
5329 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330 ASSERT_TRUE(flags[0]);
5331 ASSERT_FALSE(flags[1]);
5332}
5333
5334TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005335 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005336 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005337 prepareButtons();
5338 prepareAxes(POSITION);
5339 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005340 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005341
arthurhungdcef2dc2020-08-11 14:47:50 +08005342 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005343
5344 NotifyKeyArgs args;
5345
5346 // Press virtual key.
5347 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5348 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5349 processDown(mapper, x, y);
5350 processSync(mapper);
5351
5352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5353 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5354 ASSERT_EQ(DEVICE_ID, args.deviceId);
5355 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5356 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5357 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5358 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5359 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5360 ASSERT_EQ(KEY_HOME, args.scanCode);
5361 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5362 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5363
5364 // Release virtual key.
5365 processUp(mapper);
5366 processSync(mapper);
5367
5368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5369 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5370 ASSERT_EQ(DEVICE_ID, args.deviceId);
5371 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5372 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5373 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5374 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5375 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5376 ASSERT_EQ(KEY_HOME, args.scanCode);
5377 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5378 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5379
5380 // Should not have sent any motions.
5381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5382}
5383
5384TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005385 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005386 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005387 prepareButtons();
5388 prepareAxes(POSITION);
5389 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005390 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005391
arthurhungdcef2dc2020-08-11 14:47:50 +08005392 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005393
5394 NotifyKeyArgs keyArgs;
5395
5396 // Press virtual key.
5397 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5398 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5399 processDown(mapper, x, y);
5400 processSync(mapper);
5401
5402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5403 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5404 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5405 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5406 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5407 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5408 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5409 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5410 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5411 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5412 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5413
5414 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5415 // into the display area.
5416 y -= 100;
5417 processMove(mapper, x, y);
5418 processSync(mapper);
5419
5420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5421 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5422 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5423 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5424 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5425 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5426 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5427 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5428 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5429 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5430 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5431 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5432
5433 NotifyMotionArgs motionArgs;
5434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5435 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5436 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5437 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5438 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5439 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5440 ASSERT_EQ(0, motionArgs.flags);
5441 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5442 ASSERT_EQ(0, motionArgs.buttonState);
5443 ASSERT_EQ(0, motionArgs.edgeFlags);
5444 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5445 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5446 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5448 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5449 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5450 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5451 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5452
5453 // Keep moving out of bounds. Should generate a pointer move.
5454 y -= 50;
5455 processMove(mapper, x, y);
5456 processSync(mapper);
5457
5458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5459 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5460 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5461 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5462 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5464 ASSERT_EQ(0, motionArgs.flags);
5465 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5466 ASSERT_EQ(0, motionArgs.buttonState);
5467 ASSERT_EQ(0, motionArgs.edgeFlags);
5468 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5469 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5470 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5472 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5473 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5474 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5475 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5476
5477 // Release out of bounds. Should generate a pointer up.
5478 processUp(mapper);
5479 processSync(mapper);
5480
5481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5482 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5483 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5484 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5485 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5486 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5487 ASSERT_EQ(0, motionArgs.flags);
5488 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5489 ASSERT_EQ(0, motionArgs.buttonState);
5490 ASSERT_EQ(0, motionArgs.edgeFlags);
5491 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5492 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5493 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5495 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5496 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5497 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5498 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5499
5500 // Should not have sent any more keys or motions.
5501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5503}
5504
5505TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005506 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005507 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005508 prepareButtons();
5509 prepareAxes(POSITION);
5510 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005511 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005512
arthurhungdcef2dc2020-08-11 14:47:50 +08005513 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005514
5515 NotifyMotionArgs motionArgs;
5516
5517 // Initially go down out of bounds.
5518 int32_t x = -10;
5519 int32_t y = -10;
5520 processDown(mapper, x, y);
5521 processSync(mapper);
5522
5523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5524
5525 // Move into the display area. Should generate a pointer down.
5526 x = 50;
5527 y = 75;
5528 processMove(mapper, x, y);
5529 processSync(mapper);
5530
5531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5532 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5533 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5534 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5535 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5536 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5537 ASSERT_EQ(0, motionArgs.flags);
5538 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5539 ASSERT_EQ(0, motionArgs.buttonState);
5540 ASSERT_EQ(0, motionArgs.edgeFlags);
5541 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5542 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5545 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5546 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5547 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5548 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5549
5550 // Release. Should generate a pointer up.
5551 processUp(mapper);
5552 processSync(mapper);
5553
5554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5555 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5556 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5557 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5558 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5559 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5560 ASSERT_EQ(0, motionArgs.flags);
5561 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5562 ASSERT_EQ(0, motionArgs.buttonState);
5563 ASSERT_EQ(0, motionArgs.edgeFlags);
5564 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5565 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5566 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5567 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5568 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5569 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5570 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5571 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5572
5573 // Should not have sent any more keys or motions.
5574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5576}
5577
Santos Cordonfa5cf462017-04-05 10:37:00 -07005578TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005579 addConfigurationProperty("touch.deviceType", "touchScreen");
5580 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5581
Michael Wrighta9cf4192022-12-01 23:46:39 +00005582 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005583 prepareButtons();
5584 prepareAxes(POSITION);
5585 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005586 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005587
arthurhungdcef2dc2020-08-11 14:47:50 +08005588 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005589
5590 NotifyMotionArgs motionArgs;
5591
5592 // Down.
5593 int32_t x = 100;
5594 int32_t y = 125;
5595 processDown(mapper, x, y);
5596 processSync(mapper);
5597
5598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5599 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5600 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5601 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5602 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5603 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5604 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5605 ASSERT_EQ(0, motionArgs.flags);
5606 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5607 ASSERT_EQ(0, motionArgs.buttonState);
5608 ASSERT_EQ(0, motionArgs.edgeFlags);
5609 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5610 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5613 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5614 1, 0, 0, 0, 0, 0, 0, 0));
5615 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5616 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5617 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5618
5619 // Move.
5620 x += 50;
5621 y += 75;
5622 processMove(mapper, x, y);
5623 processSync(mapper);
5624
5625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5626 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5627 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5628 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5629 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5630 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5631 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5632 ASSERT_EQ(0, motionArgs.flags);
5633 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5634 ASSERT_EQ(0, motionArgs.buttonState);
5635 ASSERT_EQ(0, motionArgs.edgeFlags);
5636 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5637 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5640 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5641 1, 0, 0, 0, 0, 0, 0, 0));
5642 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5643 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5644 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5645
5646 // Up.
5647 processUp(mapper);
5648 processSync(mapper);
5649
5650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5651 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5652 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5653 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5654 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5655 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5656 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5657 ASSERT_EQ(0, motionArgs.flags);
5658 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5659 ASSERT_EQ(0, motionArgs.buttonState);
5660 ASSERT_EQ(0, motionArgs.edgeFlags);
5661 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5662 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5663 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5665 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5666 1, 0, 0, 0, 0, 0, 0, 0));
5667 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5668 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5669 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5670
5671 // Should not have sent any more keys or motions.
5672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5674}
5675
Michael Wrightd02c5b62014-02-10 15:10:22 -08005676TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005677 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005678 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005679 prepareButtons();
5680 prepareAxes(POSITION);
5681 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005682 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005683
arthurhungdcef2dc2020-08-11 14:47:50 +08005684 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005685
5686 NotifyMotionArgs motionArgs;
5687
5688 // Down.
5689 int32_t x = 100;
5690 int32_t y = 125;
5691 processDown(mapper, x, y);
5692 processSync(mapper);
5693
5694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5695 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5696 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5697 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5698 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5699 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5700 ASSERT_EQ(0, motionArgs.flags);
5701 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5702 ASSERT_EQ(0, motionArgs.buttonState);
5703 ASSERT_EQ(0, motionArgs.edgeFlags);
5704 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5705 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5706 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5707 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5708 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5709 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5710 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5711 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5712
5713 // Move.
5714 x += 50;
5715 y += 75;
5716 processMove(mapper, x, y);
5717 processSync(mapper);
5718
5719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5720 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5721 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5722 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5723 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5725 ASSERT_EQ(0, motionArgs.flags);
5726 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5727 ASSERT_EQ(0, motionArgs.buttonState);
5728 ASSERT_EQ(0, motionArgs.edgeFlags);
5729 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5730 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5731 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5733 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5734 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5735 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5736 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5737
5738 // Up.
5739 processUp(mapper);
5740 processSync(mapper);
5741
5742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5743 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5744 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5745 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5746 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5747 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5748 ASSERT_EQ(0, motionArgs.flags);
5749 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5750 ASSERT_EQ(0, motionArgs.buttonState);
5751 ASSERT_EQ(0, motionArgs.edgeFlags);
5752 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5753 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5754 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5756 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5757 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5758 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5759 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5760
5761 // Should not have sent any more keys or motions.
5762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5764}
5765
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005766TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005767 addConfigurationProperty("touch.deviceType", "touchScreen");
5768 prepareButtons();
5769 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005770 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5771 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005772 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005773
5774 NotifyMotionArgs args;
5775
5776 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005777 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005778 processDown(mapper, toRawX(50), toRawY(75));
5779 processSync(mapper);
5780
5781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5782 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5783 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5784
5785 processUp(mapper);
5786 processSync(mapper);
5787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5788}
5789
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005790TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005791 addConfigurationProperty("touch.deviceType", "touchScreen");
5792 prepareButtons();
5793 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005794 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5795 // orientation-aware are affected by display rotation.
5796 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005797 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005798
5799 NotifyMotionArgs args;
5800
5801 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005802 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005803 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005804 processDown(mapper, toRawX(50), toRawY(75));
5805 processSync(mapper);
5806
5807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5808 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5809 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5810
5811 processUp(mapper);
5812 processSync(mapper);
5813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5814
5815 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005816 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005817 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005818 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005819 processSync(mapper);
5820
5821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5822 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5823 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5824
5825 processUp(mapper);
5826 processSync(mapper);
5827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5828
5829 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005830 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005831 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005832 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5833 processSync(mapper);
5834
5835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5836 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5837 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5838
5839 processUp(mapper);
5840 processSync(mapper);
5841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5842
5843 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005844 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005845 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005846 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005847 processSync(mapper);
5848
5849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5850 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5851 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5852
5853 processUp(mapper);
5854 processSync(mapper);
5855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5856}
5857
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005858TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5859 addConfigurationProperty("touch.deviceType", "touchScreen");
5860 prepareButtons();
5861 prepareAxes(POSITION);
5862 addConfigurationProperty("touch.orientationAware", "1");
5863 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5864 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005865 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005866 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5867 NotifyMotionArgs args;
5868
5869 // Orientation 0.
5870 processDown(mapper, toRawX(50), toRawY(75));
5871 processSync(mapper);
5872
5873 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5874 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5875 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5876
5877 processUp(mapper);
5878 processSync(mapper);
5879 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5880}
5881
5882TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5883 addConfigurationProperty("touch.deviceType", "touchScreen");
5884 prepareButtons();
5885 prepareAxes(POSITION);
5886 addConfigurationProperty("touch.orientationAware", "1");
5887 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5888 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005889 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005890 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5891 NotifyMotionArgs args;
5892
5893 // Orientation 90.
5894 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5895 processSync(mapper);
5896
5897 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5898 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5899 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5900
5901 processUp(mapper);
5902 processSync(mapper);
5903 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5904}
5905
5906TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5907 addConfigurationProperty("touch.deviceType", "touchScreen");
5908 prepareButtons();
5909 prepareAxes(POSITION);
5910 addConfigurationProperty("touch.orientationAware", "1");
5911 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5912 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005913 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005914 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5915 NotifyMotionArgs args;
5916
5917 // Orientation 180.
5918 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5919 processSync(mapper);
5920
5921 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5922 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5923 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5924
5925 processUp(mapper);
5926 processSync(mapper);
5927 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5928}
5929
5930TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5931 addConfigurationProperty("touch.deviceType", "touchScreen");
5932 prepareButtons();
5933 prepareAxes(POSITION);
5934 addConfigurationProperty("touch.orientationAware", "1");
5935 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5936 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005937 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005938 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5939 NotifyMotionArgs args;
5940
5941 // Orientation 270.
5942 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5943 processSync(mapper);
5944
5945 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5946 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5947 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5948
5949 processUp(mapper);
5950 processSync(mapper);
5951 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5952}
5953
5954TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5955 addConfigurationProperty("touch.deviceType", "touchScreen");
5956 prepareButtons();
5957 prepareAxes(POSITION);
5958 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5959 // orientation-aware are affected by display rotation.
5960 addConfigurationProperty("touch.orientationAware", "0");
5961 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5962 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5963
5964 NotifyMotionArgs args;
5965
5966 // Orientation 90, Rotation 0.
5967 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005968 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005969 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5970 processSync(mapper);
5971
5972 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5973 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5974 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5975
5976 processUp(mapper);
5977 processSync(mapper);
5978 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5979
5980 // Orientation 90, Rotation 90.
5981 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005982 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005983 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
5984 processSync(mapper);
5985
5986 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5987 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5988 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5989
5990 processUp(mapper);
5991 processSync(mapper);
5992 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5993
5994 // Orientation 90, Rotation 180.
5995 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005996 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005997 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5998 processSync(mapper);
5999
6000 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6001 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6002 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6003
6004 processUp(mapper);
6005 processSync(mapper);
6006 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6007
6008 // Orientation 90, Rotation 270.
6009 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006010 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006011 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6012 RAW_Y_MAX - toRotatedRawY(75) + 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08006024TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006025 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006026 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006027 prepareButtons();
6028 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006029 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006030
6031 // These calculations are based on the input device calibration documentation.
6032 int32_t rawX = 100;
6033 int32_t rawY = 200;
6034 int32_t rawPressure = 10;
6035 int32_t rawToolMajor = 12;
6036 int32_t rawDistance = 2;
6037 int32_t rawTiltX = 30;
6038 int32_t rawTiltY = 110;
6039
6040 float x = toDisplayX(rawX);
6041 float y = toDisplayY(rawY);
6042 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6043 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6044 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6045 float distance = float(rawDistance);
6046
6047 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6048 float tiltScale = M_PI / 180;
6049 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6050 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6051 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6052 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6053
6054 processDown(mapper, rawX, rawY);
6055 processPressure(mapper, rawPressure);
6056 processToolMajor(mapper, rawToolMajor);
6057 processDistance(mapper, rawDistance);
6058 processTilt(mapper, rawTiltX, rawTiltY);
6059 processSync(mapper);
6060
6061 NotifyMotionArgs args;
6062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6064 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6065 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6066}
6067
Jason Gerecke489fda82012-09-07 17:19:40 -07006068TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006069 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006070 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07006071 prepareLocationCalibration();
6072 prepareButtons();
6073 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006074 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006075
6076 int32_t rawX = 100;
6077 int32_t rawY = 200;
6078
6079 float x = toDisplayX(toCookedX(rawX, rawY));
6080 float y = toDisplayY(toCookedY(rawX, rawY));
6081
6082 processDown(mapper, rawX, rawY);
6083 processSync(mapper);
6084
6085 NotifyMotionArgs args;
6086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6087 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6088 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6089}
6090
Michael Wrightd02c5b62014-02-10 15:10:22 -08006091TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006092 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006093 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006094 prepareButtons();
6095 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006096 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006097
6098 NotifyMotionArgs motionArgs;
6099 NotifyKeyArgs keyArgs;
6100
6101 processDown(mapper, 100, 200);
6102 processSync(mapper);
6103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6104 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6105 ASSERT_EQ(0, motionArgs.buttonState);
6106
6107 // press BTN_LEFT, release BTN_LEFT
6108 processKey(mapper, BTN_LEFT, 1);
6109 processSync(mapper);
6110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6112 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6113
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6115 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6116 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6117
Michael Wrightd02c5b62014-02-10 15:10:22 -08006118 processKey(mapper, BTN_LEFT, 0);
6119 processSync(mapper);
6120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006121 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006122 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006123
6124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006125 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006126 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006127
6128 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6129 processKey(mapper, BTN_RIGHT, 1);
6130 processKey(mapper, BTN_MIDDLE, 1);
6131 processSync(mapper);
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6134 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6135 motionArgs.buttonState);
6136
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6138 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6139 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6140
6141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6142 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6143 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6144 motionArgs.buttonState);
6145
Michael Wrightd02c5b62014-02-10 15:10:22 -08006146 processKey(mapper, BTN_RIGHT, 0);
6147 processSync(mapper);
6148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006149 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006150 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006151
6152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006153 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006154 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006155
6156 processKey(mapper, BTN_MIDDLE, 0);
6157 processSync(mapper);
6158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006159 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006160 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006161
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006164 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165
6166 // press BTN_BACK, release BTN_BACK
6167 processKey(mapper, BTN_BACK, 1);
6168 processSync(mapper);
6169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6170 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6171 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006172
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006175 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6176
6177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6178 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6179 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006180
6181 processKey(mapper, BTN_BACK, 0);
6182 processSync(mapper);
6183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006184 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006185 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006186
6187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006189 ASSERT_EQ(0, motionArgs.buttonState);
6190
Michael Wrightd02c5b62014-02-10 15:10:22 -08006191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6192 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6193 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6194
6195 // press BTN_SIDE, release BTN_SIDE
6196 processKey(mapper, BTN_SIDE, 1);
6197 processSync(mapper);
6198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6199 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6200 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006201
Michael Wrightd02c5b62014-02-10 15:10:22 -08006202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006204 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6205
6206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6207 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6208 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006209
6210 processKey(mapper, BTN_SIDE, 0);
6211 processSync(mapper);
6212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006213 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006214 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006215
6216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006218 ASSERT_EQ(0, motionArgs.buttonState);
6219
Michael Wrightd02c5b62014-02-10 15:10:22 -08006220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6221 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6222 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6223
6224 // press BTN_FORWARD, release BTN_FORWARD
6225 processKey(mapper, BTN_FORWARD, 1);
6226 processSync(mapper);
6227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6228 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6229 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006230
Michael Wrightd02c5b62014-02-10 15:10:22 -08006231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006233 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6234
6235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6236 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6237 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006238
6239 processKey(mapper, BTN_FORWARD, 0);
6240 processSync(mapper);
6241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006242 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006243 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006244
6245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006247 ASSERT_EQ(0, motionArgs.buttonState);
6248
Michael Wrightd02c5b62014-02-10 15:10:22 -08006249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6250 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6251 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6252
6253 // press BTN_EXTRA, release BTN_EXTRA
6254 processKey(mapper, BTN_EXTRA, 1);
6255 processSync(mapper);
6256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6257 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6258 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006259
Michael Wrightd02c5b62014-02-10 15:10:22 -08006260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006262 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6263
6264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6265 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6266 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267
6268 processKey(mapper, BTN_EXTRA, 0);
6269 processSync(mapper);
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006271 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006272 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006273
6274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006276 ASSERT_EQ(0, motionArgs.buttonState);
6277
Michael Wrightd02c5b62014-02-10 15:10:22 -08006278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6279 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6280 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6281
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6283
Michael Wrightd02c5b62014-02-10 15:10:22 -08006284 // press BTN_STYLUS, release BTN_STYLUS
6285 processKey(mapper, BTN_STYLUS, 1);
6286 processSync(mapper);
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006289 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6290
6291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6292 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6293 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006294
6295 processKey(mapper, BTN_STYLUS, 0);
6296 processSync(mapper);
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006298 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006299 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006300
6301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006303 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304
6305 // press BTN_STYLUS2, release BTN_STYLUS2
6306 processKey(mapper, BTN_STYLUS2, 1);
6307 processSync(mapper);
6308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006310 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6311
6312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6313 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6314 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006315
6316 processKey(mapper, BTN_STYLUS2, 0);
6317 processSync(mapper);
6318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006319 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006320 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006321
6322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006323 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006324 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006325
6326 // release touch
6327 processUp(mapper);
6328 processSync(mapper);
6329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6330 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6331 ASSERT_EQ(0, motionArgs.buttonState);
6332}
6333
6334TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006335 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006336 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006337 prepareButtons();
6338 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006339 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006340
6341 NotifyMotionArgs motionArgs;
6342
6343 // default tool type is finger
6344 processDown(mapper, 100, 200);
6345 processSync(mapper);
6346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6347 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6349
6350 // eraser
6351 processKey(mapper, BTN_TOOL_RUBBER, 1);
6352 processSync(mapper);
6353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6355 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6356
6357 // stylus
6358 processKey(mapper, BTN_TOOL_RUBBER, 0);
6359 processKey(mapper, BTN_TOOL_PEN, 1);
6360 processSync(mapper);
6361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6364
6365 // brush
6366 processKey(mapper, BTN_TOOL_PEN, 0);
6367 processKey(mapper, BTN_TOOL_BRUSH, 1);
6368 processSync(mapper);
6369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6370 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6371 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6372
6373 // pencil
6374 processKey(mapper, BTN_TOOL_BRUSH, 0);
6375 processKey(mapper, BTN_TOOL_PENCIL, 1);
6376 processSync(mapper);
6377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6378 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6379 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6380
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006381 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006382 processKey(mapper, BTN_TOOL_PENCIL, 0);
6383 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6384 processSync(mapper);
6385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6388
6389 // mouse
6390 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6391 processKey(mapper, BTN_TOOL_MOUSE, 1);
6392 processSync(mapper);
6393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6394 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6395 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6396
6397 // lens
6398 processKey(mapper, BTN_TOOL_MOUSE, 0);
6399 processKey(mapper, BTN_TOOL_LENS, 1);
6400 processSync(mapper);
6401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6403 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6404
6405 // double-tap
6406 processKey(mapper, BTN_TOOL_LENS, 0);
6407 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6408 processSync(mapper);
6409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6411 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6412
6413 // triple-tap
6414 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6415 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6416 processSync(mapper);
6417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6420
6421 // quad-tap
6422 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6423 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6424 processSync(mapper);
6425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6426 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6427 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6428
6429 // finger
6430 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6431 processKey(mapper, BTN_TOOL_FINGER, 1);
6432 processSync(mapper);
6433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6434 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6435 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6436
6437 // stylus trumps finger
6438 processKey(mapper, BTN_TOOL_PEN, 1);
6439 processSync(mapper);
6440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6441 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6442 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6443
6444 // eraser trumps stylus
6445 processKey(mapper, BTN_TOOL_RUBBER, 1);
6446 processSync(mapper);
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6449 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6450
6451 // mouse trumps eraser
6452 processKey(mapper, BTN_TOOL_MOUSE, 1);
6453 processSync(mapper);
6454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6455 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6456 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6457
6458 // back to default tool type
6459 processKey(mapper, BTN_TOOL_MOUSE, 0);
6460 processKey(mapper, BTN_TOOL_RUBBER, 0);
6461 processKey(mapper, BTN_TOOL_PEN, 0);
6462 processKey(mapper, BTN_TOOL_FINGER, 0);
6463 processSync(mapper);
6464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6465 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6466 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6467}
6468
6469TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006470 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006471 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006472 prepareButtons();
6473 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006474 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006475 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006476
6477 NotifyMotionArgs motionArgs;
6478
6479 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6480 processKey(mapper, BTN_TOOL_FINGER, 1);
6481 processMove(mapper, 100, 200);
6482 processSync(mapper);
6483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6484 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6486 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6487
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6489 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6491 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6492
6493 // move a little
6494 processMove(mapper, 150, 250);
6495 processSync(mapper);
6496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6497 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6499 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6500
6501 // down when BTN_TOUCH is pressed, pressure defaults to 1
6502 processKey(mapper, BTN_TOUCH, 1);
6503 processSync(mapper);
6504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6505 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6507 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6508
6509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6510 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6512 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6513
6514 // up when BTN_TOUCH is released, hover restored
6515 processKey(mapper, BTN_TOUCH, 0);
6516 processSync(mapper);
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6518 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6520 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6521
6522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6523 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6525 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6526
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6528 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6530 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6531
6532 // exit hover when pointer goes away
6533 processKey(mapper, BTN_TOOL_FINGER, 0);
6534 processSync(mapper);
6535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6536 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6538 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6539}
6540
6541TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006542 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006543 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006544 prepareButtons();
6545 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006546 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006547
6548 NotifyMotionArgs motionArgs;
6549
6550 // initially hovering because pressure is 0
6551 processDown(mapper, 100, 200);
6552 processPressure(mapper, 0);
6553 processSync(mapper);
6554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6555 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6557 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6558
6559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6560 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6562 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6563
6564 // move a little
6565 processMove(mapper, 150, 250);
6566 processSync(mapper);
6567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6568 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6570 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6571
6572 // down when pressure is non-zero
6573 processPressure(mapper, RAW_PRESSURE_MAX);
6574 processSync(mapper);
6575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6576 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6578 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6579
6580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6581 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6583 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6584
6585 // up when pressure becomes 0, hover restored
6586 processPressure(mapper, 0);
6587 processSync(mapper);
6588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6589 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6591 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6592
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6594 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6596 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6597
6598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6599 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6601 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6602
6603 // exit hover when pointer goes away
6604 processUp(mapper);
6605 processSync(mapper);
6606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6607 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6609 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6610}
6611
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006612TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6613 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006614 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006615 prepareButtons();
6616 prepareAxes(POSITION | PRESSURE);
6617 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6618
6619 // Touch down.
6620 processDown(mapper, 100, 200);
6621 processPressure(mapper, 1);
6622 processSync(mapper);
6623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6624 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6625
6626 // Reset the mapper. This should cancel the ongoing gesture.
6627 resetMapper(mapper, ARBITRARY_TIME);
6628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6629 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6630
6631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6632}
6633
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006634TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6635 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006636 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006637 prepareButtons();
6638 prepareAxes(POSITION | PRESSURE);
6639 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6640
6641 // Set the initial state for the touch pointer.
6642 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6643 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6644 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6645 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6646
6647 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006648 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6649 // does not generate any events.
6650 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006651
6652 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6653 // the recreated touch state to generate a down event.
6654 processSync(mapper);
6655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6656 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6657
6658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6659}
6660
lilinnan687e58f2022-07-19 16:00:50 +08006661TEST_F(SingleTouchInputMapperTest,
6662 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6663 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006664 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006665 prepareButtons();
6666 prepareAxes(POSITION);
6667 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6668 NotifyMotionArgs motionArgs;
6669
6670 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006671 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006672 processSync(mapper);
6673
6674 // We should receive a down event
6675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6676 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6677
6678 // Change display id
6679 clearViewports();
6680 prepareSecondaryDisplay(ViewportType::INTERNAL);
6681
6682 // We should receive a cancel event
6683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6684 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6685 // Then receive reset called
6686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6687}
6688
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006689TEST_F(SingleTouchInputMapperTest,
6690 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6691 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006692 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006693 prepareButtons();
6694 prepareAxes(POSITION);
6695 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6697 NotifyMotionArgs motionArgs;
6698
6699 // Start a new gesture.
6700 processDown(mapper, 100, 200);
6701 processSync(mapper);
6702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6703 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6704
6705 // Make the viewport inactive. This will put the device in disabled mode.
6706 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6707 viewport->isActive = false;
6708 mFakePolicy->updateViewport(*viewport);
6709 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6710
6711 // We should receive a cancel event for the ongoing gesture.
6712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6713 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6714 // Then we should be notified that the device was reset.
6715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6716
6717 // No events are generated while the viewport is inactive.
6718 processMove(mapper, 101, 201);
6719 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006720 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006721 processSync(mapper);
6722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6723
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006724 // Start a new gesture while the viewport is still inactive.
6725 processDown(mapper, 300, 400);
6726 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6727 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6728 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6729 processSync(mapper);
6730
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006731 // Make the viewport active again. The device should resume processing events.
6732 viewport->isActive = true;
6733 mFakePolicy->updateViewport(*viewport);
6734 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6735
6736 // The device is reset because it changes back to direct mode, without generating any events.
6737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6739
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006740 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006741 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6743 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006744
6745 // No more events.
6746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6748}
6749
Prabir Pradhan211ba622022-10-31 21:09:21 +00006750TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6751 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006752 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006753 prepareButtons();
6754 prepareAxes(POSITION);
6755 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6757
6758 // Press a stylus button.
6759 processKey(mapper, BTN_STYLUS, 1);
6760 processSync(mapper);
6761
6762 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6763 processDown(mapper, 100, 200);
6764 processSync(mapper);
6765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6766 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6767 WithCoords(toDisplayX(100), toDisplayY(200)),
6768 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6770 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6771 WithCoords(toDisplayX(100), toDisplayY(200)),
6772 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6773
6774 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6775 // the button has not actually been released, since there will be no pointers through which the
6776 // button state can be reported. The event is generated at the location of the pointer before
6777 // it went up.
6778 processUp(mapper);
6779 processSync(mapper);
6780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6781 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6782 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6784 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6785 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6786}
6787
Prabir Pradhan5632d622021-09-06 07:57:20 -07006788// --- TouchDisplayProjectionTest ---
6789
6790class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6791public:
6792 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6793 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6794 // rotated equivalent of the given un-rotated physical display bounds.
Michael Wrighta9cf4192022-12-01 23:46:39 +00006795 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006796 uint32_t inverseRotationFlags;
6797 auto width = DISPLAY_WIDTH;
6798 auto height = DISPLAY_HEIGHT;
6799 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006800 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006801 inverseRotationFlags = ui::Transform::ROT_270;
6802 std::swap(width, height);
6803 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006804 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006805 inverseRotationFlags = ui::Transform::ROT_180;
6806 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006807 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006808 inverseRotationFlags = ui::Transform::ROT_90;
6809 std::swap(width, height);
6810 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006811 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006812 inverseRotationFlags = ui::Transform::ROT_0;
6813 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006814 }
6815
6816 const ui::Transform rotation(inverseRotationFlags, width, height);
6817 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6818
6819 std::optional<DisplayViewport> internalViewport =
6820 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6821 DisplayViewport& v = *internalViewport;
6822 v.displayId = DISPLAY_ID;
6823 v.orientation = orientation;
6824
6825 v.logicalLeft = 0;
6826 v.logicalTop = 0;
6827 v.logicalRight = 100;
6828 v.logicalBottom = 100;
6829
6830 v.physicalLeft = rotatedPhysicalDisplay.left;
6831 v.physicalTop = rotatedPhysicalDisplay.top;
6832 v.physicalRight = rotatedPhysicalDisplay.right;
6833 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6834
6835 v.deviceWidth = width;
6836 v.deviceHeight = height;
6837
6838 v.isActive = true;
6839 v.uniqueId = UNIQUE_ID;
6840 v.type = ViewportType::INTERNAL;
6841 mFakePolicy->updateViewport(v);
6842 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6843 }
6844
6845 void assertReceivedMove(const Point& point) {
6846 NotifyMotionArgs motionArgs;
6847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6849 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6851 1, 0, 0, 0, 0, 0, 0, 0));
6852 }
6853};
6854
6855TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6856 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006857 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006858
6859 prepareButtons();
6860 prepareAxes(POSITION);
6861 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6862
6863 NotifyMotionArgs motionArgs;
6864
6865 // Configure the DisplayViewport such that the logical display maps to a subsection of
6866 // the display panel called the physical display. Here, the physical display is bounded by the
6867 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6868 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6869 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6870 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6871
Michael Wrighta9cf4192022-12-01 23:46:39 +00006872 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006873 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6874
6875 // Touches outside the physical display should be ignored, and should not generate any
6876 // events. Ensure touches at the following points that lie outside of the physical display
6877 // area do not generate any events.
6878 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6879 processDown(mapper, toRawX(point.x), toRawY(point.y));
6880 processSync(mapper);
6881 processUp(mapper);
6882 processSync(mapper);
6883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6884 << "Unexpected event generated for touch outside physical display at point: "
6885 << point.x << ", " << point.y;
6886 }
6887 }
6888}
6889
6890TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6891 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006892 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006893
6894 prepareButtons();
6895 prepareAxes(POSITION);
6896 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6897
6898 NotifyMotionArgs motionArgs;
6899
6900 // Configure the DisplayViewport such that the logical display maps to a subsection of
6901 // the display panel called the physical display. Here, the physical display is bounded by the
6902 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6903 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6904
Michael Wrighta9cf4192022-12-01 23:46:39 +00006905 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006906 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6907
6908 // Touches that start outside the physical display should be ignored until it enters the
6909 // physical display bounds, at which point it should generate a down event. Start a touch at
6910 // the point (5, 100), which is outside the physical display bounds.
6911 static const Point kOutsidePoint{5, 100};
6912 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6913 processSync(mapper);
6914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6915
6916 // Move the touch into the physical display area. This should generate a pointer down.
6917 processMove(mapper, toRawX(11), toRawY(21));
6918 processSync(mapper);
6919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6920 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6921 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6922 ASSERT_NO_FATAL_FAILURE(
6923 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6924
6925 // Move the touch inside the physical display area. This should generate a pointer move.
6926 processMove(mapper, toRawX(69), toRawY(159));
6927 processSync(mapper);
6928 assertReceivedMove({69, 159});
6929
6930 // Move outside the physical display area. Since the pointer is already down, this should
6931 // now continue generating events.
6932 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6933 processSync(mapper);
6934 assertReceivedMove(kOutsidePoint);
6935
6936 // Release. This should generate a pointer up.
6937 processUp(mapper);
6938 processSync(mapper);
6939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6940 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6942 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6943
6944 // Ensure no more events were generated.
6945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6947 }
6948}
6949
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006950// --- ExternalStylusFusionTest ---
6951
6952class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6953public:
6954 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6955 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006956 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006957 prepareButtons();
6958 prepareAxes(POSITION);
6959 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6960
6961 mStylusState.when = ARBITRARY_TIME;
6962 mStylusState.pressure = 0.f;
6963 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
6964 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
6965 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
6966 processExternalStylusState(mapper);
6967 return mapper;
6968 }
6969
6970 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6971 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6972 for (const NotifyArgs& args : generatedArgs) {
6973 mFakeListener->notify(args);
6974 }
6975 // Loop the reader to flush the input listener queue.
6976 mReader->loopOnce();
6977 return generatedArgs;
6978 }
6979
6980protected:
6981 StylusState mStylusState{};
6982 static constexpr uint32_t EXPECTED_SOURCE =
6983 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
6984
6985 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6986 auto toolTypeSource =
6987 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6988
6989 // The first pointer is withheld.
6990 processDown(mapper, 100, 200);
6991 processSync(mapper);
6992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6993 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6994 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6995
6996 // The external stylus reports pressure. The withheld finger pointer is released as a
6997 // stylus.
6998 mStylusState.pressure = 1.f;
6999 processExternalStylusState(mapper);
7000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7001 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7002 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7003
7004 // Subsequent pointer events are not withheld.
7005 processMove(mapper, 101, 201);
7006 processSync(mapper);
7007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7008 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7009
7010 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7012 }
7013
7014 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7015 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7016
7017 // Releasing the touch pointer ends the gesture.
7018 processUp(mapper);
7019 processSync(mapper);
7020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7021 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7022 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7023
7024 mStylusState.pressure = 0.f;
7025 processExternalStylusState(mapper);
7026 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7028 }
7029
7030 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7031 auto toolTypeSource =
7032 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER));
7033
7034 // The first pointer is withheld when an external stylus is connected,
7035 // and a timeout is requested.
7036 processDown(mapper, 100, 200);
7037 processSync(mapper);
7038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7039 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7040 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7041
7042 // If the timeout expires early, it is requested again.
7043 handleTimeout(mapper, ARBITRARY_TIME + 1);
7044 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7045 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7046
7047 // When the timeout expires, the withheld touch is released as a finger pointer.
7048 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7050 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7051
7052 // Subsequent pointer events are not withheld.
7053 processMove(mapper, 101, 201);
7054 processSync(mapper);
7055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7056 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7057 processUp(mapper);
7058 processSync(mapper);
7059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7060 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7061
7062 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7064 }
7065
7066private:
7067 InputDeviceInfo mExternalStylusDeviceInfo{};
7068};
7069
7070TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7071 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7072 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7073}
7074
7075TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7076 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7077 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7078}
7079
7080TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7081 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7082 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7083}
7084
7085// Test a successful stylus fusion gesture where the pressure is reported by the external
7086// before the touch is reported by the touchscreen.
7087TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7088 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7089 auto toolTypeSource =
7090 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7091
7092 // The external stylus reports pressure first. It is ignored for now.
7093 mStylusState.pressure = 1.f;
7094 processExternalStylusState(mapper);
7095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7096 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7097
7098 // When the touch goes down afterwards, it is reported as a stylus pointer.
7099 processDown(mapper, 100, 200);
7100 processSync(mapper);
7101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7102 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7103 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7104
7105 processMove(mapper, 101, 201);
7106 processSync(mapper);
7107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7108 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7109 processUp(mapper);
7110 processSync(mapper);
7111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7112 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7113
7114 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7116}
7117
7118TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7119 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7120
7121 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7122 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7123
7124 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7125 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7126 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7127 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7128}
7129
7130TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7131 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7132 auto toolTypeSource =
7133 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7134
7135 mStylusState.pressure = 0.8f;
7136 processExternalStylusState(mapper);
7137 processDown(mapper, 100, 200);
7138 processSync(mapper);
7139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7140 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7141 WithPressure(0.8f))));
7142 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7143
7144 // The external stylus reports a pressure change. We wait for some time for a touch event.
7145 mStylusState.pressure = 0.6f;
7146 processExternalStylusState(mapper);
7147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7148 ASSERT_NO_FATAL_FAILURE(
7149 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7150
7151 // If a touch is reported within the timeout, it reports the updated pressure.
7152 processMove(mapper, 101, 201);
7153 processSync(mapper);
7154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7155 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7156 WithPressure(0.6f))));
7157 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7158
7159 // There is another pressure change.
7160 mStylusState.pressure = 0.5f;
7161 processExternalStylusState(mapper);
7162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7163 ASSERT_NO_FATAL_FAILURE(
7164 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7165
7166 // If a touch is not reported within the timeout, a move event is generated to report
7167 // the new pressure.
7168 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7170 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7171 WithPressure(0.5f))));
7172
7173 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7174 // repeated indefinitely.
7175 mStylusState.pressure = 0.0f;
7176 processExternalStylusState(mapper);
7177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7178 ASSERT_NO_FATAL_FAILURE(
7179 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7180 processMove(mapper, 102, 202);
7181 processSync(mapper);
7182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7183 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7184 WithPressure(0.5f))));
7185 processMove(mapper, 103, 203);
7186 processSync(mapper);
7187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7188 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7189 WithPressure(0.5f))));
7190
7191 processUp(mapper);
7192 processSync(mapper);
7193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7194 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7195 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7196
7197 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7199}
7200
7201TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7202 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7203 auto source = WithSource(EXPECTED_SOURCE);
7204
7205 mStylusState.pressure = 1.f;
7206 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_ERASER;
7207 processExternalStylusState(mapper);
7208 processDown(mapper, 100, 200);
7209 processSync(mapper);
7210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7211 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7212 WithToolType(AMOTION_EVENT_TOOL_TYPE_ERASER))));
7213 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7214
7215 // The external stylus reports a tool change. We wait for some time for a touch event.
7216 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7217 processExternalStylusState(mapper);
7218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7219 ASSERT_NO_FATAL_FAILURE(
7220 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7221
7222 // If a touch is reported within the timeout, it reports the updated pressure.
7223 processMove(mapper, 101, 201);
7224 processSync(mapper);
7225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7226 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7227 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7228 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7229
7230 // There is another tool type change.
7231 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
7232 processExternalStylusState(mapper);
7233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7234 ASSERT_NO_FATAL_FAILURE(
7235 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7236
7237 // If a touch is not reported within the timeout, a move event is generated to report
7238 // the new tool type.
7239 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7241 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7242 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7243
7244 processUp(mapper);
7245 processSync(mapper);
7246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7247 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
7248 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7249
7250 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7252}
7253
7254TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7255 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7256 auto toolTypeSource =
7257 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7258
7259 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7260
7261 // The external stylus reports a button change. We wait for some time for a touch event.
7262 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7263 processExternalStylusState(mapper);
7264 ASSERT_NO_FATAL_FAILURE(
7265 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7266
7267 // If a touch is reported within the timeout, it reports the updated button state.
7268 processMove(mapper, 101, 201);
7269 processSync(mapper);
7270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7271 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7272 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7274 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7275 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7276 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7277
7278 // The button is now released.
7279 mStylusState.buttons = 0;
7280 processExternalStylusState(mapper);
7281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7282 ASSERT_NO_FATAL_FAILURE(
7283 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7284
7285 // If a touch is not reported within the timeout, a move event is generated to report
7286 // the new button state.
7287 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7289 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7290 WithButtonState(0))));
7291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007292 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7293 WithButtonState(0))));
7294
7295 processUp(mapper);
7296 processSync(mapper);
7297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007298 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7299
7300 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7302}
7303
Michael Wrightd02c5b62014-02-10 15:10:22 -08007304// --- MultiTouchInputMapperTest ---
7305
7306class MultiTouchInputMapperTest : public TouchInputMapperTest {
7307protected:
7308 void prepareAxes(int axes);
7309
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007310 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7311 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7312 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7313 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7314 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7315 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7316 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7317 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7318 void processId(MultiTouchInputMapper& mapper, int32_t id);
7319 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7320 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7321 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007322 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007323 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007324 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7325 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007326};
7327
7328void MultiTouchInputMapperTest::prepareAxes(int axes) {
7329 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007330 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7331 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007332 }
7333 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007334 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7335 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007336 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007337 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7338 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007339 }
7340 }
7341 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007342 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7343 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007344 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007345 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007346 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007347 }
7348 }
7349 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007350 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7351 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007352 }
7353 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007354 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7355 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007356 }
7357 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007358 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7359 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007360 }
7361 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007362 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7363 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007364 }
7365 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007366 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7367 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007368 }
7369 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007370 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007371 }
7372}
7373
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007374void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7375 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007378}
7379
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007380void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7381 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007382 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007383}
7384
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007385void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7386 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007387 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007388}
7389
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007390void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007391 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007392}
7393
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007394void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007395 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007396}
7397
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007398void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7399 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007401}
7402
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007403void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007404 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007405}
7406
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007407void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007409}
7410
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007411void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007413}
7414
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007415void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007416 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007417}
7418
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007419void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007421}
7422
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007423void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7424 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007426}
7427
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007428void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7429 int32_t value) {
7430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7432}
7433
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007434void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007435 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007436}
7437
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007438void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7439 nsecs_t readTime) {
7440 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007441}
7442
Michael Wrightd02c5b62014-02-10 15:10:22 -08007443TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007444 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007445 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007446 prepareAxes(POSITION);
7447 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007448 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007449
arthurhungdcef2dc2020-08-11 14:47:50 +08007450 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007451
7452 NotifyMotionArgs motionArgs;
7453
7454 // Two fingers down at once.
7455 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7456 processPosition(mapper, x1, y1);
7457 processMTSync(mapper);
7458 processPosition(mapper, x2, y2);
7459 processMTSync(mapper);
7460 processSync(mapper);
7461
7462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7463 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7464 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7465 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7466 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7467 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7468 ASSERT_EQ(0, motionArgs.flags);
7469 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7470 ASSERT_EQ(0, motionArgs.buttonState);
7471 ASSERT_EQ(0, motionArgs.edgeFlags);
7472 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7473 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7476 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7477 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7478 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7479 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7480
7481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7482 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7483 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7484 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7485 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007486 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007487 ASSERT_EQ(0, motionArgs.flags);
7488 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7489 ASSERT_EQ(0, motionArgs.buttonState);
7490 ASSERT_EQ(0, motionArgs.edgeFlags);
7491 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7492 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7493 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7494 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7495 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7497 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7499 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7500 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7501 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7502 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7503
7504 // Move.
7505 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7506 processPosition(mapper, x1, y1);
7507 processMTSync(mapper);
7508 processPosition(mapper, x2, y2);
7509 processMTSync(mapper);
7510 processSync(mapper);
7511
7512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7513 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7514 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7515 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7516 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7517 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7518 ASSERT_EQ(0, motionArgs.flags);
7519 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7520 ASSERT_EQ(0, motionArgs.buttonState);
7521 ASSERT_EQ(0, motionArgs.edgeFlags);
7522 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7523 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7524 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7525 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7526 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7528 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7530 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7531 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7532 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7533 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7534
7535 // First finger up.
7536 x2 += 15; y2 -= 20;
7537 processPosition(mapper, x2, y2);
7538 processMTSync(mapper);
7539 processSync(mapper);
7540
7541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7542 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7543 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7544 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7545 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007546 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007547 ASSERT_EQ(0, motionArgs.flags);
7548 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7549 ASSERT_EQ(0, motionArgs.buttonState);
7550 ASSERT_EQ(0, motionArgs.edgeFlags);
7551 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7552 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7554 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7555 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7557 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7559 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7560 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7561 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7562 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7563
7564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7565 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7566 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7567 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7568 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7570 ASSERT_EQ(0, motionArgs.flags);
7571 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7572 ASSERT_EQ(0, motionArgs.buttonState);
7573 ASSERT_EQ(0, motionArgs.edgeFlags);
7574 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7575 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7576 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7578 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7579 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7580 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7581 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7582
7583 // Move.
7584 x2 += 20; y2 -= 25;
7585 processPosition(mapper, x2, y2);
7586 processMTSync(mapper);
7587 processSync(mapper);
7588
7589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7590 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7591 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7592 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7593 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7594 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7595 ASSERT_EQ(0, motionArgs.flags);
7596 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7597 ASSERT_EQ(0, motionArgs.buttonState);
7598 ASSERT_EQ(0, motionArgs.edgeFlags);
7599 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7600 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7601 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7603 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7604 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7605 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7606 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7607
7608 // New finger down.
7609 int32_t x3 = 700, y3 = 300;
7610 processPosition(mapper, x2, y2);
7611 processMTSync(mapper);
7612 processPosition(mapper, x3, y3);
7613 processMTSync(mapper);
7614 processSync(mapper);
7615
7616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7617 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7618 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7619 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7620 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007621 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007622 ASSERT_EQ(0, motionArgs.flags);
7623 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7624 ASSERT_EQ(0, motionArgs.buttonState);
7625 ASSERT_EQ(0, motionArgs.edgeFlags);
7626 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7627 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7629 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7630 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7632 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7634 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7635 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7636 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7637 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7638
7639 // Second finger up.
7640 x3 += 30; y3 -= 20;
7641 processPosition(mapper, x3, y3);
7642 processMTSync(mapper);
7643 processSync(mapper);
7644
7645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7646 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7647 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7648 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7649 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007650 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007651 ASSERT_EQ(0, motionArgs.flags);
7652 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7653 ASSERT_EQ(0, motionArgs.buttonState);
7654 ASSERT_EQ(0, motionArgs.edgeFlags);
7655 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7656 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7657 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7658 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7659 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7661 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7663 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7664 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7665 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7666 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7667
7668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7669 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7670 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7671 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7672 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7673 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7674 ASSERT_EQ(0, motionArgs.flags);
7675 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7676 ASSERT_EQ(0, motionArgs.buttonState);
7677 ASSERT_EQ(0, motionArgs.edgeFlags);
7678 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7679 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7680 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7682 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7683 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7684 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7685 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7686
7687 // Last finger up.
7688 processMTSync(mapper);
7689 processSync(mapper);
7690
7691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7692 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7693 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7694 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7695 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7696 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7697 ASSERT_EQ(0, motionArgs.flags);
7698 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7699 ASSERT_EQ(0, motionArgs.buttonState);
7700 ASSERT_EQ(0, motionArgs.edgeFlags);
7701 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7702 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7703 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7705 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7706 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7707 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7708 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7709
7710 // Should not have sent any more keys or motions.
7711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7713}
7714
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007715TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7716 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007717 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007718
7719 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7720 /*fuzz*/ 0, /*resolution*/ 10);
7721 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7722 /*fuzz*/ 0, /*resolution*/ 11);
7723 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7724 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7725 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7726 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7727 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7728 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7729 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7730 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7731
7732 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7733
7734 // X and Y axes
7735 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7736 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7737 // Touch major and minor
7738 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7739 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7740 // Tool major and minor
7741 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7742 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7743}
7744
7745TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7746 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007747 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007748
7749 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7750 /*fuzz*/ 0, /*resolution*/ 10);
7751 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7752 /*fuzz*/ 0, /*resolution*/ 11);
7753
7754 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7755
7756 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7757
7758 // Touch major and minor
7759 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7760 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7761 // Tool major and minor
7762 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7763 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7764}
7765
Michael Wrightd02c5b62014-02-10 15:10:22 -08007766TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007767 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007768 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007769 prepareAxes(POSITION | ID);
7770 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007771 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007772
arthurhungdcef2dc2020-08-11 14:47:50 +08007773 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007774
7775 NotifyMotionArgs motionArgs;
7776
7777 // Two fingers down at once.
7778 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7779 processPosition(mapper, x1, y1);
7780 processId(mapper, 1);
7781 processMTSync(mapper);
7782 processPosition(mapper, x2, y2);
7783 processId(mapper, 2);
7784 processMTSync(mapper);
7785 processSync(mapper);
7786
7787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7788 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7789 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7790 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7791 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7792 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7793 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7794
7795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007796 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007797 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7798 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7799 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7800 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7803 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7805 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7806
7807 // Move.
7808 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7809 processPosition(mapper, x1, y1);
7810 processId(mapper, 1);
7811 processMTSync(mapper);
7812 processPosition(mapper, x2, y2);
7813 processId(mapper, 2);
7814 processMTSync(mapper);
7815 processSync(mapper);
7816
7817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7818 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7819 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7820 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7821 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7822 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7823 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7825 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7827 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7828
7829 // First finger up.
7830 x2 += 15; y2 -= 20;
7831 processPosition(mapper, x2, y2);
7832 processId(mapper, 2);
7833 processMTSync(mapper);
7834 processSync(mapper);
7835
7836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007837 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007838 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7839 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7840 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7841 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7842 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7844 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7845 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7846 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7847
7848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7850 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7851 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7852 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7854 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7855
7856 // Move.
7857 x2 += 20; y2 -= 25;
7858 processPosition(mapper, x2, y2);
7859 processId(mapper, 2);
7860 processMTSync(mapper);
7861 processSync(mapper);
7862
7863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7864 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7865 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7866 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7867 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7869 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7870
7871 // New finger down.
7872 int32_t x3 = 700, y3 = 300;
7873 processPosition(mapper, x2, y2);
7874 processId(mapper, 2);
7875 processMTSync(mapper);
7876 processPosition(mapper, x3, y3);
7877 processId(mapper, 3);
7878 processMTSync(mapper);
7879 processSync(mapper);
7880
7881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007882 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007883 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7884 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7885 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7886 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7887 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7889 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7891 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7892
7893 // Second finger up.
7894 x3 += 30; y3 -= 20;
7895 processPosition(mapper, x3, y3);
7896 processId(mapper, 3);
7897 processMTSync(mapper);
7898 processSync(mapper);
7899
7900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007901 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007902 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7903 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7904 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7905 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7906 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7908 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7909 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7910 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7911
7912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7913 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7914 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7915 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7916 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7918 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7919
7920 // Last finger up.
7921 processMTSync(mapper);
7922 processSync(mapper);
7923
7924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7925 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7926 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7927 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7928 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7930 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7931
7932 // Should not have sent any more keys or motions.
7933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7935}
7936
7937TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007938 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007939 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007940 prepareAxes(POSITION | ID | SLOT);
7941 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007942 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007943
arthurhungdcef2dc2020-08-11 14:47:50 +08007944 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007945
7946 NotifyMotionArgs motionArgs;
7947
7948 // Two fingers down at once.
7949 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7950 processPosition(mapper, x1, y1);
7951 processId(mapper, 1);
7952 processSlot(mapper, 1);
7953 processPosition(mapper, x2, y2);
7954 processId(mapper, 2);
7955 processSync(mapper);
7956
7957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7958 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7959 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7960 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7961 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7963 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7964
7965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007966 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007967 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7968 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7969 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7970 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7971 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7972 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7973 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7974 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7975 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7976
7977 // Move.
7978 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7979 processSlot(mapper, 0);
7980 processPosition(mapper, x1, y1);
7981 processSlot(mapper, 1);
7982 processPosition(mapper, x2, y2);
7983 processSync(mapper);
7984
7985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7986 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7987 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7988 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7989 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7990 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7991 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7992 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7993 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7995 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7996
7997 // First finger up.
7998 x2 += 15; y2 -= 20;
7999 processSlot(mapper, 0);
8000 processId(mapper, -1);
8001 processSlot(mapper, 1);
8002 processPosition(mapper, x2, y2);
8003 processSync(mapper);
8004
8005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008006 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008007 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8008 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8009 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8010 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8011 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8012 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8013 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8015 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8016
8017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8018 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8019 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8020 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8021 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8023 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8024
8025 // Move.
8026 x2 += 20; y2 -= 25;
8027 processPosition(mapper, x2, y2);
8028 processSync(mapper);
8029
8030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8031 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8032 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8033 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8034 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8035 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8036 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8037
8038 // New finger down.
8039 int32_t x3 = 700, y3 = 300;
8040 processPosition(mapper, x2, y2);
8041 processSlot(mapper, 0);
8042 processId(mapper, 3);
8043 processPosition(mapper, x3, y3);
8044 processSync(mapper);
8045
8046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008047 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008048 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8049 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8050 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8051 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8052 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8053 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8054 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8055 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8056 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8057
8058 // Second finger up.
8059 x3 += 30; y3 -= 20;
8060 processSlot(mapper, 1);
8061 processId(mapper, -1);
8062 processSlot(mapper, 0);
8063 processPosition(mapper, x3, y3);
8064 processSync(mapper);
8065
8066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008067 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008068 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8069 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8070 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8071 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8072 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8073 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8074 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8076 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8077
8078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8079 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8080 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8081 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8082 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8083 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8084 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8085
8086 // Last finger up.
8087 processId(mapper, -1);
8088 processSync(mapper);
8089
8090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8091 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8092 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8093 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8094 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8095 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8096 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8097
8098 // Should not have sent any more keys or motions.
8099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8101}
8102
8103TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008104 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008105 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008106 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008107 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008108
8109 // These calculations are based on the input device calibration documentation.
8110 int32_t rawX = 100;
8111 int32_t rawY = 200;
8112 int32_t rawTouchMajor = 7;
8113 int32_t rawTouchMinor = 6;
8114 int32_t rawToolMajor = 9;
8115 int32_t rawToolMinor = 8;
8116 int32_t rawPressure = 11;
8117 int32_t rawDistance = 0;
8118 int32_t rawOrientation = 3;
8119 int32_t id = 5;
8120
8121 float x = toDisplayX(rawX);
8122 float y = toDisplayY(rawY);
8123 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8124 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8125 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8126 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8127 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8128 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8129 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8130 float distance = float(rawDistance);
8131
8132 processPosition(mapper, rawX, rawY);
8133 processTouchMajor(mapper, rawTouchMajor);
8134 processTouchMinor(mapper, rawTouchMinor);
8135 processToolMajor(mapper, rawToolMajor);
8136 processToolMinor(mapper, rawToolMinor);
8137 processPressure(mapper, rawPressure);
8138 processOrientation(mapper, rawOrientation);
8139 processDistance(mapper, rawDistance);
8140 processId(mapper, id);
8141 processMTSync(mapper);
8142 processSync(mapper);
8143
8144 NotifyMotionArgs args;
8145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8146 ASSERT_EQ(0, args.pointerProperties[0].id);
8147 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8148 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8149 orientation, distance));
8150}
8151
8152TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008153 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008154 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008155 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8156 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008157 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008158
8159 // These calculations are based on the input device calibration documentation.
8160 int32_t rawX = 100;
8161 int32_t rawY = 200;
8162 int32_t rawTouchMajor = 140;
8163 int32_t rawTouchMinor = 120;
8164 int32_t rawToolMajor = 180;
8165 int32_t rawToolMinor = 160;
8166
8167 float x = toDisplayX(rawX);
8168 float y = toDisplayY(rawY);
8169 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8170 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8171 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8172 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8173 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8174
8175 processPosition(mapper, rawX, rawY);
8176 processTouchMajor(mapper, rawTouchMajor);
8177 processTouchMinor(mapper, rawTouchMinor);
8178 processToolMajor(mapper, rawToolMajor);
8179 processToolMinor(mapper, rawToolMinor);
8180 processMTSync(mapper);
8181 processSync(mapper);
8182
8183 NotifyMotionArgs args;
8184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8186 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8187}
8188
8189TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008190 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008191 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008192 prepareAxes(POSITION | TOUCH | TOOL);
8193 addConfigurationProperty("touch.size.calibration", "diameter");
8194 addConfigurationProperty("touch.size.scale", "10");
8195 addConfigurationProperty("touch.size.bias", "160");
8196 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008197 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008198
8199 // These calculations are based on the input device calibration documentation.
8200 // Note: We only provide a single common touch/tool value because the device is assumed
8201 // not to emit separate values for each pointer (isSummed = 1).
8202 int32_t rawX = 100;
8203 int32_t rawY = 200;
8204 int32_t rawX2 = 150;
8205 int32_t rawY2 = 250;
8206 int32_t rawTouchMajor = 5;
8207 int32_t rawToolMajor = 8;
8208
8209 float x = toDisplayX(rawX);
8210 float y = toDisplayY(rawY);
8211 float x2 = toDisplayX(rawX2);
8212 float y2 = toDisplayY(rawY2);
8213 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8214 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8215 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8216
8217 processPosition(mapper, rawX, rawY);
8218 processTouchMajor(mapper, rawTouchMajor);
8219 processToolMajor(mapper, rawToolMajor);
8220 processMTSync(mapper);
8221 processPosition(mapper, rawX2, rawY2);
8222 processTouchMajor(mapper, rawTouchMajor);
8223 processToolMajor(mapper, rawToolMajor);
8224 processMTSync(mapper);
8225 processSync(mapper);
8226
8227 NotifyMotionArgs args;
8228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8229 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8230
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008232 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008233 ASSERT_EQ(size_t(2), args.pointerCount);
8234 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8235 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8237 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8238}
8239
8240TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008241 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008242 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008243 prepareAxes(POSITION | TOUCH | TOOL);
8244 addConfigurationProperty("touch.size.calibration", "area");
8245 addConfigurationProperty("touch.size.scale", "43");
8246 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008247 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008248
8249 // These calculations are based on the input device calibration documentation.
8250 int32_t rawX = 100;
8251 int32_t rawY = 200;
8252 int32_t rawTouchMajor = 5;
8253 int32_t rawToolMajor = 8;
8254
8255 float x = toDisplayX(rawX);
8256 float y = toDisplayY(rawY);
8257 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8258 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8259 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8260
8261 processPosition(mapper, rawX, rawY);
8262 processTouchMajor(mapper, rawTouchMajor);
8263 processToolMajor(mapper, rawToolMajor);
8264 processMTSync(mapper);
8265 processSync(mapper);
8266
8267 NotifyMotionArgs args;
8268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8270 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8271}
8272
8273TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008274 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008275 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008276 prepareAxes(POSITION | PRESSURE);
8277 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8278 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008279 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008280
Michael Wrightaa449c92017-12-13 21:21:43 +00008281 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008282 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008283 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8284 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8285 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8286
Michael Wrightd02c5b62014-02-10 15:10:22 -08008287 // These calculations are based on the input device calibration documentation.
8288 int32_t rawX = 100;
8289 int32_t rawY = 200;
8290 int32_t rawPressure = 60;
8291
8292 float x = toDisplayX(rawX);
8293 float y = toDisplayY(rawY);
8294 float pressure = float(rawPressure) * 0.01f;
8295
8296 processPosition(mapper, rawX, rawY);
8297 processPressure(mapper, rawPressure);
8298 processMTSync(mapper);
8299 processSync(mapper);
8300
8301 NotifyMotionArgs args;
8302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8304 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8305}
8306
8307TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008308 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008309 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008310 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008311 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008312
8313 NotifyMotionArgs motionArgs;
8314 NotifyKeyArgs keyArgs;
8315
8316 processId(mapper, 1);
8317 processPosition(mapper, 100, 200);
8318 processSync(mapper);
8319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8320 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8321 ASSERT_EQ(0, motionArgs.buttonState);
8322
8323 // press BTN_LEFT, release BTN_LEFT
8324 processKey(mapper, BTN_LEFT, 1);
8325 processSync(mapper);
8326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8328 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8329
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8331 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8332 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8333
Michael Wrightd02c5b62014-02-10 15:10:22 -08008334 processKey(mapper, BTN_LEFT, 0);
8335 processSync(mapper);
8336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008337 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008338 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008339
8340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008342 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008343
8344 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8345 processKey(mapper, BTN_RIGHT, 1);
8346 processKey(mapper, BTN_MIDDLE, 1);
8347 processSync(mapper);
8348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8350 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8351 motionArgs.buttonState);
8352
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8354 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8355 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8356
8357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8358 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8359 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8360 motionArgs.buttonState);
8361
Michael Wrightd02c5b62014-02-10 15:10:22 -08008362 processKey(mapper, BTN_RIGHT, 0);
8363 processSync(mapper);
8364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008365 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008366 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008367
8368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008369 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008370 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008371
8372 processKey(mapper, BTN_MIDDLE, 0);
8373 processSync(mapper);
8374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008375 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008376 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008377
8378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008380 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008381
8382 // press BTN_BACK, release BTN_BACK
8383 processKey(mapper, BTN_BACK, 1);
8384 processSync(mapper);
8385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8386 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8387 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008388
Michael Wrightd02c5b62014-02-10 15:10:22 -08008389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008391 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8392
8393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8394 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8395 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008396
8397 processKey(mapper, BTN_BACK, 0);
8398 processSync(mapper);
8399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008400 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008401 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008402
8403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008405 ASSERT_EQ(0, motionArgs.buttonState);
8406
Michael Wrightd02c5b62014-02-10 15:10:22 -08008407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8408 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8409 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8410
8411 // press BTN_SIDE, release BTN_SIDE
8412 processKey(mapper, BTN_SIDE, 1);
8413 processSync(mapper);
8414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8415 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8416 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008417
Michael Wrightd02c5b62014-02-10 15:10:22 -08008418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008420 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8421
8422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8423 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8424 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008425
8426 processKey(mapper, BTN_SIDE, 0);
8427 processSync(mapper);
8428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008429 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008430 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008431
8432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008433 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008434 ASSERT_EQ(0, motionArgs.buttonState);
8435
Michael Wrightd02c5b62014-02-10 15:10:22 -08008436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8437 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8438 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8439
8440 // press BTN_FORWARD, release BTN_FORWARD
8441 processKey(mapper, BTN_FORWARD, 1);
8442 processSync(mapper);
8443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8444 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8445 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008446
Michael Wrightd02c5b62014-02-10 15:10:22 -08008447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008449 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8450
8451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8452 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8453 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008454
8455 processKey(mapper, BTN_FORWARD, 0);
8456 processSync(mapper);
8457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008458 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008459 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008460
8461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008462 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008463 ASSERT_EQ(0, motionArgs.buttonState);
8464
Michael Wrightd02c5b62014-02-10 15:10:22 -08008465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8466 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8467 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8468
8469 // press BTN_EXTRA, release BTN_EXTRA
8470 processKey(mapper, BTN_EXTRA, 1);
8471 processSync(mapper);
8472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8473 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8474 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008475
Michael Wrightd02c5b62014-02-10 15:10:22 -08008476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008477 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008478 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8479
8480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8481 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8482 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008483
8484 processKey(mapper, BTN_EXTRA, 0);
8485 processSync(mapper);
8486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008487 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008488 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008489
8490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008491 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008492 ASSERT_EQ(0, motionArgs.buttonState);
8493
Michael Wrightd02c5b62014-02-10 15:10:22 -08008494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8495 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8496 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8497
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8499
Michael Wrightd02c5b62014-02-10 15:10:22 -08008500 // press BTN_STYLUS, release BTN_STYLUS
8501 processKey(mapper, BTN_STYLUS, 1);
8502 processSync(mapper);
8503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8504 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008505 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8506
8507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8508 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8509 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008510
8511 processKey(mapper, BTN_STYLUS, 0);
8512 processSync(mapper);
8513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008514 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008515 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008516
8517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008518 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008519 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008520
8521 // press BTN_STYLUS2, release BTN_STYLUS2
8522 processKey(mapper, BTN_STYLUS2, 1);
8523 processSync(mapper);
8524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8525 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008526 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8527
8528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8529 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8530 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008531
8532 processKey(mapper, BTN_STYLUS2, 0);
8533 processSync(mapper);
8534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008535 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008536 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008537
8538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008540 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008541
8542 // release touch
8543 processId(mapper, -1);
8544 processSync(mapper);
8545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8546 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8547 ASSERT_EQ(0, motionArgs.buttonState);
8548}
8549
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008550TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8551 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008552 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008553 prepareAxes(POSITION | ID | SLOT);
8554 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8555
8556 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8557 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8558
8559 // Touch down.
8560 processId(mapper, 1);
8561 processPosition(mapper, 100, 200);
8562 processSync(mapper);
8563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8564 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8565
8566 // Press and release button mapped to the primary stylus button.
8567 processKey(mapper, BTN_A, 1);
8568 processSync(mapper);
8569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8570 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8571 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8573 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8574 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8575
8576 processKey(mapper, BTN_A, 0);
8577 processSync(mapper);
8578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8579 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8581 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8582
8583 // Press and release the HID usage mapped to the secondary stylus button.
8584 processHidUsage(mapper, 0xabcd, 1);
8585 processSync(mapper);
8586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8587 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8588 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8590 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8591 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8592
8593 processHidUsage(mapper, 0xabcd, 0);
8594 processSync(mapper);
8595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8596 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8598 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8599
8600 // Release touch.
8601 processId(mapper, -1);
8602 processSync(mapper);
8603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8604 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8605}
8606
Michael Wrightd02c5b62014-02-10 15:10:22 -08008607TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008608 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008609 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008610 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008611 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008612
8613 NotifyMotionArgs motionArgs;
8614
8615 // default tool type is finger
8616 processId(mapper, 1);
8617 processPosition(mapper, 100, 200);
8618 processSync(mapper);
8619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8620 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8621 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8622
8623 // eraser
8624 processKey(mapper, BTN_TOOL_RUBBER, 1);
8625 processSync(mapper);
8626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8627 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8629
8630 // stylus
8631 processKey(mapper, BTN_TOOL_RUBBER, 0);
8632 processKey(mapper, BTN_TOOL_PEN, 1);
8633 processSync(mapper);
8634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8635 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8637
8638 // brush
8639 processKey(mapper, BTN_TOOL_PEN, 0);
8640 processKey(mapper, BTN_TOOL_BRUSH, 1);
8641 processSync(mapper);
8642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8643 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8644 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8645
8646 // pencil
8647 processKey(mapper, BTN_TOOL_BRUSH, 0);
8648 processKey(mapper, BTN_TOOL_PENCIL, 1);
8649 processSync(mapper);
8650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8651 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8653
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008654 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008655 processKey(mapper, BTN_TOOL_PENCIL, 0);
8656 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8657 processSync(mapper);
8658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8660 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8661
8662 // mouse
8663 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8664 processKey(mapper, BTN_TOOL_MOUSE, 1);
8665 processSync(mapper);
8666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8667 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8668 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8669
8670 // lens
8671 processKey(mapper, BTN_TOOL_MOUSE, 0);
8672 processKey(mapper, BTN_TOOL_LENS, 1);
8673 processSync(mapper);
8674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8675 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8676 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8677
8678 // double-tap
8679 processKey(mapper, BTN_TOOL_LENS, 0);
8680 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8681 processSync(mapper);
8682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8683 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8684 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8685
8686 // triple-tap
8687 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8688 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8689 processSync(mapper);
8690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8691 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8693
8694 // quad-tap
8695 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8696 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8697 processSync(mapper);
8698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8699 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8700 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8701
8702 // finger
8703 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8704 processKey(mapper, BTN_TOOL_FINGER, 1);
8705 processSync(mapper);
8706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8707 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8708 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8709
8710 // stylus trumps finger
8711 processKey(mapper, BTN_TOOL_PEN, 1);
8712 processSync(mapper);
8713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8714 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8715 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8716
8717 // eraser trumps stylus
8718 processKey(mapper, BTN_TOOL_RUBBER, 1);
8719 processSync(mapper);
8720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8721 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8722 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8723
8724 // mouse trumps eraser
8725 processKey(mapper, BTN_TOOL_MOUSE, 1);
8726 processSync(mapper);
8727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8728 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8729 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8730
8731 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8732 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8733 processSync(mapper);
8734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8736 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8737
8738 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8739 processToolType(mapper, MT_TOOL_PEN);
8740 processSync(mapper);
8741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8742 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8743 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8744
8745 // back to default tool type
8746 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8747 processKey(mapper, BTN_TOOL_MOUSE, 0);
8748 processKey(mapper, BTN_TOOL_RUBBER, 0);
8749 processKey(mapper, BTN_TOOL_PEN, 0);
8750 processKey(mapper, BTN_TOOL_FINGER, 0);
8751 processSync(mapper);
8752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8753 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8754 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8755}
8756
8757TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008758 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008759 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008760 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008761 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008762 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008763
8764 NotifyMotionArgs motionArgs;
8765
8766 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8767 processId(mapper, 1);
8768 processPosition(mapper, 100, 200);
8769 processSync(mapper);
8770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8771 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8772 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8773 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8774
8775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8776 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8778 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8779
8780 // move a little
8781 processPosition(mapper, 150, 250);
8782 processSync(mapper);
8783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8784 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8786 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8787
8788 // down when BTN_TOUCH is pressed, pressure defaults to 1
8789 processKey(mapper, BTN_TOUCH, 1);
8790 processSync(mapper);
8791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8792 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8794 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8795
8796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8797 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8799 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8800
8801 // up when BTN_TOUCH is released, hover restored
8802 processKey(mapper, BTN_TOUCH, 0);
8803 processSync(mapper);
8804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8805 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8806 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8807 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8808
8809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8810 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8811 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8812 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8813
8814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8815 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8817 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8818
8819 // exit hover when pointer goes away
8820 processId(mapper, -1);
8821 processSync(mapper);
8822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8823 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8825 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8826}
8827
8828TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008829 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008830 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008831 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008832 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008833
8834 NotifyMotionArgs motionArgs;
8835
8836 // initially hovering because pressure is 0
8837 processId(mapper, 1);
8838 processPosition(mapper, 100, 200);
8839 processPressure(mapper, 0);
8840 processSync(mapper);
8841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8842 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8844 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8845
8846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8847 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8849 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8850
8851 // move a little
8852 processPosition(mapper, 150, 250);
8853 processSync(mapper);
8854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8855 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8856 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8857 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8858
8859 // down when pressure becomes non-zero
8860 processPressure(mapper, RAW_PRESSURE_MAX);
8861 processSync(mapper);
8862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8863 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8864 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8865 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8866
8867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8868 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8870 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8871
8872 // up when pressure becomes 0, hover restored
8873 processPressure(mapper, 0);
8874 processSync(mapper);
8875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8876 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8877 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8878 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8879
8880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8881 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8882 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8883 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8884
8885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8886 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8888 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8889
8890 // exit hover when pointer goes away
8891 processId(mapper, -1);
8892 processSync(mapper);
8893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8894 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8895 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8896 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8897}
8898
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008899/**
8900 * Set the input device port <--> display port associations, and check that the
8901 * events are routed to the display that matches the display port.
8902 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8903 */
8904TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008905 const std::string usb2 = "USB2";
8906 const uint8_t hdmi1 = 0;
8907 const uint8_t hdmi2 = 1;
8908 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008909 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008910
8911 addConfigurationProperty("touch.deviceType", "touchScreen");
8912 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008913 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008914
8915 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8916 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8917
8918 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8919 // for this input device is specified, and the matching viewport is not present,
8920 // the input device should be disabled (at the mapper level).
8921
8922 // Add viewport for display 2 on hdmi2
8923 prepareSecondaryDisplay(type, hdmi2);
8924 // Send a touch event
8925 processPosition(mapper, 100, 100);
8926 processSync(mapper);
8927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8928
8929 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008930 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008931 // Send a touch event again
8932 processPosition(mapper, 100, 100);
8933 processSync(mapper);
8934
8935 NotifyMotionArgs args;
8936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8937 ASSERT_EQ(DISPLAY_ID, args.displayId);
8938}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008939
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008940TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8941 addConfigurationProperty("touch.deviceType", "touchScreen");
8942 prepareAxes(POSITION);
8943 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8944
8945 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8946
Michael Wrighta9cf4192022-12-01 23:46:39 +00008947 prepareDisplay(ui::ROTATION_0);
8948 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008949
8950 // Send a touch event
8951 processPosition(mapper, 100, 100);
8952 processSync(mapper);
8953
8954 NotifyMotionArgs args;
8955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8956 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8957}
8958
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008959TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008960 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008961 std::shared_ptr<FakePointerController> fakePointerController =
8962 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008963 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008964 fakePointerController->setPosition(100, 200);
8965 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008966 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008967
Garfield Tan888a6a42020-01-09 11:39:16 -08008968 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008969 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008970
Michael Wrighta9cf4192022-12-01 23:46:39 +00008971 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008972 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008973 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008974
Harry Cutts16a24cc2022-10-26 15:22:19 +00008975 // Check source is a touchpad that would obtain the PointerController.
8976 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008977
8978 NotifyMotionArgs motionArgs;
8979 processPosition(mapper, 100, 100);
8980 processSync(mapper);
8981
8982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8983 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8984 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8985}
8986
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008987/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008988 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8989 */
8990TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8991 addConfigurationProperty("touch.deviceType", "touchScreen");
8992 prepareAxes(POSITION);
8993 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8994
Michael Wrighta9cf4192022-12-01 23:46:39 +00008995 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008996 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8997 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8998 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8999 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9000
9001 NotifyMotionArgs args;
9002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9003 ASSERT_EQ(26, args.readTime);
9004
9005 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
9006 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
9007 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9008
9009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9010 ASSERT_EQ(33, args.readTime);
9011}
9012
9013/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009014 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9015 * events should not be delivered to the listener.
9016 */
9017TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9018 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009019 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009020 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
9021 false /*isActive*/, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009022 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9023 prepareAxes(POSITION);
9024 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9025
9026 NotifyMotionArgs motionArgs;
9027 processPosition(mapper, 100, 100);
9028 processSync(mapper);
9029
9030 mFakeListener->assertNotifyMotionWasNotCalled();
9031}
9032
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009033/**
9034 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9035 * the touch mapper can process the events and the events can be delivered to the listener.
9036 */
9037TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9038 addConfigurationProperty("touch.deviceType", "touchScreen");
9039 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009040 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
9041 false /*isActive*/, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009042 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9043 prepareAxes(POSITION);
9044 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9045
9046 NotifyMotionArgs motionArgs;
9047 processPosition(mapper, 100, 100);
9048 processSync(mapper);
9049
9050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9051 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9052}
9053
Garfield Tanc734e4f2021-01-15 20:01:39 -08009054TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9055 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009056 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009057 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
9058 true /*isActive*/, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009059 std::optional<DisplayViewport> optionalDisplayViewport =
9060 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9061 ASSERT_TRUE(optionalDisplayViewport.has_value());
9062 DisplayViewport displayViewport = *optionalDisplayViewport;
9063
9064 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9065 prepareAxes(POSITION);
9066 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9067
9068 // Finger down
9069 int32_t x = 100, y = 100;
9070 processPosition(mapper, x, y);
9071 processSync(mapper);
9072
9073 NotifyMotionArgs motionArgs;
9074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9075 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9076
9077 // Deactivate display viewport
9078 displayViewport.isActive = false;
9079 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9080 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9081
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009082 // The ongoing touch should be canceled immediately
9083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9084 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9085
9086 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009087 x += 10, y += 10;
9088 processPosition(mapper, x, y);
9089 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009091
9092 // Reactivate display viewport
9093 displayViewport.isActive = true;
9094 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9095 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9096
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009097 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009098 x += 10, y += 10;
9099 processPosition(mapper, x, y);
9100 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9102 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009103}
9104
Arthur Hung7c645402019-01-25 17:45:42 +08009105TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9106 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009107 prepareAxes(POSITION | ID | SLOT);
9108 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009109 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009110
9111 // Create the second touch screen device, and enable multi fingers.
9112 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009113 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009114 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009115 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009116 std::shared_ptr<InputDevice> device2 =
9117 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009118 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009119
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009120 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9121 0 /*flat*/, 0 /*fuzz*/);
9122 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9123 0 /*flat*/, 0 /*fuzz*/);
9124 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
9125 0 /*flat*/, 0 /*fuzz*/);
9126 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
9127 0 /*flat*/, 0 /*fuzz*/);
9128 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
9129 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9130 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009131
9132 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009133 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009134 std::list<NotifyArgs> unused =
9135 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9136 0 /*changes*/);
9137 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009138
9139 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009140 std::shared_ptr<FakePointerController> fakePointerController =
9141 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009142 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009143
9144 // Setup policy for associated displays and show touches.
9145 const uint8_t hdmi1 = 0;
9146 const uint8_t hdmi2 = 1;
9147 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9148 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9149 mFakePolicy->setShowTouches(true);
9150
9151 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009152 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009153 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009154
9155 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009156 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9157 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9158 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009159
9160 // Two fingers down at default display.
9161 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9162 processPosition(mapper, x1, y1);
9163 processId(mapper, 1);
9164 processSlot(mapper, 1);
9165 processPosition(mapper, x2, y2);
9166 processId(mapper, 2);
9167 processSync(mapper);
9168
9169 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9170 fakePointerController->getSpots().find(DISPLAY_ID);
9171 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9172 ASSERT_EQ(size_t(2), iter->second.size());
9173
9174 // Two fingers down at second display.
9175 processPosition(mapper2, x1, y1);
9176 processId(mapper2, 1);
9177 processSlot(mapper2, 1);
9178 processPosition(mapper2, x2, y2);
9179 processId(mapper2, 2);
9180 processSync(mapper2);
9181
9182 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9183 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9184 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009185
9186 // Disable the show touches configuration and ensure the spots are cleared.
9187 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009188 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9189 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009190
9191 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009192}
9193
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009194TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009195 prepareAxes(POSITION);
9196 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009197 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009198 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009199
9200 NotifyMotionArgs motionArgs;
9201 // Unrotated video frame
9202 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9203 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009204 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009205 processPosition(mapper, 100, 200);
9206 processSync(mapper);
9207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9208 ASSERT_EQ(frames, motionArgs.videoFrames);
9209
9210 // Subsequent touch events should not have any videoframes
9211 // This is implemented separately in FakeEventHub,
9212 // but that should match the behaviour of TouchVideoDevice.
9213 processPosition(mapper, 200, 200);
9214 processSync(mapper);
9215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9216 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9217}
9218
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009219TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009220 prepareAxes(POSITION);
9221 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009222 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009223 // Unrotated video frame
9224 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9225 NotifyMotionArgs motionArgs;
9226
9227 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009228 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009229 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9230 clearViewports();
9231 prepareDisplay(orientation);
9232 std::vector<TouchVideoFrame> frames{frame};
9233 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9234 processPosition(mapper, 100, 200);
9235 processSync(mapper);
9236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9237 ASSERT_EQ(frames, motionArgs.videoFrames);
9238 }
9239}
9240
9241TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9242 prepareAxes(POSITION);
9243 addConfigurationProperty("touch.deviceType", "touchScreen");
9244 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9245 // orientation-aware are affected by display rotation.
9246 addConfigurationProperty("touch.orientationAware", "0");
9247 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9248 // Unrotated video frame
9249 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9250 NotifyMotionArgs motionArgs;
9251
9252 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009253 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009254 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9255 clearViewports();
9256 prepareDisplay(orientation);
9257 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009258 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009259 processPosition(mapper, 100, 200);
9260 processSync(mapper);
9261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009262 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9263 // compared to the display. This is so that when the window transform (which contains the
9264 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9265 // window's coordinate space.
9266 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009267 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009268
9269 // Release finger.
9270 processSync(mapper);
9271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009272 }
9273}
9274
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009275TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009276 prepareAxes(POSITION);
9277 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009278 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009279 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9280 // so mix these.
9281 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9282 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9283 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9284 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9285 NotifyMotionArgs motionArgs;
9286
Michael Wrighta9cf4192022-12-01 23:46:39 +00009287 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009288 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009289 processPosition(mapper, 100, 200);
9290 processSync(mapper);
9291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009292 ASSERT_EQ(frames, motionArgs.videoFrames);
9293}
9294
9295TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9296 prepareAxes(POSITION);
9297 addConfigurationProperty("touch.deviceType", "touchScreen");
9298 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9299 // orientation-aware are affected by display rotation.
9300 addConfigurationProperty("touch.orientationAware", "0");
9301 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9302 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9303 // so mix these.
9304 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9305 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9306 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9307 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9308 NotifyMotionArgs motionArgs;
9309
Michael Wrighta9cf4192022-12-01 23:46:39 +00009310 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009311 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9312 processPosition(mapper, 100, 200);
9313 processSync(mapper);
9314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9315 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9316 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9317 // compared to the display. This is so that when the window transform (which contains the
9318 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9319 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009320 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009321 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009322 ASSERT_EQ(frames, motionArgs.videoFrames);
9323}
9324
Arthur Hung9da14732019-09-02 16:16:58 +08009325/**
9326 * If we had defined port associations, but the viewport is not ready, the touch device would be
9327 * expected to be disabled, and it should be enabled after the viewport has found.
9328 */
9329TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009330 constexpr uint8_t hdmi2 = 1;
9331 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009332 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009333
9334 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9335
9336 addConfigurationProperty("touch.deviceType", "touchScreen");
9337 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009338 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009339
9340 ASSERT_EQ(mDevice->isEnabled(), false);
9341
9342 // Add display on hdmi2, the device should be enabled and can receive touch event.
9343 prepareSecondaryDisplay(type, hdmi2);
9344 ASSERT_EQ(mDevice->isEnabled(), true);
9345
9346 // Send a touch event.
9347 processPosition(mapper, 100, 100);
9348 processSync(mapper);
9349
9350 NotifyMotionArgs args;
9351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9352 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9353}
9354
Arthur Hung421eb1c2020-01-16 00:09:42 +08009355TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009356 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009357 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009358 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009359 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009360
9361 NotifyMotionArgs motionArgs;
9362
9363 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9364 // finger down
9365 processId(mapper, 1);
9366 processPosition(mapper, x1, y1);
9367 processSync(mapper);
9368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9369 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9370 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9371
9372 // finger move
9373 processId(mapper, 1);
9374 processPosition(mapper, x2, y2);
9375 processSync(mapper);
9376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9378 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9379
9380 // finger up.
9381 processId(mapper, -1);
9382 processSync(mapper);
9383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9384 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9385 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9386
9387 // new finger down
9388 processId(mapper, 1);
9389 processPosition(mapper, x3, y3);
9390 processSync(mapper);
9391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9392 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9394}
9395
9396/**
arthurhungcc7f9802020-04-30 17:55:40 +08009397 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9398 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009399 */
arthurhungcc7f9802020-04-30 17:55:40 +08009400TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009401 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009402 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009403 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009404 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009405
9406 NotifyMotionArgs motionArgs;
9407
9408 // default tool type is finger
9409 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009410 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009411 processPosition(mapper, x1, y1);
9412 processSync(mapper);
9413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9414 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9415 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9416
9417 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9418 processToolType(mapper, MT_TOOL_PALM);
9419 processSync(mapper);
9420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9421 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9422
9423 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009424 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009425 processPosition(mapper, x2, y2);
9426 processSync(mapper);
9427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9428
9429 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009430 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009431 processSync(mapper);
9432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9433
9434 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009435 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009436 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009437 processPosition(mapper, x3, y3);
9438 processSync(mapper);
9439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9440 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9442}
9443
arthurhungbf89a482020-04-17 17:37:55 +08009444/**
arthurhungcc7f9802020-04-30 17:55:40 +08009445 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9446 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009447 */
arthurhungcc7f9802020-04-30 17:55:40 +08009448TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009449 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009450 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009451 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9452 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9453
9454 NotifyMotionArgs motionArgs;
9455
9456 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009457 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9458 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009459 processPosition(mapper, x1, y1);
9460 processSync(mapper);
9461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9462 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9463 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9464
9465 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009466 processSlot(mapper, SECOND_SLOT);
9467 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009468 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009469 processSync(mapper);
9470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009471 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009472 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9473
9474 // If the tool type of the first finger changes to MT_TOOL_PALM,
9475 // we expect to receive ACTION_POINTER_UP with cancel flag.
9476 processSlot(mapper, FIRST_SLOT);
9477 processId(mapper, FIRST_TRACKING_ID);
9478 processToolType(mapper, MT_TOOL_PALM);
9479 processSync(mapper);
9480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009481 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009482 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9483
9484 // The following MOVE events of second finger should be processed.
9485 processSlot(mapper, SECOND_SLOT);
9486 processId(mapper, SECOND_TRACKING_ID);
9487 processPosition(mapper, x2 + 1, y2 + 1);
9488 processSync(mapper);
9489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9490 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9491 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9492
9493 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9494 // it. Second finger receive move.
9495 processSlot(mapper, FIRST_SLOT);
9496 processId(mapper, INVALID_TRACKING_ID);
9497 processSync(mapper);
9498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9500 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9501
9502 // Second finger keeps moving.
9503 processSlot(mapper, SECOND_SLOT);
9504 processId(mapper, SECOND_TRACKING_ID);
9505 processPosition(mapper, x2 + 2, y2 + 2);
9506 processSync(mapper);
9507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9508 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9509 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9510
9511 // Second finger up.
9512 processId(mapper, INVALID_TRACKING_ID);
9513 processSync(mapper);
9514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9515 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9516 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9517}
9518
9519/**
9520 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9521 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9522 */
9523TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9524 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009525 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009526 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9527 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9528
9529 NotifyMotionArgs motionArgs;
9530
9531 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9532 // First finger down.
9533 processId(mapper, FIRST_TRACKING_ID);
9534 processPosition(mapper, x1, y1);
9535 processSync(mapper);
9536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9537 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9538 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9539
9540 // Second finger down.
9541 processSlot(mapper, SECOND_SLOT);
9542 processId(mapper, SECOND_TRACKING_ID);
9543 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009544 processSync(mapper);
9545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009546 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009547 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9548
arthurhungcc7f9802020-04-30 17:55:40 +08009549 // If the tool type of the first finger changes to MT_TOOL_PALM,
9550 // we expect to receive ACTION_POINTER_UP with cancel flag.
9551 processSlot(mapper, FIRST_SLOT);
9552 processId(mapper, FIRST_TRACKING_ID);
9553 processToolType(mapper, MT_TOOL_PALM);
9554 processSync(mapper);
9555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009556 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009557 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9558
9559 // Second finger keeps moving.
9560 processSlot(mapper, SECOND_SLOT);
9561 processId(mapper, SECOND_TRACKING_ID);
9562 processPosition(mapper, x2 + 1, y2 + 1);
9563 processSync(mapper);
9564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9566
9567 // second finger becomes palm, receive cancel due to only 1 finger is active.
9568 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009569 processToolType(mapper, MT_TOOL_PALM);
9570 processSync(mapper);
9571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9572 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9573
arthurhungcc7f9802020-04-30 17:55:40 +08009574 // third finger down.
9575 processSlot(mapper, THIRD_SLOT);
9576 processId(mapper, THIRD_TRACKING_ID);
9577 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009578 processPosition(mapper, x3, y3);
9579 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9581 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009583 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9584
9585 // third finger move
9586 processId(mapper, THIRD_TRACKING_ID);
9587 processPosition(mapper, x3 + 1, y3 + 1);
9588 processSync(mapper);
9589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9591
9592 // first finger up, third finger receive move.
9593 processSlot(mapper, FIRST_SLOT);
9594 processId(mapper, INVALID_TRACKING_ID);
9595 processSync(mapper);
9596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9597 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9598 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9599
9600 // second finger up, third finger receive move.
9601 processSlot(mapper, SECOND_SLOT);
9602 processId(mapper, INVALID_TRACKING_ID);
9603 processSync(mapper);
9604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9605 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9606 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9607
9608 // third finger up.
9609 processSlot(mapper, THIRD_SLOT);
9610 processId(mapper, INVALID_TRACKING_ID);
9611 processSync(mapper);
9612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9613 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9614 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9615}
9616
9617/**
9618 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9619 * and the active finger could still be allowed to receive the events
9620 */
9621TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9622 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009623 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009624 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9625 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9626
9627 NotifyMotionArgs motionArgs;
9628
9629 // default tool type is finger
9630 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9631 processId(mapper, FIRST_TRACKING_ID);
9632 processPosition(mapper, x1, y1);
9633 processSync(mapper);
9634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9635 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9637
9638 // Second finger down.
9639 processSlot(mapper, SECOND_SLOT);
9640 processId(mapper, SECOND_TRACKING_ID);
9641 processPosition(mapper, x2, y2);
9642 processSync(mapper);
9643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009644 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009645 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9646
9647 // If the tool type of the second finger changes to MT_TOOL_PALM,
9648 // we expect to receive ACTION_POINTER_UP with cancel flag.
9649 processId(mapper, SECOND_TRACKING_ID);
9650 processToolType(mapper, MT_TOOL_PALM);
9651 processSync(mapper);
9652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009653 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009654 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9655
9656 // The following MOVE event should be processed.
9657 processSlot(mapper, FIRST_SLOT);
9658 processId(mapper, FIRST_TRACKING_ID);
9659 processPosition(mapper, x1 + 1, y1 + 1);
9660 processSync(mapper);
9661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9662 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9663 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9664
9665 // second finger up.
9666 processSlot(mapper, SECOND_SLOT);
9667 processId(mapper, INVALID_TRACKING_ID);
9668 processSync(mapper);
9669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9671
9672 // first finger keep moving
9673 processSlot(mapper, FIRST_SLOT);
9674 processId(mapper, FIRST_TRACKING_ID);
9675 processPosition(mapper, x1 + 2, y1 + 2);
9676 processSync(mapper);
9677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9678 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9679
9680 // first finger up.
9681 processId(mapper, INVALID_TRACKING_ID);
9682 processSync(mapper);
9683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9684 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9685 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009686}
9687
Arthur Hung9ad18942021-06-19 02:04:46 +00009688/**
9689 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9690 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9691 * cause slot be valid again.
9692 */
9693TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9694 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009695 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009696 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9697 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9698
9699 NotifyMotionArgs motionArgs;
9700
9701 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9702 // First finger down.
9703 processId(mapper, FIRST_TRACKING_ID);
9704 processPosition(mapper, x1, y1);
9705 processPressure(mapper, RAW_PRESSURE_MAX);
9706 processSync(mapper);
9707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9708 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9709 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9710
9711 // First finger move.
9712 processId(mapper, FIRST_TRACKING_ID);
9713 processPosition(mapper, x1 + 1, y1 + 1);
9714 processPressure(mapper, RAW_PRESSURE_MAX);
9715 processSync(mapper);
9716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9717 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9718 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9719
9720 // Second finger down.
9721 processSlot(mapper, SECOND_SLOT);
9722 processId(mapper, SECOND_TRACKING_ID);
9723 processPosition(mapper, x2, y2);
9724 processPressure(mapper, RAW_PRESSURE_MAX);
9725 processSync(mapper);
9726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009727 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009728 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9729
9730 // second finger up with some unexpected data.
9731 processSlot(mapper, SECOND_SLOT);
9732 processId(mapper, INVALID_TRACKING_ID);
9733 processPosition(mapper, x2, y2);
9734 processSync(mapper);
9735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009736 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009737 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9738
9739 // first finger up with some unexpected data.
9740 processSlot(mapper, FIRST_SLOT);
9741 processId(mapper, INVALID_TRACKING_ID);
9742 processPosition(mapper, x2, y2);
9743 processPressure(mapper, RAW_PRESSURE_MAX);
9744 processSync(mapper);
9745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9746 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9747 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9748}
9749
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009750TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9751 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009752 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009753 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9754 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9755
9756 // First finger down.
9757 processId(mapper, FIRST_TRACKING_ID);
9758 processPosition(mapper, 100, 200);
9759 processPressure(mapper, RAW_PRESSURE_MAX);
9760 processSync(mapper);
9761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9762 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9763
9764 // Second finger down.
9765 processSlot(mapper, SECOND_SLOT);
9766 processId(mapper, SECOND_TRACKING_ID);
9767 processPosition(mapper, 300, 400);
9768 processPressure(mapper, RAW_PRESSURE_MAX);
9769 processSync(mapper);
9770 ASSERT_NO_FATAL_FAILURE(
9771 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9772
9773 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009774 // preserved. Resetting should cancel the ongoing gesture.
9775 resetMapper(mapper, ARBITRARY_TIME);
9776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9777 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009778
9779 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9780 // the existing touch state to generate a down event.
9781 processPosition(mapper, 301, 302);
9782 processSync(mapper);
9783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9784 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9786 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9787
9788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9789}
9790
9791TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9792 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009793 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009794 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9795 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9796
9797 // First finger touches down and releases.
9798 processId(mapper, FIRST_TRACKING_ID);
9799 processPosition(mapper, 100, 200);
9800 processPressure(mapper, RAW_PRESSURE_MAX);
9801 processSync(mapper);
9802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9803 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9804 processId(mapper, INVALID_TRACKING_ID);
9805 processSync(mapper);
9806 ASSERT_NO_FATAL_FAILURE(
9807 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9808
9809 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9810 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009811 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9813
9814 // Send an empty sync frame. Since there are no pointers, no events are generated.
9815 processSync(mapper);
9816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9817}
9818
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009819TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009820 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009821 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009822 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
9823 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009825
9826 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9827 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9828 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9829 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9830 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9831
9832 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009833 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009834 processId(mapper, FIRST_TRACKING_ID);
9835 processToolType(mapper, MT_TOOL_PEN);
9836 processPosition(mapper, 100, 200);
9837 processPressure(mapper, RAW_PRESSURE_MAX);
9838 processSync(mapper);
9839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9840 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9841 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9842 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
9843
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009844 // Now that we know the device supports styluses, ensure that the device is re-configured with
9845 // the stylus source.
9846 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9847 {
9848 const auto& devices = mReader->getInputDevices();
9849 auto deviceInfo =
9850 std::find_if(devices.begin(), devices.end(),
9851 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9852 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9853 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9854 }
9855
9856 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9858
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009859 processId(mapper, INVALID_TRACKING_ID);
9860 processSync(mapper);
9861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9862 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9863 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9864 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009865}
9866
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009867// --- MultiTouchInputMapperTest_ExternalDevice ---
9868
9869class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9870protected:
Chris Yea52ade12020-08-27 16:49:20 -07009871 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009872};
9873
9874/**
9875 * Expect fallback to internal viewport if device is external and external viewport is not present.
9876 */
9877TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9878 prepareAxes(POSITION);
9879 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009880 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009881 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9882
9883 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9884
9885 NotifyMotionArgs motionArgs;
9886
9887 // Expect the event to be sent to the internal viewport,
9888 // because an external viewport is not present.
9889 processPosition(mapper, 100, 100);
9890 processSync(mapper);
9891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9892 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9893
9894 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009895 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009896 processPosition(mapper, 100, 100);
9897 processSync(mapper);
9898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9899 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9900}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009901
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009902TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9903 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9904 std::shared_ptr<FakePointerController> fakePointerController =
9905 std::make_shared<FakePointerController>();
9906 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9907 fakePointerController->setPosition(0, 0);
9908 fakePointerController->setButtonState(0);
9909
9910 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009911 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009912 prepareAxes(POSITION | ID | SLOT);
9913 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9914 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9915 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009916 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009917 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9918
9919 // captured touchpad should be a touchpad source
9920 NotifyDeviceResetArgs resetArgs;
9921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9922 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9923
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009924 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009925
9926 const InputDeviceInfo::MotionRange* relRangeX =
9927 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9928 ASSERT_NE(relRangeX, nullptr);
9929 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9930 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9931 const InputDeviceInfo::MotionRange* relRangeY =
9932 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9933 ASSERT_NE(relRangeY, nullptr);
9934 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9935 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9936
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009937 // run captured pointer tests - note that this is unscaled, so input listener events should be
9938 // identical to what the hardware sends (accounting for any
9939 // calibration).
9940 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009941 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009942 processId(mapper, 1);
9943 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9944 processKey(mapper, BTN_TOUCH, 1);
9945 processSync(mapper);
9946
9947 // expect coord[0] to contain initial location of touch 0
9948 NotifyMotionArgs args;
9949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9950 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9951 ASSERT_EQ(1U, args.pointerCount);
9952 ASSERT_EQ(0, args.pointerProperties[0].id);
9953 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9954 ASSERT_NO_FATAL_FAILURE(
9955 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9956
9957 // FINGER 1 DOWN
9958 processSlot(mapper, 1);
9959 processId(mapper, 2);
9960 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9961 processSync(mapper);
9962
9963 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009965 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009966 ASSERT_EQ(2U, args.pointerCount);
9967 ASSERT_EQ(0, args.pointerProperties[0].id);
9968 ASSERT_EQ(1, args.pointerProperties[1].id);
9969 ASSERT_NO_FATAL_FAILURE(
9970 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9971 ASSERT_NO_FATAL_FAILURE(
9972 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9973
9974 // FINGER 1 MOVE
9975 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9976 processSync(mapper);
9977
9978 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9979 // from move
9980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9982 ASSERT_NO_FATAL_FAILURE(
9983 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9984 ASSERT_NO_FATAL_FAILURE(
9985 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9986
9987 // FINGER 0 MOVE
9988 processSlot(mapper, 0);
9989 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9990 processSync(mapper);
9991
9992 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9994 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9995 ASSERT_NO_FATAL_FAILURE(
9996 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9997 ASSERT_NO_FATAL_FAILURE(
9998 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9999
10000 // BUTTON DOWN
10001 processKey(mapper, BTN_LEFT, 1);
10002 processSync(mapper);
10003
10004 // touchinputmapper design sends a move before button press
10005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10008 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10009
10010 // BUTTON UP
10011 processKey(mapper, BTN_LEFT, 0);
10012 processSync(mapper);
10013
10014 // touchinputmapper design sends a move after button release
10015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10016 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10018 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10019
10020 // FINGER 0 UP
10021 processId(mapper, -1);
10022 processSync(mapper);
10023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10024 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10025
10026 // FINGER 1 MOVE
10027 processSlot(mapper, 1);
10028 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10029 processSync(mapper);
10030
10031 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10033 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10034 ASSERT_EQ(1U, args.pointerCount);
10035 ASSERT_EQ(1, args.pointerProperties[0].id);
10036 ASSERT_NO_FATAL_FAILURE(
10037 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10038
10039 // FINGER 1 UP
10040 processId(mapper, -1);
10041 processKey(mapper, BTN_TOUCH, 0);
10042 processSync(mapper);
10043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10044 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10045
Harry Cutts16a24cc2022-10-26 15:22:19 +000010046 // A non captured touchpad should have a mouse and touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010047 mFakePolicy->setPointerCapture(false);
10048 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Harry Cutts16a24cc2022-10-26 15:22:19 +000010050 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010051}
10052
10053TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10054 std::shared_ptr<FakePointerController> fakePointerController =
10055 std::make_shared<FakePointerController>();
10056 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10057 fakePointerController->setPosition(0, 0);
10058 fakePointerController->setButtonState(0);
10059
10060 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010061 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010062 prepareAxes(POSITION | ID | SLOT);
10063 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10064 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010065 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010066 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10067 // run uncaptured pointer tests - pushes out generic events
10068 // FINGER 0 DOWN
10069 processId(mapper, 3);
10070 processPosition(mapper, 100, 100);
10071 processKey(mapper, BTN_TOUCH, 1);
10072 processSync(mapper);
10073
10074 // start at (100,100), cursor should be at (0,0) * scale
10075 NotifyMotionArgs args;
10076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10077 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10078 ASSERT_NO_FATAL_FAILURE(
10079 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10080
10081 // FINGER 0 MOVE
10082 processPosition(mapper, 200, 200);
10083 processSync(mapper);
10084
10085 // compute scaling to help with touch position checking
10086 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10087 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10088 float scale =
10089 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10090
10091 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10093 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10095 0, 0, 0, 0, 0, 0, 0));
10096}
10097
10098TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10099 std::shared_ptr<FakePointerController> fakePointerController =
10100 std::make_shared<FakePointerController>();
10101
Michael Wrighta9cf4192022-12-01 23:46:39 +000010102 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010103 prepareAxes(POSITION | ID | SLOT);
10104 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010105 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010106 mFakePolicy->setPointerCapture(false);
10107 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10108
Harry Cutts16a24cc2022-10-26 15:22:19 +000010109 // An uncaptured touchpad should be a pointer device, with additional touchpad source.
10110 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010111
Harry Cutts16a24cc2022-10-26 15:22:19 +000010112 // A captured touchpad should just have a touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010113 mFakePolicy->setPointerCapture(true);
10114 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10115 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10116}
10117
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010118// --- BluetoothMultiTouchInputMapperTest ---
10119
10120class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10121protected:
10122 void SetUp() override {
10123 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10124 }
10125};
10126
10127TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10128 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010129 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010130 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10131 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10132
10133 nsecs_t kernelEventTime = ARBITRARY_TIME;
10134 nsecs_t expectedEventTime = ARBITRARY_TIME;
10135 // Touch down.
10136 processId(mapper, FIRST_TRACKING_ID);
10137 processPosition(mapper, 100, 200);
10138 processPressure(mapper, RAW_PRESSURE_MAX);
10139 processSync(mapper, ARBITRARY_TIME);
10140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10141 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10142
10143 // Process several events that come in quick succession, according to their timestamps.
10144 for (int i = 0; i < 3; i++) {
10145 constexpr static nsecs_t delta = ms2ns(1);
10146 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10147 kernelEventTime += delta;
10148 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10149
10150 processPosition(mapper, 101 + i, 201 + i);
10151 processSync(mapper, kernelEventTime);
10152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10153 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10154 WithEventTime(expectedEventTime))));
10155 }
10156
10157 // Release the touch.
10158 processId(mapper, INVALID_TRACKING_ID);
10159 processPressure(mapper, RAW_PRESSURE_MIN);
10160 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10162 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10163 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10164}
10165
10166// --- MultiTouchPointerModeTest ---
10167
HQ Liue6983c72022-04-19 22:14:56 +000010168class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10169protected:
10170 float mPointerMovementScale;
10171 float mPointerXZoomScale;
10172 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10173 addConfigurationProperty("touch.deviceType", "pointer");
10174 std::shared_ptr<FakePointerController> fakePointerController =
10175 std::make_shared<FakePointerController>();
10176 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10177 fakePointerController->setPosition(0, 0);
10178 fakePointerController->setButtonState(0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010179 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010180
10181 prepareAxes(POSITION);
10182 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10183 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10184 // needs to be disabled, and the pointer gesture needs to be enabled.
10185 mFakePolicy->setPointerCapture(false);
10186 mFakePolicy->setPointerGestureEnabled(true);
10187 mFakePolicy->setPointerController(fakePointerController);
10188
10189 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10190 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10191 mPointerMovementScale =
10192 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10193 mPointerXZoomScale =
10194 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10195 }
10196
10197 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10198 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10199 /*flat*/ 0,
10200 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10201 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10202 /*flat*/ 0,
10203 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10204 }
10205};
10206
10207/**
10208 * Two fingers down on a pointer mode touch pad. The width
10209 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10210 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10211 * be greater than the both value to be freeform gesture, so that after two
10212 * fingers start to move downwards, the gesture should be swipe.
10213 */
10214TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10215 // The min freeform gesture width is 25units/mm x 30mm = 750
10216 // which is greater than fraction of the diagnal length of the touchpad (349).
10217 // Thus, MaxSwipWidth is 750.
10218 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10219 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10220 NotifyMotionArgs motionArgs;
10221
10222 // Two fingers down at once.
10223 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10224 // Pointer's initial position is used the [0,0] coordinate.
10225 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10226
10227 processId(mapper, FIRST_TRACKING_ID);
10228 processPosition(mapper, x1, y1);
10229 processMTSync(mapper);
10230 processId(mapper, SECOND_TRACKING_ID);
10231 processPosition(mapper, x2, y2);
10232 processMTSync(mapper);
10233 processSync(mapper);
10234
10235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10236 ASSERT_EQ(1U, motionArgs.pointerCount);
10237 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10238 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010239 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010240 ASSERT_NO_FATAL_FAILURE(
10241 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10242
10243 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10244 // that there should be 1 pointer.
10245 int32_t movingDistance = 200;
10246 y1 += movingDistance;
10247 y2 += movingDistance;
10248
10249 processId(mapper, FIRST_TRACKING_ID);
10250 processPosition(mapper, x1, y1);
10251 processMTSync(mapper);
10252 processId(mapper, SECOND_TRACKING_ID);
10253 processPosition(mapper, x2, y2);
10254 processMTSync(mapper);
10255 processSync(mapper);
10256
10257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10258 ASSERT_EQ(1U, motionArgs.pointerCount);
10259 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10260 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010261 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10263 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10264 0, 0, 0, 0));
10265}
10266
10267/**
10268 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10269 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10270 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10271 * value to be freeform gesture, so that after two fingers start to move downwards,
10272 * the gesture should be swipe.
10273 */
10274TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10275 // The min freeform gesture width is 5units/mm x 30mm = 150
10276 // which is greater than fraction of the diagnal length of the touchpad (349).
10277 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
10278 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
10279 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10280 NotifyMotionArgs motionArgs;
10281
10282 // Two fingers down at once.
10283 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10284 // Pointer's initial position is used the [0,0] coordinate.
10285 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10286
10287 processId(mapper, FIRST_TRACKING_ID);
10288 processPosition(mapper, x1, y1);
10289 processMTSync(mapper);
10290 processId(mapper, SECOND_TRACKING_ID);
10291 processPosition(mapper, x2, y2);
10292 processMTSync(mapper);
10293 processSync(mapper);
10294
10295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10296 ASSERT_EQ(1U, motionArgs.pointerCount);
10297 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10298 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010299 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010300 ASSERT_NO_FATAL_FAILURE(
10301 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10302
10303 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10304 // and there should be 1 pointer.
10305 int32_t movingDistance = 200;
10306 y1 += movingDistance;
10307 y2 += movingDistance;
10308
10309 processId(mapper, FIRST_TRACKING_ID);
10310 processPosition(mapper, x1, y1);
10311 processMTSync(mapper);
10312 processId(mapper, SECOND_TRACKING_ID);
10313 processPosition(mapper, x2, y2);
10314 processMTSync(mapper);
10315 processSync(mapper);
10316
10317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10318 ASSERT_EQ(1U, motionArgs.pointerCount);
10319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10320 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010321 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010322 // New coordinate is the scaled relative coordinate from the initial coordinate.
10323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10324 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10325 0, 0, 0, 0));
10326}
10327
10328/**
10329 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10330 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10331 * freeform gestures after two fingers start to move downwards.
10332 */
10333TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
10334 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10335 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10336
10337 NotifyMotionArgs motionArgs;
10338
10339 // Two fingers down at once. Wider than the max swipe width.
10340 // The gesture is expected to be PRESS, then transformed to FREEFORM
10341 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10342
10343 processId(mapper, FIRST_TRACKING_ID);
10344 processPosition(mapper, x1, y1);
10345 processMTSync(mapper);
10346 processId(mapper, SECOND_TRACKING_ID);
10347 processPosition(mapper, x2, y2);
10348 processMTSync(mapper);
10349 processSync(mapper);
10350
10351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10352 ASSERT_EQ(1U, motionArgs.pointerCount);
10353 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10354 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010355 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010356 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10357 ASSERT_NO_FATAL_FAILURE(
10358 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10359
10360 int32_t movingDistance = 200;
10361
10362 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10363 // then two down events for two pointers.
10364 y1 += movingDistance;
10365 y2 += movingDistance;
10366
10367 processId(mapper, FIRST_TRACKING_ID);
10368 processPosition(mapper, x1, y1);
10369 processMTSync(mapper);
10370 processId(mapper, SECOND_TRACKING_ID);
10371 processPosition(mapper, x2, y2);
10372 processMTSync(mapper);
10373 processSync(mapper);
10374
10375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10376 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10377 ASSERT_EQ(1U, motionArgs.pointerCount);
10378 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10380 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10381 ASSERT_EQ(1U, motionArgs.pointerCount);
10382 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10384 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010385 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010386 ASSERT_EQ(2U, motionArgs.pointerCount);
10387 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10388 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010389 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010390 // Two pointers' scaled relative coordinates from their initial centroid.
10391 // Initial y coordinates are 0 as y1 and y2 have the same value.
10392 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10393 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10394 // When pointers move, the new coordinates equal to the initial coordinates plus
10395 // scaled moving distance.
10396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10397 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10398 0, 0, 0, 0));
10399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10400 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10401 0, 0, 0, 0));
10402
10403 // Move two fingers down again, expect one MOVE motion event.
10404 y1 += movingDistance;
10405 y2 += movingDistance;
10406
10407 processId(mapper, FIRST_TRACKING_ID);
10408 processPosition(mapper, x1, y1);
10409 processMTSync(mapper);
10410 processId(mapper, SECOND_TRACKING_ID);
10411 processPosition(mapper, x2, y2);
10412 processMTSync(mapper);
10413 processSync(mapper);
10414
10415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10416 ASSERT_EQ(2U, motionArgs.pointerCount);
10417 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10418 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010419 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10421 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10422 0, 0, 0, 0, 0));
10423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10424 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10425 0, 0, 0, 0, 0));
10426}
10427
Harry Cutts39b7ca22022-10-05 15:55:48 +000010428TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
10429 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10430 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10431 NotifyMotionArgs motionArgs;
10432
10433 // Place two fingers down.
10434 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10435
10436 processId(mapper, FIRST_TRACKING_ID);
10437 processPosition(mapper, x1, y1);
10438 processMTSync(mapper);
10439 processId(mapper, SECOND_TRACKING_ID);
10440 processPosition(mapper, x2, y2);
10441 processMTSync(mapper);
10442 processSync(mapper);
10443
10444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10445 ASSERT_EQ(1U, motionArgs.pointerCount);
10446 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10447 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10448 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10449 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10450
10451 // Move the two fingers down and to the left.
10452 int32_t movingDistance = 200;
10453 x1 -= movingDistance;
10454 y1 += movingDistance;
10455 x2 -= movingDistance;
10456 y2 += movingDistance;
10457
10458 processId(mapper, FIRST_TRACKING_ID);
10459 processPosition(mapper, x1, y1);
10460 processMTSync(mapper);
10461 processId(mapper, SECOND_TRACKING_ID);
10462 processPosition(mapper, x2, y2);
10463 processMTSync(mapper);
10464 processSync(mapper);
10465
10466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10467 ASSERT_EQ(1U, motionArgs.pointerCount);
10468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10469 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10470 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10471 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10472}
10473
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010474TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
10475 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10476 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10477 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10479
10480 // Start a stylus gesture.
10481 processKey(mapper, BTN_TOOL_PEN, 1);
10482 processId(mapper, FIRST_TRACKING_ID);
10483 processPosition(mapper, 100, 200);
10484 processSync(mapper);
10485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10486 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10487 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10488 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10489 // TODO(b/257078296): Pointer mode generates extra event.
10490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10491 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10492 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10493 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10495
10496 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10497 // gesture should be disabled.
10498 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10499 viewport->isActive = false;
10500 mFakePolicy->updateViewport(*viewport);
10501 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10503 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10504 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10505 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10506 // TODO(b/257078296): Pointer mode generates extra event.
10507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10508 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10509 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10510 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10512}
10513
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010514// --- JoystickInputMapperTest ---
10515
10516class JoystickInputMapperTest : public InputMapperTest {
10517protected:
10518 static const int32_t RAW_X_MIN;
10519 static const int32_t RAW_X_MAX;
10520 static const int32_t RAW_Y_MIN;
10521 static const int32_t RAW_Y_MAX;
10522
10523 void SetUp() override {
10524 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10525 }
10526 void prepareAxes() {
10527 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10528 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10529 }
10530
10531 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10532 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10533 }
10534
10535 void processSync(JoystickInputMapper& mapper) {
10536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10537 }
10538
Michael Wrighta9cf4192022-12-01 23:46:39 +000010539 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010540 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10541 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10542 NO_PORT, ViewportType::VIRTUAL);
10543 }
10544};
10545
10546const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10547const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10548const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10549const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10550
10551TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10552 prepareAxes();
10553 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10554
10555 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10556
Michael Wrighta9cf4192022-12-01 23:46:39 +000010557 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010558
10559 // Send an axis event
10560 processAxis(mapper, ABS_X, 100);
10561 processSync(mapper);
10562
10563 NotifyMotionArgs args;
10564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10565 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10566
10567 // Send another axis event
10568 processAxis(mapper, ABS_Y, 100);
10569 processSync(mapper);
10570
10571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10572 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10573}
10574
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010575// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010576
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010577class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010578protected:
10579 static const char* DEVICE_NAME;
10580 static const char* DEVICE_LOCATION;
10581 static const int32_t DEVICE_ID;
10582 static const int32_t DEVICE_GENERATION;
10583 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010584 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010585 static const int32_t EVENTHUB_ID;
10586
10587 std::shared_ptr<FakeEventHub> mFakeEventHub;
10588 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010589 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010590 std::unique_ptr<InstrumentedInputReader> mReader;
10591 std::shared_ptr<InputDevice> mDevice;
10592
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010593 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010594 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010595 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010596 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010597 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010598 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010599 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10600 }
10601
10602 void SetUp() override { SetUp(DEVICE_CLASSES); }
10603
10604 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010605 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010606 mFakePolicy.clear();
10607 }
10608
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010609 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010610 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10611 mReader->requestRefreshConfiguration(changes);
10612 mReader->loopOnce();
10613 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010614 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010615 }
10616
10617 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10618 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010619 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010620 InputDeviceIdentifier identifier;
10621 identifier.name = name;
10622 identifier.location = location;
10623 std::shared_ptr<InputDevice> device =
10624 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10625 identifier);
10626 mReader->pushNextDevice(device);
10627 mFakeEventHub->addDevice(eventHubId, name, classes);
10628 mReader->loopOnce();
10629 return device;
10630 }
10631
10632 template <class T, typename... Args>
10633 T& addControllerAndConfigure(Args... args) {
10634 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10635
10636 return controller;
10637 }
10638};
10639
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010640const char* PeripheralControllerTest::DEVICE_NAME = "device";
10641const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10642const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10643const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10644const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010645const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10646 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010647const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010648
10649// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010650class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010651protected:
10652 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010653 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010654 }
10655};
10656
10657TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010658 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010659
Harry Cuttsa5b71292022-11-28 12:56:17 +000010660 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10661 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10662 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010663}
10664
10665TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010666 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010667
Harry Cuttsa5b71292022-11-28 12:56:17 +000010668 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10669 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10670 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010671}
10672
10673// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010674class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010675protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010676 void SetUp() override {
10677 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10678 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010679};
10680
Chris Ye85758332021-05-16 23:05:17 -070010681TEST_F(LightControllerTest, MonoLight) {
10682 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010683 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010684 .maxBrightness = 255,
10685 .flags = InputLightClass::BRIGHTNESS,
10686 .path = ""};
10687 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010688
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010689 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010690 InputDeviceInfo info;
10691 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010692 std::vector<InputDeviceLightInfo> lights = info.getLights();
10693 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010694 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10695 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10696
10697 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10698 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10699}
10700
10701TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10702 RawLightInfo infoMono = {.id = 1,
10703 .name = "mono_keyboard_backlight",
10704 .maxBrightness = 255,
10705 .flags = InputLightClass::BRIGHTNESS |
10706 InputLightClass::KEYBOARD_BACKLIGHT,
10707 .path = ""};
10708 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10709
10710 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10711 InputDeviceInfo info;
10712 controller.populateDeviceInfo(&info);
10713 std::vector<InputDeviceLightInfo> lights = info.getLights();
10714 ASSERT_EQ(1U, lights.size());
10715 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10716 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010717
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010718 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10719 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010720}
10721
10722TEST_F(LightControllerTest, RGBLight) {
10723 RawLightInfo infoRed = {.id = 1,
10724 .name = "red",
10725 .maxBrightness = 255,
10726 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10727 .path = ""};
10728 RawLightInfo infoGreen = {.id = 2,
10729 .name = "green",
10730 .maxBrightness = 255,
10731 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10732 .path = ""};
10733 RawLightInfo infoBlue = {.id = 3,
10734 .name = "blue",
10735 .maxBrightness = 255,
10736 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10737 .path = ""};
10738 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10739 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10740 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10741
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010742 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010743 InputDeviceInfo info;
10744 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010745 std::vector<InputDeviceLightInfo> lights = info.getLights();
10746 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010747 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10748 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10749 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10750
10751 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10752 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10753}
10754
10755TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10756 RawLightInfo infoRed = {.id = 1,
10757 .name = "red_keyboard_backlight",
10758 .maxBrightness = 255,
10759 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10760 InputLightClass::KEYBOARD_BACKLIGHT,
10761 .path = ""};
10762 RawLightInfo infoGreen = {.id = 2,
10763 .name = "green_keyboard_backlight",
10764 .maxBrightness = 255,
10765 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10766 InputLightClass::KEYBOARD_BACKLIGHT,
10767 .path = ""};
10768 RawLightInfo infoBlue = {.id = 3,
10769 .name = "blue_keyboard_backlight",
10770 .maxBrightness = 255,
10771 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10772 InputLightClass::KEYBOARD_BACKLIGHT,
10773 .path = ""};
10774 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10775 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10776 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10777
10778 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10779 InputDeviceInfo info;
10780 controller.populateDeviceInfo(&info);
10781 std::vector<InputDeviceLightInfo> lights = info.getLights();
10782 ASSERT_EQ(1U, lights.size());
10783 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10784 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10785 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10786
10787 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10788 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10789}
10790
10791TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10792 RawLightInfo infoRed = {.id = 1,
10793 .name = "red",
10794 .maxBrightness = 255,
10795 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10796 .path = ""};
10797 RawLightInfo infoGreen = {.id = 2,
10798 .name = "green",
10799 .maxBrightness = 255,
10800 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10801 .path = ""};
10802 RawLightInfo infoBlue = {.id = 3,
10803 .name = "blue",
10804 .maxBrightness = 255,
10805 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10806 .path = ""};
10807 RawLightInfo infoGlobal = {.id = 3,
10808 .name = "global_keyboard_backlight",
10809 .maxBrightness = 255,
10810 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10811 InputLightClass::KEYBOARD_BACKLIGHT,
10812 .path = ""};
10813 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10814 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10815 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10816 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10817
10818 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10819 InputDeviceInfo info;
10820 controller.populateDeviceInfo(&info);
10821 std::vector<InputDeviceLightInfo> lights = info.getLights();
10822 ASSERT_EQ(1U, lights.size());
10823 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10824 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10825 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010826
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010827 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10828 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010829}
10830
10831TEST_F(LightControllerTest, MultiColorRGBLight) {
10832 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010833 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010834 .maxBrightness = 255,
10835 .flags = InputLightClass::BRIGHTNESS |
10836 InputLightClass::MULTI_INTENSITY |
10837 InputLightClass::MULTI_INDEX,
10838 .path = ""};
10839
10840 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10841
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010842 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010843 InputDeviceInfo info;
10844 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010845 std::vector<InputDeviceLightInfo> lights = info.getLights();
10846 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010847 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10848 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10849 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10850
10851 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10852 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10853}
10854
10855TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10856 RawLightInfo infoColor = {.id = 1,
10857 .name = "multi_color_keyboard_backlight",
10858 .maxBrightness = 255,
10859 .flags = InputLightClass::BRIGHTNESS |
10860 InputLightClass::MULTI_INTENSITY |
10861 InputLightClass::MULTI_INDEX |
10862 InputLightClass::KEYBOARD_BACKLIGHT,
10863 .path = ""};
10864
10865 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10866
10867 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10868 InputDeviceInfo info;
10869 controller.populateDeviceInfo(&info);
10870 std::vector<InputDeviceLightInfo> lights = info.getLights();
10871 ASSERT_EQ(1U, lights.size());
10872 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10873 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10874 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010875
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010876 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10877 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010878}
10879
10880TEST_F(LightControllerTest, PlayerIdLight) {
10881 RawLightInfo info1 = {.id = 1,
10882 .name = "player1",
10883 .maxBrightness = 255,
10884 .flags = InputLightClass::BRIGHTNESS,
10885 .path = ""};
10886 RawLightInfo info2 = {.id = 2,
10887 .name = "player2",
10888 .maxBrightness = 255,
10889 .flags = InputLightClass::BRIGHTNESS,
10890 .path = ""};
10891 RawLightInfo info3 = {.id = 3,
10892 .name = "player3",
10893 .maxBrightness = 255,
10894 .flags = InputLightClass::BRIGHTNESS,
10895 .path = ""};
10896 RawLightInfo info4 = {.id = 4,
10897 .name = "player4",
10898 .maxBrightness = 255,
10899 .flags = InputLightClass::BRIGHTNESS,
10900 .path = ""};
10901 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10902 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10903 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10904 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10905
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010906 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010907 InputDeviceInfo info;
10908 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010909 std::vector<InputDeviceLightInfo> lights = info.getLights();
10910 ASSERT_EQ(1U, lights.size());
10911 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010912 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10913 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010914
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010915 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10916 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10917 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010918}
10919
Michael Wrightd02c5b62014-02-10 15:10:22 -080010920} // namespace android