blob: 3bb2bde3550926f35938dab30c34c19f71ec32a6 [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 Cuttse6512e12022-11-28 18:44:01 +000048#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000049#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000050#include "TestConstants.h"
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000051#include "android/hardware/input/InputDeviceCountryCode.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000052#include "input/DisplayViewport.h"
53#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010054
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000055using android::hardware::input::InputDeviceCountryCode;
56
Michael Wrightd02c5b62014-02-10 15:10:22 -080057namespace android {
58
Dominik Laskowski2f01d772022-03-23 16:01:29 -070059using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000060using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070061using std::chrono_literals::operator""ms;
62
Michael Wrightd02c5b62014-02-10 15:10:22 -080063// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080064static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000065static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080066static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000067static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080068static constexpr int32_t DISPLAY_WIDTH = 480;
69static constexpr int32_t DISPLAY_HEIGHT = 800;
70static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
71static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
72static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070073static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070074static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080075
arthurhungcc7f9802020-04-30 17:55:40 +080076static constexpr int32_t FIRST_SLOT = 0;
77static constexpr int32_t SECOND_SLOT = 1;
78static constexpr int32_t THIRD_SLOT = 2;
79static constexpr int32_t INVALID_TRACKING_ID = -1;
80static constexpr int32_t FIRST_TRACKING_ID = 0;
81static constexpr int32_t SECOND_TRACKING_ID = 1;
82static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080083static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
84static constexpr int32_t LIGHT_COLOR = 0x7F448866;
85static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080086
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080087static constexpr int32_t ACTION_POINTER_0_DOWN =
88 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
89static constexpr int32_t ACTION_POINTER_0_UP =
90 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
91static constexpr int32_t ACTION_POINTER_1_DOWN =
92 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
93static constexpr int32_t ACTION_POINTER_1_UP =
94 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
95
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000096// Minimum timestamp separation between subsequent input events from a Bluetooth device.
97static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
98// Maximum smoothing time delta so that we don't generate events too far into the future.
99constexpr static nsecs_t MAX_BLUETOOTH_SMOOTHING_DELTA = ms2ns(32);
100
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101template<typename T>
102static inline T min(T a, T b) {
103 return a < b ? a : b;
104}
105
106static inline float avg(float x, float y) {
107 return (x + y) / 2;
108}
109
Chris Ye3fdbfef2021-01-06 18:45:18 -0800110// Mapping for light color name and the light color
111const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
112 {"green", LightColor::GREEN},
113 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114
Michael Wrighta9cf4192022-12-01 23:46:39 +0000115static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700116 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000117 case ui::ROTATION_90:
118 return ui::ROTATION_270;
119 case ui::ROTATION_270:
120 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700121 default:
122 return orientation;
123 }
124}
125
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800126static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
127 InputDeviceInfo info;
128 mapper.populateDeviceInfo(&info);
129
130 const InputDeviceInfo::MotionRange* motionRange =
131 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
132 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
133}
134
135static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
136 InputDeviceInfo info;
137 mapper.populateDeviceInfo(&info);
138
139 const InputDeviceInfo::MotionRange* motionRange =
140 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
141 ASSERT_EQ(nullptr, motionRange);
142}
143
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700144[[maybe_unused]] static void dumpReader(InputReader& reader) {
145 std::string dump;
146 reader.dump(dump);
147 std::istringstream iss(dump);
148 for (std::string line; std::getline(iss, line);) {
149 ALOGE("%s", line.c_str());
150 std::this_thread::sleep_for(std::chrono::milliseconds(1));
151 }
152}
153
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154// --- FakeInputMapper ---
155
156class FakeInputMapper : public InputMapper {
157 uint32_t mSources;
158 int32_t mKeyboardType;
159 int32_t mMetaState;
160 KeyedVector<int32_t, int32_t> mKeyCodeStates;
161 KeyedVector<int32_t, int32_t> mScanCodeStates;
162 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100163 // fake mapping which would normally come from keyCharacterMap
164 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800165 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700167 std::mutex mLock;
168 std::condition_variable mStateChangedCondition;
169 bool mConfigureWasCalled GUARDED_BY(mLock);
170 bool mResetWasCalled GUARDED_BY(mLock);
171 bool mProcessWasCalled GUARDED_BY(mLock);
172 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800173
Arthur Hungc23540e2018-11-29 20:42:11 +0800174 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800176 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
177 : InputMapper(deviceContext),
178 mSources(sources),
179 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800181 mConfigureWasCalled(false),
182 mResetWasCalled(false),
183 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184
Chris Yea52ade12020-08-27 16:49:20 -0700185 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186
187 void setKeyboardType(int32_t keyboardType) {
188 mKeyboardType = keyboardType;
189 }
190
191 void setMetaState(int32_t metaState) {
192 mMetaState = metaState;
193 }
194
195 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700196 std::unique_lock<std::mutex> lock(mLock);
197 base::ScopedLockAssertion assumeLocked(mLock);
198 const bool configureCalled =
199 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
200 return mConfigureWasCalled;
201 });
202 if (!configureCalled) {
203 FAIL() << "Expected configure() to have been called.";
204 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800205 mConfigureWasCalled = false;
206 }
207
208 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700209 std::unique_lock<std::mutex> lock(mLock);
210 base::ScopedLockAssertion assumeLocked(mLock);
211 const bool resetCalled =
212 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
213 return mResetWasCalled;
214 });
215 if (!resetCalled) {
216 FAIL() << "Expected reset() to have been called.";
217 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218 mResetWasCalled = false;
219 }
220
Yi Kong9b14ac62018-07-17 13:48:38 -0700221 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700222 std::unique_lock<std::mutex> lock(mLock);
223 base::ScopedLockAssertion assumeLocked(mLock);
224 const bool processCalled =
225 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
226 return mProcessWasCalled;
227 });
228 if (!processCalled) {
229 FAIL() << "Expected process() to have been called.";
230 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800231 if (outLastEvent) {
232 *outLastEvent = mLastEvent;
233 }
234 mProcessWasCalled = false;
235 }
236
237 void setKeyCodeState(int32_t keyCode, int32_t state) {
238 mKeyCodeStates.replaceValueFor(keyCode, state);
239 }
240
241 void setScanCodeState(int32_t scanCode, int32_t state) {
242 mScanCodeStates.replaceValueFor(scanCode, state);
243 }
244
245 void setSwitchState(int32_t switchCode, int32_t state) {
246 mSwitchStates.replaceValueFor(switchCode, state);
247 }
248
249 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800250 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800251 }
252
Philip Junker4af3b3d2021-12-14 10:36:55 +0100253 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
254 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
255 }
256
Michael Wrightd02c5b62014-02-10 15:10:22 -0800257private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100258 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259
Chris Yea52ade12020-08-27 16:49:20 -0700260 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261 InputMapper::populateDeviceInfo(deviceInfo);
262
263 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
264 deviceInfo->setKeyboardType(mKeyboardType);
265 }
266 }
267
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700268 std::list<NotifyArgs> configure(nsecs_t, const InputReaderConfiguration* config,
269 uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700270 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800271 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800272
273 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800274 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +0800275 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
276 mViewport = config->getDisplayViewportByPort(*displayPort);
277 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700278
279 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700280 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281 }
282
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700283 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700284 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800285 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700286 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700287 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800288 }
289
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700290 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700291 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800292 mLastEvent = *rawEvent;
293 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700294 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700295 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296 }
297
Chris Yea52ade12020-08-27 16:49:20 -0700298 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800299 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
300 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
301 }
302
Philip Junker4af3b3d2021-12-14 10:36:55 +0100303 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
304 auto it = mKeyCodeMapping.find(locationKeyCode);
305 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
306 }
307
Chris Yea52ade12020-08-27 16:49:20 -0700308 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800309 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
310 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
311 }
312
Chris Yea52ade12020-08-27 16:49:20 -0700313 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800314 ssize_t index = mSwitchStates.indexOfKey(switchCode);
315 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
316 }
317
Chris Yea52ade12020-08-27 16:49:20 -0700318 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700319 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700320 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700321 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800322 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
323 if (keyCodes[i] == mSupportedKeyCodes[j]) {
324 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325 }
326 }
327 }
Chris Yea52ade12020-08-27 16:49:20 -0700328 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329 return result;
330 }
331
332 virtual int32_t getMetaState() {
333 return mMetaState;
334 }
335
336 virtual void fadePointer() {
337 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800338
339 virtual std::optional<int32_t> getAssociatedDisplay() {
340 if (mViewport) {
341 return std::make_optional(mViewport->displayId);
342 }
343 return std::nullopt;
344 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800345};
346
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700347// --- InputReaderPolicyTest ---
348class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700349protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700350 sp<FakeInputReaderPolicy> mFakePolicy;
351
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700352 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700353 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700354};
355
356/**
357 * Check that empty set of viewports is an acceptable configuration.
358 * Also try to get internal viewport two different ways - by type and by uniqueId.
359 *
360 * There will be confusion if two viewports with empty uniqueId and identical type are present.
361 * Such configuration is not currently allowed.
362 */
363TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700364 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700365
366 // We didn't add any viewports yet, so there shouldn't be any.
367 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100368 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700369 ASSERT_FALSE(internalViewport);
370
371 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000372 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
373 true /*isActive*/, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700374
375 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700376 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700377 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100378 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700379
380 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100381 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700382 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700383 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700384
385 mFakePolicy->clearViewports();
386 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700387 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700388 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100389 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700390 ASSERT_FALSE(internalViewport);
391}
392
393TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
394 const std::string internalUniqueId = "local:0";
395 const std::string externalUniqueId = "local:1";
396 const std::string virtualUniqueId1 = "virtual:2";
397 const std::string virtualUniqueId2 = "virtual:3";
398 constexpr int32_t virtualDisplayId1 = 2;
399 constexpr int32_t virtualDisplayId2 = 3;
400
401 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000402 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
403 true /*isActive*/, internalUniqueId, NO_PORT,
404 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700405 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000406 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
407 true /*isActive*/, externalUniqueId, NO_PORT,
408 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700409 // Add an virtual viewport
410 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000411 ui::ROTATION_0, true /*isActive*/, virtualUniqueId1, NO_PORT,
412 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700413 // Add another virtual viewport
414 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000415 ui::ROTATION_0, true /*isActive*/, virtualUniqueId2, NO_PORT,
416 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700417
418 // Check matching by type for internal
419 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100420 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700421 ASSERT_TRUE(internalViewport);
422 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
423
424 // Check matching by type for external
425 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100426 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700427 ASSERT_TRUE(externalViewport);
428 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
429
430 // Check matching by uniqueId for virtual viewport #1
431 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700432 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700433 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100434 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700435 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
436 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
437
438 // Check matching by uniqueId for virtual viewport #2
439 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700440 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700441 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100442 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700443 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
444 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
445}
446
447
448/**
449 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
450 * that lookup works by checking display id.
451 * Check that 2 viewports of each kind is possible, for all existing viewport types.
452 */
453TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
454 const std::string uniqueId1 = "uniqueId1";
455 const std::string uniqueId2 = "uniqueId2";
456 constexpr int32_t displayId1 = 2;
457 constexpr int32_t displayId2 = 3;
458
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100459 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
460 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700461 for (const ViewportType& type : types) {
462 mFakePolicy->clearViewports();
463 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000464 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
465 true /*isActive*/, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700466 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000467 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
468 true /*isActive*/, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700469
470 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700471 std::optional<DisplayViewport> viewport1 =
472 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700473 ASSERT_TRUE(viewport1);
474 ASSERT_EQ(displayId1, viewport1->displayId);
475 ASSERT_EQ(type, viewport1->type);
476
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700477 std::optional<DisplayViewport> viewport2 =
478 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700479 ASSERT_TRUE(viewport2);
480 ASSERT_EQ(displayId2, viewport2->displayId);
481 ASSERT_EQ(type, viewport2->type);
482
483 // When there are multiple viewports of the same kind, and uniqueId is not specified
484 // in the call to getDisplayViewport, then that situation is not supported.
485 // The viewports can be stored in any order, so we cannot rely on the order, since that
486 // is just implementation detail.
487 // However, we can check that it still returns *a* viewport, we just cannot assert
488 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700489 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700490 ASSERT_TRUE(someViewport);
491 }
492}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800493
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700494/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000495 * When we have multiple internal displays make sure we always return the default display when
496 * querying by type.
497 */
498TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
499 const std::string uniqueId1 = "uniqueId1";
500 const std::string uniqueId2 = "uniqueId2";
501 constexpr int32_t nonDefaultDisplayId = 2;
502 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
503 "Test display ID should not be ADISPLAY_ID_DEFAULT");
504
505 // Add the default display first and ensure it gets returned.
506 mFakePolicy->clearViewports();
507 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000508 ui::ROTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000509 ViewportType::INTERNAL);
510 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000511 ui::ROTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000512 ViewportType::INTERNAL);
513
514 std::optional<DisplayViewport> viewport =
515 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
516 ASSERT_TRUE(viewport);
517 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
518 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
519
520 // Add the default display second to make sure order doesn't matter.
521 mFakePolicy->clearViewports();
522 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000523 ui::ROTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000524 ViewportType::INTERNAL);
525 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000526 ui::ROTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000527 ViewportType::INTERNAL);
528
529 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
530 ASSERT_TRUE(viewport);
531 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
532 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
533}
534
535/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700536 * Check getDisplayViewportByPort
537 */
538TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100539 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700540 const std::string uniqueId1 = "uniqueId1";
541 const std::string uniqueId2 = "uniqueId2";
542 constexpr int32_t displayId1 = 1;
543 constexpr int32_t displayId2 = 2;
544 const uint8_t hdmi1 = 0;
545 const uint8_t hdmi2 = 1;
546 const uint8_t hdmi3 = 2;
547
548 mFakePolicy->clearViewports();
549 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000550 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
551 true /*isActive*/, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700552 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000553 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
554 true /*isActive*/, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700555
556 // Check that correct display viewport was returned by comparing the display ports.
557 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
558 ASSERT_TRUE(hdmi1Viewport);
559 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
560 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
561
562 // Check that we can still get the same viewport using the uniqueId
563 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
564 ASSERT_TRUE(hdmi1Viewport);
565 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
566 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
567 ASSERT_EQ(type, hdmi1Viewport->type);
568
569 // Check that we cannot find a port with "HDMI2", because we never added one
570 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
571 ASSERT_FALSE(hdmi2Viewport);
572}
573
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574// --- InputReaderTest ---
575
576class InputReaderTest : public testing::Test {
577protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700578 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700580 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000581 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800582
Chris Yea52ade12020-08-27 16:49:20 -0700583 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700584 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700585 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700586 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800587
Prabir Pradhan28efc192019-11-05 01:10:04 +0000588 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700589 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800590 }
591
Chris Yea52ade12020-08-27 16:49:20 -0700592 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700593 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800595 }
596
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700597 void addDevice(int32_t eventHubId, const std::string& name,
598 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800599 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600
601 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800602 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800603 }
604 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000605 mReader->loopOnce();
606 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700607 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
608 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609 }
610
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800611 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700612 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000613 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700614 }
615
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800616 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700617 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000618 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700619 }
620
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800621 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700622 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700623 ftl::Flags<InputDeviceClass> classes,
624 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800625 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800626 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
627 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800628 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800629 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800630 return mapper;
631 }
632};
633
Chris Ye98d3f532020-10-01 21:48:59 -0700634TEST_F(InputReaderTest, PolicyGetInputDevices) {
635 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700636 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700637 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800638
639 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700640 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800641 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800642 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100643 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800644 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
645 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000646 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800647}
648
Chris Yee7310032020-09-22 15:36:28 -0700649TEST_F(InputReaderTest, GetMergedInputDevices) {
650 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
651 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
652 // Add two subdevices to device
653 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
654 // Must add at least one mapper or the device will be ignored!
655 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
656 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
657
658 // Push same device instance for next device to be added, so they'll have same identifier.
659 mReader->pushNextDevice(device);
660 mReader->pushNextDevice(device);
661 ASSERT_NO_FATAL_FAILURE(
662 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
663 ASSERT_NO_FATAL_FAILURE(
664 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
665
666 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000667 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700668}
669
Chris Yee14523a2020-12-19 13:46:00 -0800670TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
671 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
672 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
673 // Add two subdevices to device
674 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
675 // Must add at least one mapper or the device will be ignored!
676 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
677 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
678
679 // Push same device instance for next device to be added, so they'll have same identifier.
680 mReader->pushNextDevice(device);
681 mReader->pushNextDevice(device);
682 // Sensor device is initially disabled
683 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
684 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
685 nullptr));
686 // Device is disabled because the only sub device is a sensor device and disabled initially.
687 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
688 ASSERT_FALSE(device->isEnabled());
689 ASSERT_NO_FATAL_FAILURE(
690 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
691 // The merged device is enabled if any sub device is enabled
692 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
693 ASSERT_TRUE(device->isEnabled());
694}
695
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700696TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800697 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700698 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800699 constexpr int32_t eventHubId = 1;
700 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700701 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800702 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800703 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800704 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700705
Yi Kong9b14ac62018-07-17 13:48:38 -0700706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700707
708 NotifyDeviceResetArgs resetArgs;
709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700710 ASSERT_EQ(deviceId, resetArgs.deviceId);
711
712 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800713 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000714 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700715
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700717 ASSERT_EQ(deviceId, resetArgs.deviceId);
718 ASSERT_EQ(device->isEnabled(), false);
719
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800720 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000721 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700724 ASSERT_EQ(device->isEnabled(), false);
725
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800726 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000727 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700729 ASSERT_EQ(deviceId, resetArgs.deviceId);
730 ASSERT_EQ(device->isEnabled(), true);
731}
732
Michael Wrightd02c5b62014-02-10 15:10:22 -0800733TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800734 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700735 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800736 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800737 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800738 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800739 AINPUT_SOURCE_KEYBOARD, nullptr);
740 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800741
742 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
743 AINPUT_SOURCE_ANY, AKEYCODE_A))
744 << "Should return unknown when the device id is >= 0 but unknown.";
745
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800746 ASSERT_EQ(AKEY_STATE_UNKNOWN,
747 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
748 << "Should return unknown when the device id is valid but the sources are not "
749 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800751 ASSERT_EQ(AKEY_STATE_DOWN,
752 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
753 AKEYCODE_A))
754 << "Should return value provided by mapper when device id is valid and the device "
755 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756
757 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
758 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
759 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
760
761 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
762 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
763 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
764}
765
Philip Junker4af3b3d2021-12-14 10:36:55 +0100766TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
767 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
768 constexpr int32_t eventHubId = 1;
769 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
770 InputDeviceClass::KEYBOARD,
771 AINPUT_SOURCE_KEYBOARD, nullptr);
772 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
773
774 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
775 << "Should return unknown when the device with the specified id is not found.";
776
777 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
778 << "Should return correct mapping when device id is valid and mapping exists.";
779
780 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
781 << "Should return the location key code when device id is valid and there's no "
782 "mapping.";
783}
784
785TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
786 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
787 constexpr int32_t eventHubId = 1;
788 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
789 InputDeviceClass::JOYSTICK,
790 AINPUT_SOURCE_GAMEPAD, nullptr);
791 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
792
793 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
794 << "Should return unknown when the device id is valid but there is no keyboard mapper";
795}
796
Michael Wrightd02c5b62014-02-10 15:10:22 -0800797TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800798 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700799 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800800 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800801 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800802 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800803 AINPUT_SOURCE_KEYBOARD, nullptr);
804 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805
806 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
807 AINPUT_SOURCE_ANY, KEY_A))
808 << "Should return unknown when the device id is >= 0 but unknown.";
809
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800810 ASSERT_EQ(AKEY_STATE_UNKNOWN,
811 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
812 << "Should return unknown when the device id is valid but the sources are not "
813 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800814
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800815 ASSERT_EQ(AKEY_STATE_DOWN,
816 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
817 KEY_A))
818 << "Should return value provided by mapper when device id is valid and the device "
819 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820
821 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
822 AINPUT_SOURCE_TRACKBALL, KEY_A))
823 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
824
825 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
826 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
827 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
828}
829
830TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800831 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700832 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800833 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800834 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800835 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800836 AINPUT_SOURCE_KEYBOARD, nullptr);
837 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800838
839 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
840 AINPUT_SOURCE_ANY, SW_LID))
841 << "Should return unknown when the device id is >= 0 but unknown.";
842
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800843 ASSERT_EQ(AKEY_STATE_UNKNOWN,
844 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
845 << "Should return unknown when the device id is valid but the sources are not "
846 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800847
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800848 ASSERT_EQ(AKEY_STATE_DOWN,
849 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
850 SW_LID))
851 << "Should return value provided by mapper when device id is valid and the device "
852 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800853
854 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
855 AINPUT_SOURCE_TRACKBALL, SW_LID))
856 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
857
858 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
859 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
860 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
861}
862
863TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800864 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700865 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800866 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800867 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800868 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800869 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100870
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800871 mapper.addSupportedKeyCode(AKEYCODE_A);
872 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800873
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700874 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800875 uint8_t flags[4] = { 0, 0, 0, 1 };
876
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700877 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800878 << "Should return false when device id is >= 0 but unknown.";
879 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
880
881 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700882 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800883 << "Should return false when device id is valid but the sources are not supported by "
884 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800885 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
886
887 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700888 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800889 keyCodes, flags))
890 << "Should return value provided by mapper when device id is valid and the device "
891 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
893
894 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700895 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
896 << "Should return false when the device id is < 0 but the sources are not supported by "
897 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
899
900 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700901 ASSERT_TRUE(
902 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
903 << "Should return value provided by mapper when device id is < 0 and one of the "
904 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
906}
907
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000908TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800909 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700910 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800911
912 NotifyConfigurationChangedArgs args;
913
914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
915 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
916}
917
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000918TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800919 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700920 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000921 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800922 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000923 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800924 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800925 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800926 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000928 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000929 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800930 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
931
932 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800933 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000934 ASSERT_EQ(when, event.when);
935 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800936 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800937 ASSERT_EQ(EV_KEY, event.type);
938 ASSERT_EQ(KEY_A, event.code);
939 ASSERT_EQ(1, event.value);
940}
941
Garfield Tan1c7bc862020-01-28 13:24:04 -0800942TEST_F(InputReaderTest, DeviceReset_RandomId) {
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;
946 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800947 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800948 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800949 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800950 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800951
952 NotifyDeviceResetArgs resetArgs;
953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800954 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800955
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800956 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000957 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800959 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800960 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800961
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800962 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000963 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800965 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800966 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800967
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800968 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000969 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800971 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800972 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800973}
974
Garfield Tan1c7bc862020-01-28 13:24:04 -0800975TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
976 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700977 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800978 constexpr int32_t eventHubId = 1;
979 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
980 // Must add at least one mapper or the device will be ignored!
981 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800982 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -0800983 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
984
985 NotifyDeviceResetArgs resetArgs;
986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
987 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
988}
989
Arthur Hungc23540e2018-11-29 20:42:11 +0800990TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800991 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700992 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800993 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +0800994 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800995 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
996 FakeInputMapper& mapper =
997 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800998 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +0800999
1000 const uint8_t hdmi1 = 1;
1001
1002 // Associated touch screen with second display.
1003 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1004
1005 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001006 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001007 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1008 true /*isActive*/, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001009 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001010 ui::ROTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001011 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001012 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001013 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001014
1015 // Add the device, and make sure all of the callbacks are triggered.
1016 // The device is added after the input port associations are processed since
1017 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001018 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001021 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001022
Arthur Hung2c9a3342019-07-23 14:18:59 +08001023 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001024 ASSERT_EQ(deviceId, device->getId());
1025 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1026 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001027
1028 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001029 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001030 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001031 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001032}
1033
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001034TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1035 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001036 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001037 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1038 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1039 // Must add at least one mapper or the device will be ignored!
1040 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1041 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1042 mReader->pushNextDevice(device);
1043 mReader->pushNextDevice(device);
1044 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1045 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1046
1047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1048
1049 NotifyDeviceResetArgs resetArgs;
1050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1051 ASSERT_EQ(deviceId, resetArgs.deviceId);
1052 ASSERT_TRUE(device->isEnabled());
1053 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1054 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1055
1056 disableDevice(deviceId);
1057 mReader->loopOnce();
1058
1059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1060 ASSERT_EQ(deviceId, resetArgs.deviceId);
1061 ASSERT_FALSE(device->isEnabled());
1062 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1063 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1064
1065 enableDevice(deviceId);
1066 mReader->loopOnce();
1067
1068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1069 ASSERT_EQ(deviceId, resetArgs.deviceId);
1070 ASSERT_TRUE(device->isEnabled());
1071 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1072 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1073}
1074
1075TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1076 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001077 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001078 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1079 // Add two subdevices to device
1080 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1081 FakeInputMapper& mapperDevice1 =
1082 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1083 FakeInputMapper& mapperDevice2 =
1084 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1085 mReader->pushNextDevice(device);
1086 mReader->pushNextDevice(device);
1087 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1088 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1089
1090 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1091 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1092
1093 ASSERT_EQ(AKEY_STATE_DOWN,
1094 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1095 ASSERT_EQ(AKEY_STATE_DOWN,
1096 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1097 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1098 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1099}
1100
Prabir Pradhan7e186182020-11-10 13:56:45 -08001101TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1102 NotifyPointerCaptureChangedArgs args;
1103
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001104 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001105 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1106 mReader->loopOnce();
1107 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001108 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1109 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001110
1111 mFakePolicy->setPointerCapture(false);
1112 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1113 mReader->loopOnce();
1114 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001115 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001116
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001117 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001118 // does not change.
1119 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1120 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001121 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001122}
1123
Chris Ye87143712020-11-10 05:05:58 +00001124class FakeVibratorInputMapper : public FakeInputMapper {
1125public:
1126 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1127 : FakeInputMapper(deviceContext, sources) {}
1128
1129 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1130};
1131
1132TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1133 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001134 ftl::Flags<InputDeviceClass> deviceClass =
1135 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001136 constexpr int32_t eventHubId = 1;
1137 const char* DEVICE_LOCATION = "BLUETOOTH";
1138 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1139 FakeVibratorInputMapper& mapper =
1140 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1141 mReader->pushNextDevice(device);
1142
1143 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1144 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1145
1146 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1147 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1148}
1149
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001150// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001151
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001152class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001153public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001154 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001155
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001156 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001157
Andy Chenf9f1a022022-08-29 20:07:10 -04001158 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1159
Chris Yee2b1e5c2021-03-10 22:45:12 -08001160 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1161
1162 void dump(std::string& dump) override {}
1163
1164 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1165 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001166 }
1167
Chris Yee2b1e5c2021-03-10 22:45:12 -08001168 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1169 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001170 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001171
1172 bool setLightColor(int32_t lightId, int32_t color) override {
1173 getDeviceContext().setLightBrightness(lightId, color >> 24);
1174 return true;
1175 }
1176
1177 std::optional<int32_t> getLightColor(int32_t lightId) override {
1178 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1179 if (!result.has_value()) {
1180 return std::nullopt;
1181 }
1182 return result.value() << 24;
1183 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001184
1185 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1186
1187 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1188
1189private:
1190 InputDeviceContext& mDeviceContext;
1191 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1192 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001193 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001194};
1195
Chris Yee2b1e5c2021-03-10 22:45:12 -08001196TEST_F(InputReaderTest, BatteryGetCapacity) {
1197 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001198 ftl::Flags<InputDeviceClass> deviceClass =
1199 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001200 constexpr int32_t eventHubId = 1;
1201 const char* DEVICE_LOCATION = "BLUETOOTH";
1202 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001203 FakePeripheralController& controller =
1204 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001205 mReader->pushNextDevice(device);
1206
1207 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1208
Harry Cuttsa5b71292022-11-28 12:56:17 +00001209 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1210 FakeEventHub::BATTERY_CAPACITY);
1211 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001212}
1213
1214TEST_F(InputReaderTest, BatteryGetStatus) {
1215 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001216 ftl::Flags<InputDeviceClass> deviceClass =
1217 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001218 constexpr int32_t eventHubId = 1;
1219 const char* DEVICE_LOCATION = "BLUETOOTH";
1220 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001221 FakePeripheralController& controller =
1222 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001223 mReader->pushNextDevice(device);
1224
1225 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1226
Harry Cuttsa5b71292022-11-28 12:56:17 +00001227 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1228 FakeEventHub::BATTERY_STATUS);
1229 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001230}
1231
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001232TEST_F(InputReaderTest, BatteryGetDevicePath) {
1233 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1234 ftl::Flags<InputDeviceClass> deviceClass =
1235 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1236 constexpr int32_t eventHubId = 1;
1237 const char* DEVICE_LOCATION = "BLUETOOTH";
1238 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1239 device->addController<FakePeripheralController>(eventHubId);
1240 mReader->pushNextDevice(device);
1241
1242 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1243
Harry Cuttsa5b71292022-11-28 12:56:17 +00001244 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001245}
1246
Chris Ye3fdbfef2021-01-06 18:45:18 -08001247TEST_F(InputReaderTest, LightGetColor) {
1248 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001249 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001250 constexpr int32_t eventHubId = 1;
1251 const char* DEVICE_LOCATION = "BLUETOOTH";
1252 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001253 FakePeripheralController& controller =
1254 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001255 mReader->pushNextDevice(device);
1256 RawLightInfo info = {.id = 1,
1257 .name = "Mono",
1258 .maxBrightness = 255,
1259 .flags = InputLightClass::BRIGHTNESS,
1260 .path = ""};
1261 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
1262 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
1263
1264 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001265
Chris Yee2b1e5c2021-03-10 22:45:12 -08001266 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
1267 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001268 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
1269 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
1270}
1271
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001272// --- InputReaderIntegrationTest ---
1273
1274// These tests create and interact with the InputReader only through its interface.
1275// The InputReader is started during SetUp(), which starts its processing in its own
1276// thread. The tests use linux uinput to emulate input devices.
1277// NOTE: Interacting with the physical device while these tests are running may cause
1278// the tests to fail.
1279class InputReaderIntegrationTest : public testing::Test {
1280protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001281 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001282 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001283 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001284
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001285 std::shared_ptr<FakePointerController> mFakePointerController;
1286
Chris Yea52ade12020-08-27 16:49:20 -07001287 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001288#if !defined(__ANDROID__)
1289 GTEST_SKIP();
1290#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001291 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001292 mFakePointerController = std::make_shared<FakePointerController>();
1293 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001294 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
1295 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001296
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001297 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1298 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001299 ASSERT_EQ(mReader->start(), OK);
1300
1301 // Since this test is run on a real device, all the input devices connected
1302 // to the test device will show up in mReader. We wait for those input devices to
1303 // show up before beginning the tests.
1304 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1305 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1306 }
1307
Chris Yea52ade12020-08-27 16:49:20 -07001308 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001309#if !defined(__ANDROID__)
1310 return;
1311#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001312 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001313 mReader.reset();
1314 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001315 mFakePolicy.clear();
1316 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001317
1318 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1319 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1320 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1321 [&name](const InputDeviceInfo& info) {
1322 return info.getIdentifier().name == name;
1323 });
1324 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1325 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001326};
1327
1328TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1329 // An invalid input device that is only used for this test.
1330 class InvalidUinputDevice : public UinputDevice {
1331 public:
Prabir Pradhanb7d434e2022-10-14 22:41:38 +00001332 InvalidUinputDevice() : UinputDevice("Invalid Device", 99 /*productId*/) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001333
1334 private:
1335 void configureDevice(int fd, uinput_user_dev* device) override {}
1336 };
1337
1338 const size_t numDevices = mFakePolicy->getInputDevices().size();
1339
1340 // UinputDevice does not set any event or key bits, so InputReader should not
1341 // consider it as a valid device.
1342 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1343 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1344 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1345 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1346
1347 invalidDevice.reset();
1348 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1349 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1350 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1351}
1352
1353TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1354 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1355
1356 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1357 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1358 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1359 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1360
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001361 const auto device = findDeviceByName(keyboard->getName());
1362 ASSERT_TRUE(device.has_value());
1363 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1364 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1365 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001366
1367 keyboard.reset();
1368 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1369 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1370 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1371}
1372
1373TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1374 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1375 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1376
1377 NotifyConfigurationChangedArgs configChangedArgs;
1378 ASSERT_NO_FATAL_FAILURE(
1379 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001380 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001381 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1382
1383 NotifyKeyArgs keyArgs;
1384 keyboard->pressAndReleaseHomeKey();
1385 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1386 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001387 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001388 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001389 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001390 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001391 prevTimestamp = keyArgs.eventTime;
1392
1393 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1394 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001395 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001396 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001397 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001398}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001399
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001400TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1401 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1402 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1403
1404 const auto device = findDeviceByName(stylus->getName());
1405 ASSERT_TRUE(device.has_value());
1406
Prabir Pradhana3621852022-10-14 18:57:23 +00001407 // An external stylus with buttons should also be recognized as a keyboard.
1408 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001409 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1410 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1411
1412 const auto DOWN =
1413 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1414 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1415
1416 stylus->pressAndReleaseKey(BTN_STYLUS);
1417 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1418 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1419 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1420 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1421
1422 stylus->pressAndReleaseKey(BTN_STYLUS2);
1423 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1424 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1425 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1426 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1427
1428 stylus->pressAndReleaseKey(BTN_STYLUS3);
1429 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1430 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1431 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1432 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1433}
1434
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001435/**
1436 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1437 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1438 * are passed to the listener.
1439 */
1440static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1441TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1442 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1443 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1444 NotifyKeyArgs keyArgs;
1445
1446 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1447 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1448 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1449 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1450
1451 controller->pressAndReleaseKey(BTN_GEAR_UP);
1452 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1453 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1454 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1455}
1456
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001457// --- TouchIntegrationTest ---
1458
Arthur Hungaab25622020-01-16 11:22:11 +08001459class TouchIntegrationTest : public InputReaderIntegrationTest {
1460protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001461 const std::string UNIQUE_ID = "local:0";
1462
Chris Yea52ade12020-08-27 16:49:20 -07001463 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001464#if !defined(__ANDROID__)
1465 GTEST_SKIP();
1466#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001467 InputReaderIntegrationTest::SetUp();
1468 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001469 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1470 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001471
1472 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1473 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1474 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001475 const auto info = findDeviceByName(mDevice->getName());
1476 ASSERT_TRUE(info);
1477 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001478 }
1479
1480 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001481 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001482 std::optional<uint8_t> physicalPort,
1483 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001484 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
1485 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08001486 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1487 }
1488
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001489 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1490 NotifyMotionArgs args;
1491 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1492 EXPECT_EQ(action, args.action);
1493 ASSERT_EQ(points.size(), args.pointerCount);
1494 for (size_t i = 0; i < args.pointerCount; i++) {
1495 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1496 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1497 }
1498 }
1499
Arthur Hungaab25622020-01-16 11:22:11 +08001500 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001501 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001502};
1503
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001504TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
1505 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1506 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1507 // presses).
1508 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1509 mDeviceInfo.getSources());
1510}
1511
Arthur Hungaab25622020-01-16 11:22:11 +08001512TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1513 NotifyMotionArgs args;
1514 const Point centerPoint = mDevice->getCenterPoint();
1515
1516 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001517 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001518 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001519 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001520 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1521 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1522
1523 // ACTION_MOVE
1524 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001525 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001526 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1528
1529 // ACTION_UP
1530 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001531 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001532 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1533 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1534}
1535
1536TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1537 NotifyMotionArgs args;
1538 const Point centerPoint = mDevice->getCenterPoint();
1539
1540 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001541 mDevice->sendSlot(FIRST_SLOT);
1542 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001543 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001544 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001545 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1546 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1547
1548 // ACTION_POINTER_DOWN (Second slot)
1549 const Point secondPoint = centerPoint + Point(100, 100);
1550 mDevice->sendSlot(SECOND_SLOT);
1551 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001552 mDevice->sendDown(secondPoint);
1553 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001554 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001555 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001556
1557 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001558 mDevice->sendMove(secondPoint + Point(1, 1));
1559 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001560 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1561 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1562
1563 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001564 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001565 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001566 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001567 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001568
1569 // ACTION_UP
1570 mDevice->sendSlot(FIRST_SLOT);
1571 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001572 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001573 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1574 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1575}
1576
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001577/**
1578 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1579 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1580 * data?
1581 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1582 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1583 * for Pointer 0 only is generated after.
1584 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1585 * events, we will not miss any information.
1586 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1587 * event generated afterwards that contains the newest movement of pointer 0.
1588 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1589 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1590 * losing information about non-palm pointers.
1591 */
1592TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1593 NotifyMotionArgs args;
1594 const Point centerPoint = mDevice->getCenterPoint();
1595
1596 // ACTION_DOWN
1597 mDevice->sendSlot(FIRST_SLOT);
1598 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1599 mDevice->sendDown(centerPoint);
1600 mDevice->sendSync();
1601 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1602
1603 // ACTION_POINTER_DOWN (Second slot)
1604 const Point secondPoint = centerPoint + Point(100, 100);
1605 mDevice->sendSlot(SECOND_SLOT);
1606 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1607 mDevice->sendDown(secondPoint);
1608 mDevice->sendSync();
1609 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1610
1611 // ACTION_MOVE (First slot)
1612 mDevice->sendSlot(FIRST_SLOT);
1613 mDevice->sendMove(centerPoint + Point(5, 5));
1614 // ACTION_POINTER_UP (Second slot)
1615 mDevice->sendSlot(SECOND_SLOT);
1616 mDevice->sendPointerUp();
1617 // Send a single sync for the above 2 pointer updates
1618 mDevice->sendSync();
1619
1620 // First, we should get POINTER_UP for the second pointer
1621 assertReceivedMotion(ACTION_POINTER_1_UP,
1622 {/*first pointer */ centerPoint + Point(5, 5),
1623 /*second pointer*/ secondPoint});
1624
1625 // Next, the MOVE event for the first pointer
1626 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1627}
1628
1629/**
1630 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1631 * move, and then it will go up, all in the same frame.
1632 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1633 * gets sent to the listener.
1634 */
1635TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1636 NotifyMotionArgs args;
1637 const Point centerPoint = mDevice->getCenterPoint();
1638
1639 // ACTION_DOWN
1640 mDevice->sendSlot(FIRST_SLOT);
1641 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1642 mDevice->sendDown(centerPoint);
1643 mDevice->sendSync();
1644 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1645
1646 // ACTION_POINTER_DOWN (Second slot)
1647 const Point secondPoint = centerPoint + Point(100, 100);
1648 mDevice->sendSlot(SECOND_SLOT);
1649 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1650 mDevice->sendDown(secondPoint);
1651 mDevice->sendSync();
1652 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1653
1654 // ACTION_MOVE (First slot)
1655 mDevice->sendSlot(FIRST_SLOT);
1656 mDevice->sendMove(centerPoint + Point(5, 5));
1657 // ACTION_POINTER_UP (Second slot)
1658 mDevice->sendSlot(SECOND_SLOT);
1659 mDevice->sendMove(secondPoint + Point(6, 6));
1660 mDevice->sendPointerUp();
1661 // Send a single sync for the above 2 pointer updates
1662 mDevice->sendSync();
1663
1664 // First, we should get POINTER_UP for the second pointer
1665 // The movement of the second pointer during the liftoff frame is ignored.
1666 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1667 assertReceivedMotion(ACTION_POINTER_1_UP,
1668 {/*first pointer */ centerPoint + Point(5, 5),
1669 /*second pointer*/ secondPoint});
1670
1671 // Next, the MOVE event for the first pointer
1672 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1673}
1674
Arthur Hungaab25622020-01-16 11:22:11 +08001675TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1676 NotifyMotionArgs args;
1677 const Point centerPoint = mDevice->getCenterPoint();
1678
1679 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001680 mDevice->sendSlot(FIRST_SLOT);
1681 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001682 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001683 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001684 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1685 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1686
arthurhungcc7f9802020-04-30 17:55:40 +08001687 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001688 const Point secondPoint = centerPoint + Point(100, 100);
1689 mDevice->sendSlot(SECOND_SLOT);
1690 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1691 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001692 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001693 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001694 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001695
arthurhungcc7f9802020-04-30 17:55:40 +08001696 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001697 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001698 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001699 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1700 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1701
arthurhungcc7f9802020-04-30 17:55:40 +08001702 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1703 // a palm event.
1704 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001705 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001706 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001707 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001708 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001709 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001710
arthurhungcc7f9802020-04-30 17:55:40 +08001711 // Send up to second slot, expect first slot send moving.
1712 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001713 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001714 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1715 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001716
arthurhungcc7f9802020-04-30 17:55:40 +08001717 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001718 mDevice->sendSlot(FIRST_SLOT);
1719 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001720 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001721
arthurhungcc7f9802020-04-30 17:55:40 +08001722 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1723 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001724}
1725
Prabir Pradhanda20b172022-09-26 17:01:18 +00001726TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
1727 const Point centerPoint = mDevice->getCenterPoint();
1728
1729 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1730 mDevice->sendSlot(FIRST_SLOT);
1731 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1732 mDevice->sendToolType(MT_TOOL_PEN);
1733 mDevice->sendDown(centerPoint);
1734 mDevice->sendSync();
1735 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1736 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1737 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
1738
1739 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1740
1741 // Release the stylus touch.
1742 mDevice->sendUp();
1743 mDevice->sendSync();
1744 ASSERT_NO_FATAL_FAILURE(
1745 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1746
1747 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1748
1749 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1750 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1751 mDevice->sendToolType(MT_TOOL_FINGER);
1752 mDevice->sendDown(centerPoint);
1753 mDevice->sendSync();
1754 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1755 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1756 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
1757
1758 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1759
1760 mDevice->sendUp();
1761 mDevice->sendSync();
1762 ASSERT_NO_FATAL_FAILURE(
1763 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1764
1765 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1766 // The policy should be notified of the stylus presence.
1767 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1768 mDevice->sendToolType(MT_TOOL_PEN);
1769 mDevice->sendMove(centerPoint);
1770 mDevice->sendSync();
1771 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1772 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1773 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
1774
1775 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1776}
1777
Prabir Pradhan124ea442022-10-28 20:27:44 +00001778// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001779
Prabir Pradhan124ea442022-10-28 20:27:44 +00001780// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1781// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1782// stylus.
1783template <typename UinputStylusDevice>
1784class StylusButtonIntegrationTest : public TouchIntegrationTest {
1785protected:
1786 void SetUp() override {
1787#if !defined(__ANDROID__)
1788 GTEST_SKIP();
1789#endif
1790 TouchIntegrationTest::SetUp();
1791 mTouchscreen = mDevice.get();
1792 mTouchscreenInfo = mDeviceInfo;
1793
1794 setUpStylusDevice();
1795 }
1796
1797 UinputStylusDevice* mStylus{nullptr};
1798 InputDeviceInfo mStylusInfo{};
1799
1800 UinputTouchScreen* mTouchscreen{nullptr};
1801 InputDeviceInfo mTouchscreenInfo{};
1802
1803private:
1804 // When we are attempting to test stylus button events that are sent from the touchscreen,
1805 // use the same Uinput device for the touchscreen and the stylus.
1806 template <typename T = UinputStylusDevice>
1807 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1808 mStylus = mDevice.get();
1809 mStylusInfo = mDeviceInfo;
1810 }
1811
1812 // When we are attempting to stylus buttons from an external stylus being merged with touches
1813 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1814 template <typename T = UinputStylusDevice>
1815 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1816 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1817 mStylus = mStylusDeviceLifecycleTracker.get();
1818 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1819 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1820 const auto info = findDeviceByName(mStylus->getName());
1821 ASSERT_TRUE(info);
1822 mStylusInfo = *info;
1823 }
1824
1825 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1826
1827 // Hide the base class's device to expose it with a different name for readability.
1828 using TouchIntegrationTest::mDevice;
1829 using TouchIntegrationTest::mDeviceInfo;
1830};
1831
1832using StylusButtonIntegrationTestTypes =
1833 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1834TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1835
1836TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
1837 const auto stylusId = TestFixture::mStylusInfo.getId();
1838
1839 TestFixture::mStylus->pressKey(BTN_STYLUS);
1840 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1841 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1842 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1843
1844 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1845 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001846 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001847 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001848}
1849
Prabir Pradhan124ea442022-10-28 20:27:44 +00001850TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
1851 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1852 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1853 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001854
1855 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001856 TestFixture::mStylus->pressKey(BTN_STYLUS);
1857 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001858 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001859 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001860
1861 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001862 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1863 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1864 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1865 TestFixture::mTouchscreen->sendDown(centerPoint);
1866 TestFixture::mTouchscreen->sendSync();
1867 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001868 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1869 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001870 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1871 WithDeviceId(touchscreenId))));
1872 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001873 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1874 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001875 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1876 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001877
Prabir Pradhan124ea442022-10-28 20:27:44 +00001878 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1879 TestFixture::mTouchscreen->sendSync();
1880 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001881 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001882 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1883 WithDeviceId(touchscreenId))));
1884 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001885 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001886 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1887 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001888
1889 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001890 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1891 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001892 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001893 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001894}
1895
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001896TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
1897 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1898 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1899 const auto stylusId = TestFixture::mStylusInfo.getId();
1900 auto toolTypeDevice =
1901 AllOf(WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithDeviceId(touchscreenId));
1902
1903 // Press the stylus button.
1904 TestFixture::mStylus->pressKey(BTN_STYLUS);
1905 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1906 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1907 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1908
1909 // Start hovering with the stylus.
1910 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1911 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1912 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1913 TestFixture::mTouchscreen->sendMove(centerPoint);
1914 TestFixture::mTouchscreen->sendSync();
1915 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1916 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1917 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1918 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1919 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1920 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1921 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1922 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1923 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1924
1925 // Touch down with the stylus.
1926 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1927 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1928 TestFixture::mTouchscreen->sendDown(centerPoint);
1929 TestFixture::mTouchscreen->sendSync();
1930 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1931 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1932 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1933
1934 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1935 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1936 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1937
1938 // Stop touching with the stylus, and start hovering.
1939 TestFixture::mTouchscreen->sendUp();
1940 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1941 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1942 TestFixture::mTouchscreen->sendMove(centerPoint);
1943 TestFixture::mTouchscreen->sendSync();
1944 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1945 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
1946 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1947 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1948 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1949 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1950 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1951 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1952 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1953
1954 // Stop hovering.
1955 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1956 TestFixture::mTouchscreen->sendSync();
1957 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1958 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1959 WithButtonState(0))));
1960 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
1961 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1962 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1963 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1964
1965 // Release the stylus button.
1966 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1967 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1968 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
1969 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1970}
1971
Prabir Pradhan124ea442022-10-28 20:27:44 +00001972TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
1973 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1974 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1975 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001976
1977 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001978 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1979 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1980 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1981 TestFixture::mTouchscreen->sendDown(centerPoint);
1982 TestFixture::mTouchscreen->sendSync();
1983 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001984 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001985 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1986 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001987
1988 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001989 TestFixture::mStylus->pressKey(BTN_STYLUS);
1990 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001991 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001992 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1993 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001994 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1995 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001996 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1997 WithDeviceId(touchscreenId))));
1998 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001999 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2000 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002001 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2002 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002003
Prabir Pradhan124ea442022-10-28 20:27:44 +00002004 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2005 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002006 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002007 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2008 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002009 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002010 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2011 WithDeviceId(touchscreenId))));
2012 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002013 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002014 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2015 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002016
2017 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002018 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2019 TestFixture::mTouchscreen->sendSync();
2020 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002021 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002022 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2023 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002024}
2025
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002026TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
2027 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2028 TestFixture::mReader->requestRefreshConfiguration(
2029 InputReaderConfiguration::CHANGE_STYLUS_BUTTON_REPORTING);
2030
2031 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2032 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2033 const auto stylusId = TestFixture::mStylusInfo.getId();
2034
2035 // Start a stylus gesture. By the time this event is processed, the configuration change that
2036 // was requested is guaranteed to be completed.
2037 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
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(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2044 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2045 WithDeviceId(touchscreenId))));
2046
2047 // Press and release a stylus button. Each change only generates a MOVE motion event.
2048 // Key events are unaffected.
2049 TestFixture::mStylus->pressKey(BTN_STYLUS);
2050 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2051 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2052 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2053 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2054 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2055 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2056 WithDeviceId(touchscreenId))));
2057
2058 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2059 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2060 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2061 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2062 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2063 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2064 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2065 WithDeviceId(touchscreenId))));
2066
2067 // Finish the stylus gesture.
2068 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2069 TestFixture::mTouchscreen->sendSync();
2070 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2071 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2072 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2073 WithDeviceId(touchscreenId))));
2074}
2075
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002076// --- ExternalStylusIntegrationTest ---
2077
2078// Verify the behavior of an external stylus. An external stylus can report pressure or button
2079// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2080// ongoing stylus gesture that is being emitted by the touchscreen.
2081using ExternalStylusIntegrationTest = TouchIntegrationTest;
2082
2083TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
2084 const Point centerPoint = mDevice->getCenterPoint();
2085
2086 // Create an external stylus capable of reporting pressure data that
2087 // should be fused with a touch pointer.
2088 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2089 createUinputDevice<UinputExternalStylusWithPressure>();
2090 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2091 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2092 const auto stylusInfo = findDeviceByName(stylus->getName());
2093 ASSERT_TRUE(stylusInfo);
2094
2095 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2096
2097 const auto touchscreenId = mDeviceInfo.getId();
2098
2099 // Set a pressure value on the stylus. It doesn't generate any events.
2100 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2101 stylus->setPressure(100);
2102 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2103
2104 // Start a finger gesture, and ensure it shows up as stylus gesture
2105 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002106 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002107 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002108 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002109 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002110 mDevice->sendSync();
2111 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2112 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002113 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2114 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002115
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002116 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2117 // event with the updated pressure.
2118 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002119 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2120 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002121 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2122 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002123
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002124 // The external stylus did not generate any events.
2125 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2126 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2127}
2128
2129TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
2130 const Point centerPoint = mDevice->getCenterPoint();
2131
2132 // Create an external stylus capable of reporting pressure data that
2133 // should be fused with a touch pointer.
2134 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2135 createUinputDevice<UinputExternalStylusWithPressure>();
2136 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2137 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2138 const auto stylusInfo = findDeviceByName(stylus->getName());
2139 ASSERT_TRUE(stylusInfo);
2140
2141 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2142
2143 const auto touchscreenId = mDeviceInfo.getId();
2144
2145 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2146 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002147 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2148 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002149 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002150 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002151
2152 // Start a finger gesture. The touch device will withhold generating any touches for
2153 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2154 mDevice->sendSlot(FIRST_SLOT);
2155 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2156 mDevice->sendToolType(MT_TOOL_FINGER);
2157 mDevice->sendDown(centerPoint);
2158 auto waitUntil = std::chrono::system_clock::now() +
2159 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002160 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002161 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002162
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002163 // Since the external stylus did not report a pressure value within the timeout,
2164 // it shows up as a finger pointer.
2165 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2166 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2167 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithDeviceId(touchscreenId),
2168 WithPressure(1.f))));
2169
2170 // Change the pressure on the external stylus. Since the pressure was not present at the start
2171 // of the gesture, it is ignored for now.
2172 stylus->setPressure(200);
2173 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2174
2175 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002176 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2177 mDevice->sendSync();
2178 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2179 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002180 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2181
2182 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2183 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2184 mDevice->sendToolType(MT_TOOL_FINGER);
2185 mDevice->sendDown(centerPoint);
2186 mDevice->sendSync();
2187 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2188 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2189 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2190 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2191
2192 // The external stylus did not generate any events.
2193 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2194 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002195}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002196
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002197TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
2198 const Point centerPoint = mDevice->getCenterPoint();
2199
2200 // Create an external stylus device that does not support pressure. It should not affect any
2201 // touch pointers.
2202 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2203 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2204 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2205 const auto stylusInfo = findDeviceByName(stylus->getName());
2206 ASSERT_TRUE(stylusInfo);
2207
2208 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2209
2210 const auto touchscreenId = mDeviceInfo.getId();
2211
2212 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2213 // pressure data from the external stylus.
2214 mDevice->sendSlot(FIRST_SLOT);
2215 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2216 mDevice->sendToolType(MT_TOOL_FINGER);
2217 mDevice->sendDown(centerPoint);
2218 auto waitUntil = std::chrono::system_clock::now() +
2219 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2220 mDevice->sendSync();
2221 ASSERT_NO_FATAL_FAILURE(
2222 mTestListener
2223 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2224 WithToolType(
2225 AMOTION_EVENT_TOOL_TYPE_FINGER),
2226 WithButtonState(0),
2227 WithDeviceId(touchscreenId),
2228 WithPressure(1.f)),
2229 waitUntil));
2230
2231 // The external stylus did not generate any events.
2232 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2233 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2234}
2235
Michael Wrightd02c5b62014-02-10 15:10:22 -08002236// --- InputDeviceTest ---
2237class InputDeviceTest : public testing::Test {
2238protected:
2239 static const char* DEVICE_NAME;
2240 static const char* DEVICE_LOCATION;
2241 static const int32_t DEVICE_ID;
2242 static const int32_t DEVICE_GENERATION;
2243 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002244 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002245 static const int32_t EVENTHUB_ID;
2246 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2247
2248 std::shared_ptr<FakeEventHub> mFakeEventHub;
2249 sp<FakeInputReaderPolicy> mFakePolicy;
2250 std::unique_ptr<TestInputListener> mFakeListener;
2251 std::unique_ptr<InstrumentedInputReader> mReader;
2252 std::shared_ptr<InputDevice> mDevice;
2253
2254 void SetUp() override {
2255 mFakeEventHub = std::make_unique<FakeEventHub>();
2256 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2257 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002258 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002259 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002260 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002261 identifier.name = DEVICE_NAME;
2262 identifier.location = DEVICE_LOCATION;
2263 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2264 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2265 identifier);
2266 mReader->pushNextDevice(mDevice);
2267 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002268 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002269 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002270
2271 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002272 mFakeListener.reset();
2273 mFakePolicy.clear();
2274 }
2275};
2276
2277const char* InputDeviceTest::DEVICE_NAME = "device";
2278const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2279const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2280const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002281const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002282const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2283 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002284const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002285const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2286
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002287TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002288 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002289 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2290 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002291}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002292
Michael Wrightd02c5b62014-02-10 15:10:22 -08002293TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002294 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2295
Michael Wrightd02c5b62014-02-10 15:10:22 -08002296 // Configuration
2297 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002298 InputReaderConfiguration config;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002299 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2300
Michael Wrightd02c5b62014-02-10 15:10:22 -08002301 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2302}
2303
2304TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2305 ASSERT_EQ(mDevice->isEnabled(), false);
2306}
2307
2308TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2309 // Configuration.
2310 InputReaderConfiguration config;
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002311 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002312
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002313 // Reset.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002314 unused += mDevice->reset(ARBITRARY_TIME);
2315
2316 NotifyDeviceResetArgs resetArgs;
2317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2318 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2319 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2320
2321 // Metadata.
2322 ASSERT_TRUE(mDevice->isIgnored());
2323 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2324
2325 InputDeviceInfo info = mDevice->getDeviceInfo();
2326 ASSERT_EQ(DEVICE_ID, info.getId());
2327 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2328 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2329 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2330
2331 // State queries.
2332 ASSERT_EQ(0, mDevice->getMetaState());
2333
2334 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002335 << "Ignored device should return unknown key code state.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002336 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002337 << "Ignored device should return unknown scan code state.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002338 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2339 << "Ignored device should return unknown switch state.";
2340
2341 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2342 uint8_t flags[2] = { 0, 1 };
2343 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002344 << "Ignored device should never mark any key codes.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002345 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2346 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2347}
2348
2349TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2350 // Configuration.
2351 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2352
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002353 FakeInputMapper& mapper1 =
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002354 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002355 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002356 mapper1.setMetaState(AMETA_ALT_ON);
2357 mapper1.addSupportedKeyCode(AKEYCODE_A);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002358 mapper1.addSupportedKeyCode(AKEYCODE_B);
2359 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002360 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2361 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2362 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2363 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002364
Arthur Hung2c9a3342019-07-23 14:18:59 +08002365 FakeInputMapper& mapper2 =
2366 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002367 mapper2.setMetaState(AMETA_SHIFT_ON);
2368
Arthur Hung2c9a3342019-07-23 14:18:59 +08002369 InputReaderConfiguration config;
2370 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2371
2372 std::string propertyValue;
2373 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
2374 << "Device should have read configuration during configuration phase.";
2375 ASSERT_EQ("value", propertyValue);
2376
2377 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002378 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
2379
Arthur Hung2c9a3342019-07-23 14:18:59 +08002380 // Reset
2381 unused += mDevice->reset(ARBITRARY_TIME);
2382 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2383 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
2384
2385 NotifyDeviceResetArgs resetArgs;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2387 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002388 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2389
Arthur Hung2c9a3342019-07-23 14:18:59 +08002390 // Metadata.
2391 ASSERT_FALSE(mDevice->isIgnored());
2392 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2393
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002394 InputDeviceInfo info = mDevice->getDeviceInfo();
2395 ASSERT_EQ(DEVICE_ID, info.getId());
Arthur Hung2c9a3342019-07-23 14:18:59 +08002396 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2397 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2398 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002399
2400 // State queries.
Arthur Hung2c9a3342019-07-23 14:18:59 +08002401 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2402 << "Should query mappers and combine meta states.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002403
Christine Franks1ba71cc2021-04-07 14:37:42 -07002404 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2405 << "Should return unknown key code state when source not supported.";
2406 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2407 << "Should return unknown scan code state when source not supported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002408 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2409 << "Should return unknown switch state when source not supported.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002410
2411 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2412 << "Should query mapper when source is supported.";
2413 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
Christine Franks2a2293c2022-01-18 11:51:16 -08002414 << "Should query mapper when source is supported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002415 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2416 << "Should query mapper when source is supported.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002417
2418 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
2419 uint8_t flags[4] = { 0, 0, 0, 1 };
2420 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
2421 << "Should do nothing when source is unsupported.";
2422 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002423 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2424 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002425 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2426
2427 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2428 << "Should query mapper when source is supported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002429 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2430 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002431 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2432 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2433
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002434 // Event handling.
2435 RawEvent event;
Christine Franks1ba71cc2021-04-07 14:37:42 -07002436 event.deviceId = EVENTHUB_ID;
2437 unused += mDevice->process(&event, 1);
2438
Christine Franks2a2293c2022-01-18 11:51:16 -08002439 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2440 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
2441}
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002442
2443// A single input device is associated with a specific display. Check that:
Christine Franks2a2293c2022-01-18 11:51:16 -08002444// 1. Device is disabled if the viewport corresponding to the associated display is not found
Christine Franks2a2293c2022-01-18 11:51:16 -08002445// 2. Device is disabled when setEnabled API is called
2446TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
2447 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
2448
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002449 // First Configuration.
2450 std::list<NotifyArgs> unused =
Christine Franks2a2293c2022-01-18 11:51:16 -08002451 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2452
2453 // Device should be enabled by default.
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002454 ASSERT_TRUE(mDevice->isEnabled());
2455
2456 // Prepare associated info.
2457 constexpr uint8_t hdmi = 1;
2458 const std::string UNIQUE_ID = "local:1";
2459
2460 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2461 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2462 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2463 // Device should be disabled because it is associated with a specific display via
2464 // input port <-> display port association, but the corresponding display is not found
2465 ASSERT_FALSE(mDevice->isEnabled());
2466
2467 // Prepare displays.
2468 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002469 ui::ROTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002470 ViewportType::INTERNAL);
2471 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2472 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2473 ASSERT_TRUE(mDevice->isEnabled());
2474
2475 // Device should be disabled after set disable.
2476 mFakePolicy->addDisabledDevice(mDevice->getId());
2477 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2478 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2479 ASSERT_FALSE(mDevice->isEnabled());
2480
2481 // Device should still be disabled even found the associated display.
2482 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2483 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2484 ASSERT_FALSE(mDevice->isEnabled());
2485}
2486
2487TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2488 // Device should be enabled by default.
2489 mFakePolicy->clearViewports();
2490 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2491 std::list<NotifyArgs> unused =
2492 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2493 ASSERT_TRUE(mDevice->isEnabled());
2494
2495 // Device should be disabled because it is associated with a specific display, but the
2496 // corresponding display is not found.
2497 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2498 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2499 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2500 ASSERT_FALSE(mDevice->isEnabled());
2501
2502 // Device should be enabled when a display is found.
2503 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002504 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002505 NO_PORT, ViewportType::INTERNAL);
2506 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2507 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2508 ASSERT_TRUE(mDevice->isEnabled());
2509
2510 // Device should be disabled after set disable.
2511 mFakePolicy->addDisabledDevice(mDevice->getId());
2512 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2513 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2514 ASSERT_FALSE(mDevice->isEnabled());
2515
2516 // Device should still be disabled even found the associated display.
2517 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2518 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2519 ASSERT_FALSE(mDevice->isEnabled());
2520}
2521
2522TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2523 mFakePolicy->clearViewports();
2524 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2525 std::list<NotifyArgs> unused =
2526 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002527
Michael Wrightd02c5b62014-02-10 15:10:22 -08002528 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2529 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002530 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Chris Ye1b0c7342020-07-28 21:57:03 -07002531 NO_PORT, ViewportType::INTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002532 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002533 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002534 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002535}
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002536
arthurhungdcef2dc2020-08-11 14:47:50 +08002537/**
2538 * This test reproduces a crash caused by a dangling reference that remains after device is added
Michael Wrightd02c5b62014-02-10 15:10:22 -08002539 * and removed. The reference is accessed in InputDevice::dump(..);
Chris Ye1b0c7342020-07-28 21:57:03 -07002540 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002541TEST_F(InputDeviceTest, DumpDoesNotCrash) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002542 constexpr int32_t TEST_EVENTHUB_ID = 10;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002543 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
arthurhungdcef2dc2020-08-11 14:47:50 +08002544
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002545 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002546 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002547 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2548 std::string dumpStr, eventHubDevStr;
2549 device.dump(dumpStr, eventHubDevStr);
2550}
2551
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002552TEST_F(InputDeviceTest, GetBluetoothAddress) {
2553 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2554 ASSERT_TRUE(address);
2555 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2556}
2557
Michael Wrightd02c5b62014-02-10 15:10:22 -08002558// --- SwitchInputMapperTest ---
2559
2560class SwitchInputMapperTest : public InputMapperTest {
2561protected:
2562};
2563
2564TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002565 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002566
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002567 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002568}
2569
2570TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002571 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002572
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002573 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002574 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002575
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002576 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002577 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002578}
2579
2580TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002581 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002582 std::list<NotifyArgs> out;
2583 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2584 ASSERT_TRUE(out.empty());
2585 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2586 ASSERT_TRUE(out.empty());
2587 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2588 ASSERT_TRUE(out.empty());
2589 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002590
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002591 ASSERT_EQ(1u, out.size());
2592 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002593 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002594 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2595 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002596 args.switchMask);
2597 ASSERT_EQ(uint32_t(0), args.policyFlags);
2598}
2599
Chris Ye87143712020-11-10 05:05:58 +00002600// --- VibratorInputMapperTest ---
2601class VibratorInputMapperTest : public InputMapperTest {
2602protected:
2603 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2604};
2605
2606TEST_F(VibratorInputMapperTest, GetSources) {
2607 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2608
2609 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2610}
2611
2612TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2613 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2614
2615 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2616}
2617
2618TEST_F(VibratorInputMapperTest, Vibrate) {
2619 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002620 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002621 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2622
2623 VibrationElement pattern(2);
2624 VibrationSequence sequence(2);
2625 pattern.duration = std::chrono::milliseconds(200);
2626 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2627 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2628 sequence.addElement(pattern);
2629 pattern.duration = std::chrono::milliseconds(500);
2630 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2631 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2632 sequence.addElement(pattern);
2633
2634 std::vector<int64_t> timings = {0, 1};
2635 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2636
2637 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002638 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002639 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002640 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002641 // Verify vibrator state listener was notified.
2642 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002643 ASSERT_EQ(1u, out.size());
2644 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2645 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2646 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002647 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002648 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002649 ASSERT_FALSE(mapper.isVibrating());
2650 // Verify vibrator state listener was notified.
2651 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002652 ASSERT_EQ(1u, out.size());
2653 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2654 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2655 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002656}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002657
Chris Yef59a2f42020-10-16 12:55:26 -07002658// --- SensorInputMapperTest ---
2659
2660class SensorInputMapperTest : public InputMapperTest {
2661protected:
2662 static const int32_t ACCEL_RAW_MIN;
2663 static const int32_t ACCEL_RAW_MAX;
2664 static const int32_t ACCEL_RAW_FUZZ;
2665 static const int32_t ACCEL_RAW_FLAT;
2666 static const int32_t ACCEL_RAW_RESOLUTION;
2667
2668 static const int32_t GYRO_RAW_MIN;
2669 static const int32_t GYRO_RAW_MAX;
2670 static const int32_t GYRO_RAW_FUZZ;
2671 static const int32_t GYRO_RAW_FLAT;
2672 static const int32_t GYRO_RAW_RESOLUTION;
2673
2674 static const float GRAVITY_MS2_UNIT;
2675 static const float DEGREE_RADIAN_UNIT;
2676
2677 void prepareAccelAxes();
2678 void prepareGyroAxes();
2679 void setAccelProperties();
2680 void setGyroProperties();
2681 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2682};
2683
2684const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2685const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2686const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2687const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2688const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2689
2690const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2691const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2692const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2693const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2694const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2695
2696const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2697const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2698
2699void SensorInputMapperTest::prepareAccelAxes() {
2700 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2701 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2702 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2703 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2704 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2705 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2706}
2707
2708void SensorInputMapperTest::prepareGyroAxes() {
2709 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2710 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2711 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2712 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2713 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2714 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2715}
2716
2717void SensorInputMapperTest::setAccelProperties() {
2718 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2719 /* sensorDataIndex */ 0);
2720 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2721 /* sensorDataIndex */ 1);
2722 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2723 /* sensorDataIndex */ 2);
2724 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2725 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2726 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2727 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2728 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2729}
2730
2731void SensorInputMapperTest::setGyroProperties() {
2732 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2733 /* sensorDataIndex */ 0);
2734 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2735 /* sensorDataIndex */ 1);
2736 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2737 /* sensorDataIndex */ 2);
2738 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2739 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2740 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2741 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2742 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2743}
2744
2745TEST_F(SensorInputMapperTest, GetSources) {
2746 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2747
2748 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2749}
2750
2751TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2752 setAccelProperties();
2753 prepareAccelAxes();
2754 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2755
2756 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2757 std::chrono::microseconds(10000),
2758 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002759 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002760 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2761 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2762 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2763 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2764 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002765
2766 NotifySensorArgs args;
2767 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2768 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2769 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2770
2771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2772 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2773 ASSERT_EQ(args.deviceId, DEVICE_ID);
2774 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2775 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2776 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2777 ASSERT_EQ(args.values, values);
2778 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2779}
2780
2781TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2782 setGyroProperties();
2783 prepareGyroAxes();
2784 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2785
2786 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2787 std::chrono::microseconds(10000),
2788 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002789 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002795
2796 NotifySensorArgs args;
2797 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2798 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2799 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2800
2801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2802 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2803 ASSERT_EQ(args.deviceId, DEVICE_ID);
2804 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2805 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2806 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2807 ASSERT_EQ(args.values, values);
2808 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2809}
2810
Michael Wrightd02c5b62014-02-10 15:10:22 -08002811// --- KeyboardInputMapperTest ---
2812
2813class KeyboardInputMapperTest : public InputMapperTest {
2814protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002815 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002816 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002817 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002818
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002819 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002820 int32_t originalKeyCode, int32_t rotatedKeyCode,
2821 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002822};
2823
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002824/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2825 * orientation.
2826 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002827void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002828 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2829 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002830}
2831
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002832void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002833 int32_t originalScanCode, int32_t originalKeyCode,
2834 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002835 NotifyKeyArgs args;
2836
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002837 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2839 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2840 ASSERT_EQ(originalScanCode, args.scanCode);
2841 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002842 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002843
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002844 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2846 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2847 ASSERT_EQ(originalScanCode, args.scanCode);
2848 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002849 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002850}
2851
Michael Wrightd02c5b62014-02-10 15:10:22 -08002852TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002853 KeyboardInputMapper& mapper =
2854 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2855 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002856
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002857 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002858}
2859
2860TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2861 const int32_t USAGE_A = 0x070004;
2862 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002863 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2864 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002865 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2866 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2867 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002868
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002869 KeyboardInputMapper& mapper =
2870 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2871 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002872 // Initial metastate is AMETA_NONE.
2873 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002874
2875 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002876 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002877 NotifyKeyArgs args;
2878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2879 ASSERT_EQ(DEVICE_ID, args.deviceId);
2880 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2881 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2882 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2883 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2884 ASSERT_EQ(KEY_HOME, args.scanCode);
2885 ASSERT_EQ(AMETA_NONE, args.metaState);
2886 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2887 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2888 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2889
2890 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002891 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2893 ASSERT_EQ(DEVICE_ID, args.deviceId);
2894 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2895 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2896 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2897 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2898 ASSERT_EQ(KEY_HOME, args.scanCode);
2899 ASSERT_EQ(AMETA_NONE, args.metaState);
2900 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2901 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2902 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2903
2904 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002905 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2906 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2908 ASSERT_EQ(DEVICE_ID, args.deviceId);
2909 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2910 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2911 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2912 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2913 ASSERT_EQ(0, args.scanCode);
2914 ASSERT_EQ(AMETA_NONE, args.metaState);
2915 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2916 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2917 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2918
2919 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002920 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2921 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2923 ASSERT_EQ(DEVICE_ID, args.deviceId);
2924 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2925 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2926 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2927 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2928 ASSERT_EQ(0, args.scanCode);
2929 ASSERT_EQ(AMETA_NONE, args.metaState);
2930 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2931 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2932 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2933
2934 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002935 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2936 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2938 ASSERT_EQ(DEVICE_ID, args.deviceId);
2939 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2940 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2941 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2942 ASSERT_EQ(0, args.keyCode);
2943 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2944 ASSERT_EQ(AMETA_NONE, args.metaState);
2945 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2946 ASSERT_EQ(0U, args.policyFlags);
2947 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2948
2949 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2951 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2953 ASSERT_EQ(DEVICE_ID, args.deviceId);
2954 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2955 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2956 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2957 ASSERT_EQ(0, args.keyCode);
2958 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2959 ASSERT_EQ(AMETA_NONE, args.metaState);
2960 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2961 ASSERT_EQ(0U, args.policyFlags);
2962 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2963}
2964
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00002965TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
2966 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
2967 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
2968 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
2969
2970 KeyboardInputMapper& mapper =
2971 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2972 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2973
2974 // Key down by scan code.
2975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
2976 NotifyKeyArgs args;
2977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2978 ASSERT_EQ(AKEYCODE_B, args.keyCode);
2979
2980 // Key up by scan code.
2981 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
2982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2983 ASSERT_EQ(AKEYCODE_B, args.keyCode);
2984}
2985
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002986/**
2987 * Ensure that the readTime is set to the time when the EV_KEY is received.
2988 */
2989TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
2990 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2991
2992 KeyboardInputMapper& mapper =
2993 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2994 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2995 NotifyKeyArgs args;
2996
2997 // Key down
2998 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
2999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3000 ASSERT_EQ(12, args.readTime);
3001
3002 // Key up
3003 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3005 ASSERT_EQ(15, args.readTime);
3006}
3007
Michael Wrightd02c5b62014-02-10 15:10:22 -08003008TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003009 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3010 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003011 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3012 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3013 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003014
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003015 KeyboardInputMapper& mapper =
3016 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3017 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003018
Arthur Hung95f68612022-04-07 14:08:22 +08003019 // Initial metastate is AMETA_NONE.
3020 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003021
3022 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003024 NotifyKeyArgs args;
3025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3026 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003027 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003028 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003029
3030 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003031 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3033 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003034 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003035
3036 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003037 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3039 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003040 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003041
3042 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003043 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3045 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003046 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003047 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003048}
3049
3050TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003051 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3052 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3053 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3054 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003055
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003056 KeyboardInputMapper& mapper =
3057 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3058 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059
Michael Wrighta9cf4192022-12-01 23:46:39 +00003060 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003061 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3062 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3063 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3064 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3065 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3066 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3067 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3068 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3069}
3070
3071TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003072 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3073 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3074 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3075 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003076
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003078 KeyboardInputMapper& mapper =
3079 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3080 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081
Michael Wrighta9cf4192022-12-01 23:46:39 +00003082 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003083 ASSERT_NO_FATAL_FAILURE(
3084 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3085 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3086 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3087 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3088 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3089 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3090 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003091
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003092 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003093 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003094 ASSERT_NO_FATAL_FAILURE(
3095 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3096 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3097 AKEYCODE_DPAD_UP, DISPLAY_ID));
3098 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3099 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3100 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3101 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003103 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003104 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003105 ASSERT_NO_FATAL_FAILURE(
3106 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3107 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3108 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3109 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3110 AKEYCODE_DPAD_UP, DISPLAY_ID));
3111 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3112 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003113
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003114 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003115 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003116 ASSERT_NO_FATAL_FAILURE(
3117 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3118 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3119 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3120 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3121 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3122 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3123 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003124
3125 // Special case: if orientation changes while key is down, we still emit the same keycode
3126 // in the key up as we did in the key down.
3127 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003128 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003129 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003130 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3132 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3133 ASSERT_EQ(KEY_UP, args.scanCode);
3134 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3135
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003136 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003137 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3140 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3141 ASSERT_EQ(KEY_UP, args.scanCode);
3142 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3143}
3144
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003145TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3146 // If the keyboard is not orientation aware,
3147 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003148 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003149
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003150 KeyboardInputMapper& mapper =
3151 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3152 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003153 NotifyKeyArgs args;
3154
3155 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3160 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3161
Michael Wrighta9cf4192022-12-01 23:46:39 +00003162 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3167 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3168}
3169
3170TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3171 // If the keyboard is orientation aware,
3172 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003173 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003174
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003175 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003176 KeyboardInputMapper& mapper =
3177 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3178 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003179 NotifyKeyArgs args;
3180
3181 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3182 // ^--- already checked by the previous test
3183
Michael Wrighta9cf4192022-12-01 23:46:39 +00003184 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003185 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3190 ASSERT_EQ(DISPLAY_ID, args.displayId);
3191
3192 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003193 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003194 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003195 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003196 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003198 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3200 ASSERT_EQ(newDisplayId, args.displayId);
3201}
3202
Michael Wrightd02c5b62014-02-10 15:10:22 -08003203TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003204 KeyboardInputMapper& mapper =
3205 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3206 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003207
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003208 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003209 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003210
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003211 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003212 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003213}
3214
Philip Junker4af3b3d2021-12-14 10:36:55 +01003215TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3216 KeyboardInputMapper& mapper =
3217 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3218 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3219
3220 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3221 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3222 << "If a mapping is available, the result is equal to the mapping";
3223
3224 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3225 << "If no mapping is available, the result is the key location";
3226}
3227
Michael Wrightd02c5b62014-02-10 15:10:22 -08003228TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003229 KeyboardInputMapper& mapper =
3230 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3231 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003232
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003233 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003234 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003235
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003236 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003237 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003238}
3239
3240TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003241 KeyboardInputMapper& mapper =
3242 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3243 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003245 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003246
Michael Wrightd02c5b62014-02-10 15:10:22 -08003247 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003248 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003249 ASSERT_TRUE(flags[0]);
3250 ASSERT_FALSE(flags[1]);
3251}
3252
3253TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003254 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3255 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3256 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3257 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3258 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3259 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003261 KeyboardInputMapper& mapper =
3262 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3263 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003264 // Initial metastate is AMETA_NONE.
3265 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266
3267 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003268 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3269 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3270 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271
3272 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003275 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3276 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3277 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003278 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003279
3280 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003283 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3284 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3285 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003286 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003287
3288 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003291 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3292 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3293 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003294 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003295
3296 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003299 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3300 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3301 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003302 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003303
3304 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003307 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3308 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3309 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003310 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003311
3312 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003315 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3316 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3317 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003318 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319}
3320
Chris Yea52ade12020-08-27 16:49:20 -07003321TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3322 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3323 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3324 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3325 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3326
3327 KeyboardInputMapper& mapper =
3328 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3329 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3330
Chris Yea52ade12020-08-27 16:49:20 -07003331 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003332 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003333 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3334 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3335 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3336 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3337
3338 NotifyKeyArgs args;
3339 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3342 ASSERT_EQ(AMETA_NONE, args.metaState);
3343 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3344 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3345 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3346
3347 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003348 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3350 ASSERT_EQ(AMETA_NONE, args.metaState);
3351 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3352 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3353 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3354}
3355
Arthur Hung2c9a3342019-07-23 14:18:59 +08003356TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3357 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003358 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3359 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3360 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3361 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003362
3363 // keyboard 2.
3364 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003365 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003366 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003367 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003368 std::shared_ptr<InputDevice> device2 =
3369 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003370 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003371
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003372 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3373 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3374 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3375 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003376
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003377 KeyboardInputMapper& mapper =
3378 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3379 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003380
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003381 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003382 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003383 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003384 std::list<NotifyArgs> unused =
3385 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3386 0 /*changes*/);
3387 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003388
3389 // Prepared displays and associated info.
3390 constexpr uint8_t hdmi1 = 0;
3391 constexpr uint8_t hdmi2 = 1;
3392 const std::string SECONDARY_UNIQUE_ID = "local:1";
3393
3394 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3395 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3396
3397 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003398 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3399 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003400 ASSERT_FALSE(device2->isEnabled());
3401
3402 // Prepare second display.
3403 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003404 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003405 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003406 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003407 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003408 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003409 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3410 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003411
3412 // Device should be enabled after the associated display is found.
3413 ASSERT_TRUE(mDevice->isEnabled());
3414 ASSERT_TRUE(device2->isEnabled());
3415
3416 // Test pad key events
3417 ASSERT_NO_FATAL_FAILURE(
3418 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3419 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3420 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3421 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3422 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3423 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3424 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3425
3426 ASSERT_NO_FATAL_FAILURE(
3427 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3428 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3429 AKEYCODE_DPAD_RIGHT, newDisplayId));
3430 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3431 AKEYCODE_DPAD_DOWN, newDisplayId));
3432 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3433 AKEYCODE_DPAD_LEFT, newDisplayId));
3434}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003435
arthurhungc903df12020-08-11 15:08:42 +08003436TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3437 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3438 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3439 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3440 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3441 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3442 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3443
3444 KeyboardInputMapper& mapper =
3445 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3446 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003447 // Initial metastate is AMETA_NONE.
3448 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003449
3450 // Initialization should have turned all of the lights off.
3451 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3452 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3453 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3454
3455 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003458 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3459 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3460
3461 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003462 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3463 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003464 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3465 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3466
3467 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3469 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003470 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3471 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3472
3473 mFakeEventHub->removeDevice(EVENTHUB_ID);
3474 mReader->loopOnce();
3475
3476 // keyboard 2 should default toggle keys.
3477 const std::string USB2 = "USB2";
3478 const std::string DEVICE_NAME2 = "KEYBOARD2";
3479 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3480 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3481 std::shared_ptr<InputDevice> device2 =
3482 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003483 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003484 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3485 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3486 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3487 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3488 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3489 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3490
arthurhung6fe95782020-10-05 22:41:16 +08003491 KeyboardInputMapper& mapper2 =
3492 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3493 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003494 std::list<NotifyArgs> unused =
3495 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3496 0 /*changes*/);
3497 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003498
3499 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3500 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3501 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003502 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3503 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003504}
3505
Arthur Hungcb40a002021-08-03 14:31:01 +00003506TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3507 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3508 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3509 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3510
3511 // Suppose we have two mappers. (DPAD + KEYBOARD)
3512 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3513 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3514 KeyboardInputMapper& mapper =
3515 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3516 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003517 // Initial metastate is AMETA_NONE.
3518 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003519
3520 mReader->toggleCapsLockState(DEVICE_ID);
3521 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3522}
3523
Arthur Hungfb3cc112022-04-13 07:39:50 +00003524TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3525 // keyboard 1.
3526 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3527 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3528 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3529 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3530 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3531 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3532
3533 KeyboardInputMapper& mapper1 =
3534 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3535 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3536
3537 // keyboard 2.
3538 const std::string USB2 = "USB2";
3539 const std::string DEVICE_NAME2 = "KEYBOARD2";
3540 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3541 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3542 std::shared_ptr<InputDevice> device2 =
3543 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3544 ftl::Flags<InputDeviceClass>(0));
3545 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3546 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3547 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3548 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3549 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3550 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3551
3552 KeyboardInputMapper& mapper2 =
3553 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3554 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003555 std::list<NotifyArgs> unused =
3556 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3557 0 /*changes*/);
3558 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003559
Arthur Hung95f68612022-04-07 14:08:22 +08003560 // Initial metastate is AMETA_NONE.
3561 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3562 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3563
3564 // Toggle num lock on and off.
3565 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3566 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003567 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3568 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3569 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3570
3571 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3572 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3573 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3574 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3575 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3576
3577 // Toggle caps lock on and off.
3578 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3579 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3580 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3581 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3582 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3583
3584 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3585 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3586 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3587 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3588 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3589
3590 // Toggle scroll lock on and off.
3591 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3592 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3593 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3594 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3595 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3596
3597 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3598 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3599 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3600 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3601 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3602}
3603
Arthur Hung2141d542022-08-23 07:45:21 +00003604TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3605 const int32_t USAGE_A = 0x070004;
3606 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3607 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3608
3609 KeyboardInputMapper& mapper =
3610 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3611 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3612 // Key down by scan code.
3613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3614 NotifyKeyArgs args;
3615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3616 ASSERT_EQ(DEVICE_ID, args.deviceId);
3617 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3618 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3619 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3620 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3621 ASSERT_EQ(KEY_HOME, args.scanCode);
3622 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3623
3624 // Disable device, it should synthesize cancellation events for down events.
3625 mFakePolicy->addDisabledDevice(DEVICE_ID);
3626 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
3627
3628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3629 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3630 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3631 ASSERT_EQ(KEY_HOME, args.scanCode);
3632 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3633}
3634
Zixuan Qufecb6062022-11-12 04:44:31 +00003635TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
3636 mDevice->addMapper<KeyboardInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3637 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3638 std::list<NotifyArgs> unused =
3639 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
3640
3641 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3642
3643 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3644 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUT_ASSOCIATION);
3645
3646 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3647 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3648 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3649 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3650 deviceInfo.getKeyboardLayoutInfo()->layoutType);
3651}
3652
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003653// --- KeyboardInputMapperTest_ExternalDevice ---
3654
3655class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3656protected:
Chris Yea52ade12020-08-27 16:49:20 -07003657 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003658};
3659
3660TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003661 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3662 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003663
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003664 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3665 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3666 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3667 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003668
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003669 KeyboardInputMapper& mapper =
3670 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3671 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003672
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003673 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003674 NotifyKeyArgs args;
3675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3676 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3677
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003678 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3680 ASSERT_EQ(uint32_t(0), args.policyFlags);
3681
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003682 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3684 ASSERT_EQ(uint32_t(0), args.policyFlags);
3685
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003686 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3688 ASSERT_EQ(uint32_t(0), args.policyFlags);
3689
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3692 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3693
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003694 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3696 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3697}
3698
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003699TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003700 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003701
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003702 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3703 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3704 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003705
Powei Fengd041c5d2019-05-03 17:11:33 -07003706 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003707 KeyboardInputMapper& mapper =
3708 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3709 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003710
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003712 NotifyKeyArgs args;
3713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3714 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3715
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003716 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3718 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3719
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003720 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3722 ASSERT_EQ(uint32_t(0), args.policyFlags);
3723
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003724 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3726 ASSERT_EQ(uint32_t(0), args.policyFlags);
3727
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003728 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3730 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3731
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003732 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3734 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3735}
3736
Michael Wrightd02c5b62014-02-10 15:10:22 -08003737// --- CursorInputMapperTest ---
3738
3739class CursorInputMapperTest : public InputMapperTest {
3740protected:
3741 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3742
Michael Wright17db18e2020-06-26 20:51:44 +01003743 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003744
Chris Yea52ade12020-08-27 16:49:20 -07003745 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003746 InputMapperTest::SetUp();
3747
Michael Wright17db18e2020-06-26 20:51:44 +01003748 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003749 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003750 }
3751
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003752 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3753 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003754
Michael Wrighta9cf4192022-12-01 23:46:39 +00003755 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003756 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3757 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3758 }
3759
3760 void prepareSecondaryDisplay() {
3761 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003762 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003763 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003764 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003765
3766 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3767 float pressure) {
3768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3769 0.0f, 0.0f, 0.0f, EPSILON));
3770 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003771};
3772
3773const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3774
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003775void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3776 int32_t originalY, int32_t rotatedX,
3777 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003778 NotifyMotionArgs args;
3779
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3784 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003785 ASSERT_NO_FATAL_FAILURE(
3786 assertCursorPointerCoords(args.pointerCoords[0],
3787 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3788 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003789}
3790
3791TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003792 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003793 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003794
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003795 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796}
3797
3798TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003799 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003800 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003801
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003802 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003803}
3804
3805TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003806 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003807 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003808
3809 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003810 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003811
3812 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003813 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3814 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003815 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3816 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3817
3818 // When the bounds are set, then there should be a valid motion range.
3819 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3820
3821 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003822 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003823
3824 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3825 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3826 1, 800 - 1, 0.0f, 0.0f));
3827 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3828 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3829 2, 480 - 1, 0.0f, 0.0f));
3830 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3831 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3832 0.0f, 1.0f, 0.0f, 0.0f));
3833}
3834
3835TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003836 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003837 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003838
3839 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003840 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003841
3842 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3843 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3844 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3845 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3846 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3847 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3848 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3849 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3850 0.0f, 1.0f, 0.0f, 0.0f));
3851}
3852
3853TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003854 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003855 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003856
arthurhungdcef2dc2020-08-11 14:47:50 +08003857 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858
3859 NotifyMotionArgs args;
3860
3861 // Button press.
3862 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003863 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3864 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3866 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3867 ASSERT_EQ(DEVICE_ID, args.deviceId);
3868 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3869 ASSERT_EQ(uint32_t(0), args.policyFlags);
3870 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3871 ASSERT_EQ(0, args.flags);
3872 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3873 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3874 ASSERT_EQ(0, args.edgeFlags);
3875 ASSERT_EQ(uint32_t(1), args.pointerCount);
3876 ASSERT_EQ(0, args.pointerProperties[0].id);
3877 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003878 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003879 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3880 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3881 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3882
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3884 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3885 ASSERT_EQ(DEVICE_ID, args.deviceId);
3886 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3887 ASSERT_EQ(uint32_t(0), args.policyFlags);
3888 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3889 ASSERT_EQ(0, args.flags);
3890 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3891 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3892 ASSERT_EQ(0, args.edgeFlags);
3893 ASSERT_EQ(uint32_t(1), args.pointerCount);
3894 ASSERT_EQ(0, args.pointerProperties[0].id);
3895 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003896 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003897 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3898 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3899 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3900
Michael Wrightd02c5b62014-02-10 15:10:22 -08003901 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003902 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3903 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3905 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3906 ASSERT_EQ(DEVICE_ID, args.deviceId);
3907 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3908 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003909 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3910 ASSERT_EQ(0, args.flags);
3911 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3912 ASSERT_EQ(0, args.buttonState);
3913 ASSERT_EQ(0, args.edgeFlags);
3914 ASSERT_EQ(uint32_t(1), args.pointerCount);
3915 ASSERT_EQ(0, args.pointerProperties[0].id);
3916 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003917 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003918 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3919 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3920 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3921
3922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3923 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3924 ASSERT_EQ(DEVICE_ID, args.deviceId);
3925 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3926 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003927 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3928 ASSERT_EQ(0, args.flags);
3929 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3930 ASSERT_EQ(0, args.buttonState);
3931 ASSERT_EQ(0, args.edgeFlags);
3932 ASSERT_EQ(uint32_t(1), args.pointerCount);
3933 ASSERT_EQ(0, args.pointerProperties[0].id);
3934 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003935 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003936 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3937 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3938 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3939}
3940
3941TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003943 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003944
3945 NotifyMotionArgs args;
3946
3947 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
3949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003952 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3953 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
3954 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955
3956 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003957 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
3958 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3960 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003961 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
3962 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003963}
3964
3965TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003966 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003967 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003968
3969 NotifyMotionArgs args;
3970
3971 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003972 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3975 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003976 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003977
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3979 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003980 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003981
Michael Wrightd02c5b62014-02-10 15:10:22 -08003982 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003983 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003986 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003987 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003988
3989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003990 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003991 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003992}
3993
3994TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003995 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003996 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003997
3998 NotifyMotionArgs args;
3999
4000 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4002 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4006 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004007 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4008 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4009 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004010
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4012 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004013 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4014 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4015 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004016
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4022 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004023 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4024 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4025 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004026
4027 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004028 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004031 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004032 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004033
4034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004035 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004036 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004037}
4038
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004039TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004040 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004041 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004042 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4043 // need to be rotated.
4044 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004045 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004046
Michael Wrighta9cf4192022-12-01 23:46:39 +00004047 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004048 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4049 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4050 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4051 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4052 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4053 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4054 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4055 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4056}
4057
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004058TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004059 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004060 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004061 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4062 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004063 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004064
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004065 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004066 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004067 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4068 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4069 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4070 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4071 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4072 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4073 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4074 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4075
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004076 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004077 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004078 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4079 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4080 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4081 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4082 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4083 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4084 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4085 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004086
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004087 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004088 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4090 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4091 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4092 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4093 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4094 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4095 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4096 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4097
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004098 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004099 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004100 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4101 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4102 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4103 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4104 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4105 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4106 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4107 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004108}
4109
4110TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004111 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004112 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004113
4114 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4115 mFakePointerController->setPosition(100, 200);
4116 mFakePointerController->setButtonState(0);
4117
4118 NotifyMotionArgs motionArgs;
4119 NotifyKeyArgs keyArgs;
4120
4121 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4125 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4126 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4127 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004128 ASSERT_NO_FATAL_FAILURE(
4129 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4132 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4133 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4134 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004135 ASSERT_NO_FATAL_FAILURE(
4136 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004137
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004141 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142 ASSERT_EQ(0, motionArgs.buttonState);
4143 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004144 ASSERT_NO_FATAL_FAILURE(
4145 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004146
4147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004148 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004149 ASSERT_EQ(0, motionArgs.buttonState);
4150 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004151 ASSERT_NO_FATAL_FAILURE(
4152 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004153
4154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004156 ASSERT_EQ(0, motionArgs.buttonState);
4157 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004158 ASSERT_NO_FATAL_FAILURE(
4159 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160
4161 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4166 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4167 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4168 motionArgs.buttonState);
4169 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4170 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004171 ASSERT_NO_FATAL_FAILURE(
4172 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004173
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4175 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4176 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4177 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4178 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004179 ASSERT_NO_FATAL_FAILURE(
4180 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004181
4182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4184 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4185 motionArgs.buttonState);
4186 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4187 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004188 ASSERT_NO_FATAL_FAILURE(
4189 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004190
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4192 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004194 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4196 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004197 ASSERT_NO_FATAL_FAILURE(
4198 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004199
4200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004202 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4203 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004204 ASSERT_NO_FATAL_FAILURE(
4205 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004210 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4211 ASSERT_EQ(0, motionArgs.buttonState);
4212 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004213 ASSERT_NO_FATAL_FAILURE(
4214 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004217
4218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219 ASSERT_EQ(0, motionArgs.buttonState);
4220 ASSERT_EQ(0, mFakePointerController->getButtonState());
4221 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004222 ASSERT_NO_FATAL_FAILURE(
4223 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004224
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4226 ASSERT_EQ(0, motionArgs.buttonState);
4227 ASSERT_EQ(0, mFakePointerController->getButtonState());
4228 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004229 ASSERT_NO_FATAL_FAILURE(
4230 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231
4232 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4236 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4237 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004238
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004240 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4242 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004243 ASSERT_NO_FATAL_FAILURE(
4244 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004245
4246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4247 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4248 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4249 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004250 ASSERT_NO_FATAL_FAILURE(
4251 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4254 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004256 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004257 ASSERT_EQ(0, motionArgs.buttonState);
4258 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004259 ASSERT_NO_FATAL_FAILURE(
4260 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004261
4262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004264 ASSERT_EQ(0, motionArgs.buttonState);
4265 ASSERT_EQ(0, mFakePointerController->getButtonState());
4266
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004267 ASSERT_NO_FATAL_FAILURE(
4268 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4270 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4271 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4272
4273 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4277 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4278 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004279
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004281 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4283 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004284 ASSERT_NO_FATAL_FAILURE(
4285 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004286
4287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4288 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4289 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4290 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004291 ASSERT_NO_FATAL_FAILURE(
4292 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004293
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004297 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004298 ASSERT_EQ(0, motionArgs.buttonState);
4299 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004300 ASSERT_NO_FATAL_FAILURE(
4301 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004302
4303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4304 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4305 ASSERT_EQ(0, motionArgs.buttonState);
4306 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004307 ASSERT_NO_FATAL_FAILURE(
4308 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004309
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4311 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4312 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4313
4314 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4318 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4319 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004320
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004322 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4324 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004325 ASSERT_NO_FATAL_FAILURE(
4326 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004327
4328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4330 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4331 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004332 ASSERT_NO_FATAL_FAILURE(
4333 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004338 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339 ASSERT_EQ(0, motionArgs.buttonState);
4340 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004341 ASSERT_NO_FATAL_FAILURE(
4342 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004343
4344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4345 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4346 ASSERT_EQ(0, motionArgs.buttonState);
4347 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004348 ASSERT_NO_FATAL_FAILURE(
4349 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004350
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4352 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4353 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4354
4355 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004356 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4359 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4360 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004361
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004363 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4365 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004366 ASSERT_NO_FATAL_FAILURE(
4367 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004368
4369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4370 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4371 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4372 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004373 ASSERT_NO_FATAL_FAILURE(
4374 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004379 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380 ASSERT_EQ(0, motionArgs.buttonState);
4381 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004382 ASSERT_NO_FATAL_FAILURE(
4383 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004384
4385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4386 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4387 ASSERT_EQ(0, motionArgs.buttonState);
4388 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004389 ASSERT_NO_FATAL_FAILURE(
4390 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004391
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4393 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4394 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4395}
4396
4397TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004399 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400
4401 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4402 mFakePointerController->setPosition(100, 200);
4403 mFakePointerController->setButtonState(0);
4404
4405 NotifyMotionArgs args;
4406
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004411 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4412 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4414 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004415 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004416}
4417
4418TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004419 addConfigurationProperty("cursor.mode", "pointer");
4420 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004421 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004422
4423 NotifyDeviceResetArgs resetArgs;
4424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4425 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4426 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4427
4428 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4429 mFakePointerController->setPosition(100, 200);
4430 mFakePointerController->setButtonState(0);
4431
4432 NotifyMotionArgs args;
4433
4434 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004435 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4436 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4437 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4439 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4442 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004443 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004444
4445 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4447 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4449 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4450 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4452 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4454 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4455 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4457 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4458
4459 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004460 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4461 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4463 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4464 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4466 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4468 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4469 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4471 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4472
4473 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4478 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4481 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004482 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004483
4484 // Disable pointer capture and check that the device generation got bumped
4485 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004486 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004487 mFakePolicy->setPointerCapture(false);
4488 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004489 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004490
4491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004492 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4493
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004494 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4498 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4501 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004502 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503}
4504
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004505/**
4506 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4507 * pointer acceleration or speed processing should not be applied.
4508 */
4509TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4510 addConfigurationProperty("cursor.mode", "pointer");
4511 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4512 100.f /*high threshold*/, 10.f /*acceleration*/);
4513 mFakePolicy->setVelocityControlParams(testParams);
4514 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4515
4516 NotifyDeviceResetArgs resetArgs;
4517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4518 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4519 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4520
4521 NotifyMotionArgs args;
4522
4523 // Move and verify scale is applied.
4524 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4525 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4528 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4529 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4530 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4531 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4532 ASSERT_GT(relX, 10);
4533 ASSERT_GT(relY, 20);
4534
4535 // Enable Pointer Capture
4536 mFakePolicy->setPointerCapture(true);
4537 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4538 NotifyPointerCaptureChangedArgs captureArgs;
4539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4540 ASSERT_TRUE(captureArgs.request.enable);
4541
4542 // Move and verify scale is not applied.
4543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4547 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4549 ASSERT_EQ(10, args.pointerCoords[0].getX());
4550 ASSERT_EQ(20, args.pointerCoords[0].getY());
4551}
4552
Prabir Pradhan208360b2022-06-24 18:37:04 +00004553TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4554 addConfigurationProperty("cursor.mode", "pointer");
4555 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4556
4557 NotifyDeviceResetArgs resetArgs;
4558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4559 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4560 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4561
4562 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004563 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004564
4565 NotifyMotionArgs args;
4566
4567 // Verify that the coordinates are rotated.
4568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4570 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4572 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4573 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4574 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4575 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4576
4577 // Enable Pointer Capture.
4578 mFakePolicy->setPointerCapture(true);
4579 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4580 NotifyPointerCaptureChangedArgs captureArgs;
4581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4582 ASSERT_TRUE(captureArgs.request.enable);
4583
4584 // Move and verify rotation is not applied.
4585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4586 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4589 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4591 ASSERT_EQ(10, args.pointerCoords[0].getX());
4592 ASSERT_EQ(20, args.pointerCoords[0].getY());
4593}
4594
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004595TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004596 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004597
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004598 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004599 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004600
4601 // Set up the secondary display as the display on which the pointer should be shown.
4602 // The InputDevice is not associated with any display.
4603 prepareSecondaryDisplay();
4604 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08004605 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4606
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004607 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004608 mFakePointerController->setPosition(100, 200);
4609 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004610
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004611 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004616 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4617 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4618 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004619 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004620}
4621
4622TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
4623 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4624
4625 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004626 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004627
4628 // Set up the secondary display as the display on which the pointer should be shown,
4629 // and associate the InputDevice with the secondary display.
4630 prepareSecondaryDisplay();
4631 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4632 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4633 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4634
4635 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4636 mFakePointerController->setPosition(100, 200);
4637 mFakePointerController->setButtonState(0);
4638
4639 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4640 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004643 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4644 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4645 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004646 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004647}
4648
4649TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
4650 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4651
4652 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004653 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004654 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4655
4656 // Associate the InputDevice with the secondary display.
4657 prepareSecondaryDisplay();
4658 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4659 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4660
4661 // The mapper should not generate any events because it is associated with a display that is
4662 // different from the pointer display.
4663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4665 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004667}
4668
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004669// --- BluetoothCursorInputMapperTest ---
4670
4671class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4672protected:
4673 void SetUp() override {
4674 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4675
4676 mFakePointerController = std::make_shared<FakePointerController>();
4677 mFakePolicy->setPointerController(mFakePointerController);
4678 }
4679};
4680
4681TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4682 addConfigurationProperty("cursor.mode", "pointer");
4683 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4684
4685 nsecs_t kernelEventTime = ARBITRARY_TIME;
4686 nsecs_t expectedEventTime = ARBITRARY_TIME;
4687 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4688 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4690 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4691 WithEventTime(expectedEventTime))));
4692
4693 // Process several events that come in quick succession, according to their timestamps.
4694 for (int i = 0; i < 3; i++) {
4695 constexpr static nsecs_t delta = ms2ns(1);
4696 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4697 kernelEventTime += delta;
4698 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4699
4700 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4701 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4703 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4704 WithEventTime(expectedEventTime))));
4705 }
4706}
4707
4708TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4709 addConfigurationProperty("cursor.mode", "pointer");
4710 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4711
4712 nsecs_t expectedEventTime = ARBITRARY_TIME;
4713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4716 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4717 WithEventTime(expectedEventTime))));
4718
4719 // Process several events with the same timestamp from the kernel.
4720 // Ensure that we do not generate events too far into the future.
4721 constexpr static int32_t numEvents =
4722 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4723 for (int i = 0; i < numEvents; i++) {
4724 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4725
4726 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4727 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4729 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4730 WithEventTime(expectedEventTime))));
4731 }
4732
4733 // By processing more events with the same timestamp, we should not generate events with a
4734 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4735 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4736 for (int i = 0; i < 3; i++) {
4737 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4740 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4741 WithEventTime(cappedEventTime))));
4742 }
4743}
4744
4745TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4746 addConfigurationProperty("cursor.mode", "pointer");
4747 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4748
4749 nsecs_t kernelEventTime = ARBITRARY_TIME;
4750 nsecs_t expectedEventTime = ARBITRARY_TIME;
4751 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4752 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4754 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4755 WithEventTime(expectedEventTime))));
4756
4757 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4758 // smoothening is not needed, its timestamp is not affected.
4759 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4760 expectedEventTime = kernelEventTime;
4761
4762 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4763 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4765 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4766 WithEventTime(expectedEventTime))));
4767}
4768
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769// --- TouchInputMapperTest ---
4770
4771class TouchInputMapperTest : public InputMapperTest {
4772protected:
4773 static const int32_t RAW_X_MIN;
4774 static const int32_t RAW_X_MAX;
4775 static const int32_t RAW_Y_MIN;
4776 static const int32_t RAW_Y_MAX;
4777 static const int32_t RAW_TOUCH_MIN;
4778 static const int32_t RAW_TOUCH_MAX;
4779 static const int32_t RAW_TOOL_MIN;
4780 static const int32_t RAW_TOOL_MAX;
4781 static const int32_t RAW_PRESSURE_MIN;
4782 static const int32_t RAW_PRESSURE_MAX;
4783 static const int32_t RAW_ORIENTATION_MIN;
4784 static const int32_t RAW_ORIENTATION_MAX;
4785 static const int32_t RAW_DISTANCE_MIN;
4786 static const int32_t RAW_DISTANCE_MAX;
4787 static const int32_t RAW_TILT_MIN;
4788 static const int32_t RAW_TILT_MAX;
4789 static const int32_t RAW_ID_MIN;
4790 static const int32_t RAW_ID_MAX;
4791 static const int32_t RAW_SLOT_MIN;
4792 static const int32_t RAW_SLOT_MAX;
4793 static const float X_PRECISION;
4794 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004795 static const float X_PRECISION_VIRTUAL;
4796 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004797
4798 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004799 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004800
4801 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4802
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004803 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004804 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004805
Michael Wrightd02c5b62014-02-10 15:10:22 -08004806 enum Axes {
4807 POSITION = 1 << 0,
4808 TOUCH = 1 << 1,
4809 TOOL = 1 << 2,
4810 PRESSURE = 1 << 3,
4811 ORIENTATION = 1 << 4,
4812 MINOR = 1 << 5,
4813 ID = 1 << 6,
4814 DISTANCE = 1 << 7,
4815 TILT = 1 << 8,
4816 SLOT = 1 << 9,
4817 TOOL_TYPE = 1 << 10,
4818 };
4819
Michael Wrighta9cf4192022-12-01 23:46:39 +00004820 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004821 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004822 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004824 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004825 int32_t toRawX(float displayX);
4826 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004827 int32_t toRotatedRawX(float displayX);
4828 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004829 float toCookedX(float rawX, float rawY);
4830 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004832 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004834 float toDisplayY(int32_t rawY, int32_t displayHeight);
4835
Michael Wrightd02c5b62014-02-10 15:10:22 -08004836};
4837
4838const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4839const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4840const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4841const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4842const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4843const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4844const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4845const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004846const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4847const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4849const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4850const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4851const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4852const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4853const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4854const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4855const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4856const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4857const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4858const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4859const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004860const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4861 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4862const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4863 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004864const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4865 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004866
4867const float TouchInputMapperTest::GEOMETRIC_SCALE =
4868 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4869 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4870
4871const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4872 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4873 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4874};
4875
Michael Wrighta9cf4192022-12-01 23:46:39 +00004876void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004877 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4878 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004879}
4880
4881void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4882 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004883 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004884}
4885
Michael Wrighta9cf4192022-12-01 23:46:39 +00004886void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004887 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4888 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4889 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004890}
4891
Michael Wrightd02c5b62014-02-10 15:10:22 -08004892void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004893 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4894 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4895 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4896 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004897}
4898
Jason Gerecke489fda82012-09-07 17:19:40 -07004899void TouchInputMapperTest::prepareLocationCalibration() {
4900 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4901}
4902
Michael Wrightd02c5b62014-02-10 15:10:22 -08004903int32_t TouchInputMapperTest::toRawX(float displayX) {
4904 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4905}
4906
4907int32_t TouchInputMapperTest::toRawY(float displayY) {
4908 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4909}
4910
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004911int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4912 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4913}
4914
4915int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4916 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4917}
4918
Jason Gerecke489fda82012-09-07 17:19:40 -07004919float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4920 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4921 return rawX;
4922}
4923
4924float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4925 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4926 return rawY;
4927}
4928
Michael Wrightd02c5b62014-02-10 15:10:22 -08004929float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004930 return toDisplayX(rawX, DISPLAY_WIDTH);
4931}
4932
4933float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4934 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935}
4936
4937float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004938 return toDisplayY(rawY, DISPLAY_HEIGHT);
4939}
4940
4941float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4942 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943}
4944
4945
4946// --- SingleTouchInputMapperTest ---
4947
4948class SingleTouchInputMapperTest : public TouchInputMapperTest {
4949protected:
4950 void prepareButtons();
4951 void prepareAxes(int axes);
4952
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004953 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4954 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4955 void processUp(SingleTouchInputMapper& mappery);
4956 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4957 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4958 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4959 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4960 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4961 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004962};
4963
4964void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004965 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966}
4967
4968void SingleTouchInputMapperTest::prepareAxes(int axes) {
4969 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004970 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4971 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972 }
4973 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004974 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4975 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976 }
4977 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004978 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4979 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004980 }
4981 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004982 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4983 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984 }
4985 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004986 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4987 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004988 }
4989}
4990
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004991void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995}
4996
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004997void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004998 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000}
5001
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005002void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004}
5005
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005006void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005007 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005008}
5009
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005010void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5011 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013}
5014
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005015void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005017}
5018
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005019void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5020 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023}
5024
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005025void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5026 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005027 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005028}
5029
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005030void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005032}
5033
Michael Wrightd02c5b62014-02-10 15:10:22 -08005034TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035 prepareButtons();
5036 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005037 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005038
Harry Cutts16a24cc2022-10-26 15:22:19 +00005039 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005040}
5041
Michael Wrightd02c5b62014-02-10 15:10:22 -08005042TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005043 prepareButtons();
5044 prepareAxes(POSITION);
5045 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005046 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005047
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005048 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005049}
5050
5051TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005052 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005053 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005054 prepareButtons();
5055 prepareAxes(POSITION);
5056 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005057 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005058
5059 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005060 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061
5062 // Virtual key is down.
5063 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5064 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5065 processDown(mapper, x, y);
5066 processSync(mapper);
5067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5068
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005069 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070
5071 // Virtual key is up.
5072 processUp(mapper);
5073 processSync(mapper);
5074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5075
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005076 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077}
5078
5079TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005081 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005082 prepareButtons();
5083 prepareAxes(POSITION);
5084 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005085 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005086
5087 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005088 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005089
5090 // Virtual key is down.
5091 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5092 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5093 processDown(mapper, x, y);
5094 processSync(mapper);
5095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5096
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005097 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005098
5099 // Virtual key is up.
5100 processUp(mapper);
5101 processSync(mapper);
5102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5103
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005104 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105}
5106
5107TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005108 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005109 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110 prepareButtons();
5111 prepareAxes(POSITION);
5112 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005113 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005114
Michael Wrightd02c5b62014-02-10 15:10:22 -08005115 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005116 ASSERT_TRUE(
5117 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118 ASSERT_TRUE(flags[0]);
5119 ASSERT_FALSE(flags[1]);
5120}
5121
5122TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005124 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005125 prepareButtons();
5126 prepareAxes(POSITION);
5127 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005128 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129
arthurhungdcef2dc2020-08-11 14:47:50 +08005130 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005131
5132 NotifyKeyArgs args;
5133
5134 // Press virtual key.
5135 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5136 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5137 processDown(mapper, x, y);
5138 processSync(mapper);
5139
5140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5141 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5142 ASSERT_EQ(DEVICE_ID, args.deviceId);
5143 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5144 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5145 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5146 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5147 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5148 ASSERT_EQ(KEY_HOME, args.scanCode);
5149 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5150 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5151
5152 // Release virtual key.
5153 processUp(mapper);
5154 processSync(mapper);
5155
5156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5157 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5158 ASSERT_EQ(DEVICE_ID, args.deviceId);
5159 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5160 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5161 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5162 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5163 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5164 ASSERT_EQ(KEY_HOME, args.scanCode);
5165 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5166 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5167
5168 // Should not have sent any motions.
5169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5170}
5171
5172TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005173 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005174 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005175 prepareButtons();
5176 prepareAxes(POSITION);
5177 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005178 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005179
arthurhungdcef2dc2020-08-11 14:47:50 +08005180 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005181
5182 NotifyKeyArgs keyArgs;
5183
5184 // Press virtual key.
5185 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5186 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5187 processDown(mapper, x, y);
5188 processSync(mapper);
5189
5190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5191 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5192 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5193 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5194 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5195 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5196 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5197 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5198 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5199 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5200 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5201
5202 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5203 // into the display area.
5204 y -= 100;
5205 processMove(mapper, x, y);
5206 processSync(mapper);
5207
5208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5209 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5210 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5211 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5212 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5213 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5214 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5215 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5216 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5217 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5218 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5219 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5220
5221 NotifyMotionArgs motionArgs;
5222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5223 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5224 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5225 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5226 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5227 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5228 ASSERT_EQ(0, motionArgs.flags);
5229 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5230 ASSERT_EQ(0, motionArgs.buttonState);
5231 ASSERT_EQ(0, motionArgs.edgeFlags);
5232 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5233 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5234 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5236 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5237 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5238 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5239 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5240
5241 // Keep moving out of bounds. Should generate a pointer move.
5242 y -= 50;
5243 processMove(mapper, x, y);
5244 processSync(mapper);
5245
5246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5247 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5248 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5249 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5250 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5252 ASSERT_EQ(0, motionArgs.flags);
5253 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5254 ASSERT_EQ(0, motionArgs.buttonState);
5255 ASSERT_EQ(0, motionArgs.edgeFlags);
5256 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5257 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5258 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5260 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5261 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5262 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5263 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5264
5265 // Release out of bounds. Should generate a pointer up.
5266 processUp(mapper);
5267 processSync(mapper);
5268
5269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5270 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5271 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5272 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5273 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5274 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5275 ASSERT_EQ(0, motionArgs.flags);
5276 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5277 ASSERT_EQ(0, motionArgs.buttonState);
5278 ASSERT_EQ(0, motionArgs.edgeFlags);
5279 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5280 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5281 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5283 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5284 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5285 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5286 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5287
5288 // Should not have sent any more keys or motions.
5289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5291}
5292
5293TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005294 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005295 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005296 prepareButtons();
5297 prepareAxes(POSITION);
5298 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005299 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005300
arthurhungdcef2dc2020-08-11 14:47:50 +08005301 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005302
5303 NotifyMotionArgs motionArgs;
5304
5305 // Initially go down out of bounds.
5306 int32_t x = -10;
5307 int32_t y = -10;
5308 processDown(mapper, x, y);
5309 processSync(mapper);
5310
5311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5312
5313 // Move into the display area. Should generate a pointer down.
5314 x = 50;
5315 y = 75;
5316 processMove(mapper, x, y);
5317 processSync(mapper);
5318
5319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5320 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5321 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5322 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5323 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5324 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5325 ASSERT_EQ(0, motionArgs.flags);
5326 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5327 ASSERT_EQ(0, motionArgs.buttonState);
5328 ASSERT_EQ(0, motionArgs.edgeFlags);
5329 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5330 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5331 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5333 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5334 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5335 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5336 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5337
5338 // Release. Should generate a pointer up.
5339 processUp(mapper);
5340 processSync(mapper);
5341
5342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5343 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5344 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5345 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5346 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5347 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5348 ASSERT_EQ(0, motionArgs.flags);
5349 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5350 ASSERT_EQ(0, motionArgs.buttonState);
5351 ASSERT_EQ(0, motionArgs.edgeFlags);
5352 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5353 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5354 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5356 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5357 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5358 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5359 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5360
5361 // Should not have sent any more keys or motions.
5362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5364}
5365
Santos Cordonfa5cf462017-04-05 10:37:00 -07005366TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005367 addConfigurationProperty("touch.deviceType", "touchScreen");
5368 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5369
Michael Wrighta9cf4192022-12-01 23:46:39 +00005370 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005371 prepareButtons();
5372 prepareAxes(POSITION);
5373 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005374 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005375
arthurhungdcef2dc2020-08-11 14:47:50 +08005376 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005377
5378 NotifyMotionArgs motionArgs;
5379
5380 // Down.
5381 int32_t x = 100;
5382 int32_t y = 125;
5383 processDown(mapper, x, y);
5384 processSync(mapper);
5385
5386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5387 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5388 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5389 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5390 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5391 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5392 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5393 ASSERT_EQ(0, motionArgs.flags);
5394 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5395 ASSERT_EQ(0, motionArgs.buttonState);
5396 ASSERT_EQ(0, motionArgs.edgeFlags);
5397 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5398 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5401 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5402 1, 0, 0, 0, 0, 0, 0, 0));
5403 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5404 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5405 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5406
5407 // Move.
5408 x += 50;
5409 y += 75;
5410 processMove(mapper, x, y);
5411 processSync(mapper);
5412
5413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5414 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5415 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5416 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5417 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5418 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5420 ASSERT_EQ(0, motionArgs.flags);
5421 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5422 ASSERT_EQ(0, motionArgs.buttonState);
5423 ASSERT_EQ(0, motionArgs.edgeFlags);
5424 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5425 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5426 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5428 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5429 1, 0, 0, 0, 0, 0, 0, 0));
5430 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5431 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5432 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5433
5434 // Up.
5435 processUp(mapper);
5436 processSync(mapper);
5437
5438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5439 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5440 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5441 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5442 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5443 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5444 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5445 ASSERT_EQ(0, motionArgs.flags);
5446 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5447 ASSERT_EQ(0, motionArgs.buttonState);
5448 ASSERT_EQ(0, motionArgs.edgeFlags);
5449 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5450 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5451 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5453 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5454 1, 0, 0, 0, 0, 0, 0, 0));
5455 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5456 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5457 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5458
5459 // Should not have sent any more keys or motions.
5460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5462}
5463
Michael Wrightd02c5b62014-02-10 15:10:22 -08005464TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005465 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005466 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005467 prepareButtons();
5468 prepareAxes(POSITION);
5469 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005470 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005471
arthurhungdcef2dc2020-08-11 14:47:50 +08005472 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005473
5474 NotifyMotionArgs motionArgs;
5475
5476 // Down.
5477 int32_t x = 100;
5478 int32_t y = 125;
5479 processDown(mapper, x, y);
5480 processSync(mapper);
5481
5482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5483 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5484 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5485 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5486 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5487 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5488 ASSERT_EQ(0, motionArgs.flags);
5489 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5490 ASSERT_EQ(0, motionArgs.buttonState);
5491 ASSERT_EQ(0, motionArgs.edgeFlags);
5492 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5493 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5494 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5496 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5497 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5498 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5499 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5500
5501 // Move.
5502 x += 50;
5503 y += 75;
5504 processMove(mapper, x, y);
5505 processSync(mapper);
5506
5507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5508 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5509 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5510 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5511 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5513 ASSERT_EQ(0, motionArgs.flags);
5514 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5515 ASSERT_EQ(0, motionArgs.buttonState);
5516 ASSERT_EQ(0, motionArgs.edgeFlags);
5517 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5518 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5519 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5521 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5522 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5523 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5524 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5525
5526 // Up.
5527 processUp(mapper);
5528 processSync(mapper);
5529
5530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5531 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5532 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5533 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5534 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5535 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5536 ASSERT_EQ(0, motionArgs.flags);
5537 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5538 ASSERT_EQ(0, motionArgs.buttonState);
5539 ASSERT_EQ(0, motionArgs.edgeFlags);
5540 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5541 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5544 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5545 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5546 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5547 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5548
5549 // Should not have sent any more keys or motions.
5550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5552}
5553
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005554TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005555 addConfigurationProperty("touch.deviceType", "touchScreen");
5556 prepareButtons();
5557 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005558 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5559 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005560 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005561
5562 NotifyMotionArgs args;
5563
5564 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005565 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005566 processDown(mapper, toRawX(50), toRawY(75));
5567 processSync(mapper);
5568
5569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5570 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5571 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5572
5573 processUp(mapper);
5574 processSync(mapper);
5575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5576}
5577
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005578TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005579 addConfigurationProperty("touch.deviceType", "touchScreen");
5580 prepareButtons();
5581 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005582 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5583 // orientation-aware are affected by display rotation.
5584 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005585 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005586
5587 NotifyMotionArgs args;
5588
5589 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005590 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005591 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005592 processDown(mapper, toRawX(50), toRawY(75));
5593 processSync(mapper);
5594
5595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5596 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5597 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5598
5599 processUp(mapper);
5600 processSync(mapper);
5601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5602
5603 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005604 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005605 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005606 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005607 processSync(mapper);
5608
5609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5610 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5611 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5612
5613 processUp(mapper);
5614 processSync(mapper);
5615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5616
5617 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005618 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005619 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5621 processSync(mapper);
5622
5623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5624 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5625 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5626
5627 processUp(mapper);
5628 processSync(mapper);
5629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5630
5631 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005632 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005633 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005634 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005635 processSync(mapper);
5636
5637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5638 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5639 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5640
5641 processUp(mapper);
5642 processSync(mapper);
5643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5644}
5645
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005646TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5647 addConfigurationProperty("touch.deviceType", "touchScreen");
5648 prepareButtons();
5649 prepareAxes(POSITION);
5650 addConfigurationProperty("touch.orientationAware", "1");
5651 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5652 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005653 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005654 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5655 NotifyMotionArgs args;
5656
5657 // Orientation 0.
5658 processDown(mapper, toRawX(50), toRawY(75));
5659 processSync(mapper);
5660
5661 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5662 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5663 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5664
5665 processUp(mapper);
5666 processSync(mapper);
5667 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5668}
5669
5670TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5671 addConfigurationProperty("touch.deviceType", "touchScreen");
5672 prepareButtons();
5673 prepareAxes(POSITION);
5674 addConfigurationProperty("touch.orientationAware", "1");
5675 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5676 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005677 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005678 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5679 NotifyMotionArgs args;
5680
5681 // Orientation 90.
5682 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5683 processSync(mapper);
5684
5685 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5686 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5687 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5688
5689 processUp(mapper);
5690 processSync(mapper);
5691 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5692}
5693
5694TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5695 addConfigurationProperty("touch.deviceType", "touchScreen");
5696 prepareButtons();
5697 prepareAxes(POSITION);
5698 addConfigurationProperty("touch.orientationAware", "1");
5699 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5700 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005701 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005702 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5703 NotifyMotionArgs args;
5704
5705 // Orientation 180.
5706 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5707 processSync(mapper);
5708
5709 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5710 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5711 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5712
5713 processUp(mapper);
5714 processSync(mapper);
5715 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5716}
5717
5718TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5719 addConfigurationProperty("touch.deviceType", "touchScreen");
5720 prepareButtons();
5721 prepareAxes(POSITION);
5722 addConfigurationProperty("touch.orientationAware", "1");
5723 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5724 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005725 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005726 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5727 NotifyMotionArgs args;
5728
5729 // Orientation 270.
5730 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5731 processSync(mapper);
5732
5733 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5734 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5735 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5736
5737 processUp(mapper);
5738 processSync(mapper);
5739 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5740}
5741
5742TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5743 addConfigurationProperty("touch.deviceType", "touchScreen");
5744 prepareButtons();
5745 prepareAxes(POSITION);
5746 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5747 // orientation-aware are affected by display rotation.
5748 addConfigurationProperty("touch.orientationAware", "0");
5749 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5750 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5751
5752 NotifyMotionArgs args;
5753
5754 // Orientation 90, Rotation 0.
5755 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005756 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005757 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5758 processSync(mapper);
5759
5760 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5761 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5762 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5763
5764 processUp(mapper);
5765 processSync(mapper);
5766 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5767
5768 // Orientation 90, Rotation 90.
5769 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005770 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005771 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005772 processSync(mapper);
5773
5774 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5775 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5776 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5777
5778 processUp(mapper);
5779 processSync(mapper);
5780 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5781
5782 // Orientation 90, Rotation 180.
5783 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005784 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005785 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5786 processSync(mapper);
5787
5788 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5789 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5790 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5791
5792 processUp(mapper);
5793 processSync(mapper);
5794 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5795
5796 // Orientation 90, Rotation 270.
5797 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005798 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005799 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005800 processSync(mapper);
5801
5802 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5803 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5804 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5805
5806 processUp(mapper);
5807 processSync(mapper);
5808 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5809}
5810
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005811TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5812 addConfigurationProperty("touch.deviceType", "touchScreen");
5813 prepareButtons();
5814 prepareAxes(POSITION);
5815 addConfigurationProperty("touch.orientationAware", "1");
5816 prepareDisplay(ui::ROTATION_0);
5817 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5818
5819 // Set a physical frame in the display viewport.
5820 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5821 viewport->physicalLeft = 20;
5822 viewport->physicalTop = 600;
5823 viewport->physicalRight = 30;
5824 viewport->physicalBottom = 610;
5825 mFakePolicy->updateViewport(*viewport);
5826 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5827
5828 // Start the touch.
5829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5830 processSync(mapper);
5831
5832 // Expect all input starting outside the physical frame to be ignored.
5833 const std::array<Point, 6> outsidePoints = {
5834 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5835 for (const auto& p : outsidePoints) {
5836 processMove(mapper, toRawX(p.x), toRawY(p.y));
5837 processSync(mapper);
5838 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5839 }
5840
5841 // Move the touch into the physical frame.
5842 processMove(mapper, toRawX(25), toRawY(605));
5843 processSync(mapper);
5844 NotifyMotionArgs args;
5845 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5846 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5847 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5848 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5849
5850 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5851 for (const auto& p : outsidePoints) {
5852 processMove(mapper, toRawX(p.x), toRawY(p.y));
5853 processSync(mapper);
5854 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5855 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5856 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5857 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5858 }
5859
5860 processUp(mapper);
5861 processSync(mapper);
5862 EXPECT_NO_FATAL_FAILURE(
5863 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5864}
5865
Michael Wrightd02c5b62014-02-10 15:10:22 -08005866TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005867 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005868 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005869 prepareButtons();
5870 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005871 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005872
5873 // These calculations are based on the input device calibration documentation.
5874 int32_t rawX = 100;
5875 int32_t rawY = 200;
5876 int32_t rawPressure = 10;
5877 int32_t rawToolMajor = 12;
5878 int32_t rawDistance = 2;
5879 int32_t rawTiltX = 30;
5880 int32_t rawTiltY = 110;
5881
5882 float x = toDisplayX(rawX);
5883 float y = toDisplayY(rawY);
5884 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5885 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5886 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5887 float distance = float(rawDistance);
5888
5889 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5890 float tiltScale = M_PI / 180;
5891 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5892 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5893 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5894 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5895
5896 processDown(mapper, rawX, rawY);
5897 processPressure(mapper, rawPressure);
5898 processToolMajor(mapper, rawToolMajor);
5899 processDistance(mapper, rawDistance);
5900 processTilt(mapper, rawTiltX, rawTiltY);
5901 processSync(mapper);
5902
5903 NotifyMotionArgs args;
5904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5906 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5907 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5908}
5909
Jason Gerecke489fda82012-09-07 17:19:40 -07005910TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005911 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005912 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005913 prepareLocationCalibration();
5914 prepareButtons();
5915 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005916 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005917
5918 int32_t rawX = 100;
5919 int32_t rawY = 200;
5920
5921 float x = toDisplayX(toCookedX(rawX, rawY));
5922 float y = toDisplayY(toCookedY(rawX, rawY));
5923
5924 processDown(mapper, rawX, rawY);
5925 processSync(mapper);
5926
5927 NotifyMotionArgs args;
5928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5930 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5931}
5932
Michael Wrightd02c5b62014-02-10 15:10:22 -08005933TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005934 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005935 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005936 prepareButtons();
5937 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005938 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005939
5940 NotifyMotionArgs motionArgs;
5941 NotifyKeyArgs keyArgs;
5942
5943 processDown(mapper, 100, 200);
5944 processSync(mapper);
5945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5946 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5947 ASSERT_EQ(0, motionArgs.buttonState);
5948
5949 // press BTN_LEFT, release BTN_LEFT
5950 processKey(mapper, BTN_LEFT, 1);
5951 processSync(mapper);
5952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5953 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5954 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5955
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5957 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5958 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5959
Michael Wrightd02c5b62014-02-10 15:10:22 -08005960 processKey(mapper, BTN_LEFT, 0);
5961 processSync(mapper);
5962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005963 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005964 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005965
5966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005968 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005969
5970 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5971 processKey(mapper, BTN_RIGHT, 1);
5972 processKey(mapper, BTN_MIDDLE, 1);
5973 processSync(mapper);
5974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5975 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5976 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5977 motionArgs.buttonState);
5978
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5980 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5981 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5982
5983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5984 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5985 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5986 motionArgs.buttonState);
5987
Michael Wrightd02c5b62014-02-10 15:10:22 -08005988 processKey(mapper, BTN_RIGHT, 0);
5989 processSync(mapper);
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005991 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005992 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005993
5994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005996 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005997
5998 processKey(mapper, BTN_MIDDLE, 0);
5999 processSync(mapper);
6000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006001 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006002 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006003
6004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006006 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006007
6008 // press BTN_BACK, release BTN_BACK
6009 processKey(mapper, BTN_BACK, 1);
6010 processSync(mapper);
6011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6012 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6013 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006014
Michael Wrightd02c5b62014-02-10 15:10:22 -08006015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006016 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006017 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6018
6019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6020 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6021 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006022
6023 processKey(mapper, BTN_BACK, 0);
6024 processSync(mapper);
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006026 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006027 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006028
6029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006030 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006031 ASSERT_EQ(0, motionArgs.buttonState);
6032
Michael Wrightd02c5b62014-02-10 15:10:22 -08006033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6034 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6035 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6036
6037 // press BTN_SIDE, release BTN_SIDE
6038 processKey(mapper, BTN_SIDE, 1);
6039 processSync(mapper);
6040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6041 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6042 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006043
Michael Wrightd02c5b62014-02-10 15:10:22 -08006044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006046 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6047
6048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6049 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6050 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006051
6052 processKey(mapper, BTN_SIDE, 0);
6053 processSync(mapper);
6054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006055 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006056 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006057
6058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006060 ASSERT_EQ(0, motionArgs.buttonState);
6061
Michael Wrightd02c5b62014-02-10 15:10:22 -08006062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6063 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6064 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6065
6066 // press BTN_FORWARD, release BTN_FORWARD
6067 processKey(mapper, BTN_FORWARD, 1);
6068 processSync(mapper);
6069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6070 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6071 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006072
Michael Wrightd02c5b62014-02-10 15:10:22 -08006073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006075 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6076
6077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6078 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6079 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006080
6081 processKey(mapper, BTN_FORWARD, 0);
6082 processSync(mapper);
6083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006084 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006085 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006086
6087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006088 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006089 ASSERT_EQ(0, motionArgs.buttonState);
6090
Michael Wrightd02c5b62014-02-10 15:10:22 -08006091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6092 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6093 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6094
6095 // press BTN_EXTRA, release BTN_EXTRA
6096 processKey(mapper, BTN_EXTRA, 1);
6097 processSync(mapper);
6098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6099 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6100 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006101
Michael Wrightd02c5b62014-02-10 15:10:22 -08006102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006103 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006104 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6105
6106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6107 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6108 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006109
6110 processKey(mapper, BTN_EXTRA, 0);
6111 processSync(mapper);
6112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006113 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006114 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006115
6116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006118 ASSERT_EQ(0, motionArgs.buttonState);
6119
Michael Wrightd02c5b62014-02-10 15:10:22 -08006120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6121 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6122 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6123
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6125
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126 // press BTN_STYLUS, release BTN_STYLUS
6127 processKey(mapper, BTN_STYLUS, 1);
6128 processSync(mapper);
6129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6130 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006131 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6132
6133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6134 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6135 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006136
6137 processKey(mapper, BTN_STYLUS, 0);
6138 processSync(mapper);
6139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006140 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006141 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006142
6143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006145 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006146
6147 // press BTN_STYLUS2, release BTN_STYLUS2
6148 processKey(mapper, BTN_STYLUS2, 1);
6149 processSync(mapper);
6150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006152 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6153
6154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6155 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6156 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006157
6158 processKey(mapper, BTN_STYLUS2, 0);
6159 processSync(mapper);
6160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006161 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006162 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006163
6164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006166 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006167
6168 // release touch
6169 processUp(mapper);
6170 processSync(mapper);
6171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6172 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6173 ASSERT_EQ(0, motionArgs.buttonState);
6174}
6175
6176TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006177 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006178 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179 prepareButtons();
6180 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006181 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182
6183 NotifyMotionArgs motionArgs;
6184
6185 // default tool type is finger
6186 processDown(mapper, 100, 200);
6187 processSync(mapper);
6188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6189 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6190 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6191
6192 // eraser
6193 processKey(mapper, BTN_TOOL_RUBBER, 1);
6194 processSync(mapper);
6195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6196 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6197 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6198
6199 // stylus
6200 processKey(mapper, BTN_TOOL_RUBBER, 0);
6201 processKey(mapper, BTN_TOOL_PEN, 1);
6202 processSync(mapper);
6203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6205 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6206
6207 // brush
6208 processKey(mapper, BTN_TOOL_PEN, 0);
6209 processKey(mapper, BTN_TOOL_BRUSH, 1);
6210 processSync(mapper);
6211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6214
6215 // pencil
6216 processKey(mapper, BTN_TOOL_BRUSH, 0);
6217 processKey(mapper, BTN_TOOL_PENCIL, 1);
6218 processSync(mapper);
6219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6221 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6222
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006223 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006224 processKey(mapper, BTN_TOOL_PENCIL, 0);
6225 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6226 processSync(mapper);
6227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6229 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6230
6231 // mouse
6232 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6233 processKey(mapper, BTN_TOOL_MOUSE, 1);
6234 processSync(mapper);
6235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6237 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6238
6239 // lens
6240 processKey(mapper, BTN_TOOL_MOUSE, 0);
6241 processKey(mapper, BTN_TOOL_LENS, 1);
6242 processSync(mapper);
6243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6245 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6246
6247 // double-tap
6248 processKey(mapper, BTN_TOOL_LENS, 0);
6249 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6250 processSync(mapper);
6251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6253 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6254
6255 // triple-tap
6256 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6257 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6258 processSync(mapper);
6259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6261 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6262
6263 // quad-tap
6264 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6265 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6266 processSync(mapper);
6267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6270
6271 // finger
6272 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6273 processKey(mapper, BTN_TOOL_FINGER, 1);
6274 processSync(mapper);
6275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6278
6279 // stylus trumps finger
6280 processKey(mapper, BTN_TOOL_PEN, 1);
6281 processSync(mapper);
6282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6285
6286 // eraser trumps stylus
6287 processKey(mapper, BTN_TOOL_RUBBER, 1);
6288 processSync(mapper);
6289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6291 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6292
6293 // mouse trumps eraser
6294 processKey(mapper, BTN_TOOL_MOUSE, 1);
6295 processSync(mapper);
6296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6298 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6299
6300 // back to default tool type
6301 processKey(mapper, BTN_TOOL_MOUSE, 0);
6302 processKey(mapper, BTN_TOOL_RUBBER, 0);
6303 processKey(mapper, BTN_TOOL_PEN, 0);
6304 processKey(mapper, BTN_TOOL_FINGER, 0);
6305 processSync(mapper);
6306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6307 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6309}
6310
6311TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006312 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006313 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006314 prepareButtons();
6315 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006316 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006317 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318
6319 NotifyMotionArgs motionArgs;
6320
6321 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6322 processKey(mapper, BTN_TOOL_FINGER, 1);
6323 processMove(mapper, 100, 200);
6324 processSync(mapper);
6325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6326 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6327 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6328 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6329
6330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6331 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6333 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6334
6335 // move a little
6336 processMove(mapper, 150, 250);
6337 processSync(mapper);
6338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6339 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6341 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6342
6343 // down when BTN_TOUCH is pressed, pressure defaults to 1
6344 processKey(mapper, BTN_TOUCH, 1);
6345 processSync(mapper);
6346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6347 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6349 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6350
6351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6352 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6354 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6355
6356 // up when BTN_TOUCH is released, hover restored
6357 processKey(mapper, BTN_TOUCH, 0);
6358 processSync(mapper);
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6360 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6362 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6363
6364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6365 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6367 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6368
6369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6370 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6372 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6373
6374 // exit hover when pointer goes away
6375 processKey(mapper, BTN_TOOL_FINGER, 0);
6376 processSync(mapper);
6377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6378 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6380 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6381}
6382
6383TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006384 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006385 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386 prepareButtons();
6387 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006388 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389
6390 NotifyMotionArgs motionArgs;
6391
6392 // initially hovering because pressure is 0
6393 processDown(mapper, 100, 200);
6394 processPressure(mapper, 0);
6395 processSync(mapper);
6396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6397 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6399 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6400
6401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6402 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6404 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6405
6406 // move a little
6407 processMove(mapper, 150, 250);
6408 processSync(mapper);
6409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6410 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6412 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6413
6414 // down when pressure is non-zero
6415 processPressure(mapper, RAW_PRESSURE_MAX);
6416 processSync(mapper);
6417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6418 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6420 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6421
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6423 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6425 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6426
6427 // up when pressure becomes 0, hover restored
6428 processPressure(mapper, 0);
6429 processSync(mapper);
6430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6431 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6433 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6434
6435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6436 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6437 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6438 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6439
6440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6441 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6443 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6444
6445 // exit hover when pointer goes away
6446 processUp(mapper);
6447 processSync(mapper);
6448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6449 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6451 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6452}
6453
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006454TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6455 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006456 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006457 prepareButtons();
6458 prepareAxes(POSITION | PRESSURE);
6459 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6460
6461 // Touch down.
6462 processDown(mapper, 100, 200);
6463 processPressure(mapper, 1);
6464 processSync(mapper);
6465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6466 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6467
6468 // Reset the mapper. This should cancel the ongoing gesture.
6469 resetMapper(mapper, ARBITRARY_TIME);
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6471 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6472
6473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6474}
6475
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006476TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6477 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006478 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006479 prepareButtons();
6480 prepareAxes(POSITION | PRESSURE);
6481 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6482
6483 // Set the initial state for the touch pointer.
6484 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6485 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6486 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6487 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6488
6489 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006490 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6491 // does not generate any events.
6492 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006493
6494 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6495 // the recreated touch state to generate a down event.
6496 processSync(mapper);
6497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6498 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6499
6500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6501}
6502
lilinnan687e58f2022-07-19 16:00:50 +08006503TEST_F(SingleTouchInputMapperTest,
6504 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6505 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006506 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006507 prepareButtons();
6508 prepareAxes(POSITION);
6509 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6510 NotifyMotionArgs motionArgs;
6511
6512 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006513 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006514 processSync(mapper);
6515
6516 // We should receive a down event
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6518 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6519
6520 // Change display id
6521 clearViewports();
6522 prepareSecondaryDisplay(ViewportType::INTERNAL);
6523
6524 // We should receive a cancel event
6525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6526 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6527 // Then receive reset called
6528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6529}
6530
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006531TEST_F(SingleTouchInputMapperTest,
6532 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6533 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006534 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006535 prepareButtons();
6536 prepareAxes(POSITION);
6537 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6539 NotifyMotionArgs motionArgs;
6540
6541 // Start a new gesture.
6542 processDown(mapper, 100, 200);
6543 processSync(mapper);
6544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6545 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6546
6547 // Make the viewport inactive. This will put the device in disabled mode.
6548 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6549 viewport->isActive = false;
6550 mFakePolicy->updateViewport(*viewport);
6551 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6552
6553 // We should receive a cancel event for the ongoing gesture.
6554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6555 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6556 // Then we should be notified that the device was reset.
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6558
6559 // No events are generated while the viewport is inactive.
6560 processMove(mapper, 101, 201);
6561 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006562 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006563 processSync(mapper);
6564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6565
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006566 // Start a new gesture while the viewport is still inactive.
6567 processDown(mapper, 300, 400);
6568 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6569 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6570 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6571 processSync(mapper);
6572
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006573 // Make the viewport active again. The device should resume processing events.
6574 viewport->isActive = true;
6575 mFakePolicy->updateViewport(*viewport);
6576 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6577
6578 // The device is reset because it changes back to direct mode, without generating any events.
6579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6581
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006582 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006583 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6585 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006586
6587 // No more events.
6588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6590}
6591
Prabir Pradhan211ba622022-10-31 21:09:21 +00006592TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6593 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006594 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006595 prepareButtons();
6596 prepareAxes(POSITION);
6597 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6599
6600 // Press a stylus button.
6601 processKey(mapper, BTN_STYLUS, 1);
6602 processSync(mapper);
6603
6604 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6605 processDown(mapper, 100, 200);
6606 processSync(mapper);
6607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6608 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6609 WithCoords(toDisplayX(100), toDisplayY(200)),
6610 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6612 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6613 WithCoords(toDisplayX(100), toDisplayY(200)),
6614 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6615
6616 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6617 // the button has not actually been released, since there will be no pointers through which the
6618 // button state can be reported. The event is generated at the location of the pointer before
6619 // it went up.
6620 processUp(mapper);
6621 processSync(mapper);
6622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6623 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6624 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6626 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6627 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6628}
6629
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006630TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6631 addConfigurationProperty("touch.deviceType", "touchScreen");
6632 prepareDisplay(ui::ROTATION_0);
6633 prepareButtons();
6634 prepareAxes(POSITION);
6635
6636 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6637
6638 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6640
6641 // Press a stylus button.
6642 processKey(mapper, BTN_STYLUS, 1);
6643 processSync(mapper);
6644
6645 // Start a touch gesture and ensure that the stylus button is not reported.
6646 processDown(mapper, 100, 200);
6647 processSync(mapper);
6648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6649 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6650
6651 // Release and press the stylus button again.
6652 processKey(mapper, BTN_STYLUS, 0);
6653 processSync(mapper);
6654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6655 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6656 processKey(mapper, BTN_STYLUS, 1);
6657 processSync(mapper);
6658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6659 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6660
6661 // Release the touch gesture.
6662 processUp(mapper);
6663 processSync(mapper);
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6665 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6666
6667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6668}
6669
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006670TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6671 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6672 prepareDisplay(ui::ROTATION_0);
6673 prepareButtons();
6674 prepareAxes(POSITION);
6675 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6677
6678 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6679}
6680
Prabir Pradhan5632d622021-09-06 07:57:20 -07006681// --- TouchDisplayProjectionTest ---
6682
6683class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6684public:
6685 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6686 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6687 // rotated equivalent of the given un-rotated physical display bounds.
Michael Wrighta9cf4192022-12-01 23:46:39 +00006688 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006689 uint32_t inverseRotationFlags;
6690 auto width = DISPLAY_WIDTH;
6691 auto height = DISPLAY_HEIGHT;
6692 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006693 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006694 inverseRotationFlags = ui::Transform::ROT_270;
6695 std::swap(width, height);
6696 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006697 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006698 inverseRotationFlags = ui::Transform::ROT_180;
6699 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006700 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006701 inverseRotationFlags = ui::Transform::ROT_90;
6702 std::swap(width, height);
6703 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006704 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006705 inverseRotationFlags = ui::Transform::ROT_0;
6706 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006707 }
6708
6709 const ui::Transform rotation(inverseRotationFlags, width, height);
6710 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6711
6712 std::optional<DisplayViewport> internalViewport =
6713 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6714 DisplayViewport& v = *internalViewport;
6715 v.displayId = DISPLAY_ID;
6716 v.orientation = orientation;
6717
6718 v.logicalLeft = 0;
6719 v.logicalTop = 0;
6720 v.logicalRight = 100;
6721 v.logicalBottom = 100;
6722
6723 v.physicalLeft = rotatedPhysicalDisplay.left;
6724 v.physicalTop = rotatedPhysicalDisplay.top;
6725 v.physicalRight = rotatedPhysicalDisplay.right;
6726 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6727
6728 v.deviceWidth = width;
6729 v.deviceHeight = height;
6730
6731 v.isActive = true;
6732 v.uniqueId = UNIQUE_ID;
6733 v.type = ViewportType::INTERNAL;
6734 mFakePolicy->updateViewport(v);
6735 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6736 }
6737
6738 void assertReceivedMove(const Point& point) {
6739 NotifyMotionArgs motionArgs;
6740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6741 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6742 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6744 1, 0, 0, 0, 0, 0, 0, 0));
6745 }
6746};
6747
6748TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6749 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006750 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006751
6752 prepareButtons();
6753 prepareAxes(POSITION);
6754 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6755
6756 NotifyMotionArgs motionArgs;
6757
6758 // Configure the DisplayViewport such that the logical display maps to a subsection of
6759 // the display panel called the physical display. Here, the physical display is bounded by the
6760 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6761 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6762 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6763 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6764
Michael Wrighta9cf4192022-12-01 23:46:39 +00006765 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006766 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6767
6768 // Touches outside the physical display should be ignored, and should not generate any
6769 // events. Ensure touches at the following points that lie outside of the physical display
6770 // area do not generate any events.
6771 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6772 processDown(mapper, toRawX(point.x), toRawY(point.y));
6773 processSync(mapper);
6774 processUp(mapper);
6775 processSync(mapper);
6776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6777 << "Unexpected event generated for touch outside physical display at point: "
6778 << point.x << ", " << point.y;
6779 }
6780 }
6781}
6782
6783TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6784 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006785 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006786
6787 prepareButtons();
6788 prepareAxes(POSITION);
6789 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6790
6791 NotifyMotionArgs motionArgs;
6792
6793 // Configure the DisplayViewport such that the logical display maps to a subsection of
6794 // the display panel called the physical display. Here, the physical display is bounded by the
6795 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6796 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6797
Michael Wrighta9cf4192022-12-01 23:46:39 +00006798 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006799 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6800
6801 // Touches that start outside the physical display should be ignored until it enters the
6802 // physical display bounds, at which point it should generate a down event. Start a touch at
6803 // the point (5, 100), which is outside the physical display bounds.
6804 static const Point kOutsidePoint{5, 100};
6805 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6806 processSync(mapper);
6807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6808
6809 // Move the touch into the physical display area. This should generate a pointer down.
6810 processMove(mapper, toRawX(11), toRawY(21));
6811 processSync(mapper);
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6813 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6814 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6815 ASSERT_NO_FATAL_FAILURE(
6816 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6817
6818 // Move the touch inside the physical display area. This should generate a pointer move.
6819 processMove(mapper, toRawX(69), toRawY(159));
6820 processSync(mapper);
6821 assertReceivedMove({69, 159});
6822
6823 // Move outside the physical display area. Since the pointer is already down, this should
6824 // now continue generating events.
6825 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6826 processSync(mapper);
6827 assertReceivedMove(kOutsidePoint);
6828
6829 // Release. This should generate a pointer up.
6830 processUp(mapper);
6831 processSync(mapper);
6832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6833 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6835 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6836
6837 // Ensure no more events were generated.
6838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6840 }
6841}
6842
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006843// --- ExternalStylusFusionTest ---
6844
6845class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6846public:
6847 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6848 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006849 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006850 prepareButtons();
6851 prepareAxes(POSITION);
6852 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6853
6854 mStylusState.when = ARBITRARY_TIME;
6855 mStylusState.pressure = 0.f;
6856 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
6857 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
6858 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
6859 processExternalStylusState(mapper);
6860 return mapper;
6861 }
6862
6863 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6864 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6865 for (const NotifyArgs& args : generatedArgs) {
6866 mFakeListener->notify(args);
6867 }
6868 // Loop the reader to flush the input listener queue.
6869 mReader->loopOnce();
6870 return generatedArgs;
6871 }
6872
6873protected:
6874 StylusState mStylusState{};
6875 static constexpr uint32_t EXPECTED_SOURCE =
6876 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
6877
6878 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6879 auto toolTypeSource =
6880 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6881
6882 // The first pointer is withheld.
6883 processDown(mapper, 100, 200);
6884 processSync(mapper);
6885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6886 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6887 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6888
6889 // The external stylus reports pressure. The withheld finger pointer is released as a
6890 // stylus.
6891 mStylusState.pressure = 1.f;
6892 processExternalStylusState(mapper);
6893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6894 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6895 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6896
6897 // Subsequent pointer events are not withheld.
6898 processMove(mapper, 101, 201);
6899 processSync(mapper);
6900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6901 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6902
6903 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6905 }
6906
6907 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6908 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6909
6910 // Releasing the touch pointer ends the gesture.
6911 processUp(mapper);
6912 processSync(mapper);
6913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6914 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
6915 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6916
6917 mStylusState.pressure = 0.f;
6918 processExternalStylusState(mapper);
6919 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6921 }
6922
6923 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6924 auto toolTypeSource =
6925 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER));
6926
6927 // The first pointer is withheld when an external stylus is connected,
6928 // and a timeout is requested.
6929 processDown(mapper, 100, 200);
6930 processSync(mapper);
6931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6932 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6933 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6934
6935 // If the timeout expires early, it is requested again.
6936 handleTimeout(mapper, ARBITRARY_TIME + 1);
6937 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6938 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6939
6940 // When the timeout expires, the withheld touch is released as a finger pointer.
6941 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
6942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6943 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6944
6945 // Subsequent pointer events are not withheld.
6946 processMove(mapper, 101, 201);
6947 processSync(mapper);
6948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6949 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6950 processUp(mapper);
6951 processSync(mapper);
6952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6953 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6954
6955 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6957 }
6958
6959private:
6960 InputDeviceInfo mExternalStylusDeviceInfo{};
6961};
6962
6963TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
6964 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6965 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
6966}
6967
6968TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
6969 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6970 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6971}
6972
6973TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
6974 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6975 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6976}
6977
6978// Test a successful stylus fusion gesture where the pressure is reported by the external
6979// before the touch is reported by the touchscreen.
6980TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
6981 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6982 auto toolTypeSource =
6983 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6984
6985 // The external stylus reports pressure first. It is ignored for now.
6986 mStylusState.pressure = 1.f;
6987 processExternalStylusState(mapper);
6988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6989 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6990
6991 // When the touch goes down afterwards, it is reported as a stylus pointer.
6992 processDown(mapper, 100, 200);
6993 processSync(mapper);
6994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6995 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6996 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6997
6998 processMove(mapper, 101, 201);
6999 processSync(mapper);
7000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7001 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7002 processUp(mapper);
7003 processSync(mapper);
7004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7005 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7006
7007 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7009}
7010
7011TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7012 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7013
7014 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7015 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7016
7017 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7018 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7019 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7020 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7021}
7022
7023TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7024 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7025 auto toolTypeSource =
7026 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7027
7028 mStylusState.pressure = 0.8f;
7029 processExternalStylusState(mapper);
7030 processDown(mapper, 100, 200);
7031 processSync(mapper);
7032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7033 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7034 WithPressure(0.8f))));
7035 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7036
7037 // The external stylus reports a pressure change. We wait for some time for a touch event.
7038 mStylusState.pressure = 0.6f;
7039 processExternalStylusState(mapper);
7040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7041 ASSERT_NO_FATAL_FAILURE(
7042 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7043
7044 // If a touch is reported within the timeout, it reports the updated pressure.
7045 processMove(mapper, 101, 201);
7046 processSync(mapper);
7047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7048 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7049 WithPressure(0.6f))));
7050 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7051
7052 // There is another pressure change.
7053 mStylusState.pressure = 0.5f;
7054 processExternalStylusState(mapper);
7055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7056 ASSERT_NO_FATAL_FAILURE(
7057 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7058
7059 // If a touch is not reported within the timeout, a move event is generated to report
7060 // the new pressure.
7061 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7063 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7064 WithPressure(0.5f))));
7065
7066 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7067 // repeated indefinitely.
7068 mStylusState.pressure = 0.0f;
7069 processExternalStylusState(mapper);
7070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7071 ASSERT_NO_FATAL_FAILURE(
7072 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7073 processMove(mapper, 102, 202);
7074 processSync(mapper);
7075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7076 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7077 WithPressure(0.5f))));
7078 processMove(mapper, 103, 203);
7079 processSync(mapper);
7080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7081 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7082 WithPressure(0.5f))));
7083
7084 processUp(mapper);
7085 processSync(mapper);
7086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7087 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7088 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7089
7090 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7092}
7093
7094TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7095 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7096 auto source = WithSource(EXPECTED_SOURCE);
7097
7098 mStylusState.pressure = 1.f;
7099 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_ERASER;
7100 processExternalStylusState(mapper);
7101 processDown(mapper, 100, 200);
7102 processSync(mapper);
7103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7104 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7105 WithToolType(AMOTION_EVENT_TOOL_TYPE_ERASER))));
7106 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7107
7108 // The external stylus reports a tool change. We wait for some time for a touch event.
7109 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7110 processExternalStylusState(mapper);
7111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7112 ASSERT_NO_FATAL_FAILURE(
7113 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7114
7115 // If a touch is reported within the timeout, it reports the updated pressure.
7116 processMove(mapper, 101, 201);
7117 processSync(mapper);
7118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7119 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7120 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7121 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7122
7123 // There is another tool type change.
7124 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
7125 processExternalStylusState(mapper);
7126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7127 ASSERT_NO_FATAL_FAILURE(
7128 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7129
7130 // If a touch is not reported within the timeout, a move event is generated to report
7131 // the new tool type.
7132 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7134 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7135 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7136
7137 processUp(mapper);
7138 processSync(mapper);
7139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7140 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
7141 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7142
7143 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7145}
7146
7147TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7148 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7149 auto toolTypeSource =
7150 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7151
7152 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7153
7154 // The external stylus reports a button change. We wait for some time for a touch event.
7155 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7156 processExternalStylusState(mapper);
7157 ASSERT_NO_FATAL_FAILURE(
7158 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7159
7160 // If a touch is reported within the timeout, it reports the updated button state.
7161 processMove(mapper, 101, 201);
7162 processSync(mapper);
7163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7164 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7165 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7167 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7168 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7169 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7170
7171 // The button is now released.
7172 mStylusState.buttons = 0;
7173 processExternalStylusState(mapper);
7174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7175 ASSERT_NO_FATAL_FAILURE(
7176 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7177
7178 // If a touch is not reported within the timeout, a move event is generated to report
7179 // the new button state.
7180 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7182 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7183 WithButtonState(0))));
7184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007185 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7186 WithButtonState(0))));
7187
7188 processUp(mapper);
7189 processSync(mapper);
7190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007191 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7192
7193 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7195}
7196
Michael Wrightd02c5b62014-02-10 15:10:22 -08007197// --- MultiTouchInputMapperTest ---
7198
7199class MultiTouchInputMapperTest : public TouchInputMapperTest {
7200protected:
7201 void prepareAxes(int axes);
7202
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007203 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7204 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7205 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7206 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7207 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7208 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7209 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7210 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7211 void processId(MultiTouchInputMapper& mapper, int32_t id);
7212 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7213 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7214 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007215 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007216 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007217 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7218 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007219};
7220
7221void MultiTouchInputMapperTest::prepareAxes(int axes) {
7222 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007223 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7224 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007225 }
7226 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007227 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7228 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007229 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007230 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7231 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007232 }
7233 }
7234 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007235 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7236 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007237 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007238 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007239 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007240 }
7241 }
7242 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007243 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7244 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007245 }
7246 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007247 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7248 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007249 }
7250 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007251 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7252 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007253 }
7254 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007255 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7256 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007257 }
7258 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007259 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7260 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007261 }
7262 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007263 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007264 }
7265}
7266
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007267void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7268 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007269 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007271}
7272
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007273void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7274 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007276}
7277
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007278void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7279 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007281}
7282
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007283void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007285}
7286
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007287void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007289}
7290
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007291void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7292 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007294}
7295
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007296void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007298}
7299
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007300void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007302}
7303
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007304void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007306}
7307
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007308void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007310}
7311
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007312void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007314}
7315
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007316void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7317 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007319}
7320
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007321void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7322 int32_t value) {
7323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7324 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7325}
7326
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007327void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007328 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007329}
7330
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007331void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7332 nsecs_t readTime) {
7333 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007334}
7335
Michael Wrightd02c5b62014-02-10 15:10:22 -08007336TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007337 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007338 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007339 prepareAxes(POSITION);
7340 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007341 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007342
arthurhungdcef2dc2020-08-11 14:47:50 +08007343 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007344
7345 NotifyMotionArgs motionArgs;
7346
7347 // Two fingers down at once.
7348 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7349 processPosition(mapper, x1, y1);
7350 processMTSync(mapper);
7351 processPosition(mapper, x2, y2);
7352 processMTSync(mapper);
7353 processSync(mapper);
7354
7355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7356 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7357 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7358 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7359 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7360 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7361 ASSERT_EQ(0, motionArgs.flags);
7362 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7363 ASSERT_EQ(0, motionArgs.buttonState);
7364 ASSERT_EQ(0, motionArgs.edgeFlags);
7365 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7366 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7369 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7370 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7371 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7372 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7373
7374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7375 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7376 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7377 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7378 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007379 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007380 ASSERT_EQ(0, motionArgs.flags);
7381 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7382 ASSERT_EQ(0, motionArgs.buttonState);
7383 ASSERT_EQ(0, motionArgs.edgeFlags);
7384 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7385 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7386 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7387 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7388 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7390 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7392 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7393 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7394 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7395 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7396
7397 // Move.
7398 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7399 processPosition(mapper, x1, y1);
7400 processMTSync(mapper);
7401 processPosition(mapper, x2, y2);
7402 processMTSync(mapper);
7403 processSync(mapper);
7404
7405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7406 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7407 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7408 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7409 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7411 ASSERT_EQ(0, motionArgs.flags);
7412 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7413 ASSERT_EQ(0, motionArgs.buttonState);
7414 ASSERT_EQ(0, motionArgs.edgeFlags);
7415 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7416 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7417 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7418 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7421 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7423 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7424 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7425 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7426 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7427
7428 // First finger up.
7429 x2 += 15; y2 -= 20;
7430 processPosition(mapper, x2, y2);
7431 processMTSync(mapper);
7432 processSync(mapper);
7433
7434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7435 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7436 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7437 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7438 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007439 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007440 ASSERT_EQ(0, motionArgs.flags);
7441 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7442 ASSERT_EQ(0, motionArgs.buttonState);
7443 ASSERT_EQ(0, motionArgs.edgeFlags);
7444 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7445 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7446 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7447 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7448 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7450 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7452 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7453 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7454 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7455 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7456
7457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7458 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7459 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7460 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7461 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7462 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7463 ASSERT_EQ(0, motionArgs.flags);
7464 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7465 ASSERT_EQ(0, motionArgs.buttonState);
7466 ASSERT_EQ(0, motionArgs.edgeFlags);
7467 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7468 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7469 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7471 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7472 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7473 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7474 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7475
7476 // Move.
7477 x2 += 20; y2 -= 25;
7478 processPosition(mapper, x2, y2);
7479 processMTSync(mapper);
7480 processSync(mapper);
7481
7482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7483 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7484 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7485 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7486 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7488 ASSERT_EQ(0, motionArgs.flags);
7489 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7490 ASSERT_EQ(0, motionArgs.buttonState);
7491 ASSERT_EQ(0, motionArgs.edgeFlags);
7492 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7493 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7494 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7496 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7497 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7498 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7499 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7500
7501 // New finger down.
7502 int32_t x3 = 700, y3 = 300;
7503 processPosition(mapper, x2, y2);
7504 processMTSync(mapper);
7505 processPosition(mapper, x3, y3);
7506 processMTSync(mapper);
7507 processSync(mapper);
7508
7509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7510 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7511 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7512 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7513 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007514 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007515 ASSERT_EQ(0, motionArgs.flags);
7516 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7517 ASSERT_EQ(0, motionArgs.buttonState);
7518 ASSERT_EQ(0, motionArgs.edgeFlags);
7519 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7520 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7521 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7522 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7523 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7525 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7527 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7528 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7529 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7530 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7531
7532 // Second finger up.
7533 x3 += 30; y3 -= 20;
7534 processPosition(mapper, x3, y3);
7535 processMTSync(mapper);
7536 processSync(mapper);
7537
7538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7539 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7540 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7541 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7542 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007543 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007544 ASSERT_EQ(0, motionArgs.flags);
7545 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7546 ASSERT_EQ(0, motionArgs.buttonState);
7547 ASSERT_EQ(0, motionArgs.edgeFlags);
7548 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7549 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7550 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7551 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7554 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7556 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7557 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7558 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7559 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7560
7561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7562 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7563 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7564 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7565 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7566 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7567 ASSERT_EQ(0, motionArgs.flags);
7568 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7569 ASSERT_EQ(0, motionArgs.buttonState);
7570 ASSERT_EQ(0, motionArgs.edgeFlags);
7571 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7572 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7573 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7575 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7576 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7577 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7578 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7579
7580 // Last finger up.
7581 processMTSync(mapper);
7582 processSync(mapper);
7583
7584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7585 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7586 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7587 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7588 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7589 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7590 ASSERT_EQ(0, motionArgs.flags);
7591 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7592 ASSERT_EQ(0, motionArgs.buttonState);
7593 ASSERT_EQ(0, motionArgs.edgeFlags);
7594 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7595 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7596 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7598 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7599 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7600 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7601 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7602
7603 // Should not have sent any more keys or motions.
7604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7606}
7607
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007608TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7609 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007610 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007611
7612 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7613 /*fuzz*/ 0, /*resolution*/ 10);
7614 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7615 /*fuzz*/ 0, /*resolution*/ 11);
7616 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7617 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7618 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7619 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7620 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7621 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7622 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7623 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7624
7625 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7626
7627 // X and Y axes
7628 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7629 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7630 // Touch major and minor
7631 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7632 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7633 // Tool major and minor
7634 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7635 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7636}
7637
7638TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7639 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007640 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007641
7642 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7643 /*fuzz*/ 0, /*resolution*/ 10);
7644 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7645 /*fuzz*/ 0, /*resolution*/ 11);
7646
7647 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7648
7649 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7650
7651 // Touch major and minor
7652 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7653 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7654 // Tool major and minor
7655 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7656 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7657}
7658
Michael Wrightd02c5b62014-02-10 15:10:22 -08007659TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007660 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007661 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007662 prepareAxes(POSITION | ID);
7663 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007664 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007665
arthurhungdcef2dc2020-08-11 14:47:50 +08007666 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007667
7668 NotifyMotionArgs motionArgs;
7669
7670 // Two fingers down at once.
7671 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7672 processPosition(mapper, x1, y1);
7673 processId(mapper, 1);
7674 processMTSync(mapper);
7675 processPosition(mapper, x2, y2);
7676 processId(mapper, 2);
7677 processMTSync(mapper);
7678 processSync(mapper);
7679
7680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7681 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7682 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7683 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7684 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7686 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7687
7688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007689 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007690 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7691 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7693 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7696 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7698 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7699
7700 // Move.
7701 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7702 processPosition(mapper, x1, y1);
7703 processId(mapper, 1);
7704 processMTSync(mapper);
7705 processPosition(mapper, x2, y2);
7706 processId(mapper, 2);
7707 processMTSync(mapper);
7708 processSync(mapper);
7709
7710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7711 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7712 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7713 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7714 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7715 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7716 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7718 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7719 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7720 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7721
7722 // First finger up.
7723 x2 += 15; y2 -= 20;
7724 processPosition(mapper, x2, y2);
7725 processId(mapper, 2);
7726 processMTSync(mapper);
7727 processSync(mapper);
7728
7729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007730 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007731 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7732 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7733 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7734 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7735 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7737 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7739 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7740
7741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7742 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7743 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7744 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7745 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7747 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7748
7749 // Move.
7750 x2 += 20; y2 -= 25;
7751 processPosition(mapper, x2, y2);
7752 processId(mapper, 2);
7753 processMTSync(mapper);
7754 processSync(mapper);
7755
7756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7757 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7758 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7759 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7760 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7762 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7763
7764 // New finger down.
7765 int32_t x3 = 700, y3 = 300;
7766 processPosition(mapper, x2, y2);
7767 processId(mapper, 2);
7768 processMTSync(mapper);
7769 processPosition(mapper, x3, y3);
7770 processId(mapper, 3);
7771 processMTSync(mapper);
7772 processSync(mapper);
7773
7774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007775 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007776 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7777 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7778 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7779 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7782 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7784 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7785
7786 // Second finger up.
7787 x3 += 30; y3 -= 20;
7788 processPosition(mapper, x3, y3);
7789 processId(mapper, 3);
7790 processMTSync(mapper);
7791 processSync(mapper);
7792
7793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007794 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007795 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7796 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7797 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7798 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7799 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7800 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7801 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7803 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7804
7805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7806 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7807 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7808 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7811 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7812
7813 // Last finger up.
7814 processMTSync(mapper);
7815 processSync(mapper);
7816
7817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7818 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7819 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7820 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7821 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7823 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7824
7825 // Should not have sent any more keys or motions.
7826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7828}
7829
7830TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007831 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007832 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007833 prepareAxes(POSITION | ID | SLOT);
7834 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007835 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007836
arthurhungdcef2dc2020-08-11 14:47:50 +08007837 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007838
7839 NotifyMotionArgs motionArgs;
7840
7841 // Two fingers down at once.
7842 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7843 processPosition(mapper, x1, y1);
7844 processId(mapper, 1);
7845 processSlot(mapper, 1);
7846 processPosition(mapper, x2, y2);
7847 processId(mapper, 2);
7848 processSync(mapper);
7849
7850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7851 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7852 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7853 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7854 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7856 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7857
7858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007859 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007860 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7861 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7862 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7863 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7864 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7865 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7866 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7867 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7868 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7869
7870 // Move.
7871 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7872 processSlot(mapper, 0);
7873 processPosition(mapper, x1, y1);
7874 processSlot(mapper, 1);
7875 processPosition(mapper, x2, y2);
7876 processSync(mapper);
7877
7878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7879 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7880 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7881 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7882 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7883 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7884 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7886 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7888 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7889
7890 // First finger up.
7891 x2 += 15; y2 -= 20;
7892 processSlot(mapper, 0);
7893 processId(mapper, -1);
7894 processSlot(mapper, 1);
7895 processPosition(mapper, x2, y2);
7896 processSync(mapper);
7897
7898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007899 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007900 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7901 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7902 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7903 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7904 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7906 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7908 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7909
7910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7911 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7912 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7913 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7914 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7916 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7917
7918 // Move.
7919 x2 += 20; y2 -= 25;
7920 processPosition(mapper, x2, y2);
7921 processSync(mapper);
7922
7923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7925 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7926 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7927 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7929 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7930
7931 // New finger down.
7932 int32_t x3 = 700, y3 = 300;
7933 processPosition(mapper, x2, y2);
7934 processSlot(mapper, 0);
7935 processId(mapper, 3);
7936 processPosition(mapper, x3, y3);
7937 processSync(mapper);
7938
7939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007940 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007941 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7942 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7943 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7944 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7945 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7947 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7949 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7950
7951 // Second finger up.
7952 x3 += 30; y3 -= 20;
7953 processSlot(mapper, 1);
7954 processId(mapper, -1);
7955 processSlot(mapper, 0);
7956 processPosition(mapper, x3, y3);
7957 processSync(mapper);
7958
7959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007960 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007961 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7962 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7963 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7964 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7965 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7967 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7969 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7970
7971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7972 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7973 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7974 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7975 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7976 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7977 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7978
7979 // Last finger up.
7980 processId(mapper, -1);
7981 processSync(mapper);
7982
7983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7984 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7985 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7986 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7987 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7988 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7989 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7990
7991 // Should not have sent any more keys or motions.
7992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7994}
7995
7996TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007997 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007998 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007999 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008000 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008001
8002 // These calculations are based on the input device calibration documentation.
8003 int32_t rawX = 100;
8004 int32_t rawY = 200;
8005 int32_t rawTouchMajor = 7;
8006 int32_t rawTouchMinor = 6;
8007 int32_t rawToolMajor = 9;
8008 int32_t rawToolMinor = 8;
8009 int32_t rawPressure = 11;
8010 int32_t rawDistance = 0;
8011 int32_t rawOrientation = 3;
8012 int32_t id = 5;
8013
8014 float x = toDisplayX(rawX);
8015 float y = toDisplayY(rawY);
8016 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8017 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8018 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8019 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8020 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8021 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8022 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8023 float distance = float(rawDistance);
8024
8025 processPosition(mapper, rawX, rawY);
8026 processTouchMajor(mapper, rawTouchMajor);
8027 processTouchMinor(mapper, rawTouchMinor);
8028 processToolMajor(mapper, rawToolMajor);
8029 processToolMinor(mapper, rawToolMinor);
8030 processPressure(mapper, rawPressure);
8031 processOrientation(mapper, rawOrientation);
8032 processDistance(mapper, rawDistance);
8033 processId(mapper, id);
8034 processMTSync(mapper);
8035 processSync(mapper);
8036
8037 NotifyMotionArgs args;
8038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8039 ASSERT_EQ(0, args.pointerProperties[0].id);
8040 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8041 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8042 orientation, distance));
8043}
8044
8045TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008046 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008047 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008048 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8049 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008050 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008051
8052 // These calculations are based on the input device calibration documentation.
8053 int32_t rawX = 100;
8054 int32_t rawY = 200;
8055 int32_t rawTouchMajor = 140;
8056 int32_t rawTouchMinor = 120;
8057 int32_t rawToolMajor = 180;
8058 int32_t rawToolMinor = 160;
8059
8060 float x = toDisplayX(rawX);
8061 float y = toDisplayY(rawY);
8062 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8063 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8064 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8065 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8066 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8067
8068 processPosition(mapper, rawX, rawY);
8069 processTouchMajor(mapper, rawTouchMajor);
8070 processTouchMinor(mapper, rawTouchMinor);
8071 processToolMajor(mapper, rawToolMajor);
8072 processToolMinor(mapper, rawToolMinor);
8073 processMTSync(mapper);
8074 processSync(mapper);
8075
8076 NotifyMotionArgs args;
8077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8078 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8079 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8080}
8081
8082TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008083 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008084 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008085 prepareAxes(POSITION | TOUCH | TOOL);
8086 addConfigurationProperty("touch.size.calibration", "diameter");
8087 addConfigurationProperty("touch.size.scale", "10");
8088 addConfigurationProperty("touch.size.bias", "160");
8089 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008090 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008091
8092 // These calculations are based on the input device calibration documentation.
8093 // Note: We only provide a single common touch/tool value because the device is assumed
8094 // not to emit separate values for each pointer (isSummed = 1).
8095 int32_t rawX = 100;
8096 int32_t rawY = 200;
8097 int32_t rawX2 = 150;
8098 int32_t rawY2 = 250;
8099 int32_t rawTouchMajor = 5;
8100 int32_t rawToolMajor = 8;
8101
8102 float x = toDisplayX(rawX);
8103 float y = toDisplayY(rawY);
8104 float x2 = toDisplayX(rawX2);
8105 float y2 = toDisplayY(rawY2);
8106 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8107 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8108 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8109
8110 processPosition(mapper, rawX, rawY);
8111 processTouchMajor(mapper, rawTouchMajor);
8112 processToolMajor(mapper, rawToolMajor);
8113 processMTSync(mapper);
8114 processPosition(mapper, rawX2, rawY2);
8115 processTouchMajor(mapper, rawTouchMajor);
8116 processToolMajor(mapper, rawToolMajor);
8117 processMTSync(mapper);
8118 processSync(mapper);
8119
8120 NotifyMotionArgs args;
8121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8122 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8123
8124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008125 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008126 ASSERT_EQ(size_t(2), args.pointerCount);
8127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8128 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8129 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8130 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8131}
8132
8133TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008134 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008135 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008136 prepareAxes(POSITION | TOUCH | TOOL);
8137 addConfigurationProperty("touch.size.calibration", "area");
8138 addConfigurationProperty("touch.size.scale", "43");
8139 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008140 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008141
8142 // These calculations are based on the input device calibration documentation.
8143 int32_t rawX = 100;
8144 int32_t rawY = 200;
8145 int32_t rawTouchMajor = 5;
8146 int32_t rawToolMajor = 8;
8147
8148 float x = toDisplayX(rawX);
8149 float y = toDisplayY(rawY);
8150 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8151 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8152 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8153
8154 processPosition(mapper, rawX, rawY);
8155 processTouchMajor(mapper, rawTouchMajor);
8156 processToolMajor(mapper, rawToolMajor);
8157 processMTSync(mapper);
8158 processSync(mapper);
8159
8160 NotifyMotionArgs args;
8161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8162 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8163 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8164}
8165
8166TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008167 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008168 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008169 prepareAxes(POSITION | PRESSURE);
8170 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8171 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008172 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008173
Michael Wrightaa449c92017-12-13 21:21:43 +00008174 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008175 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008176 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8177 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8178 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8179
Michael Wrightd02c5b62014-02-10 15:10:22 -08008180 // These calculations are based on the input device calibration documentation.
8181 int32_t rawX = 100;
8182 int32_t rawY = 200;
8183 int32_t rawPressure = 60;
8184
8185 float x = toDisplayX(rawX);
8186 float y = toDisplayY(rawY);
8187 float pressure = float(rawPressure) * 0.01f;
8188
8189 processPosition(mapper, rawX, rawY);
8190 processPressure(mapper, rawPressure);
8191 processMTSync(mapper);
8192 processSync(mapper);
8193
8194 NotifyMotionArgs args;
8195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8196 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8197 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8198}
8199
8200TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008201 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008202 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008203 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008204 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008205
8206 NotifyMotionArgs motionArgs;
8207 NotifyKeyArgs keyArgs;
8208
8209 processId(mapper, 1);
8210 processPosition(mapper, 100, 200);
8211 processSync(mapper);
8212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8213 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8214 ASSERT_EQ(0, motionArgs.buttonState);
8215
8216 // press BTN_LEFT, release BTN_LEFT
8217 processKey(mapper, BTN_LEFT, 1);
8218 processSync(mapper);
8219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8221 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8222
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8224 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8225 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8226
Michael Wrightd02c5b62014-02-10 15:10:22 -08008227 processKey(mapper, BTN_LEFT, 0);
8228 processSync(mapper);
8229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008230 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008231 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008232
8233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008235 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008236
8237 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8238 processKey(mapper, BTN_RIGHT, 1);
8239 processKey(mapper, BTN_MIDDLE, 1);
8240 processSync(mapper);
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8243 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8244 motionArgs.buttonState);
8245
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8247 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8248 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8249
8250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8251 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8252 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8253 motionArgs.buttonState);
8254
Michael Wrightd02c5b62014-02-10 15:10:22 -08008255 processKey(mapper, BTN_RIGHT, 0);
8256 processSync(mapper);
8257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008258 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008259 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008260
8261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008262 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008263 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008264
8265 processKey(mapper, BTN_MIDDLE, 0);
8266 processSync(mapper);
8267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008268 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008269 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008270
8271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008272 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008273 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008274
8275 // press BTN_BACK, release BTN_BACK
8276 processKey(mapper, BTN_BACK, 1);
8277 processSync(mapper);
8278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8279 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8280 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008281
Michael Wrightd02c5b62014-02-10 15:10:22 -08008282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008284 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8285
8286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8287 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8288 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008289
8290 processKey(mapper, BTN_BACK, 0);
8291 processSync(mapper);
8292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008293 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008294 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008295
8296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008298 ASSERT_EQ(0, motionArgs.buttonState);
8299
Michael Wrightd02c5b62014-02-10 15:10:22 -08008300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8301 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8302 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8303
8304 // press BTN_SIDE, release BTN_SIDE
8305 processKey(mapper, BTN_SIDE, 1);
8306 processSync(mapper);
8307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8308 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8309 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008310
Michael Wrightd02c5b62014-02-10 15:10:22 -08008311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008312 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008313 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8314
8315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8316 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8317 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008318
8319 processKey(mapper, BTN_SIDE, 0);
8320 processSync(mapper);
8321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008322 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008323 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008324
8325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008327 ASSERT_EQ(0, motionArgs.buttonState);
8328
Michael Wrightd02c5b62014-02-10 15:10:22 -08008329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8330 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8331 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8332
8333 // press BTN_FORWARD, release BTN_FORWARD
8334 processKey(mapper, BTN_FORWARD, 1);
8335 processSync(mapper);
8336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8337 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8338 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008339
Michael Wrightd02c5b62014-02-10 15:10:22 -08008340 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(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8343
8344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8345 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8346 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008347
8348 processKey(mapper, BTN_FORWARD, 0);
8349 processSync(mapper);
8350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008351 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008352 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008353
8354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008355 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008356 ASSERT_EQ(0, motionArgs.buttonState);
8357
Michael Wrightd02c5b62014-02-10 15:10:22 -08008358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8359 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8360 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8361
8362 // press BTN_EXTRA, release BTN_EXTRA
8363 processKey(mapper, BTN_EXTRA, 1);
8364 processSync(mapper);
8365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8366 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8367 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008368
Michael Wrightd02c5b62014-02-10 15:10:22 -08008369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008370 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008371 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8372
8373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8374 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8375 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008376
8377 processKey(mapper, BTN_EXTRA, 0);
8378 processSync(mapper);
8379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008380 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008381 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008382
8383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008385 ASSERT_EQ(0, motionArgs.buttonState);
8386
Michael Wrightd02c5b62014-02-10 15:10:22 -08008387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8388 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8389 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8390
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8392
Michael Wrightd02c5b62014-02-10 15:10:22 -08008393 // press BTN_STYLUS, release BTN_STYLUS
8394 processKey(mapper, BTN_STYLUS, 1);
8395 processSync(mapper);
8396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008398 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8399
8400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8401 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8402 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008403
8404 processKey(mapper, BTN_STYLUS, 0);
8405 processSync(mapper);
8406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008407 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008408 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008409
8410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008412 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008413
8414 // press BTN_STYLUS2, release BTN_STYLUS2
8415 processKey(mapper, BTN_STYLUS2, 1);
8416 processSync(mapper);
8417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008419 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8420
8421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8422 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8423 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008424
8425 processKey(mapper, BTN_STYLUS2, 0);
8426 processSync(mapper);
8427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008428 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008429 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008430
8431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008432 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008433 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008434
8435 // release touch
8436 processId(mapper, -1);
8437 processSync(mapper);
8438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8439 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8440 ASSERT_EQ(0, motionArgs.buttonState);
8441}
8442
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008443TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8444 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008445 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008446 prepareAxes(POSITION | ID | SLOT);
8447 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8448
8449 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8450 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8451
8452 // Touch down.
8453 processId(mapper, 1);
8454 processPosition(mapper, 100, 200);
8455 processSync(mapper);
8456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8457 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8458
8459 // Press and release button mapped to the primary stylus button.
8460 processKey(mapper, BTN_A, 1);
8461 processSync(mapper);
8462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8463 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8464 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8466 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8467 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8468
8469 processKey(mapper, BTN_A, 0);
8470 processSync(mapper);
8471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8472 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8474 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8475
8476 // Press and release the HID usage mapped to the secondary stylus button.
8477 processHidUsage(mapper, 0xabcd, 1);
8478 processSync(mapper);
8479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8480 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8481 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8483 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8484 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8485
8486 processHidUsage(mapper, 0xabcd, 0);
8487 processSync(mapper);
8488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8489 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8491 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8492
8493 // Release touch.
8494 processId(mapper, -1);
8495 processSync(mapper);
8496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8497 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8498}
8499
Michael Wrightd02c5b62014-02-10 15:10:22 -08008500TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008501 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008502 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008503 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008504 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008505
8506 NotifyMotionArgs motionArgs;
8507
8508 // default tool type is finger
8509 processId(mapper, 1);
8510 processPosition(mapper, 100, 200);
8511 processSync(mapper);
8512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8513 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8515
8516 // eraser
8517 processKey(mapper, BTN_TOOL_RUBBER, 1);
8518 processSync(mapper);
8519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8521 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8522
8523 // stylus
8524 processKey(mapper, BTN_TOOL_RUBBER, 0);
8525 processKey(mapper, BTN_TOOL_PEN, 1);
8526 processSync(mapper);
8527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8529 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8530
8531 // brush
8532 processKey(mapper, BTN_TOOL_PEN, 0);
8533 processKey(mapper, BTN_TOOL_BRUSH, 1);
8534 processSync(mapper);
8535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8536 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8538
8539 // pencil
8540 processKey(mapper, BTN_TOOL_BRUSH, 0);
8541 processKey(mapper, BTN_TOOL_PENCIL, 1);
8542 processSync(mapper);
8543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8544 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8545 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8546
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008547 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008548 processKey(mapper, BTN_TOOL_PENCIL, 0);
8549 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8550 processSync(mapper);
8551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8554
8555 // mouse
8556 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8557 processKey(mapper, BTN_TOOL_MOUSE, 1);
8558 processSync(mapper);
8559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8560 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8561 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8562
8563 // lens
8564 processKey(mapper, BTN_TOOL_MOUSE, 0);
8565 processKey(mapper, BTN_TOOL_LENS, 1);
8566 processSync(mapper);
8567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8568 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8569 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8570
8571 // double-tap
8572 processKey(mapper, BTN_TOOL_LENS, 0);
8573 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8574 processSync(mapper);
8575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8576 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8577 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8578
8579 // triple-tap
8580 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8581 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8582 processSync(mapper);
8583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8584 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8585 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8586
8587 // quad-tap
8588 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8589 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8590 processSync(mapper);
8591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8592 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8593 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8594
8595 // finger
8596 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8597 processKey(mapper, BTN_TOOL_FINGER, 1);
8598 processSync(mapper);
8599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8601 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8602
8603 // stylus trumps finger
8604 processKey(mapper, BTN_TOOL_PEN, 1);
8605 processSync(mapper);
8606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8607 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8608 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8609
8610 // eraser trumps stylus
8611 processKey(mapper, BTN_TOOL_RUBBER, 1);
8612 processSync(mapper);
8613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8615 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8616
8617 // mouse trumps eraser
8618 processKey(mapper, BTN_TOOL_MOUSE, 1);
8619 processSync(mapper);
8620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8622 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8623
8624 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8625 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8626 processSync(mapper);
8627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8628 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8630
8631 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8632 processToolType(mapper, MT_TOOL_PEN);
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 // back to default tool type
8639 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8640 processKey(mapper, BTN_TOOL_MOUSE, 0);
8641 processKey(mapper, BTN_TOOL_RUBBER, 0);
8642 processKey(mapper, BTN_TOOL_PEN, 0);
8643 processKey(mapper, BTN_TOOL_FINGER, 0);
8644 processSync(mapper);
8645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8647 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8648}
8649
8650TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008651 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008652 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008653 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008654 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008655 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008656
8657 NotifyMotionArgs motionArgs;
8658
8659 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8660 processId(mapper, 1);
8661 processPosition(mapper, 100, 200);
8662 processSync(mapper);
8663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8664 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8666 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8667
8668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8669 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8671 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8672
8673 // move a little
8674 processPosition(mapper, 150, 250);
8675 processSync(mapper);
8676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8677 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8679 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8680
8681 // down when BTN_TOUCH is pressed, pressure defaults to 1
8682 processKey(mapper, BTN_TOUCH, 1);
8683 processSync(mapper);
8684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8685 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8686 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8687 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8688
8689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8690 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8692 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8693
8694 // up when BTN_TOUCH is released, hover restored
8695 processKey(mapper, BTN_TOUCH, 0);
8696 processSync(mapper);
8697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8698 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8700 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8701
8702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8703 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8705 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8706
8707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8708 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8710 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8711
8712 // exit hover when pointer goes away
8713 processId(mapper, -1);
8714 processSync(mapper);
8715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8716 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8718 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8719}
8720
8721TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008722 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008723 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008724 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008725 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008726
8727 NotifyMotionArgs motionArgs;
8728
8729 // initially hovering because pressure is 0
8730 processId(mapper, 1);
8731 processPosition(mapper, 100, 200);
8732 processPressure(mapper, 0);
8733 processSync(mapper);
8734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8735 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8737 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8738
8739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8740 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8742 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8743
8744 // move a little
8745 processPosition(mapper, 150, 250);
8746 processSync(mapper);
8747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8748 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8750 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8751
8752 // down when pressure becomes non-zero
8753 processPressure(mapper, RAW_PRESSURE_MAX);
8754 processSync(mapper);
8755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8756 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8758 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8759
8760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8761 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8763 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8764
8765 // up when pressure becomes 0, hover restored
8766 processPressure(mapper, 0);
8767 processSync(mapper);
8768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8769 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8771 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8772
8773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8774 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8776 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8777
8778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8779 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8781 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8782
8783 // exit hover when pointer goes away
8784 processId(mapper, -1);
8785 processSync(mapper);
8786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8787 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8789 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8790}
8791
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008792/**
8793 * Set the input device port <--> display port associations, and check that the
8794 * events are routed to the display that matches the display port.
8795 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8796 */
8797TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008798 const std::string usb2 = "USB2";
8799 const uint8_t hdmi1 = 0;
8800 const uint8_t hdmi2 = 1;
8801 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008802 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008803
8804 addConfigurationProperty("touch.deviceType", "touchScreen");
8805 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008806 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008807
8808 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8809 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8810
8811 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8812 // for this input device is specified, and the matching viewport is not present,
8813 // the input device should be disabled (at the mapper level).
8814
8815 // Add viewport for display 2 on hdmi2
8816 prepareSecondaryDisplay(type, hdmi2);
8817 // Send a touch event
8818 processPosition(mapper, 100, 100);
8819 processSync(mapper);
8820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8821
8822 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008823 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008824 // Send a touch event again
8825 processPosition(mapper, 100, 100);
8826 processSync(mapper);
8827
8828 NotifyMotionArgs args;
8829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8830 ASSERT_EQ(DISPLAY_ID, args.displayId);
8831}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008832
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008833TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8834 addConfigurationProperty("touch.deviceType", "touchScreen");
8835 prepareAxes(POSITION);
8836 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8837
8838 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8839
Michael Wrighta9cf4192022-12-01 23:46:39 +00008840 prepareDisplay(ui::ROTATION_0);
8841 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008842
8843 // Send a touch event
8844 processPosition(mapper, 100, 100);
8845 processSync(mapper);
8846
8847 NotifyMotionArgs args;
8848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8849 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8850}
8851
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008852TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008853 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008854 std::shared_ptr<FakePointerController> fakePointerController =
8855 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008856 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008857 fakePointerController->setPosition(100, 200);
8858 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008859 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008860
Garfield Tan888a6a42020-01-09 11:39:16 -08008861 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008862 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008863
Michael Wrighta9cf4192022-12-01 23:46:39 +00008864 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008865 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008866 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008867
Harry Cutts16a24cc2022-10-26 15:22:19 +00008868 // Check source is a touchpad that would obtain the PointerController.
8869 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008870
8871 NotifyMotionArgs motionArgs;
8872 processPosition(mapper, 100, 100);
8873 processSync(mapper);
8874
8875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8876 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8877 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8878}
8879
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008880/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008881 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8882 */
8883TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8884 addConfigurationProperty("touch.deviceType", "touchScreen");
8885 prepareAxes(POSITION);
8886 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8887
Michael Wrighta9cf4192022-12-01 23:46:39 +00008888 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008889 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8890 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8891 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8892 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8893
8894 NotifyMotionArgs args;
8895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8896 ASSERT_EQ(26, args.readTime);
8897
8898 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8899 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8900 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8901
8902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8903 ASSERT_EQ(33, args.readTime);
8904}
8905
8906/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008907 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8908 * events should not be delivered to the listener.
8909 */
8910TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8911 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008912 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008913 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8914 false /*isActive*/, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008915 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8916 prepareAxes(POSITION);
8917 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8918
8919 NotifyMotionArgs motionArgs;
8920 processPosition(mapper, 100, 100);
8921 processSync(mapper);
8922
8923 mFakeListener->assertNotifyMotionWasNotCalled();
8924}
8925
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008926/**
8927 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8928 * the touch mapper can process the events and the events can be delivered to the listener.
8929 */
8930TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8931 addConfigurationProperty("touch.deviceType", "touchScreen");
8932 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008933 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8934 false /*isActive*/, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008935 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8936 prepareAxes(POSITION);
8937 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8938
8939 NotifyMotionArgs motionArgs;
8940 processPosition(mapper, 100, 100);
8941 processSync(mapper);
8942
8943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8944 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8945}
8946
Garfield Tanc734e4f2021-01-15 20:01:39 -08008947TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8948 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008949 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008950 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8951 true /*isActive*/, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008952 std::optional<DisplayViewport> optionalDisplayViewport =
8953 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8954 ASSERT_TRUE(optionalDisplayViewport.has_value());
8955 DisplayViewport displayViewport = *optionalDisplayViewport;
8956
8957 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8958 prepareAxes(POSITION);
8959 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8960
8961 // Finger down
8962 int32_t x = 100, y = 100;
8963 processPosition(mapper, x, y);
8964 processSync(mapper);
8965
8966 NotifyMotionArgs motionArgs;
8967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8968 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8969
8970 // Deactivate display viewport
8971 displayViewport.isActive = false;
8972 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8973 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8974
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008975 // The ongoing touch should be canceled immediately
8976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8977 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8978
8979 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008980 x += 10, y += 10;
8981 processPosition(mapper, x, y);
8982 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008984
8985 // Reactivate display viewport
8986 displayViewport.isActive = true;
8987 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8988 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8989
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008990 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008991 x += 10, y += 10;
8992 processPosition(mapper, x, y);
8993 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8995 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008996}
8997
Arthur Hung7c645402019-01-25 17:45:42 +08008998TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8999 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009000 prepareAxes(POSITION | ID | SLOT);
9001 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009002 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009003
9004 // Create the second touch screen device, and enable multi fingers.
9005 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009006 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009007 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009008 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009009 std::shared_ptr<InputDevice> device2 =
9010 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009011 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009012
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009013 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9014 0 /*flat*/, 0 /*fuzz*/);
9015 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9016 0 /*flat*/, 0 /*fuzz*/);
9017 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
9018 0 /*flat*/, 0 /*fuzz*/);
9019 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
9020 0 /*flat*/, 0 /*fuzz*/);
9021 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
9022 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9023 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009024
9025 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009026 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009027 std::list<NotifyArgs> unused =
9028 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9029 0 /*changes*/);
9030 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009031
9032 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009033 std::shared_ptr<FakePointerController> fakePointerController =
9034 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009035 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009036
9037 // Setup policy for associated displays and show touches.
9038 const uint8_t hdmi1 = 0;
9039 const uint8_t hdmi2 = 1;
9040 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9041 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9042 mFakePolicy->setShowTouches(true);
9043
9044 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009045 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009046 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009047
9048 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009049 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9050 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9051 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009052
9053 // Two fingers down at default display.
9054 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9055 processPosition(mapper, x1, y1);
9056 processId(mapper, 1);
9057 processSlot(mapper, 1);
9058 processPosition(mapper, x2, y2);
9059 processId(mapper, 2);
9060 processSync(mapper);
9061
9062 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9063 fakePointerController->getSpots().find(DISPLAY_ID);
9064 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9065 ASSERT_EQ(size_t(2), iter->second.size());
9066
9067 // Two fingers down at second display.
9068 processPosition(mapper2, x1, y1);
9069 processId(mapper2, 1);
9070 processSlot(mapper2, 1);
9071 processPosition(mapper2, x2, y2);
9072 processId(mapper2, 2);
9073 processSync(mapper2);
9074
9075 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9076 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9077 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009078
9079 // Disable the show touches configuration and ensure the spots are cleared.
9080 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009081 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9082 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009083
9084 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009085}
9086
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009087TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009088 prepareAxes(POSITION);
9089 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009090 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009091 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009092
9093 NotifyMotionArgs motionArgs;
9094 // Unrotated video frame
9095 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9096 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009097 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009098 processPosition(mapper, 100, 200);
9099 processSync(mapper);
9100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9101 ASSERT_EQ(frames, motionArgs.videoFrames);
9102
9103 // Subsequent touch events should not have any videoframes
9104 // This is implemented separately in FakeEventHub,
9105 // but that should match the behaviour of TouchVideoDevice.
9106 processPosition(mapper, 200, 200);
9107 processSync(mapper);
9108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9109 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9110}
9111
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009112TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009113 prepareAxes(POSITION);
9114 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009115 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009116 // Unrotated video frame
9117 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9118 NotifyMotionArgs motionArgs;
9119
9120 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009121 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009122 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9123 clearViewports();
9124 prepareDisplay(orientation);
9125 std::vector<TouchVideoFrame> frames{frame};
9126 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9127 processPosition(mapper, 100, 200);
9128 processSync(mapper);
9129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9130 ASSERT_EQ(frames, motionArgs.videoFrames);
9131 }
9132}
9133
9134TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9135 prepareAxes(POSITION);
9136 addConfigurationProperty("touch.deviceType", "touchScreen");
9137 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9138 // orientation-aware are affected by display rotation.
9139 addConfigurationProperty("touch.orientationAware", "0");
9140 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9141 // Unrotated video frame
9142 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9143 NotifyMotionArgs motionArgs;
9144
9145 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009146 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009147 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9148 clearViewports();
9149 prepareDisplay(orientation);
9150 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009151 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009152 processPosition(mapper, 100, 200);
9153 processSync(mapper);
9154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009155 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9156 // compared to the display. This is so that when the window transform (which contains the
9157 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9158 // window's coordinate space.
9159 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009160 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009161
9162 // Release finger.
9163 processSync(mapper);
9164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009165 }
9166}
9167
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009168TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009169 prepareAxes(POSITION);
9170 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009171 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009172 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9173 // so mix these.
9174 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9175 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9176 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9177 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9178 NotifyMotionArgs motionArgs;
9179
Michael Wrighta9cf4192022-12-01 23:46:39 +00009180 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009181 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009182 processPosition(mapper, 100, 200);
9183 processSync(mapper);
9184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009185 ASSERT_EQ(frames, motionArgs.videoFrames);
9186}
9187
9188TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9189 prepareAxes(POSITION);
9190 addConfigurationProperty("touch.deviceType", "touchScreen");
9191 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9192 // orientation-aware are affected by display rotation.
9193 addConfigurationProperty("touch.orientationAware", "0");
9194 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9195 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9196 // so mix these.
9197 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9198 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9199 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9200 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9201 NotifyMotionArgs motionArgs;
9202
Michael Wrighta9cf4192022-12-01 23:46:39 +00009203 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009204 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9205 processPosition(mapper, 100, 200);
9206 processSync(mapper);
9207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9208 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9209 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9210 // compared to the display. This is so that when the window transform (which contains the
9211 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9212 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009213 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009214 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009215 ASSERT_EQ(frames, motionArgs.videoFrames);
9216}
9217
Arthur Hung9da14732019-09-02 16:16:58 +08009218/**
9219 * If we had defined port associations, but the viewport is not ready, the touch device would be
9220 * expected to be disabled, and it should be enabled after the viewport has found.
9221 */
9222TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009223 constexpr uint8_t hdmi2 = 1;
9224 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009225 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009226
9227 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9228
9229 addConfigurationProperty("touch.deviceType", "touchScreen");
9230 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009231 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009232
9233 ASSERT_EQ(mDevice->isEnabled(), false);
9234
9235 // Add display on hdmi2, the device should be enabled and can receive touch event.
9236 prepareSecondaryDisplay(type, hdmi2);
9237 ASSERT_EQ(mDevice->isEnabled(), true);
9238
9239 // Send a touch event.
9240 processPosition(mapper, 100, 100);
9241 processSync(mapper);
9242
9243 NotifyMotionArgs args;
9244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9245 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9246}
9247
Arthur Hung421eb1c2020-01-16 00:09:42 +08009248TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009249 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009250 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009251 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009252 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009253
9254 NotifyMotionArgs motionArgs;
9255
9256 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9257 // finger down
9258 processId(mapper, 1);
9259 processPosition(mapper, x1, y1);
9260 processSync(mapper);
9261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9262 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9263 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9264
9265 // finger move
9266 processId(mapper, 1);
9267 processPosition(mapper, x2, y2);
9268 processSync(mapper);
9269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9270 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9271 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9272
9273 // finger up.
9274 processId(mapper, -1);
9275 processSync(mapper);
9276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9277 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9278 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9279
9280 // new finger down
9281 processId(mapper, 1);
9282 processPosition(mapper, x3, y3);
9283 processSync(mapper);
9284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9285 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9286 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9287}
9288
9289/**
arthurhungcc7f9802020-04-30 17:55:40 +08009290 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9291 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009292 */
arthurhungcc7f9802020-04-30 17:55:40 +08009293TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009294 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009295 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009296 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009297 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009298
9299 NotifyMotionArgs motionArgs;
9300
9301 // default tool type is finger
9302 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009303 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009304 processPosition(mapper, x1, y1);
9305 processSync(mapper);
9306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9307 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9309
9310 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9311 processToolType(mapper, MT_TOOL_PALM);
9312 processSync(mapper);
9313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9314 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9315
9316 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009317 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009318 processPosition(mapper, x2, y2);
9319 processSync(mapper);
9320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9321
9322 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009323 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009324 processSync(mapper);
9325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9326
9327 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009328 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009329 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009330 processPosition(mapper, x3, y3);
9331 processSync(mapper);
9332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9333 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9334 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9335}
9336
arthurhungbf89a482020-04-17 17:37:55 +08009337/**
arthurhungcc7f9802020-04-30 17:55:40 +08009338 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9339 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009340 */
arthurhungcc7f9802020-04-30 17:55:40 +08009341TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009342 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009343 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009344 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9345 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9346
9347 NotifyMotionArgs motionArgs;
9348
9349 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009350 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9351 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009352 processPosition(mapper, x1, y1);
9353 processSync(mapper);
9354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9355 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9356 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9357
9358 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009359 processSlot(mapper, SECOND_SLOT);
9360 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009361 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009362 processSync(mapper);
9363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009364 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009365 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9366
9367 // If the tool type of the first finger changes to MT_TOOL_PALM,
9368 // we expect to receive ACTION_POINTER_UP with cancel flag.
9369 processSlot(mapper, FIRST_SLOT);
9370 processId(mapper, FIRST_TRACKING_ID);
9371 processToolType(mapper, MT_TOOL_PALM);
9372 processSync(mapper);
9373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009374 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009375 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9376
9377 // The following MOVE events of second finger should be processed.
9378 processSlot(mapper, SECOND_SLOT);
9379 processId(mapper, SECOND_TRACKING_ID);
9380 processPosition(mapper, x2 + 1, y2 + 1);
9381 processSync(mapper);
9382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9384 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9385
9386 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9387 // it. Second finger receive move.
9388 processSlot(mapper, FIRST_SLOT);
9389 processId(mapper, INVALID_TRACKING_ID);
9390 processSync(mapper);
9391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9393 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9394
9395 // Second finger keeps moving.
9396 processSlot(mapper, SECOND_SLOT);
9397 processId(mapper, SECOND_TRACKING_ID);
9398 processPosition(mapper, x2 + 2, y2 + 2);
9399 processSync(mapper);
9400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9402 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9403
9404 // Second finger up.
9405 processId(mapper, INVALID_TRACKING_ID);
9406 processSync(mapper);
9407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9408 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9409 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9410}
9411
9412/**
9413 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9414 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9415 */
9416TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9417 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009418 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009419 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9420 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9421
9422 NotifyMotionArgs motionArgs;
9423
9424 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9425 // First finger down.
9426 processId(mapper, FIRST_TRACKING_ID);
9427 processPosition(mapper, x1, y1);
9428 processSync(mapper);
9429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9430 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9432
9433 // Second finger down.
9434 processSlot(mapper, SECOND_SLOT);
9435 processId(mapper, SECOND_TRACKING_ID);
9436 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009437 processSync(mapper);
9438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009439 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9441
arthurhungcc7f9802020-04-30 17:55:40 +08009442 // If the tool type of the first finger changes to MT_TOOL_PALM,
9443 // we expect to receive ACTION_POINTER_UP with cancel flag.
9444 processSlot(mapper, FIRST_SLOT);
9445 processId(mapper, FIRST_TRACKING_ID);
9446 processToolType(mapper, MT_TOOL_PALM);
9447 processSync(mapper);
9448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009449 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009450 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9451
9452 // Second finger keeps moving.
9453 processSlot(mapper, SECOND_SLOT);
9454 processId(mapper, SECOND_TRACKING_ID);
9455 processPosition(mapper, x2 + 1, y2 + 1);
9456 processSync(mapper);
9457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9459
9460 // second finger becomes palm, receive cancel due to only 1 finger is active.
9461 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009462 processToolType(mapper, MT_TOOL_PALM);
9463 processSync(mapper);
9464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9465 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9466
arthurhungcc7f9802020-04-30 17:55:40 +08009467 // third finger down.
9468 processSlot(mapper, THIRD_SLOT);
9469 processId(mapper, THIRD_TRACKING_ID);
9470 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009471 processPosition(mapper, x3, y3);
9472 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9474 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9475 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009476 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9477
9478 // third finger move
9479 processId(mapper, THIRD_TRACKING_ID);
9480 processPosition(mapper, x3 + 1, y3 + 1);
9481 processSync(mapper);
9482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9483 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9484
9485 // first finger up, third finger receive move.
9486 processSlot(mapper, FIRST_SLOT);
9487 processId(mapper, INVALID_TRACKING_ID);
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 // second finger up, third finger receive move.
9494 processSlot(mapper, SECOND_SLOT);
9495 processId(mapper, INVALID_TRACKING_ID);
9496 processSync(mapper);
9497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9498 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9499 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9500
9501 // third finger up.
9502 processSlot(mapper, THIRD_SLOT);
9503 processId(mapper, INVALID_TRACKING_ID);
9504 processSync(mapper);
9505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9506 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9507 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9508}
9509
9510/**
9511 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9512 * and the active finger could still be allowed to receive the events
9513 */
9514TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9515 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009516 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009517 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9518 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9519
9520 NotifyMotionArgs motionArgs;
9521
9522 // default tool type is finger
9523 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9524 processId(mapper, FIRST_TRACKING_ID);
9525 processPosition(mapper, x1, y1);
9526 processSync(mapper);
9527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9528 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9529 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9530
9531 // Second finger down.
9532 processSlot(mapper, SECOND_SLOT);
9533 processId(mapper, SECOND_TRACKING_ID);
9534 processPosition(mapper, x2, y2);
9535 processSync(mapper);
9536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009537 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009538 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9539
9540 // If the tool type of the second finger changes to MT_TOOL_PALM,
9541 // we expect to receive ACTION_POINTER_UP with cancel flag.
9542 processId(mapper, SECOND_TRACKING_ID);
9543 processToolType(mapper, MT_TOOL_PALM);
9544 processSync(mapper);
9545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009546 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009547 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9548
9549 // The following MOVE event should be processed.
9550 processSlot(mapper, FIRST_SLOT);
9551 processId(mapper, FIRST_TRACKING_ID);
9552 processPosition(mapper, x1 + 1, y1 + 1);
9553 processSync(mapper);
9554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9555 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9556 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9557
9558 // second finger up.
9559 processSlot(mapper, SECOND_SLOT);
9560 processId(mapper, INVALID_TRACKING_ID);
9561 processSync(mapper);
9562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9563 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9564
9565 // first finger keep moving
9566 processSlot(mapper, FIRST_SLOT);
9567 processId(mapper, FIRST_TRACKING_ID);
9568 processPosition(mapper, x1 + 2, y1 + 2);
9569 processSync(mapper);
9570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9572
9573 // first finger up.
9574 processId(mapper, INVALID_TRACKING_ID);
9575 processSync(mapper);
9576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9577 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9578 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009579}
9580
Arthur Hung9ad18942021-06-19 02:04:46 +00009581/**
9582 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9583 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9584 * cause slot be valid again.
9585 */
9586TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9587 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009588 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009589 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9590 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9591
9592 NotifyMotionArgs motionArgs;
9593
9594 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9595 // First finger down.
9596 processId(mapper, FIRST_TRACKING_ID);
9597 processPosition(mapper, x1, y1);
9598 processPressure(mapper, RAW_PRESSURE_MAX);
9599 processSync(mapper);
9600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9601 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9602 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9603
9604 // First finger move.
9605 processId(mapper, FIRST_TRACKING_ID);
9606 processPosition(mapper, x1 + 1, y1 + 1);
9607 processPressure(mapper, RAW_PRESSURE_MAX);
9608 processSync(mapper);
9609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9611 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9612
9613 // Second finger down.
9614 processSlot(mapper, SECOND_SLOT);
9615 processId(mapper, SECOND_TRACKING_ID);
9616 processPosition(mapper, x2, y2);
9617 processPressure(mapper, RAW_PRESSURE_MAX);
9618 processSync(mapper);
9619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009620 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009621 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9622
9623 // second finger up with some unexpected data.
9624 processSlot(mapper, SECOND_SLOT);
9625 processId(mapper, INVALID_TRACKING_ID);
9626 processPosition(mapper, x2, y2);
9627 processSync(mapper);
9628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009629 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009630 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9631
9632 // first finger up with some unexpected data.
9633 processSlot(mapper, FIRST_SLOT);
9634 processId(mapper, INVALID_TRACKING_ID);
9635 processPosition(mapper, x2, y2);
9636 processPressure(mapper, RAW_PRESSURE_MAX);
9637 processSync(mapper);
9638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9639 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9640 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9641}
9642
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009643TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9644 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009645 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009646 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9647 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9648
9649 // First finger down.
9650 processId(mapper, FIRST_TRACKING_ID);
9651 processPosition(mapper, 100, 200);
9652 processPressure(mapper, RAW_PRESSURE_MAX);
9653 processSync(mapper);
9654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9655 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9656
9657 // Second finger down.
9658 processSlot(mapper, SECOND_SLOT);
9659 processId(mapper, SECOND_TRACKING_ID);
9660 processPosition(mapper, 300, 400);
9661 processPressure(mapper, RAW_PRESSURE_MAX);
9662 processSync(mapper);
9663 ASSERT_NO_FATAL_FAILURE(
9664 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9665
9666 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009667 // preserved. Resetting should cancel the ongoing gesture.
9668 resetMapper(mapper, ARBITRARY_TIME);
9669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9670 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009671
9672 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9673 // the existing touch state to generate a down event.
9674 processPosition(mapper, 301, 302);
9675 processSync(mapper);
9676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9677 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9679 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9680
9681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9682}
9683
9684TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9685 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009686 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009687 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9688 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9689
9690 // First finger touches down and releases.
9691 processId(mapper, FIRST_TRACKING_ID);
9692 processPosition(mapper, 100, 200);
9693 processPressure(mapper, RAW_PRESSURE_MAX);
9694 processSync(mapper);
9695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9696 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9697 processId(mapper, INVALID_TRACKING_ID);
9698 processSync(mapper);
9699 ASSERT_NO_FATAL_FAILURE(
9700 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9701
9702 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9703 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009704 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9706
9707 // Send an empty sync frame. Since there are no pointers, no events are generated.
9708 processSync(mapper);
9709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9710}
9711
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009712TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009713 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009714 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009715 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
9716 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009718
9719 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9720 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9721 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9722 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9723 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9724
9725 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009726 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009727 processId(mapper, FIRST_TRACKING_ID);
9728 processToolType(mapper, MT_TOOL_PEN);
9729 processPosition(mapper, 100, 200);
9730 processPressure(mapper, RAW_PRESSURE_MAX);
9731 processSync(mapper);
9732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9733 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9734 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9735 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
9736
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009737 // Now that we know the device supports styluses, ensure that the device is re-configured with
9738 // the stylus source.
9739 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9740 {
9741 const auto& devices = mReader->getInputDevices();
9742 auto deviceInfo =
9743 std::find_if(devices.begin(), devices.end(),
9744 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9745 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9746 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9747 }
9748
9749 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9751
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009752 processId(mapper, INVALID_TRACKING_ID);
9753 processSync(mapper);
9754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9755 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9756 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9757 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009758}
9759
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009760// --- MultiTouchInputMapperTest_ExternalDevice ---
9761
9762class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9763protected:
Chris Yea52ade12020-08-27 16:49:20 -07009764 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009765};
9766
9767/**
9768 * Expect fallback to internal viewport if device is external and external viewport is not present.
9769 */
9770TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9771 prepareAxes(POSITION);
9772 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009773 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009774 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9775
9776 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9777
9778 NotifyMotionArgs motionArgs;
9779
9780 // Expect the event to be sent to the internal viewport,
9781 // because an external viewport is not present.
9782 processPosition(mapper, 100, 100);
9783 processSync(mapper);
9784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9785 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9786
9787 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009788 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009789 processPosition(mapper, 100, 100);
9790 processSync(mapper);
9791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9792 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9793}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009794
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009795TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9796 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9797 std::shared_ptr<FakePointerController> fakePointerController =
9798 std::make_shared<FakePointerController>();
9799 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9800 fakePointerController->setPosition(0, 0);
9801 fakePointerController->setButtonState(0);
9802
9803 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009804 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009805 prepareAxes(POSITION | ID | SLOT);
9806 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9807 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9808 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009809 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009810 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9811
9812 // captured touchpad should be a touchpad source
9813 NotifyDeviceResetArgs resetArgs;
9814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9815 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9816
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009817 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009818
9819 const InputDeviceInfo::MotionRange* relRangeX =
9820 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9821 ASSERT_NE(relRangeX, nullptr);
9822 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9823 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9824 const InputDeviceInfo::MotionRange* relRangeY =
9825 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9826 ASSERT_NE(relRangeY, nullptr);
9827 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9828 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9829
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009830 // run captured pointer tests - note that this is unscaled, so input listener events should be
9831 // identical to what the hardware sends (accounting for any
9832 // calibration).
9833 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009834 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009835 processId(mapper, 1);
9836 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9837 processKey(mapper, BTN_TOUCH, 1);
9838 processSync(mapper);
9839
9840 // expect coord[0] to contain initial location of touch 0
9841 NotifyMotionArgs args;
9842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9843 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9844 ASSERT_EQ(1U, args.pointerCount);
9845 ASSERT_EQ(0, args.pointerProperties[0].id);
9846 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9847 ASSERT_NO_FATAL_FAILURE(
9848 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9849
9850 // FINGER 1 DOWN
9851 processSlot(mapper, 1);
9852 processId(mapper, 2);
9853 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9854 processSync(mapper);
9855
9856 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009858 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009859 ASSERT_EQ(2U, args.pointerCount);
9860 ASSERT_EQ(0, args.pointerProperties[0].id);
9861 ASSERT_EQ(1, args.pointerProperties[1].id);
9862 ASSERT_NO_FATAL_FAILURE(
9863 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9864 ASSERT_NO_FATAL_FAILURE(
9865 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9866
9867 // FINGER 1 MOVE
9868 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9869 processSync(mapper);
9870
9871 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9872 // from move
9873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9875 ASSERT_NO_FATAL_FAILURE(
9876 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9877 ASSERT_NO_FATAL_FAILURE(
9878 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9879
9880 // FINGER 0 MOVE
9881 processSlot(mapper, 0);
9882 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9883 processSync(mapper);
9884
9885 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9888 ASSERT_NO_FATAL_FAILURE(
9889 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9890 ASSERT_NO_FATAL_FAILURE(
9891 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9892
9893 // BUTTON DOWN
9894 processKey(mapper, BTN_LEFT, 1);
9895 processSync(mapper);
9896
9897 // touchinputmapper design sends a move before button press
9898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9899 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9901 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9902
9903 // BUTTON UP
9904 processKey(mapper, BTN_LEFT, 0);
9905 processSync(mapper);
9906
9907 // touchinputmapper design sends a move after button release
9908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9909 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9911 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9912
9913 // FINGER 0 UP
9914 processId(mapper, -1);
9915 processSync(mapper);
9916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9917 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9918
9919 // FINGER 1 MOVE
9920 processSlot(mapper, 1);
9921 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9922 processSync(mapper);
9923
9924 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9926 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9927 ASSERT_EQ(1U, args.pointerCount);
9928 ASSERT_EQ(1, args.pointerProperties[0].id);
9929 ASSERT_NO_FATAL_FAILURE(
9930 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9931
9932 // FINGER 1 UP
9933 processId(mapper, -1);
9934 processKey(mapper, BTN_TOUCH, 0);
9935 processSync(mapper);
9936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9937 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9938
Harry Cutts16a24cc2022-10-26 15:22:19 +00009939 // A non captured touchpad should have a mouse and touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009940 mFakePolicy->setPointerCapture(false);
9941 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Harry Cutts16a24cc2022-10-26 15:22:19 +00009943 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009944}
9945
9946TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9947 std::shared_ptr<FakePointerController> fakePointerController =
9948 std::make_shared<FakePointerController>();
9949 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9950 fakePointerController->setPosition(0, 0);
9951 fakePointerController->setButtonState(0);
9952
9953 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009954 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009955 prepareAxes(POSITION | ID | SLOT);
9956 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9957 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009958 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009959 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9960 // run uncaptured pointer tests - pushes out generic events
9961 // FINGER 0 DOWN
9962 processId(mapper, 3);
9963 processPosition(mapper, 100, 100);
9964 processKey(mapper, BTN_TOUCH, 1);
9965 processSync(mapper);
9966
9967 // start at (100,100), cursor should be at (0,0) * scale
9968 NotifyMotionArgs args;
9969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9970 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9971 ASSERT_NO_FATAL_FAILURE(
9972 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9973
9974 // FINGER 0 MOVE
9975 processPosition(mapper, 200, 200);
9976 processSync(mapper);
9977
9978 // compute scaling to help with touch position checking
9979 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9980 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9981 float scale =
9982 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9983
9984 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9986 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9987 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9988 0, 0, 0, 0, 0, 0, 0));
9989}
9990
9991TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9992 std::shared_ptr<FakePointerController> fakePointerController =
9993 std::make_shared<FakePointerController>();
9994
Michael Wrighta9cf4192022-12-01 23:46:39 +00009995 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009996 prepareAxes(POSITION | ID | SLOT);
9997 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009998 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009999 mFakePolicy->setPointerCapture(false);
10000 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10001
Harry Cutts16a24cc2022-10-26 15:22:19 +000010002 // An uncaptured touchpad should be a pointer device, with additional touchpad source.
10003 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010004
Harry Cutts16a24cc2022-10-26 15:22:19 +000010005 // A captured touchpad should just have a touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010006 mFakePolicy->setPointerCapture(true);
10007 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10008 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10009}
10010
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010011// --- BluetoothMultiTouchInputMapperTest ---
10012
10013class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10014protected:
10015 void SetUp() override {
10016 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10017 }
10018};
10019
10020TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10021 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010022 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010023 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10024 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10025
10026 nsecs_t kernelEventTime = ARBITRARY_TIME;
10027 nsecs_t expectedEventTime = ARBITRARY_TIME;
10028 // Touch down.
10029 processId(mapper, FIRST_TRACKING_ID);
10030 processPosition(mapper, 100, 200);
10031 processPressure(mapper, RAW_PRESSURE_MAX);
10032 processSync(mapper, ARBITRARY_TIME);
10033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10034 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10035
10036 // Process several events that come in quick succession, according to their timestamps.
10037 for (int i = 0; i < 3; i++) {
10038 constexpr static nsecs_t delta = ms2ns(1);
10039 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10040 kernelEventTime += delta;
10041 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10042
10043 processPosition(mapper, 101 + i, 201 + i);
10044 processSync(mapper, kernelEventTime);
10045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10046 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10047 WithEventTime(expectedEventTime))));
10048 }
10049
10050 // Release the touch.
10051 processId(mapper, INVALID_TRACKING_ID);
10052 processPressure(mapper, RAW_PRESSURE_MIN);
10053 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10055 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10056 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10057}
10058
10059// --- MultiTouchPointerModeTest ---
10060
HQ Liue6983c72022-04-19 22:14:56 +000010061class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10062protected:
10063 float mPointerMovementScale;
10064 float mPointerXZoomScale;
10065 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10066 addConfigurationProperty("touch.deviceType", "pointer");
10067 std::shared_ptr<FakePointerController> fakePointerController =
10068 std::make_shared<FakePointerController>();
10069 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10070 fakePointerController->setPosition(0, 0);
10071 fakePointerController->setButtonState(0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010072 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010073
10074 prepareAxes(POSITION);
10075 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10076 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10077 // needs to be disabled, and the pointer gesture needs to be enabled.
10078 mFakePolicy->setPointerCapture(false);
10079 mFakePolicy->setPointerGestureEnabled(true);
10080 mFakePolicy->setPointerController(fakePointerController);
10081
10082 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10083 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10084 mPointerMovementScale =
10085 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10086 mPointerXZoomScale =
10087 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10088 }
10089
10090 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10091 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10092 /*flat*/ 0,
10093 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10094 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10095 /*flat*/ 0,
10096 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10097 }
10098};
10099
10100/**
10101 * Two fingers down on a pointer mode touch pad. The width
10102 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10103 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10104 * be greater than the both value to be freeform gesture, so that after two
10105 * fingers start to move downwards, the gesture should be swipe.
10106 */
10107TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10108 // The min freeform gesture width is 25units/mm x 30mm = 750
10109 // which is greater than fraction of the diagnal length of the touchpad (349).
10110 // Thus, MaxSwipWidth is 750.
10111 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10112 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10113 NotifyMotionArgs motionArgs;
10114
10115 // Two fingers down at once.
10116 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10117 // Pointer's initial position is used the [0,0] coordinate.
10118 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10119
10120 processId(mapper, FIRST_TRACKING_ID);
10121 processPosition(mapper, x1, y1);
10122 processMTSync(mapper);
10123 processId(mapper, SECOND_TRACKING_ID);
10124 processPosition(mapper, x2, y2);
10125 processMTSync(mapper);
10126 processSync(mapper);
10127
10128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10129 ASSERT_EQ(1U, motionArgs.pointerCount);
10130 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10131 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010132 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010133 ASSERT_NO_FATAL_FAILURE(
10134 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10135
10136 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10137 // that there should be 1 pointer.
10138 int32_t movingDistance = 200;
10139 y1 += movingDistance;
10140 y2 += movingDistance;
10141
10142 processId(mapper, FIRST_TRACKING_ID);
10143 processPosition(mapper, x1, y1);
10144 processMTSync(mapper);
10145 processId(mapper, SECOND_TRACKING_ID);
10146 processPosition(mapper, x2, y2);
10147 processMTSync(mapper);
10148 processSync(mapper);
10149
10150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10151 ASSERT_EQ(1U, motionArgs.pointerCount);
10152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10153 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010154 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010155 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10156 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10157 0, 0, 0, 0));
10158}
10159
10160/**
10161 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10162 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10163 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10164 * value to be freeform gesture, so that after two fingers start to move downwards,
10165 * the gesture should be swipe.
10166 */
10167TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10168 // The min freeform gesture width is 5units/mm x 30mm = 150
10169 // which is greater than fraction of the diagnal length of the touchpad (349).
10170 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
10171 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
10172 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10173 NotifyMotionArgs motionArgs;
10174
10175 // Two fingers down at once.
10176 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10177 // Pointer's initial position is used the [0,0] coordinate.
10178 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10179
10180 processId(mapper, FIRST_TRACKING_ID);
10181 processPosition(mapper, x1, y1);
10182 processMTSync(mapper);
10183 processId(mapper, SECOND_TRACKING_ID);
10184 processPosition(mapper, x2, y2);
10185 processMTSync(mapper);
10186 processSync(mapper);
10187
10188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10189 ASSERT_EQ(1U, motionArgs.pointerCount);
10190 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010192 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010193 ASSERT_NO_FATAL_FAILURE(
10194 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10195
10196 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10197 // and there should be 1 pointer.
10198 int32_t movingDistance = 200;
10199 y1 += movingDistance;
10200 y2 += movingDistance;
10201
10202 processId(mapper, FIRST_TRACKING_ID);
10203 processPosition(mapper, x1, y1);
10204 processMTSync(mapper);
10205 processId(mapper, SECOND_TRACKING_ID);
10206 processPosition(mapper, x2, y2);
10207 processMTSync(mapper);
10208 processSync(mapper);
10209
10210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10211 ASSERT_EQ(1U, motionArgs.pointerCount);
10212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010214 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010215 // New coordinate is the scaled relative coordinate from the initial coordinate.
10216 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10217 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10218 0, 0, 0, 0));
10219}
10220
10221/**
10222 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10223 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10224 * freeform gestures after two fingers start to move downwards.
10225 */
10226TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
10227 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10228 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10229
10230 NotifyMotionArgs motionArgs;
10231
10232 // Two fingers down at once. Wider than the max swipe width.
10233 // The gesture is expected to be PRESS, then transformed to FREEFORM
10234 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10235
10236 processId(mapper, FIRST_TRACKING_ID);
10237 processPosition(mapper, x1, y1);
10238 processMTSync(mapper);
10239 processId(mapper, SECOND_TRACKING_ID);
10240 processPosition(mapper, x2, y2);
10241 processMTSync(mapper);
10242 processSync(mapper);
10243
10244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10245 ASSERT_EQ(1U, motionArgs.pointerCount);
10246 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10247 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010248 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010249 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10250 ASSERT_NO_FATAL_FAILURE(
10251 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10252
10253 int32_t movingDistance = 200;
10254
10255 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10256 // then two down events for two pointers.
10257 y1 += movingDistance;
10258 y2 += movingDistance;
10259
10260 processId(mapper, FIRST_TRACKING_ID);
10261 processPosition(mapper, x1, y1);
10262 processMTSync(mapper);
10263 processId(mapper, SECOND_TRACKING_ID);
10264 processPosition(mapper, x2, y2);
10265 processMTSync(mapper);
10266 processSync(mapper);
10267
10268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10269 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10270 ASSERT_EQ(1U, motionArgs.pointerCount);
10271 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10273 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10274 ASSERT_EQ(1U, motionArgs.pointerCount);
10275 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010278 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010279 ASSERT_EQ(2U, motionArgs.pointerCount);
10280 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10281 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010282 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010283 // Two pointers' scaled relative coordinates from their initial centroid.
10284 // Initial y coordinates are 0 as y1 and y2 have the same value.
10285 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10286 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10287 // When pointers move, the new coordinates equal to the initial coordinates plus
10288 // scaled moving distance.
10289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10290 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10291 0, 0, 0, 0));
10292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10293 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10294 0, 0, 0, 0));
10295
10296 // Move two fingers down again, expect one MOVE motion event.
10297 y1 += movingDistance;
10298 y2 += movingDistance;
10299
10300 processId(mapper, FIRST_TRACKING_ID);
10301 processPosition(mapper, x1, y1);
10302 processMTSync(mapper);
10303 processId(mapper, SECOND_TRACKING_ID);
10304 processPosition(mapper, x2, y2);
10305 processMTSync(mapper);
10306 processSync(mapper);
10307
10308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10309 ASSERT_EQ(2U, motionArgs.pointerCount);
10310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10311 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010312 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10314 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10315 0, 0, 0, 0, 0));
10316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10317 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10318 0, 0, 0, 0, 0));
10319}
10320
Harry Cutts39b7ca22022-10-05 15:55:48 +000010321TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
10322 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10323 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10324 NotifyMotionArgs motionArgs;
10325
10326 // Place two fingers down.
10327 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10328
10329 processId(mapper, FIRST_TRACKING_ID);
10330 processPosition(mapper, x1, y1);
10331 processMTSync(mapper);
10332 processId(mapper, SECOND_TRACKING_ID);
10333 processPosition(mapper, x2, y2);
10334 processMTSync(mapper);
10335 processSync(mapper);
10336
10337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10338 ASSERT_EQ(1U, motionArgs.pointerCount);
10339 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10340 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10341 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10342 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10343
10344 // Move the two fingers down and to the left.
10345 int32_t movingDistance = 200;
10346 x1 -= movingDistance;
10347 y1 += movingDistance;
10348 x2 -= movingDistance;
10349 y2 += movingDistance;
10350
10351 processId(mapper, FIRST_TRACKING_ID);
10352 processPosition(mapper, x1, y1);
10353 processMTSync(mapper);
10354 processId(mapper, SECOND_TRACKING_ID);
10355 processPosition(mapper, x2, y2);
10356 processMTSync(mapper);
10357 processSync(mapper);
10358
10359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10360 ASSERT_EQ(1U, motionArgs.pointerCount);
10361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10362 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10363 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10364 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10365}
10366
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010367TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
10368 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10369 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10370 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10372
10373 // Start a stylus gesture.
10374 processKey(mapper, BTN_TOOL_PEN, 1);
10375 processId(mapper, FIRST_TRACKING_ID);
10376 processPosition(mapper, 100, 200);
10377 processSync(mapper);
10378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10379 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10380 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10381 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10382 // TODO(b/257078296): Pointer mode generates extra event.
10383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10384 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10385 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10386 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10388
10389 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10390 // gesture should be disabled.
10391 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10392 viewport->isActive = false;
10393 mFakePolicy->updateViewport(*viewport);
10394 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10396 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10397 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10398 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10399 // TODO(b/257078296): Pointer mode generates extra event.
10400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10401 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10402 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10403 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10405}
10406
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010407// --- JoystickInputMapperTest ---
10408
10409class JoystickInputMapperTest : public InputMapperTest {
10410protected:
10411 static const int32_t RAW_X_MIN;
10412 static const int32_t RAW_X_MAX;
10413 static const int32_t RAW_Y_MIN;
10414 static const int32_t RAW_Y_MAX;
10415
10416 void SetUp() override {
10417 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10418 }
10419 void prepareAxes() {
10420 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10421 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10422 }
10423
10424 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10426 }
10427
10428 void processSync(JoystickInputMapper& mapper) {
10429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10430 }
10431
Michael Wrighta9cf4192022-12-01 23:46:39 +000010432 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010433 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10434 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10435 NO_PORT, ViewportType::VIRTUAL);
10436 }
10437};
10438
10439const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10440const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10441const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10442const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10443
10444TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10445 prepareAxes();
10446 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10447
10448 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10449
Michael Wrighta9cf4192022-12-01 23:46:39 +000010450 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010451
10452 // Send an axis event
10453 processAxis(mapper, ABS_X, 100);
10454 processSync(mapper);
10455
10456 NotifyMotionArgs args;
10457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10458 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10459
10460 // Send another axis event
10461 processAxis(mapper, ABS_Y, 100);
10462 processSync(mapper);
10463
10464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10465 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10466}
10467
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010468// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010469
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010470class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010471protected:
10472 static const char* DEVICE_NAME;
10473 static const char* DEVICE_LOCATION;
10474 static const int32_t DEVICE_ID;
10475 static const int32_t DEVICE_GENERATION;
10476 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010477 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010478 static const int32_t EVENTHUB_ID;
10479
10480 std::shared_ptr<FakeEventHub> mFakeEventHub;
10481 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010482 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010483 std::unique_ptr<InstrumentedInputReader> mReader;
10484 std::shared_ptr<InputDevice> mDevice;
10485
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010486 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010487 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010488 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010489 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010490 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010491 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010492 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10493 }
10494
10495 void SetUp() override { SetUp(DEVICE_CLASSES); }
10496
10497 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010498 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010499 mFakePolicy.clear();
10500 }
10501
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010502 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010503 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10504 mReader->requestRefreshConfiguration(changes);
10505 mReader->loopOnce();
10506 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010507 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010508 }
10509
10510 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10511 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010512 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010513 InputDeviceIdentifier identifier;
10514 identifier.name = name;
10515 identifier.location = location;
10516 std::shared_ptr<InputDevice> device =
10517 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10518 identifier);
10519 mReader->pushNextDevice(device);
10520 mFakeEventHub->addDevice(eventHubId, name, classes);
10521 mReader->loopOnce();
10522 return device;
10523 }
10524
10525 template <class T, typename... Args>
10526 T& addControllerAndConfigure(Args... args) {
10527 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10528
10529 return controller;
10530 }
10531};
10532
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010533const char* PeripheralControllerTest::DEVICE_NAME = "device";
10534const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10535const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10536const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10537const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010538const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10539 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010540const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010541
10542// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010543class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010544protected:
10545 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010546 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010547 }
10548};
10549
10550TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010551 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010552
Harry Cuttsa5b71292022-11-28 12:56:17 +000010553 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10554 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10555 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010556}
10557
10558TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010559 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010560
Harry Cuttsa5b71292022-11-28 12:56:17 +000010561 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10562 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10563 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010564}
10565
10566// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010567class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010568protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010569 void SetUp() override {
10570 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10571 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010572};
10573
Chris Ye85758332021-05-16 23:05:17 -070010574TEST_F(LightControllerTest, MonoLight) {
10575 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010576 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010577 .maxBrightness = 255,
10578 .flags = InputLightClass::BRIGHTNESS,
10579 .path = ""};
10580 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010581
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010582 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010583 InputDeviceInfo info;
10584 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010585 std::vector<InputDeviceLightInfo> lights = info.getLights();
10586 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010587 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10588 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10589
10590 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10591 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10592}
10593
10594TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10595 RawLightInfo infoMono = {.id = 1,
10596 .name = "mono_keyboard_backlight",
10597 .maxBrightness = 255,
10598 .flags = InputLightClass::BRIGHTNESS |
10599 InputLightClass::KEYBOARD_BACKLIGHT,
10600 .path = ""};
10601 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10602
10603 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10604 InputDeviceInfo info;
10605 controller.populateDeviceInfo(&info);
10606 std::vector<InputDeviceLightInfo> lights = info.getLights();
10607 ASSERT_EQ(1U, lights.size());
10608 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10609 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010610
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010611 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10612 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010613}
10614
10615TEST_F(LightControllerTest, RGBLight) {
10616 RawLightInfo infoRed = {.id = 1,
10617 .name = "red",
10618 .maxBrightness = 255,
10619 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10620 .path = ""};
10621 RawLightInfo infoGreen = {.id = 2,
10622 .name = "green",
10623 .maxBrightness = 255,
10624 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10625 .path = ""};
10626 RawLightInfo infoBlue = {.id = 3,
10627 .name = "blue",
10628 .maxBrightness = 255,
10629 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10630 .path = ""};
10631 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10632 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10633 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10634
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010635 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010636 InputDeviceInfo info;
10637 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010638 std::vector<InputDeviceLightInfo> lights = info.getLights();
10639 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010640 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10641 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10642 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10643
10644 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10645 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10646}
10647
10648TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10649 RawLightInfo infoRed = {.id = 1,
10650 .name = "red_keyboard_backlight",
10651 .maxBrightness = 255,
10652 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10653 InputLightClass::KEYBOARD_BACKLIGHT,
10654 .path = ""};
10655 RawLightInfo infoGreen = {.id = 2,
10656 .name = "green_keyboard_backlight",
10657 .maxBrightness = 255,
10658 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10659 InputLightClass::KEYBOARD_BACKLIGHT,
10660 .path = ""};
10661 RawLightInfo infoBlue = {.id = 3,
10662 .name = "blue_keyboard_backlight",
10663 .maxBrightness = 255,
10664 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10665 InputLightClass::KEYBOARD_BACKLIGHT,
10666 .path = ""};
10667 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10668 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10669 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10670
10671 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10672 InputDeviceInfo info;
10673 controller.populateDeviceInfo(&info);
10674 std::vector<InputDeviceLightInfo> lights = info.getLights();
10675 ASSERT_EQ(1U, lights.size());
10676 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10677 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10678 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10679
10680 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10681 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10682}
10683
10684TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10685 RawLightInfo infoRed = {.id = 1,
10686 .name = "red",
10687 .maxBrightness = 255,
10688 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10689 .path = ""};
10690 RawLightInfo infoGreen = {.id = 2,
10691 .name = "green",
10692 .maxBrightness = 255,
10693 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10694 .path = ""};
10695 RawLightInfo infoBlue = {.id = 3,
10696 .name = "blue",
10697 .maxBrightness = 255,
10698 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10699 .path = ""};
10700 RawLightInfo infoGlobal = {.id = 3,
10701 .name = "global_keyboard_backlight",
10702 .maxBrightness = 255,
10703 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10704 InputLightClass::KEYBOARD_BACKLIGHT,
10705 .path = ""};
10706 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10707 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10708 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10709 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10710
10711 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10712 InputDeviceInfo info;
10713 controller.populateDeviceInfo(&info);
10714 std::vector<InputDeviceLightInfo> lights = info.getLights();
10715 ASSERT_EQ(1U, lights.size());
10716 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10717 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10718 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010719
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010720 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10721 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010722}
10723
10724TEST_F(LightControllerTest, MultiColorRGBLight) {
10725 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010726 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010727 .maxBrightness = 255,
10728 .flags = InputLightClass::BRIGHTNESS |
10729 InputLightClass::MULTI_INTENSITY |
10730 InputLightClass::MULTI_INDEX,
10731 .path = ""};
10732
10733 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10734
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010735 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010736 InputDeviceInfo info;
10737 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010738 std::vector<InputDeviceLightInfo> lights = info.getLights();
10739 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010740 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10741 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10742 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10743
10744 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10745 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10746}
10747
10748TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10749 RawLightInfo infoColor = {.id = 1,
10750 .name = "multi_color_keyboard_backlight",
10751 .maxBrightness = 255,
10752 .flags = InputLightClass::BRIGHTNESS |
10753 InputLightClass::MULTI_INTENSITY |
10754 InputLightClass::MULTI_INDEX |
10755 InputLightClass::KEYBOARD_BACKLIGHT,
10756 .path = ""};
10757
10758 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10759
10760 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10761 InputDeviceInfo info;
10762 controller.populateDeviceInfo(&info);
10763 std::vector<InputDeviceLightInfo> lights = info.getLights();
10764 ASSERT_EQ(1U, lights.size());
10765 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10766 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10767 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010768
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010769 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10770 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010771}
10772
10773TEST_F(LightControllerTest, PlayerIdLight) {
10774 RawLightInfo info1 = {.id = 1,
10775 .name = "player1",
10776 .maxBrightness = 255,
10777 .flags = InputLightClass::BRIGHTNESS,
10778 .path = ""};
10779 RawLightInfo info2 = {.id = 2,
10780 .name = "player2",
10781 .maxBrightness = 255,
10782 .flags = InputLightClass::BRIGHTNESS,
10783 .path = ""};
10784 RawLightInfo info3 = {.id = 3,
10785 .name = "player3",
10786 .maxBrightness = 255,
10787 .flags = InputLightClass::BRIGHTNESS,
10788 .path = ""};
10789 RawLightInfo info4 = {.id = 4,
10790 .name = "player4",
10791 .maxBrightness = 255,
10792 .flags = InputLightClass::BRIGHTNESS,
10793 .path = ""};
10794 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10795 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10796 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10797 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10798
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010799 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010800 InputDeviceInfo info;
10801 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010802 std::vector<InputDeviceLightInfo> lights = info.getLights();
10803 ASSERT_EQ(1U, lights.size());
10804 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010805 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10806 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010807
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010808 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10809 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10810 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010811}
10812
Michael Wrightd02c5b62014-02-10 15:10:22 -080010813} // namespace android