blob: 621bad7633ca6407b25426fa1cc159e1d9d429e5 [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 Pradhan484d55a2022-10-14 23:17:16 +00002026// --- ExternalStylusIntegrationTest ---
2027
2028// Verify the behavior of an external stylus. An external stylus can report pressure or button
2029// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2030// ongoing stylus gesture that is being emitted by the touchscreen.
2031using ExternalStylusIntegrationTest = TouchIntegrationTest;
2032
2033TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
2034 const Point centerPoint = mDevice->getCenterPoint();
2035
2036 // Create an external stylus capable of reporting pressure data that
2037 // should be fused with a touch pointer.
2038 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2039 createUinputDevice<UinputExternalStylusWithPressure>();
2040 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2041 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2042 const auto stylusInfo = findDeviceByName(stylus->getName());
2043 ASSERT_TRUE(stylusInfo);
2044
2045 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2046
2047 const auto touchscreenId = mDeviceInfo.getId();
2048
2049 // Set a pressure value on the stylus. It doesn't generate any events.
2050 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2051 stylus->setPressure(100);
2052 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2053
2054 // Start a finger gesture, and ensure it shows up as stylus gesture
2055 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002056 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002057 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002058 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002059 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002060 mDevice->sendSync();
2061 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2062 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002063 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2064 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002065
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002066 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2067 // event with the updated pressure.
2068 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002069 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2070 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002071 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2072 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002073
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002074 // The external stylus did not generate any events.
2075 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2076 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2077}
2078
2079TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
2080 const Point centerPoint = mDevice->getCenterPoint();
2081
2082 // Create an external stylus capable of reporting pressure data that
2083 // should be fused with a touch pointer.
2084 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2085 createUinputDevice<UinputExternalStylusWithPressure>();
2086 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2087 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2088 const auto stylusInfo = findDeviceByName(stylus->getName());
2089 ASSERT_TRUE(stylusInfo);
2090
2091 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2092
2093 const auto touchscreenId = mDeviceInfo.getId();
2094
2095 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2096 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002097 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2098 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002099 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002100 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002101
2102 // Start a finger gesture. The touch device will withhold generating any touches for
2103 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2104 mDevice->sendSlot(FIRST_SLOT);
2105 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2106 mDevice->sendToolType(MT_TOOL_FINGER);
2107 mDevice->sendDown(centerPoint);
2108 auto waitUntil = std::chrono::system_clock::now() +
2109 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002110 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002111 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002112
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002113 // Since the external stylus did not report a pressure value within the timeout,
2114 // it shows up as a finger pointer.
2115 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2116 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2117 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithDeviceId(touchscreenId),
2118 WithPressure(1.f))));
2119
2120 // Change the pressure on the external stylus. Since the pressure was not present at the start
2121 // of the gesture, it is ignored for now.
2122 stylus->setPressure(200);
2123 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2124
2125 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002126 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2127 mDevice->sendSync();
2128 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2129 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002130 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2131
2132 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2133 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2134 mDevice->sendToolType(MT_TOOL_FINGER);
2135 mDevice->sendDown(centerPoint);
2136 mDevice->sendSync();
2137 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2138 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2139 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2140 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2141
2142 // The external stylus did not generate any events.
2143 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2144 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002145}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002146
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002147TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
2148 const Point centerPoint = mDevice->getCenterPoint();
2149
2150 // Create an external stylus device that does not support pressure. It should not affect any
2151 // touch pointers.
2152 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2153 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2154 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2155 const auto stylusInfo = findDeviceByName(stylus->getName());
2156 ASSERT_TRUE(stylusInfo);
2157
2158 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2159
2160 const auto touchscreenId = mDeviceInfo.getId();
2161
2162 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2163 // pressure data from the external stylus.
2164 mDevice->sendSlot(FIRST_SLOT);
2165 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2166 mDevice->sendToolType(MT_TOOL_FINGER);
2167 mDevice->sendDown(centerPoint);
2168 auto waitUntil = std::chrono::system_clock::now() +
2169 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2170 mDevice->sendSync();
2171 ASSERT_NO_FATAL_FAILURE(
2172 mTestListener
2173 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2174 WithToolType(
2175 AMOTION_EVENT_TOOL_TYPE_FINGER),
2176 WithButtonState(0),
2177 WithDeviceId(touchscreenId),
2178 WithPressure(1.f)),
2179 waitUntil));
2180
2181 // The external stylus did not generate any events.
2182 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2183 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2184}
2185
Michael Wrightd02c5b62014-02-10 15:10:22 -08002186// --- InputDeviceTest ---
2187class InputDeviceTest : public testing::Test {
2188protected:
2189 static const char* DEVICE_NAME;
2190 static const char* DEVICE_LOCATION;
2191 static const int32_t DEVICE_ID;
2192 static const int32_t DEVICE_GENERATION;
2193 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002194 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002195 static const int32_t EVENTHUB_ID;
2196 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2197
2198 std::shared_ptr<FakeEventHub> mFakeEventHub;
2199 sp<FakeInputReaderPolicy> mFakePolicy;
2200 std::unique_ptr<TestInputListener> mFakeListener;
2201 std::unique_ptr<InstrumentedInputReader> mReader;
2202 std::shared_ptr<InputDevice> mDevice;
2203
2204 void SetUp() override {
2205 mFakeEventHub = std::make_unique<FakeEventHub>();
2206 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2207 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002208 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002209 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002210 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002211 identifier.name = DEVICE_NAME;
2212 identifier.location = DEVICE_LOCATION;
2213 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2214 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2215 identifier);
2216 mReader->pushNextDevice(mDevice);
2217 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002218 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002219 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002220
2221 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002222 mFakeListener.reset();
2223 mFakePolicy.clear();
2224 }
2225};
2226
2227const char* InputDeviceTest::DEVICE_NAME = "device";
2228const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2229const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2230const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002231const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002232const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2233 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002234const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002235const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2236
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002237TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002238 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002239 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2240 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002241}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002242
Michael Wrightd02c5b62014-02-10 15:10:22 -08002243TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002244 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2245
Michael Wrightd02c5b62014-02-10 15:10:22 -08002246 // Configuration
2247 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002248 InputReaderConfiguration config;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002249 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2250
Michael Wrightd02c5b62014-02-10 15:10:22 -08002251 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2252}
2253
2254TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2255 ASSERT_EQ(mDevice->isEnabled(), false);
2256}
2257
2258TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2259 // Configuration.
2260 InputReaderConfiguration config;
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002261 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002262
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002263 // Reset.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002264 unused += mDevice->reset(ARBITRARY_TIME);
2265
2266 NotifyDeviceResetArgs resetArgs;
2267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2268 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2269 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2270
2271 // Metadata.
2272 ASSERT_TRUE(mDevice->isIgnored());
2273 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2274
2275 InputDeviceInfo info = mDevice->getDeviceInfo();
2276 ASSERT_EQ(DEVICE_ID, info.getId());
2277 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2278 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2279 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2280
2281 // State queries.
2282 ASSERT_EQ(0, mDevice->getMetaState());
2283
2284 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002285 << "Ignored device should return unknown key code state.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002286 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002287 << "Ignored device should return unknown scan code state.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002288 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2289 << "Ignored device should return unknown switch state.";
2290
2291 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2292 uint8_t flags[2] = { 0, 1 };
2293 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002294 << "Ignored device should never mark any key codes.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002295 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2296 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2297}
2298
2299TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2300 // Configuration.
2301 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2302
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002303 FakeInputMapper& mapper1 =
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002304 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002305 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002306 mapper1.setMetaState(AMETA_ALT_ON);
2307 mapper1.addSupportedKeyCode(AKEYCODE_A);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002308 mapper1.addSupportedKeyCode(AKEYCODE_B);
2309 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002310 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2311 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2312 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2313 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002314
Arthur Hung2c9a3342019-07-23 14:18:59 +08002315 FakeInputMapper& mapper2 =
2316 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002317 mapper2.setMetaState(AMETA_SHIFT_ON);
2318
Arthur Hung2c9a3342019-07-23 14:18:59 +08002319 InputReaderConfiguration config;
2320 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2321
2322 std::string propertyValue;
2323 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
2324 << "Device should have read configuration during configuration phase.";
2325 ASSERT_EQ("value", propertyValue);
2326
2327 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002328 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
2329
Arthur Hung2c9a3342019-07-23 14:18:59 +08002330 // Reset
2331 unused += mDevice->reset(ARBITRARY_TIME);
2332 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2333 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
2334
2335 NotifyDeviceResetArgs resetArgs;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2337 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002338 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2339
Arthur Hung2c9a3342019-07-23 14:18:59 +08002340 // Metadata.
2341 ASSERT_FALSE(mDevice->isIgnored());
2342 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2343
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002344 InputDeviceInfo info = mDevice->getDeviceInfo();
2345 ASSERT_EQ(DEVICE_ID, info.getId());
Arthur Hung2c9a3342019-07-23 14:18:59 +08002346 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2347 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2348 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002349
2350 // State queries.
Arthur Hung2c9a3342019-07-23 14:18:59 +08002351 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2352 << "Should query mappers and combine meta states.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002353
Christine Franks1ba71cc2021-04-07 14:37:42 -07002354 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2355 << "Should return unknown key code state when source not supported.";
2356 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2357 << "Should return unknown scan code state when source not supported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002358 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2359 << "Should return unknown switch state when source not supported.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002360
2361 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2362 << "Should query mapper when source is supported.";
2363 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
Christine Franks2a2293c2022-01-18 11:51:16 -08002364 << "Should query mapper when source is supported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002365 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2366 << "Should query mapper when source is supported.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002367
2368 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
2369 uint8_t flags[4] = { 0, 0, 0, 1 };
2370 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
2371 << "Should do nothing when source is unsupported.";
2372 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002373 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2374 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002375 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2376
2377 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2378 << "Should query mapper when source is supported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002379 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2380 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002381 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2382 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2383
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002384 // Event handling.
2385 RawEvent event;
Christine Franks1ba71cc2021-04-07 14:37:42 -07002386 event.deviceId = EVENTHUB_ID;
2387 unused += mDevice->process(&event, 1);
2388
Christine Franks2a2293c2022-01-18 11:51:16 -08002389 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2390 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
2391}
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002392
2393// A single input device is associated with a specific display. Check that:
Christine Franks2a2293c2022-01-18 11:51:16 -08002394// 1. Device is disabled if the viewport corresponding to the associated display is not found
Christine Franks2a2293c2022-01-18 11:51:16 -08002395// 2. Device is disabled when setEnabled API is called
2396TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
2397 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
2398
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002399 // First Configuration.
2400 std::list<NotifyArgs> unused =
Christine Franks2a2293c2022-01-18 11:51:16 -08002401 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2402
2403 // Device should be enabled by default.
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002404 ASSERT_TRUE(mDevice->isEnabled());
2405
2406 // Prepare associated info.
2407 constexpr uint8_t hdmi = 1;
2408 const std::string UNIQUE_ID = "local:1";
2409
2410 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2411 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2412 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2413 // Device should be disabled because it is associated with a specific display via
2414 // input port <-> display port association, but the corresponding display is not found
2415 ASSERT_FALSE(mDevice->isEnabled());
2416
2417 // Prepare displays.
2418 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002419 ui::ROTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002420 ViewportType::INTERNAL);
2421 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2422 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2423 ASSERT_TRUE(mDevice->isEnabled());
2424
2425 // Device should be disabled after set disable.
2426 mFakePolicy->addDisabledDevice(mDevice->getId());
2427 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2428 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2429 ASSERT_FALSE(mDevice->isEnabled());
2430
2431 // Device should still be disabled even found the associated display.
2432 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2433 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2434 ASSERT_FALSE(mDevice->isEnabled());
2435}
2436
2437TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2438 // Device should be enabled by default.
2439 mFakePolicy->clearViewports();
2440 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2441 std::list<NotifyArgs> unused =
2442 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2443 ASSERT_TRUE(mDevice->isEnabled());
2444
2445 // Device should be disabled because it is associated with a specific display, but the
2446 // corresponding display is not found.
2447 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2448 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2449 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2450 ASSERT_FALSE(mDevice->isEnabled());
2451
2452 // Device should be enabled when a display is found.
2453 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002454 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002455 NO_PORT, ViewportType::INTERNAL);
2456 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2457 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2458 ASSERT_TRUE(mDevice->isEnabled());
2459
2460 // Device should be disabled after set disable.
2461 mFakePolicy->addDisabledDevice(mDevice->getId());
2462 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2463 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2464 ASSERT_FALSE(mDevice->isEnabled());
2465
2466 // Device should still be disabled even found the associated display.
2467 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2468 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2469 ASSERT_FALSE(mDevice->isEnabled());
2470}
2471
2472TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2473 mFakePolicy->clearViewports();
2474 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2475 std::list<NotifyArgs> unused =
2476 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002477
Michael Wrightd02c5b62014-02-10 15:10:22 -08002478 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2479 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002480 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Chris Ye1b0c7342020-07-28 21:57:03 -07002481 NO_PORT, ViewportType::INTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002482 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002483 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002484 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002485}
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002486
arthurhungdcef2dc2020-08-11 14:47:50 +08002487/**
2488 * This test reproduces a crash caused by a dangling reference that remains after device is added
Michael Wrightd02c5b62014-02-10 15:10:22 -08002489 * and removed. The reference is accessed in InputDevice::dump(..);
Chris Ye1b0c7342020-07-28 21:57:03 -07002490 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002491TEST_F(InputDeviceTest, DumpDoesNotCrash) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002492 constexpr int32_t TEST_EVENTHUB_ID = 10;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002493 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
arthurhungdcef2dc2020-08-11 14:47:50 +08002494
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002495 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002496 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002497 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2498 std::string dumpStr, eventHubDevStr;
2499 device.dump(dumpStr, eventHubDevStr);
2500}
2501
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002502TEST_F(InputDeviceTest, GetBluetoothAddress) {
2503 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2504 ASSERT_TRUE(address);
2505 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2506}
2507
Michael Wrightd02c5b62014-02-10 15:10:22 -08002508// --- SwitchInputMapperTest ---
2509
2510class SwitchInputMapperTest : public InputMapperTest {
2511protected:
2512};
2513
2514TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002515 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002516
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002517 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002518}
2519
2520TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002521 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002522
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002523 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002524 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002525
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002526 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002527 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002528}
2529
2530TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002531 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002532 std::list<NotifyArgs> out;
2533 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2534 ASSERT_TRUE(out.empty());
2535 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2536 ASSERT_TRUE(out.empty());
2537 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2538 ASSERT_TRUE(out.empty());
2539 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002540
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002541 ASSERT_EQ(1u, out.size());
2542 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002543 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002544 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2545 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002546 args.switchMask);
2547 ASSERT_EQ(uint32_t(0), args.policyFlags);
2548}
2549
Chris Ye87143712020-11-10 05:05:58 +00002550// --- VibratorInputMapperTest ---
2551class VibratorInputMapperTest : public InputMapperTest {
2552protected:
2553 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2554};
2555
2556TEST_F(VibratorInputMapperTest, GetSources) {
2557 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2558
2559 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2560}
2561
2562TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2563 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2564
2565 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2566}
2567
2568TEST_F(VibratorInputMapperTest, Vibrate) {
2569 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002570 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002571 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2572
2573 VibrationElement pattern(2);
2574 VibrationSequence sequence(2);
2575 pattern.duration = std::chrono::milliseconds(200);
2576 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2577 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2578 sequence.addElement(pattern);
2579 pattern.duration = std::chrono::milliseconds(500);
2580 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2581 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2582 sequence.addElement(pattern);
2583
2584 std::vector<int64_t> timings = {0, 1};
2585 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2586
2587 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002588 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002589 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002590 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002591 // Verify vibrator state listener was notified.
2592 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002593 ASSERT_EQ(1u, out.size());
2594 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2595 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2596 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002597 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002598 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002599 ASSERT_FALSE(mapper.isVibrating());
2600 // Verify vibrator state listener was notified.
2601 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002602 ASSERT_EQ(1u, out.size());
2603 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2604 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2605 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002606}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002607
Chris Yef59a2f42020-10-16 12:55:26 -07002608// --- SensorInputMapperTest ---
2609
2610class SensorInputMapperTest : public InputMapperTest {
2611protected:
2612 static const int32_t ACCEL_RAW_MIN;
2613 static const int32_t ACCEL_RAW_MAX;
2614 static const int32_t ACCEL_RAW_FUZZ;
2615 static const int32_t ACCEL_RAW_FLAT;
2616 static const int32_t ACCEL_RAW_RESOLUTION;
2617
2618 static const int32_t GYRO_RAW_MIN;
2619 static const int32_t GYRO_RAW_MAX;
2620 static const int32_t GYRO_RAW_FUZZ;
2621 static const int32_t GYRO_RAW_FLAT;
2622 static const int32_t GYRO_RAW_RESOLUTION;
2623
2624 static const float GRAVITY_MS2_UNIT;
2625 static const float DEGREE_RADIAN_UNIT;
2626
2627 void prepareAccelAxes();
2628 void prepareGyroAxes();
2629 void setAccelProperties();
2630 void setGyroProperties();
2631 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2632};
2633
2634const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2635const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2636const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2637const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2638const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2639
2640const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2641const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2642const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2643const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2644const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2645
2646const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2647const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2648
2649void SensorInputMapperTest::prepareAccelAxes() {
2650 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2651 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2652 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2653 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2654 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2655 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2656}
2657
2658void SensorInputMapperTest::prepareGyroAxes() {
2659 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2660 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2661 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2662 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2663 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2664 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2665}
2666
2667void SensorInputMapperTest::setAccelProperties() {
2668 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2669 /* sensorDataIndex */ 0);
2670 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2671 /* sensorDataIndex */ 1);
2672 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2673 /* sensorDataIndex */ 2);
2674 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2675 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2676 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2677 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2678 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2679}
2680
2681void SensorInputMapperTest::setGyroProperties() {
2682 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2683 /* sensorDataIndex */ 0);
2684 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2685 /* sensorDataIndex */ 1);
2686 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2687 /* sensorDataIndex */ 2);
2688 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2689 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2690 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2691 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2692 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2693}
2694
2695TEST_F(SensorInputMapperTest, GetSources) {
2696 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2697
2698 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2699}
2700
2701TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2702 setAccelProperties();
2703 prepareAccelAxes();
2704 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2705
2706 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2707 std::chrono::microseconds(10000),
2708 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002709 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002710 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002715
2716 NotifySensorArgs args;
2717 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2718 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2719 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2720
2721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2722 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2723 ASSERT_EQ(args.deviceId, DEVICE_ID);
2724 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2725 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2726 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2727 ASSERT_EQ(args.values, values);
2728 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2729}
2730
2731TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2732 setGyroProperties();
2733 prepareGyroAxes();
2734 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2735
2736 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2737 std::chrono::microseconds(10000),
2738 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002739 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2741 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2742 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2743 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2744 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002745
2746 NotifySensorArgs args;
2747 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2748 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2749 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2750
2751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2752 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2753 ASSERT_EQ(args.deviceId, DEVICE_ID);
2754 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2755 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2756 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2757 ASSERT_EQ(args.values, values);
2758 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2759}
2760
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761// --- KeyboardInputMapperTest ---
2762
2763class KeyboardInputMapperTest : public InputMapperTest {
2764protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002765 const std::string UNIQUE_ID = "local:0";
2766
Michael Wrighta9cf4192022-12-01 23:46:39 +00002767 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002768
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002769 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002770 int32_t originalKeyCode, int32_t rotatedKeyCode,
2771 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002772};
2773
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002774/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2775 * orientation.
2776 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002777void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002778 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2779 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002780}
2781
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002782void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002783 int32_t originalScanCode, int32_t originalKeyCode,
2784 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002785 NotifyKeyArgs args;
2786
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2789 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2790 ASSERT_EQ(originalScanCode, args.scanCode);
2791 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002792 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002793
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2796 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2797 ASSERT_EQ(originalScanCode, args.scanCode);
2798 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002799 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002800}
2801
Michael Wrightd02c5b62014-02-10 15:10:22 -08002802TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002803 KeyboardInputMapper& mapper =
2804 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2805 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002807 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808}
2809
2810TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2811 const int32_t USAGE_A = 0x070004;
2812 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002813 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2814 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002815 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2816 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2817 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002818
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002819 KeyboardInputMapper& mapper =
2820 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2821 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002822 // Initial metastate is AMETA_NONE.
2823 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824
2825 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002826 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002827 NotifyKeyArgs args;
2828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2829 ASSERT_EQ(DEVICE_ID, args.deviceId);
2830 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2831 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2832 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2833 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2834 ASSERT_EQ(KEY_HOME, args.scanCode);
2835 ASSERT_EQ(AMETA_NONE, args.metaState);
2836 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2837 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2838 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2839
2840 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002841 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2843 ASSERT_EQ(DEVICE_ID, args.deviceId);
2844 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2845 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2846 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2847 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2848 ASSERT_EQ(KEY_HOME, args.scanCode);
2849 ASSERT_EQ(AMETA_NONE, args.metaState);
2850 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2851 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2852 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2853
2854 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2858 ASSERT_EQ(DEVICE_ID, args.deviceId);
2859 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2860 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2861 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2862 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2863 ASSERT_EQ(0, args.scanCode);
2864 ASSERT_EQ(AMETA_NONE, args.metaState);
2865 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2866 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2867 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2868
2869 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002870 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2871 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2873 ASSERT_EQ(DEVICE_ID, args.deviceId);
2874 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2875 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2876 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2877 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2878 ASSERT_EQ(0, args.scanCode);
2879 ASSERT_EQ(AMETA_NONE, args.metaState);
2880 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2881 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2882 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2883
2884 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002885 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2886 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2888 ASSERT_EQ(DEVICE_ID, args.deviceId);
2889 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2890 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2891 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2892 ASSERT_EQ(0, args.keyCode);
2893 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2894 ASSERT_EQ(AMETA_NONE, args.metaState);
2895 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2896 ASSERT_EQ(0U, args.policyFlags);
2897 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2898
2899 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2901 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2903 ASSERT_EQ(DEVICE_ID, args.deviceId);
2904 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2905 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2906 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2907 ASSERT_EQ(0, args.keyCode);
2908 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2909 ASSERT_EQ(AMETA_NONE, args.metaState);
2910 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2911 ASSERT_EQ(0U, args.policyFlags);
2912 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2913}
2914
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00002915TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
2916 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
2917 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
2918 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
2919
2920 KeyboardInputMapper& mapper =
2921 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2922 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2923
2924 // Key down by scan code.
2925 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
2926 NotifyKeyArgs args;
2927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2928 ASSERT_EQ(AKEYCODE_B, args.keyCode);
2929
2930 // Key up by scan code.
2931 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
2932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2933 ASSERT_EQ(AKEYCODE_B, args.keyCode);
2934}
2935
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002936/**
2937 * Ensure that the readTime is set to the time when the EV_KEY is received.
2938 */
2939TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
2940 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2941
2942 KeyboardInputMapper& mapper =
2943 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2944 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2945 NotifyKeyArgs args;
2946
2947 // Key down
2948 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
2949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2950 ASSERT_EQ(12, args.readTime);
2951
2952 // Key up
2953 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
2954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2955 ASSERT_EQ(15, args.readTime);
2956}
2957
Michael Wrightd02c5b62014-02-10 15:10:22 -08002958TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002959 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2960 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07002961 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
2962 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
2963 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002964
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002965 KeyboardInputMapper& mapper =
2966 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2967 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002968
Arthur Hung95f68612022-04-07 14:08:22 +08002969 // Initial metastate is AMETA_NONE.
2970 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002971
2972 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974 NotifyKeyArgs args;
2975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2976 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002977 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002978 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002979
2980 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002981 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2983 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002984 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002985
2986 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002987 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2989 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002990 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002991
2992 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002993 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2995 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002996 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002997 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002998}
2999
3000TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003001 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3002 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3003 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3004 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003005
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003006 KeyboardInputMapper& mapper =
3007 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3008 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003009
Michael Wrighta9cf4192022-12-01 23:46:39 +00003010 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003011 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3012 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3013 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3014 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3015 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3016 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3017 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3018 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3019}
3020
3021TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003022 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3023 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3024 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3025 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003026
Michael Wrightd02c5b62014-02-10 15:10:22 -08003027 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003028 KeyboardInputMapper& mapper =
3029 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3030 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031
Michael Wrighta9cf4192022-12-01 23:46:39 +00003032 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003033 ASSERT_NO_FATAL_FAILURE(
3034 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3035 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3036 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3037 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3038 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3039 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3040 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003041
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003042 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003043 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003044 ASSERT_NO_FATAL_FAILURE(
3045 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3046 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3047 AKEYCODE_DPAD_UP, DISPLAY_ID));
3048 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3049 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3050 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3051 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003052
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003053 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003054 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003055 ASSERT_NO_FATAL_FAILURE(
3056 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3057 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3058 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3059 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3060 AKEYCODE_DPAD_UP, DISPLAY_ID));
3061 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3062 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003064 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003065 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003066 ASSERT_NO_FATAL_FAILURE(
3067 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3068 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3069 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3070 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3071 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3072 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3073 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074
3075 // Special case: if orientation changes while key is down, we still emit the same keycode
3076 // in the key up as we did in the key down.
3077 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003078 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003079 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3082 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3083 ASSERT_EQ(KEY_UP, args.scanCode);
3084 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3085
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003086 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003087 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003088 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3090 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3091 ASSERT_EQ(KEY_UP, args.scanCode);
3092 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3093}
3094
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003095TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3096 // If the keyboard is not orientation aware,
3097 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003098 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003099
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003100 KeyboardInputMapper& mapper =
3101 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3102 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003103 NotifyKeyArgs args;
3104
3105 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3110 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3111
Michael Wrighta9cf4192022-12-01 23:46:39 +00003112 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003113 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3117 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3118}
3119
3120TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3121 // If the keyboard is orientation aware,
3122 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003123 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003124
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003125 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003126 KeyboardInputMapper& mapper =
3127 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3128 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003129 NotifyKeyArgs args;
3130
3131 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3132 // ^--- already checked by the previous test
3133
Michael Wrighta9cf4192022-12-01 23:46:39 +00003134 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003135 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3140 ASSERT_EQ(DISPLAY_ID, args.displayId);
3141
3142 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003143 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003144 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003145 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3150 ASSERT_EQ(newDisplayId, args.displayId);
3151}
3152
Michael Wrightd02c5b62014-02-10 15:10:22 -08003153TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003154 KeyboardInputMapper& mapper =
3155 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3156 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003157
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003158 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003159 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003160
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003161 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003162 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003163}
3164
Philip Junker4af3b3d2021-12-14 10:36:55 +01003165TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3166 KeyboardInputMapper& mapper =
3167 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3168 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3169
3170 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3171 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3172 << "If a mapping is available, the result is equal to the mapping";
3173
3174 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3175 << "If no mapping is available, the result is the key location";
3176}
3177
Michael Wrightd02c5b62014-02-10 15:10:22 -08003178TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003179 KeyboardInputMapper& mapper =
3180 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3181 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003182
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003183 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003184 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003185
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003186 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003187 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003188}
3189
3190TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003191 KeyboardInputMapper& mapper =
3192 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3193 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003194
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003195 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003196
Michael Wrightd02c5b62014-02-10 15:10:22 -08003197 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003198 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003199 ASSERT_TRUE(flags[0]);
3200 ASSERT_FALSE(flags[1]);
3201}
3202
3203TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003204 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3205 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3206 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3207 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3208 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3209 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003210
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003211 KeyboardInputMapper& mapper =
3212 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3213 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003214 // Initial metastate is AMETA_NONE.
3215 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003216
3217 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003218 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3219 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3220 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003221
3222 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003223 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3224 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003225 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3226 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3227 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003228 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003229
3230 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003231 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3232 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003233 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3234 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3235 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003236 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003237
3238 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003239 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003241 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3242 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3243 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003244 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003245
3246 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3248 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003249 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3250 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3251 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003252 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003253
3254 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003255 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3256 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003257 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3258 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3259 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003260 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261
3262 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003265 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3266 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3267 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003268 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003269}
3270
Chris Yea52ade12020-08-27 16:49:20 -07003271TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3272 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3273 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3274 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3275 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3276
3277 KeyboardInputMapper& mapper =
3278 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3279 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3280
Chris Yea52ade12020-08-27 16:49:20 -07003281 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003282 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003283 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3284 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3285 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3286 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3287
3288 NotifyKeyArgs args;
3289 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3292 ASSERT_EQ(AMETA_NONE, args.metaState);
3293 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3294 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3295 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3296
3297 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003298 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3300 ASSERT_EQ(AMETA_NONE, args.metaState);
3301 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3302 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3303 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3304}
3305
Arthur Hung2c9a3342019-07-23 14:18:59 +08003306TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3307 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003308 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3309 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3310 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3311 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003312
3313 // keyboard 2.
3314 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003315 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003316 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003317 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003318 std::shared_ptr<InputDevice> device2 =
3319 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003320 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003321
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003322 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3323 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3324 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3325 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003326
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003327 KeyboardInputMapper& mapper =
3328 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3329 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003330
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003331 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003332 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003333 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003334 std::list<NotifyArgs> unused =
3335 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3336 0 /*changes*/);
3337 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003338
3339 // Prepared displays and associated info.
3340 constexpr uint8_t hdmi1 = 0;
3341 constexpr uint8_t hdmi2 = 1;
3342 const std::string SECONDARY_UNIQUE_ID = "local:1";
3343
3344 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3345 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3346
3347 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003348 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3349 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003350 ASSERT_FALSE(device2->isEnabled());
3351
3352 // Prepare second display.
3353 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003354 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003355 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003356 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003357 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003358 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003359 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3360 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003361
3362 // Device should be enabled after the associated display is found.
3363 ASSERT_TRUE(mDevice->isEnabled());
3364 ASSERT_TRUE(device2->isEnabled());
3365
3366 // Test pad key events
3367 ASSERT_NO_FATAL_FAILURE(
3368 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3369 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3370 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3371 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3372 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3373 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3374 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3375
3376 ASSERT_NO_FATAL_FAILURE(
3377 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3378 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3379 AKEYCODE_DPAD_RIGHT, newDisplayId));
3380 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3381 AKEYCODE_DPAD_DOWN, newDisplayId));
3382 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3383 AKEYCODE_DPAD_LEFT, newDisplayId));
3384}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385
arthurhungc903df12020-08-11 15:08:42 +08003386TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3387 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3388 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3389 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3390 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3391 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3392 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3393
3394 KeyboardInputMapper& mapper =
3395 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3396 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003397 // Initial metastate is AMETA_NONE.
3398 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003399
3400 // Initialization should have turned all of the lights off.
3401 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3402 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3403 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3404
3405 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003408 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3409 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3410
3411 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003414 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3415 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3416
3417 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003420 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3421 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3422
3423 mFakeEventHub->removeDevice(EVENTHUB_ID);
3424 mReader->loopOnce();
3425
3426 // keyboard 2 should default toggle keys.
3427 const std::string USB2 = "USB2";
3428 const std::string DEVICE_NAME2 = "KEYBOARD2";
3429 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3430 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3431 std::shared_ptr<InputDevice> device2 =
3432 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003433 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003434 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3435 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3436 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3437 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3438 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3439 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3440
arthurhung6fe95782020-10-05 22:41:16 +08003441 KeyboardInputMapper& mapper2 =
3442 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3443 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003444 std::list<NotifyArgs> unused =
3445 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3446 0 /*changes*/);
3447 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003448
3449 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3450 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3451 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003452 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3453 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003454}
3455
Arthur Hungcb40a002021-08-03 14:31:01 +00003456TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3457 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3458 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3459 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3460
3461 // Suppose we have two mappers. (DPAD + KEYBOARD)
3462 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3463 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3464 KeyboardInputMapper& mapper =
3465 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3466 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003467 // Initial metastate is AMETA_NONE.
3468 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003469
3470 mReader->toggleCapsLockState(DEVICE_ID);
3471 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3472}
3473
Arthur Hungfb3cc112022-04-13 07:39:50 +00003474TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3475 // keyboard 1.
3476 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3477 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3478 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3479 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3480 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3481 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3482
3483 KeyboardInputMapper& mapper1 =
3484 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3485 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3486
3487 // keyboard 2.
3488 const std::string USB2 = "USB2";
3489 const std::string DEVICE_NAME2 = "KEYBOARD2";
3490 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3491 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3492 std::shared_ptr<InputDevice> device2 =
3493 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3494 ftl::Flags<InputDeviceClass>(0));
3495 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3496 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3497 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3498 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3499 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3500 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3501
3502 KeyboardInputMapper& mapper2 =
3503 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3504 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003505 std::list<NotifyArgs> unused =
3506 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3507 0 /*changes*/);
3508 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003509
Arthur Hung95f68612022-04-07 14:08:22 +08003510 // Initial metastate is AMETA_NONE.
3511 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3512 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3513
3514 // Toggle num lock on and off.
3515 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3516 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003517 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3518 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3519 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3520
3521 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3522 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3523 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3524 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3525 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3526
3527 // Toggle caps lock on and off.
3528 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3529 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3530 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3531 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3532 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3533
3534 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3535 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3536 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3537 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3538 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3539
3540 // Toggle scroll lock on and off.
3541 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3542 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3543 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3544 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3545 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3546
3547 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3548 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3549 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3550 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3551 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3552}
3553
Arthur Hung2141d542022-08-23 07:45:21 +00003554TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3555 const int32_t USAGE_A = 0x070004;
3556 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3557 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3558
3559 KeyboardInputMapper& mapper =
3560 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3561 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3562 // Key down by scan code.
3563 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3564 NotifyKeyArgs args;
3565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3566 ASSERT_EQ(DEVICE_ID, args.deviceId);
3567 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3568 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3569 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3570 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3571 ASSERT_EQ(KEY_HOME, args.scanCode);
3572 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3573
3574 // Disable device, it should synthesize cancellation events for down events.
3575 mFakePolicy->addDisabledDevice(DEVICE_ID);
3576 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
3577
3578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3579 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3580 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3581 ASSERT_EQ(KEY_HOME, args.scanCode);
3582 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3583}
3584
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003585// --- KeyboardInputMapperTest_ExternalDevice ---
3586
3587class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3588protected:
Chris Yea52ade12020-08-27 16:49:20 -07003589 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003590};
3591
3592TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003593 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3594 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003595
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003596 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3597 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3598 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3599 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003600
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003601 KeyboardInputMapper& mapper =
3602 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3603 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003604
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003605 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003606 NotifyKeyArgs args;
3607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3608 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3609
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003610 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3612 ASSERT_EQ(uint32_t(0), args.policyFlags);
3613
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3616 ASSERT_EQ(uint32_t(0), args.policyFlags);
3617
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003618 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3620 ASSERT_EQ(uint32_t(0), args.policyFlags);
3621
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003622 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3624 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3625
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003626 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3628 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3629}
3630
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003631TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003632 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003633
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003634 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3635 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3636 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003637
Powei Fengd041c5d2019-05-03 17:11:33 -07003638 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003639 KeyboardInputMapper& mapper =
3640 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3641 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003642
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003643 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003644 NotifyKeyArgs args;
3645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3646 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3647
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003648 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3650 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3651
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3654 ASSERT_EQ(uint32_t(0), args.policyFlags);
3655
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003656 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3658 ASSERT_EQ(uint32_t(0), args.policyFlags);
3659
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003660 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3662 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3663
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003664 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3666 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3667}
3668
Michael Wrightd02c5b62014-02-10 15:10:22 -08003669// --- CursorInputMapperTest ---
3670
3671class CursorInputMapperTest : public InputMapperTest {
3672protected:
3673 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3674
Michael Wright17db18e2020-06-26 20:51:44 +01003675 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003676
Chris Yea52ade12020-08-27 16:49:20 -07003677 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003678 InputMapperTest::SetUp();
3679
Michael Wright17db18e2020-06-26 20:51:44 +01003680 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003681 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003682 }
3683
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003684 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3685 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003686
Michael Wrighta9cf4192022-12-01 23:46:39 +00003687 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003688 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3689 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3690 }
3691
3692 void prepareSecondaryDisplay() {
3693 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003694 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003695 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003696 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003697
3698 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3699 float pressure) {
3700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3701 0.0f, 0.0f, 0.0f, EPSILON));
3702 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003703};
3704
3705const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3706
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003707void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3708 int32_t originalY, int32_t rotatedX,
3709 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003710 NotifyMotionArgs args;
3711
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003717 ASSERT_NO_FATAL_FAILURE(
3718 assertCursorPointerCoords(args.pointerCoords[0],
3719 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3720 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003721}
3722
3723TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003724 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003725 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003726
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003727 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728}
3729
3730TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003731 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003732 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003734 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735}
3736
3737TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003738 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003739 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003740
3741 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003742 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003743
3744 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003745 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3746 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003747 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3748 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3749
3750 // When the bounds are set, then there should be a valid motion range.
3751 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3752
3753 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003754 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003755
3756 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3757 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3758 1, 800 - 1, 0.0f, 0.0f));
3759 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3760 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3761 2, 480 - 1, 0.0f, 0.0f));
3762 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3763 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3764 0.0f, 1.0f, 0.0f, 0.0f));
3765}
3766
3767TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003768 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003769 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003770
3771 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003772 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003773
3774 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3775 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3776 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3777 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3778 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3779 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3780 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3781 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3782 0.0f, 1.0f, 0.0f, 0.0f));
3783}
3784
3785TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003786 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003787 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003788
arthurhungdcef2dc2020-08-11 14:47:50 +08003789 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003790
3791 NotifyMotionArgs args;
3792
3793 // Button press.
3794 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3796 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3798 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3799 ASSERT_EQ(DEVICE_ID, args.deviceId);
3800 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3801 ASSERT_EQ(uint32_t(0), args.policyFlags);
3802 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3803 ASSERT_EQ(0, args.flags);
3804 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3805 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3806 ASSERT_EQ(0, args.edgeFlags);
3807 ASSERT_EQ(uint32_t(1), args.pointerCount);
3808 ASSERT_EQ(0, args.pointerProperties[0].id);
3809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003810 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003811 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3812 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3813 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3814
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3816 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3817 ASSERT_EQ(DEVICE_ID, args.deviceId);
3818 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3819 ASSERT_EQ(uint32_t(0), args.policyFlags);
3820 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3821 ASSERT_EQ(0, args.flags);
3822 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3823 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3824 ASSERT_EQ(0, args.edgeFlags);
3825 ASSERT_EQ(uint32_t(1), args.pointerCount);
3826 ASSERT_EQ(0, args.pointerProperties[0].id);
3827 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003828 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003829 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3830 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3831 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3832
Michael Wrightd02c5b62014-02-10 15:10:22 -08003833 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003834 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3835 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3837 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3838 ASSERT_EQ(DEVICE_ID, args.deviceId);
3839 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3840 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003841 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3842 ASSERT_EQ(0, args.flags);
3843 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3844 ASSERT_EQ(0, args.buttonState);
3845 ASSERT_EQ(0, args.edgeFlags);
3846 ASSERT_EQ(uint32_t(1), args.pointerCount);
3847 ASSERT_EQ(0, args.pointerProperties[0].id);
3848 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003849 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003850 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3851 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3852 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3853
3854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3855 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3856 ASSERT_EQ(DEVICE_ID, args.deviceId);
3857 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3858 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003859 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3860 ASSERT_EQ(0, args.flags);
3861 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3862 ASSERT_EQ(0, args.buttonState);
3863 ASSERT_EQ(0, args.edgeFlags);
3864 ASSERT_EQ(uint32_t(1), args.pointerCount);
3865 ASSERT_EQ(0, args.pointerProperties[0].id);
3866 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003867 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003868 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3869 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3870 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3871}
3872
3873TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003874 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003875 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003876
3877 NotifyMotionArgs args;
3878
3879 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
3881 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3883 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003884 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3885 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
3886 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003887
3888 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
3890 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3892 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003893 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
3894 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003895}
3896
3897TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003898 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003899 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003900
3901 NotifyMotionArgs args;
3902
3903 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003904 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3905 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3907 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003908 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003909
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3911 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003912 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003913
Michael Wrightd02c5b62014-02-10 15:10:22 -08003914 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003915 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3916 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003918 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003919 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003920
3921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003922 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003923 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003924}
3925
3926TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003927 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003928 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929
3930 NotifyMotionArgs args;
3931
3932 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003933 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
3934 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
3935 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3936 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3938 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003939 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3940 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3941 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3944 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003945 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3946 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3947 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003948
Michael Wrightd02c5b62014-02-10 15:10:22 -08003949 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
3951 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
3952 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3954 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003955 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3956 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3957 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003958
3959 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003960 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003963 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003964 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003965
3966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003967 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003968 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003969}
3970
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003971TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003972 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003973 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003974 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
3975 // need to be rotated.
3976 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003977 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003978
Michael Wrighta9cf4192022-12-01 23:46:39 +00003979 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003980 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3981 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3982 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3983 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3984 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3985 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3986 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3987 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3988}
3989
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003990TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003991 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003992 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003993 // Since InputReader works in the un-rotated coordinate space, only devices that are not
3994 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003995 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003996
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003997 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003998 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003999 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4000 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4001 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4002 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4003 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4004 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4005 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4006 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4007
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004008 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004009 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004010 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4011 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4012 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4013 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4014 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4015 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4016 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4017 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004018
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004019 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004020 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4022 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4023 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4024 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4025 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4026 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4027 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4028 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4029
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004030 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004031 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004032 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4033 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4034 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4035 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4036 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4037 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4038 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4039 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004040}
4041
4042TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004043 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004044 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004045
4046 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4047 mFakePointerController->setPosition(100, 200);
4048 mFakePointerController->setButtonState(0);
4049
4050 NotifyMotionArgs motionArgs;
4051 NotifyKeyArgs keyArgs;
4052
4053 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004054 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4055 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4057 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4058 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4059 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004060 ASSERT_NO_FATAL_FAILURE(
4061 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004062
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4064 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4065 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4066 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004067 ASSERT_NO_FATAL_FAILURE(
4068 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004069
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004070 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004073 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004074 ASSERT_EQ(0, motionArgs.buttonState);
4075 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004076 ASSERT_NO_FATAL_FAILURE(
4077 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078
4079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004080 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004081 ASSERT_EQ(0, motionArgs.buttonState);
4082 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004083 ASSERT_NO_FATAL_FAILURE(
4084 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004085
4086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004088 ASSERT_EQ(0, motionArgs.buttonState);
4089 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004090 ASSERT_NO_FATAL_FAILURE(
4091 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092
4093 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4095 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4098 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4099 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4100 motionArgs.buttonState);
4101 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4102 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004103 ASSERT_NO_FATAL_FAILURE(
4104 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4107 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4108 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4109 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4110 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004111 ASSERT_NO_FATAL_FAILURE(
4112 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004113
4114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4115 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4116 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4117 motionArgs.buttonState);
4118 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4119 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004120 ASSERT_NO_FATAL_FAILURE(
4121 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004122
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004126 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004127 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4128 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004129 ASSERT_NO_FATAL_FAILURE(
4130 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004131
4132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004134 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4135 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004136 ASSERT_NO_FATAL_FAILURE(
4137 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004142 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4143 ASSERT_EQ(0, motionArgs.buttonState);
4144 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004145 ASSERT_NO_FATAL_FAILURE(
4146 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004147 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004149
4150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004151 ASSERT_EQ(0, motionArgs.buttonState);
4152 ASSERT_EQ(0, mFakePointerController->getButtonState());
4153 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004154 ASSERT_NO_FATAL_FAILURE(
4155 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004156
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4158 ASSERT_EQ(0, motionArgs.buttonState);
4159 ASSERT_EQ(0, mFakePointerController->getButtonState());
4160 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004161 ASSERT_NO_FATAL_FAILURE(
4162 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004163
4164 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4168 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4169 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004170
Michael Wrightd02c5b62014-02-10 15:10:22 -08004171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004172 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004173 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4174 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004175 ASSERT_NO_FATAL_FAILURE(
4176 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004177
4178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4179 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4180 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4181 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004182 ASSERT_NO_FATAL_FAILURE(
4183 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004188 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004189 ASSERT_EQ(0, motionArgs.buttonState);
4190 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004191 ASSERT_NO_FATAL_FAILURE(
4192 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004193
4194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004196 ASSERT_EQ(0, motionArgs.buttonState);
4197 ASSERT_EQ(0, mFakePointerController->getButtonState());
4198
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004199 ASSERT_NO_FATAL_FAILURE(
4200 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4202 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4203 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4204
4205 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4209 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4210 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004211
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004213 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4215 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004216 ASSERT_NO_FATAL_FAILURE(
4217 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004218
4219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4220 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4221 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4222 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004223 ASSERT_NO_FATAL_FAILURE(
4224 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4227 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004229 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230 ASSERT_EQ(0, motionArgs.buttonState);
4231 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004232 ASSERT_NO_FATAL_FAILURE(
4233 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004234
4235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4236 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4237 ASSERT_EQ(0, motionArgs.buttonState);
4238 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004239 ASSERT_NO_FATAL_FAILURE(
4240 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004241
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4243 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4244 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4245
4246 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4248 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4250 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4251 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004252
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004254 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004255 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4256 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004257 ASSERT_NO_FATAL_FAILURE(
4258 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004259
4260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4261 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4262 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4263 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004264 ASSERT_NO_FATAL_FAILURE(
4265 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4268 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004270 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271 ASSERT_EQ(0, motionArgs.buttonState);
4272 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004273 ASSERT_NO_FATAL_FAILURE(
4274 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004275
4276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4277 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4278 ASSERT_EQ(0, motionArgs.buttonState);
4279 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004280 ASSERT_NO_FATAL_FAILURE(
4281 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004282
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4284 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4285 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4286
4287 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4291 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4292 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004293
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004295 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4297 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004298 ASSERT_NO_FATAL_FAILURE(
4299 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004300
4301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4302 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4303 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4304 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004305 ASSERT_NO_FATAL_FAILURE(
4306 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004311 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312 ASSERT_EQ(0, motionArgs.buttonState);
4313 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004314 ASSERT_NO_FATAL_FAILURE(
4315 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004316
4317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4318 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4319 ASSERT_EQ(0, motionArgs.buttonState);
4320 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004321 ASSERT_NO_FATAL_FAILURE(
4322 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004323
Michael Wrightd02c5b62014-02-10 15:10:22 -08004324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4325 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4326 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4327}
4328
4329TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004331 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332
4333 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4334 mFakePointerController->setPosition(100, 200);
4335 mFakePointerController->setButtonState(0);
4336
4337 NotifyMotionArgs args;
4338
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004343 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4344 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4346 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 +00004347 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004348}
4349
4350TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004351 addConfigurationProperty("cursor.mode", "pointer");
4352 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004353 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004354
4355 NotifyDeviceResetArgs resetArgs;
4356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4357 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4358 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4359
4360 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4361 mFakePointerController->setPosition(100, 200);
4362 mFakePointerController->setButtonState(0);
4363
4364 NotifyMotionArgs args;
4365
4366 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4371 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4374 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 +00004375 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004376
4377 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4381 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4382 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4384 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4386 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4387 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4389 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4390
4391 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004392 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4393 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4395 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4396 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4398 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4400 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4401 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4403 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4404
4405 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4410 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4413 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 +00004414 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004415
4416 // Disable pointer capture and check that the device generation got bumped
4417 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004418 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004419 mFakePolicy->setPointerCapture(false);
4420 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004421 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004422
4423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004424 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4425
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4427 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4430 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4433 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 +00004434 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435}
4436
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004437/**
4438 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4439 * pointer acceleration or speed processing should not be applied.
4440 */
4441TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4442 addConfigurationProperty("cursor.mode", "pointer");
4443 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4444 100.f /*high threshold*/, 10.f /*acceleration*/);
4445 mFakePolicy->setVelocityControlParams(testParams);
4446 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4447
4448 NotifyDeviceResetArgs resetArgs;
4449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4450 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4451 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4452
4453 NotifyMotionArgs args;
4454
4455 // Move and verify scale is applied.
4456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4460 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4461 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4462 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4463 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4464 ASSERT_GT(relX, 10);
4465 ASSERT_GT(relY, 20);
4466
4467 // Enable Pointer Capture
4468 mFakePolicy->setPointerCapture(true);
4469 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4470 NotifyPointerCaptureChangedArgs captureArgs;
4471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4472 ASSERT_TRUE(captureArgs.request.enable);
4473
4474 // Move and verify scale is not applied.
4475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4479 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4481 ASSERT_EQ(10, args.pointerCoords[0].getX());
4482 ASSERT_EQ(20, args.pointerCoords[0].getY());
4483}
4484
Prabir Pradhan208360b2022-06-24 18:37:04 +00004485TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4486 addConfigurationProperty("cursor.mode", "pointer");
4487 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4488
4489 NotifyDeviceResetArgs resetArgs;
4490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4491 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4492 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4493
4494 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004495 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004496
4497 NotifyMotionArgs args;
4498
4499 // Verify that the coordinates are rotated.
4500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4504 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4505 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4506 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4507 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4508
4509 // Enable Pointer Capture.
4510 mFakePolicy->setPointerCapture(true);
4511 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4512 NotifyPointerCaptureChangedArgs captureArgs;
4513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4514 ASSERT_TRUE(captureArgs.request.enable);
4515
4516 // Move and verify rotation is not applied.
4517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4521 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4522 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4523 ASSERT_EQ(10, args.pointerCoords[0].getX());
4524 ASSERT_EQ(20, args.pointerCoords[0].getY());
4525}
4526
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004527TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004528 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004529
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004530 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004531 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004532
4533 // Set up the secondary display as the display on which the pointer should be shown.
4534 // The InputDevice is not associated with any display.
4535 prepareSecondaryDisplay();
4536 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08004537 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4538
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004539 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004540 mFakePointerController->setPosition(100, 200);
4541 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004542
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004543 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004548 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4549 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4550 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004551 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004552}
4553
4554TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
4555 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4556
4557 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004558 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004559
4560 // Set up the secondary display as the display on which the pointer should be shown,
4561 // and associate the InputDevice with the secondary display.
4562 prepareSecondaryDisplay();
4563 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4564 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4565 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4566
4567 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4568 mFakePointerController->setPosition(100, 200);
4569 mFakePointerController->setButtonState(0);
4570
4571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4572 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4573 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004575 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4576 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4577 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004578 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004579}
4580
4581TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
4582 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4583
4584 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004585 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004586 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4587
4588 // Associate the InputDevice with the secondary display.
4589 prepareSecondaryDisplay();
4590 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4591 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4592
4593 // The mapper should not generate any events because it is associated with a display that is
4594 // different from the pointer display.
4595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4596 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4597 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004599}
4600
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004601// --- BluetoothCursorInputMapperTest ---
4602
4603class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4604protected:
4605 void SetUp() override {
4606 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4607
4608 mFakePointerController = std::make_shared<FakePointerController>();
4609 mFakePolicy->setPointerController(mFakePointerController);
4610 }
4611};
4612
4613TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4614 addConfigurationProperty("cursor.mode", "pointer");
4615 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4616
4617 nsecs_t kernelEventTime = ARBITRARY_TIME;
4618 nsecs_t expectedEventTime = ARBITRARY_TIME;
4619 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4620 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4622 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4623 WithEventTime(expectedEventTime))));
4624
4625 // Process several events that come in quick succession, according to their timestamps.
4626 for (int i = 0; i < 3; i++) {
4627 constexpr static nsecs_t delta = ms2ns(1);
4628 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4629 kernelEventTime += delta;
4630 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4631
4632 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4633 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4635 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4636 WithEventTime(expectedEventTime))));
4637 }
4638}
4639
4640TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4641 addConfigurationProperty("cursor.mode", "pointer");
4642 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4643
4644 nsecs_t expectedEventTime = ARBITRARY_TIME;
4645 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4646 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4648 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4649 WithEventTime(expectedEventTime))));
4650
4651 // Process several events with the same timestamp from the kernel.
4652 // Ensure that we do not generate events too far into the future.
4653 constexpr static int32_t numEvents =
4654 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4655 for (int i = 0; i < numEvents; i++) {
4656 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4657
4658 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4659 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4661 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4662 WithEventTime(expectedEventTime))));
4663 }
4664
4665 // By processing more events with the same timestamp, we should not generate events with a
4666 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4667 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4668 for (int i = 0; i < 3; i++) {
4669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4672 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4673 WithEventTime(cappedEventTime))));
4674 }
4675}
4676
4677TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4678 addConfigurationProperty("cursor.mode", "pointer");
4679 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4680
4681 nsecs_t kernelEventTime = ARBITRARY_TIME;
4682 nsecs_t expectedEventTime = ARBITRARY_TIME;
4683 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4684 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4686 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4687 WithEventTime(expectedEventTime))));
4688
4689 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4690 // smoothening is not needed, its timestamp is not affected.
4691 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4692 expectedEventTime = kernelEventTime;
4693
4694 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4695 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4697 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4698 WithEventTime(expectedEventTime))));
4699}
4700
Michael Wrightd02c5b62014-02-10 15:10:22 -08004701// --- TouchInputMapperTest ---
4702
4703class TouchInputMapperTest : public InputMapperTest {
4704protected:
4705 static const int32_t RAW_X_MIN;
4706 static const int32_t RAW_X_MAX;
4707 static const int32_t RAW_Y_MIN;
4708 static const int32_t RAW_Y_MAX;
4709 static const int32_t RAW_TOUCH_MIN;
4710 static const int32_t RAW_TOUCH_MAX;
4711 static const int32_t RAW_TOOL_MIN;
4712 static const int32_t RAW_TOOL_MAX;
4713 static const int32_t RAW_PRESSURE_MIN;
4714 static const int32_t RAW_PRESSURE_MAX;
4715 static const int32_t RAW_ORIENTATION_MIN;
4716 static const int32_t RAW_ORIENTATION_MAX;
4717 static const int32_t RAW_DISTANCE_MIN;
4718 static const int32_t RAW_DISTANCE_MAX;
4719 static const int32_t RAW_TILT_MIN;
4720 static const int32_t RAW_TILT_MAX;
4721 static const int32_t RAW_ID_MIN;
4722 static const int32_t RAW_ID_MAX;
4723 static const int32_t RAW_SLOT_MIN;
4724 static const int32_t RAW_SLOT_MAX;
4725 static const float X_PRECISION;
4726 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004727 static const float X_PRECISION_VIRTUAL;
4728 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004729
4730 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004731 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732
4733 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4734
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004735 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004736 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004737
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738 enum Axes {
4739 POSITION = 1 << 0,
4740 TOUCH = 1 << 1,
4741 TOOL = 1 << 2,
4742 PRESSURE = 1 << 3,
4743 ORIENTATION = 1 << 4,
4744 MINOR = 1 << 5,
4745 ID = 1 << 6,
4746 DISTANCE = 1 << 7,
4747 TILT = 1 << 8,
4748 SLOT = 1 << 9,
4749 TOOL_TYPE = 1 << 10,
4750 };
4751
Michael Wrighta9cf4192022-12-01 23:46:39 +00004752 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004753 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004754 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004755 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004756 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757 int32_t toRawX(float displayX);
4758 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004759 int32_t toRotatedRawX(float displayX);
4760 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004761 float toCookedX(float rawX, float rawY);
4762 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004764 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004765 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004766 float toDisplayY(int32_t rawY, int32_t displayHeight);
4767
Michael Wrightd02c5b62014-02-10 15:10:22 -08004768};
4769
4770const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4771const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4772const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4773const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4774const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4775const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4776const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4777const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004778const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4779const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004780const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4781const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4782const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4783const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4784const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4785const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4786const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4787const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4788const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4789const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4790const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4791const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004792const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4793 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4794const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4795 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004796const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4797 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798
4799const float TouchInputMapperTest::GEOMETRIC_SCALE =
4800 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4801 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4802
4803const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4804 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4805 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4806};
4807
Michael Wrighta9cf4192022-12-01 23:46:39 +00004808void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004809 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4810 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004811}
4812
4813void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4814 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004815 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004816}
4817
Michael Wrighta9cf4192022-12-01 23:46:39 +00004818void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004819 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4820 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4821 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004822}
4823
Michael Wrightd02c5b62014-02-10 15:10:22 -08004824void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004825 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4826 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4827 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4828 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829}
4830
Jason Gerecke489fda82012-09-07 17:19:40 -07004831void TouchInputMapperTest::prepareLocationCalibration() {
4832 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4833}
4834
Michael Wrightd02c5b62014-02-10 15:10:22 -08004835int32_t TouchInputMapperTest::toRawX(float displayX) {
4836 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4837}
4838
4839int32_t TouchInputMapperTest::toRawY(float displayY) {
4840 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4841}
4842
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004843int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4844 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4845}
4846
4847int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4848 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4849}
4850
Jason Gerecke489fda82012-09-07 17:19:40 -07004851float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4852 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4853 return rawX;
4854}
4855
4856float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4857 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4858 return rawY;
4859}
4860
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004862 return toDisplayX(rawX, DISPLAY_WIDTH);
4863}
4864
4865float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4866 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004867}
4868
4869float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004870 return toDisplayY(rawY, DISPLAY_HEIGHT);
4871}
4872
4873float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4874 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004875}
4876
4877
4878// --- SingleTouchInputMapperTest ---
4879
4880class SingleTouchInputMapperTest : public TouchInputMapperTest {
4881protected:
4882 void prepareButtons();
4883 void prepareAxes(int axes);
4884
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004885 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4886 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4887 void processUp(SingleTouchInputMapper& mappery);
4888 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4889 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4890 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4891 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4892 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4893 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004894};
4895
4896void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004897 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004898}
4899
4900void SingleTouchInputMapperTest::prepareAxes(int axes) {
4901 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004902 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4903 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004904 }
4905 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004906 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4907 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004908 }
4909 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004910 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4911 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912 }
4913 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004914 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4915 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004916 }
4917 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004918 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4919 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004920 }
4921}
4922
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004923void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004924 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4925 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4926 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927}
4928
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004929void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004930 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4931 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004932}
4933
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004934void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004935 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936}
4937
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004938void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004939 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004940}
4941
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004942void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4943 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004944 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004945}
4946
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004947void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004949}
4950
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004951void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4952 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4954 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004955}
4956
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004957void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4958 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004960}
4961
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004962void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964}
4965
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967 prepareButtons();
4968 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004969 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004970
Harry Cutts16a24cc2022-10-26 15:22:19 +00004971 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972}
4973
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004975 prepareButtons();
4976 prepareAxes(POSITION);
4977 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004978 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004979
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004980 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004981}
4982
4983TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004985 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004986 prepareButtons();
4987 prepareAxes(POSITION);
4988 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004989 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004990
4991 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004992 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004993
4994 // Virtual key is down.
4995 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4996 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4997 processDown(mapper, x, y);
4998 processSync(mapper);
4999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5000
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005001 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002
5003 // Virtual key is up.
5004 processUp(mapper);
5005 processSync(mapper);
5006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5007
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005008 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009}
5010
5011TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005012 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005013 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005014 prepareButtons();
5015 prepareAxes(POSITION);
5016 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005017 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005018
5019 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005020 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005021
5022 // Virtual key is down.
5023 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5024 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5025 processDown(mapper, x, y);
5026 processSync(mapper);
5027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5028
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005029 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030
5031 // Virtual key is up.
5032 processUp(mapper);
5033 processSync(mapper);
5034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5035
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005036 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005037}
5038
5039TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005040 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005041 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005042 prepareButtons();
5043 prepareAxes(POSITION);
5044 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005045 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005046
Michael Wrightd02c5b62014-02-10 15:10:22 -08005047 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005048 ASSERT_TRUE(
5049 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005050 ASSERT_TRUE(flags[0]);
5051 ASSERT_FALSE(flags[1]);
5052}
5053
5054TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005055 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005056 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005057 prepareButtons();
5058 prepareAxes(POSITION);
5059 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005060 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061
arthurhungdcef2dc2020-08-11 14:47:50 +08005062 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005063
5064 NotifyKeyArgs args;
5065
5066 // Press virtual key.
5067 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5068 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5069 processDown(mapper, x, y);
5070 processSync(mapper);
5071
5072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5073 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5074 ASSERT_EQ(DEVICE_ID, args.deviceId);
5075 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5076 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5077 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5078 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5079 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5080 ASSERT_EQ(KEY_HOME, args.scanCode);
5081 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5082 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5083
5084 // Release virtual key.
5085 processUp(mapper);
5086 processSync(mapper);
5087
5088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5089 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5090 ASSERT_EQ(DEVICE_ID, args.deviceId);
5091 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5092 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5093 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5094 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5095 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5096 ASSERT_EQ(KEY_HOME, args.scanCode);
5097 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5098 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5099
5100 // Should not have sent any motions.
5101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5102}
5103
5104TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005106 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005107 prepareButtons();
5108 prepareAxes(POSITION);
5109 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005110 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111
arthurhungdcef2dc2020-08-11 14:47:50 +08005112 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113
5114 NotifyKeyArgs keyArgs;
5115
5116 // Press virtual key.
5117 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5118 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5119 processDown(mapper, x, y);
5120 processSync(mapper);
5121
5122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5123 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5124 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5125 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5126 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5127 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5128 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5129 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5130 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5131 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5132 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5133
5134 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5135 // into the display area.
5136 y -= 100;
5137 processMove(mapper, x, y);
5138 processSync(mapper);
5139
5140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5141 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5142 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5143 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5144 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5145 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5146 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5147 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5148 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5149 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5150 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5151 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5152
5153 NotifyMotionArgs motionArgs;
5154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5155 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5156 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5157 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5158 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5159 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5160 ASSERT_EQ(0, motionArgs.flags);
5161 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5162 ASSERT_EQ(0, motionArgs.buttonState);
5163 ASSERT_EQ(0, motionArgs.edgeFlags);
5164 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5165 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5166 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5167 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5168 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5169 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5170 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5171 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5172
5173 // Keep moving out of bounds. Should generate a pointer move.
5174 y -= 50;
5175 processMove(mapper, x, y);
5176 processSync(mapper);
5177
5178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5179 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5180 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5181 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5182 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5183 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5184 ASSERT_EQ(0, motionArgs.flags);
5185 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5186 ASSERT_EQ(0, motionArgs.buttonState);
5187 ASSERT_EQ(0, motionArgs.edgeFlags);
5188 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5189 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5190 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5191 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5192 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5193 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5194 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5195 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5196
5197 // Release out of bounds. Should generate a pointer up.
5198 processUp(mapper);
5199 processSync(mapper);
5200
5201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5202 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5203 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5204 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5205 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5206 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5207 ASSERT_EQ(0, motionArgs.flags);
5208 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5209 ASSERT_EQ(0, motionArgs.buttonState);
5210 ASSERT_EQ(0, motionArgs.edgeFlags);
5211 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5212 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5215 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5216 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5217 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5218 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5219
5220 // Should not have sent any more keys or motions.
5221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5223}
5224
5225TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005226 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005227 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005228 prepareButtons();
5229 prepareAxes(POSITION);
5230 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005231 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005232
arthurhungdcef2dc2020-08-11 14:47:50 +08005233 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005234
5235 NotifyMotionArgs motionArgs;
5236
5237 // Initially go down out of bounds.
5238 int32_t x = -10;
5239 int32_t y = -10;
5240 processDown(mapper, x, y);
5241 processSync(mapper);
5242
5243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5244
5245 // Move into the display area. Should generate a pointer down.
5246 x = 50;
5247 y = 75;
5248 processMove(mapper, x, y);
5249 processSync(mapper);
5250
5251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5252 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5253 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5254 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5255 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5256 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5257 ASSERT_EQ(0, motionArgs.flags);
5258 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5259 ASSERT_EQ(0, motionArgs.buttonState);
5260 ASSERT_EQ(0, motionArgs.edgeFlags);
5261 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5262 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5263 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5265 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5266 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5267 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5268 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5269
5270 // Release. Should generate a pointer up.
5271 processUp(mapper);
5272 processSync(mapper);
5273
5274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5275 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5276 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5277 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5278 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5279 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5280 ASSERT_EQ(0, motionArgs.flags);
5281 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5282 ASSERT_EQ(0, motionArgs.buttonState);
5283 ASSERT_EQ(0, motionArgs.edgeFlags);
5284 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5285 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5286 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5288 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5289 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5290 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5291 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5292
5293 // Should not have sent any more keys or motions.
5294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5296}
5297
Santos Cordonfa5cf462017-04-05 10:37:00 -07005298TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005299 addConfigurationProperty("touch.deviceType", "touchScreen");
5300 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5301
Michael Wrighta9cf4192022-12-01 23:46:39 +00005302 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005303 prepareButtons();
5304 prepareAxes(POSITION);
5305 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005306 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005307
arthurhungdcef2dc2020-08-11 14:47:50 +08005308 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005309
5310 NotifyMotionArgs motionArgs;
5311
5312 // Down.
5313 int32_t x = 100;
5314 int32_t y = 125;
5315 processDown(mapper, x, y);
5316 processSync(mapper);
5317
5318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5319 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5320 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5321 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
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, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5334 1, 0, 0, 0, 0, 0, 0, 0));
5335 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5336 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5337 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5338
5339 // Move.
5340 x += 50;
5341 y += 75;
5342 processMove(mapper, x, y);
5343 processSync(mapper);
5344
5345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5346 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5347 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5348 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5349 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5350 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5351 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5352 ASSERT_EQ(0, motionArgs.flags);
5353 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5354 ASSERT_EQ(0, motionArgs.buttonState);
5355 ASSERT_EQ(0, motionArgs.edgeFlags);
5356 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5357 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5360 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5361 1, 0, 0, 0, 0, 0, 0, 0));
5362 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5363 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5364 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5365
5366 // Up.
5367 processUp(mapper);
5368 processSync(mapper);
5369
5370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5371 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5372 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5373 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5374 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5375 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5376 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5377 ASSERT_EQ(0, motionArgs.flags);
5378 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5379 ASSERT_EQ(0, motionArgs.buttonState);
5380 ASSERT_EQ(0, motionArgs.edgeFlags);
5381 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5382 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5383 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5385 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5386 1, 0, 0, 0, 0, 0, 0, 0));
5387 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5388 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5389 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5390
5391 // Should not have sent any more keys or motions.
5392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5394}
5395
Michael Wrightd02c5b62014-02-10 15:10:22 -08005396TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005397 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005398 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005399 prepareButtons();
5400 prepareAxes(POSITION);
5401 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005402 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403
arthurhungdcef2dc2020-08-11 14:47:50 +08005404 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005405
5406 NotifyMotionArgs motionArgs;
5407
5408 // Down.
5409 int32_t x = 100;
5410 int32_t y = 125;
5411 processDown(mapper, x, y);
5412 processSync(mapper);
5413
5414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5415 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5416 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5417 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5418 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5419 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, 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), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5429 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5430 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5431 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5432
5433 // Move.
5434 x += 50;
5435 y += 75;
5436 processMove(mapper, x, y);
5437 processSync(mapper);
5438
5439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5440 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5441 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5442 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5443 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, 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), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5454 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5455 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5456 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5457
5458 // Up.
5459 processUp(mapper);
5460 processSync(mapper);
5461
5462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5463 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5464 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5465 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5466 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5467 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5468 ASSERT_EQ(0, motionArgs.flags);
5469 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5470 ASSERT_EQ(0, motionArgs.buttonState);
5471 ASSERT_EQ(0, motionArgs.edgeFlags);
5472 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5473 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5476 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5477 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5478 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5479 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5480
5481 // Should not have sent any more keys or motions.
5482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5484}
5485
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005486TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005487 addConfigurationProperty("touch.deviceType", "touchScreen");
5488 prepareButtons();
5489 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005490 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5491 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005492 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493
5494 NotifyMotionArgs args;
5495
5496 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005497 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005498 processDown(mapper, toRawX(50), toRawY(75));
5499 processSync(mapper);
5500
5501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5502 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5503 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5504
5505 processUp(mapper);
5506 processSync(mapper);
5507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5508}
5509
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005510TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005511 addConfigurationProperty("touch.deviceType", "touchScreen");
5512 prepareButtons();
5513 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005514 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5515 // orientation-aware are affected by display rotation.
5516 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005517 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005518
5519 NotifyMotionArgs args;
5520
5521 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005522 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005523 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005524 processDown(mapper, toRawX(50), toRawY(75));
5525 processSync(mapper);
5526
5527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5528 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5529 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5530
5531 processUp(mapper);
5532 processSync(mapper);
5533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5534
5535 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005536 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005537 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005538 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005539 processSync(mapper);
5540
5541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5542 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5543 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5544
5545 processUp(mapper);
5546 processSync(mapper);
5547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5548
5549 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005550 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005551 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005552 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5553 processSync(mapper);
5554
5555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5556 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5557 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5558
5559 processUp(mapper);
5560 processSync(mapper);
5561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5562
5563 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005564 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005565 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005566 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005567 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 Pradhanac1c74f2021-08-20 16:09:32 -07005578TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5579 addConfigurationProperty("touch.deviceType", "touchScreen");
5580 prepareButtons();
5581 prepareAxes(POSITION);
5582 addConfigurationProperty("touch.orientationAware", "1");
5583 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5584 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005585 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005586 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5587 NotifyMotionArgs args;
5588
5589 // Orientation 0.
5590 processDown(mapper, toRawX(50), toRawY(75));
5591 processSync(mapper);
5592
5593 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5594 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5595 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5596
5597 processUp(mapper);
5598 processSync(mapper);
5599 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5600}
5601
5602TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5603 addConfigurationProperty("touch.deviceType", "touchScreen");
5604 prepareButtons();
5605 prepareAxes(POSITION);
5606 addConfigurationProperty("touch.orientationAware", "1");
5607 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5608 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005609 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005610 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5611 NotifyMotionArgs args;
5612
5613 // Orientation 90.
5614 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5615 processSync(mapper);
5616
5617 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5618 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5619 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5620
5621 processUp(mapper);
5622 processSync(mapper);
5623 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5624}
5625
5626TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5627 addConfigurationProperty("touch.deviceType", "touchScreen");
5628 prepareButtons();
5629 prepareAxes(POSITION);
5630 addConfigurationProperty("touch.orientationAware", "1");
5631 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5632 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005633 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005634 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5635 NotifyMotionArgs args;
5636
5637 // Orientation 180.
5638 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5639 processSync(mapper);
5640
5641 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5642 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5643 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5644
5645 processUp(mapper);
5646 processSync(mapper);
5647 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5648}
5649
5650TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5651 addConfigurationProperty("touch.deviceType", "touchScreen");
5652 prepareButtons();
5653 prepareAxes(POSITION);
5654 addConfigurationProperty("touch.orientationAware", "1");
5655 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5656 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005657 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005658 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5659 NotifyMotionArgs args;
5660
5661 // Orientation 270.
5662 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5663 processSync(mapper);
5664
5665 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5666 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5667 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5668
5669 processUp(mapper);
5670 processSync(mapper);
5671 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5672}
5673
5674TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5675 addConfigurationProperty("touch.deviceType", "touchScreen");
5676 prepareButtons();
5677 prepareAxes(POSITION);
5678 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5679 // orientation-aware are affected by display rotation.
5680 addConfigurationProperty("touch.orientationAware", "0");
5681 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5682 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5683
5684 NotifyMotionArgs args;
5685
5686 // Orientation 90, Rotation 0.
5687 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005688 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005689 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5690 processSync(mapper);
5691
5692 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5693 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5694 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5695
5696 processUp(mapper);
5697 processSync(mapper);
5698 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5699
5700 // Orientation 90, Rotation 90.
5701 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005702 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005703 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005704 processSync(mapper);
5705
5706 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5707 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5708 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5709
5710 processUp(mapper);
5711 processSync(mapper);
5712 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5713
5714 // Orientation 90, Rotation 180.
5715 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005716 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005717 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5718 processSync(mapper);
5719
5720 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5721 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5722 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5723
5724 processUp(mapper);
5725 processSync(mapper);
5726 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5727
5728 // Orientation 90, Rotation 270.
5729 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005730 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005731 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 -07005732 processSync(mapper);
5733
5734 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5735 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5736 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5737
5738 processUp(mapper);
5739 processSync(mapper);
5740 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5741}
5742
Michael Wrightd02c5b62014-02-10 15:10:22 -08005743TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005744 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005745 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005746 prepareButtons();
5747 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005748 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005749
5750 // These calculations are based on the input device calibration documentation.
5751 int32_t rawX = 100;
5752 int32_t rawY = 200;
5753 int32_t rawPressure = 10;
5754 int32_t rawToolMajor = 12;
5755 int32_t rawDistance = 2;
5756 int32_t rawTiltX = 30;
5757 int32_t rawTiltY = 110;
5758
5759 float x = toDisplayX(rawX);
5760 float y = toDisplayY(rawY);
5761 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5762 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5763 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5764 float distance = float(rawDistance);
5765
5766 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5767 float tiltScale = M_PI / 180;
5768 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5769 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5770 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5771 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5772
5773 processDown(mapper, rawX, rawY);
5774 processPressure(mapper, rawPressure);
5775 processToolMajor(mapper, rawToolMajor);
5776 processDistance(mapper, rawDistance);
5777 processTilt(mapper, rawTiltX, rawTiltY);
5778 processSync(mapper);
5779
5780 NotifyMotionArgs args;
5781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5783 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5784 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5785}
5786
Jason Gerecke489fda82012-09-07 17:19:40 -07005787TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005788 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005789 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005790 prepareLocationCalibration();
5791 prepareButtons();
5792 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005793 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005794
5795 int32_t rawX = 100;
5796 int32_t rawY = 200;
5797
5798 float x = toDisplayX(toCookedX(rawX, rawY));
5799 float y = toDisplayY(toCookedY(rawX, rawY));
5800
5801 processDown(mapper, rawX, rawY);
5802 processSync(mapper);
5803
5804 NotifyMotionArgs args;
5805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5806 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5807 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5808}
5809
Michael Wrightd02c5b62014-02-10 15:10:22 -08005810TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005811 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005812 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005813 prepareButtons();
5814 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005815 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005816
5817 NotifyMotionArgs motionArgs;
5818 NotifyKeyArgs keyArgs;
5819
5820 processDown(mapper, 100, 200);
5821 processSync(mapper);
5822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5823 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5824 ASSERT_EQ(0, motionArgs.buttonState);
5825
5826 // press BTN_LEFT, release BTN_LEFT
5827 processKey(mapper, BTN_LEFT, 1);
5828 processSync(mapper);
5829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5830 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5831 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5832
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5834 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5835 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5836
Michael Wrightd02c5b62014-02-10 15:10:22 -08005837 processKey(mapper, BTN_LEFT, 0);
5838 processSync(mapper);
5839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005840 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005841 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005842
5843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005844 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005845 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005846
5847 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5848 processKey(mapper, BTN_RIGHT, 1);
5849 processKey(mapper, BTN_MIDDLE, 1);
5850 processSync(mapper);
5851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5852 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5853 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5854 motionArgs.buttonState);
5855
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5857 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5858 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5859
5860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5861 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5862 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5863 motionArgs.buttonState);
5864
Michael Wrightd02c5b62014-02-10 15:10:22 -08005865 processKey(mapper, BTN_RIGHT, 0);
5866 processSync(mapper);
5867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005868 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005869 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005870
5871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005873 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005874
5875 processKey(mapper, BTN_MIDDLE, 0);
5876 processSync(mapper);
5877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005878 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005879 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005880
5881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005882 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005883 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884
5885 // press BTN_BACK, release BTN_BACK
5886 processKey(mapper, BTN_BACK, 1);
5887 processSync(mapper);
5888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5889 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5890 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005891
Michael Wrightd02c5b62014-02-10 15:10:22 -08005892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005893 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005894 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5895
5896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5897 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5898 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005899
5900 processKey(mapper, BTN_BACK, 0);
5901 processSync(mapper);
5902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005903 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005904 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005905
5906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005907 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005908 ASSERT_EQ(0, motionArgs.buttonState);
5909
Michael Wrightd02c5b62014-02-10 15:10:22 -08005910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5911 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5912 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5913
5914 // press BTN_SIDE, release BTN_SIDE
5915 processKey(mapper, BTN_SIDE, 1);
5916 processSync(mapper);
5917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5918 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5919 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005920
Michael Wrightd02c5b62014-02-10 15:10:22 -08005921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005922 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005923 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5924
5925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5926 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5927 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005928
5929 processKey(mapper, BTN_SIDE, 0);
5930 processSync(mapper);
5931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005932 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005933 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005934
5935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005936 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005937 ASSERT_EQ(0, motionArgs.buttonState);
5938
Michael Wrightd02c5b62014-02-10 15:10:22 -08005939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5940 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5941 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5942
5943 // press BTN_FORWARD, release BTN_FORWARD
5944 processKey(mapper, BTN_FORWARD, 1);
5945 processSync(mapper);
5946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5947 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5948 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005949
Michael Wrightd02c5b62014-02-10 15:10:22 -08005950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005952 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5953
5954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5955 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5956 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005957
5958 processKey(mapper, BTN_FORWARD, 0);
5959 processSync(mapper);
5960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005961 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005962 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005963
5964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005965 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005966 ASSERT_EQ(0, motionArgs.buttonState);
5967
Michael Wrightd02c5b62014-02-10 15:10:22 -08005968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5969 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5970 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5971
5972 // press BTN_EXTRA, release BTN_EXTRA
5973 processKey(mapper, BTN_EXTRA, 1);
5974 processSync(mapper);
5975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5976 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5977 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005978
Michael Wrightd02c5b62014-02-10 15:10:22 -08005979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005980 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005981 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, 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_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005986
5987 processKey(mapper, BTN_EXTRA, 0);
5988 processSync(mapper);
5989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005990 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005991 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005992
5993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005994 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005995 ASSERT_EQ(0, motionArgs.buttonState);
5996
Michael Wrightd02c5b62014-02-10 15:10:22 -08005997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5998 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5999 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6000
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6002
Michael Wrightd02c5b62014-02-10 15:10:22 -08006003 // press BTN_STYLUS, release BTN_STYLUS
6004 processKey(mapper, BTN_STYLUS, 1);
6005 processSync(mapper);
6006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006008 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6009
6010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6011 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6012 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006013
6014 processKey(mapper, BTN_STYLUS, 0);
6015 processSync(mapper);
6016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006017 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006018 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006019
6020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006021 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006022 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006023
6024 // press BTN_STYLUS2, release BTN_STYLUS2
6025 processKey(mapper, BTN_STYLUS2, 1);
6026 processSync(mapper);
6027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006029 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6030
6031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6032 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6033 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006034
6035 processKey(mapper, BTN_STYLUS2, 0);
6036 processSync(mapper);
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006038 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006039 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006040
6041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006042 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006043 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006044
6045 // release touch
6046 processUp(mapper);
6047 processSync(mapper);
6048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6049 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6050 ASSERT_EQ(0, motionArgs.buttonState);
6051}
6052
6053TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006054 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006055 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006056 prepareButtons();
6057 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006058 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059
6060 NotifyMotionArgs motionArgs;
6061
6062 // default tool type is finger
6063 processDown(mapper, 100, 200);
6064 processSync(mapper);
6065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6066 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6067 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6068
6069 // eraser
6070 processKey(mapper, BTN_TOOL_RUBBER, 1);
6071 processSync(mapper);
6072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6074 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6075
6076 // stylus
6077 processKey(mapper, BTN_TOOL_RUBBER, 0);
6078 processKey(mapper, BTN_TOOL_PEN, 1);
6079 processSync(mapper);
6080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6082 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6083
6084 // brush
6085 processKey(mapper, BTN_TOOL_PEN, 0);
6086 processKey(mapper, BTN_TOOL_BRUSH, 1);
6087 processSync(mapper);
6088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6089 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6090 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6091
6092 // pencil
6093 processKey(mapper, BTN_TOOL_BRUSH, 0);
6094 processKey(mapper, BTN_TOOL_PENCIL, 1);
6095 processSync(mapper);
6096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6097 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6098 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6099
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006100 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006101 processKey(mapper, BTN_TOOL_PENCIL, 0);
6102 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6103 processSync(mapper);
6104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6105 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6106 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6107
6108 // mouse
6109 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6110 processKey(mapper, BTN_TOOL_MOUSE, 1);
6111 processSync(mapper);
6112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6113 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6114 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6115
6116 // lens
6117 processKey(mapper, BTN_TOOL_MOUSE, 0);
6118 processKey(mapper, BTN_TOOL_LENS, 1);
6119 processSync(mapper);
6120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6121 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6122 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6123
6124 // double-tap
6125 processKey(mapper, BTN_TOOL_LENS, 0);
6126 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6127 processSync(mapper);
6128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6130 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6131
6132 // triple-tap
6133 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6134 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6135 processSync(mapper);
6136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6138 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6139
6140 // quad-tap
6141 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6142 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6143 processSync(mapper);
6144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6145 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6146 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6147
6148 // finger
6149 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6150 processKey(mapper, BTN_TOOL_FINGER, 1);
6151 processSync(mapper);
6152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6153 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6154 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6155
6156 // stylus trumps finger
6157 processKey(mapper, BTN_TOOL_PEN, 1);
6158 processSync(mapper);
6159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6160 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6161 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6162
6163 // eraser trumps stylus
6164 processKey(mapper, BTN_TOOL_RUBBER, 1);
6165 processSync(mapper);
6166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6167 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6168 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6169
6170 // mouse trumps eraser
6171 processKey(mapper, BTN_TOOL_MOUSE, 1);
6172 processSync(mapper);
6173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6175 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6176
6177 // back to default tool type
6178 processKey(mapper, BTN_TOOL_MOUSE, 0);
6179 processKey(mapper, BTN_TOOL_RUBBER, 0);
6180 processKey(mapper, BTN_TOOL_PEN, 0);
6181 processKey(mapper, BTN_TOOL_FINGER, 0);
6182 processSync(mapper);
6183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6185 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6186}
6187
6188TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006189 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006190 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006191 prepareButtons();
6192 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006193 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006194 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006195
6196 NotifyMotionArgs motionArgs;
6197
6198 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6199 processKey(mapper, BTN_TOOL_FINGER, 1);
6200 processMove(mapper, 100, 200);
6201 processSync(mapper);
6202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6203 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6204 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6205 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6206
6207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6208 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6210 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6211
6212 // move a little
6213 processMove(mapper, 150, 250);
6214 processSync(mapper);
6215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6216 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6217 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6218 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6219
6220 // down when BTN_TOUCH is pressed, pressure defaults to 1
6221 processKey(mapper, BTN_TOUCH, 1);
6222 processSync(mapper);
6223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6224 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6225 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6226 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6227
6228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6229 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6230 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6231 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6232
6233 // up when BTN_TOUCH is released, hover restored
6234 processKey(mapper, BTN_TOUCH, 0);
6235 processSync(mapper);
6236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6237 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6238 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6239 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6240
6241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6242 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6244 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6245
6246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6247 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6249 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6250
6251 // exit hover when pointer goes away
6252 processKey(mapper, BTN_TOOL_FINGER, 0);
6253 processSync(mapper);
6254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6255 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6256 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6257 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6258}
6259
6260TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006261 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006262 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006263 prepareButtons();
6264 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006265 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006266
6267 NotifyMotionArgs motionArgs;
6268
6269 // initially hovering because pressure is 0
6270 processDown(mapper, 100, 200);
6271 processPressure(mapper, 0);
6272 processSync(mapper);
6273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6274 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6276 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6277
6278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6279 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6281 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6282
6283 // move a little
6284 processMove(mapper, 150, 250);
6285 processSync(mapper);
6286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6287 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6289 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6290
6291 // down when pressure is non-zero
6292 processPressure(mapper, RAW_PRESSURE_MAX);
6293 processSync(mapper);
6294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6295 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6297 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6298
6299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6300 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6302 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6303
6304 // up when pressure becomes 0, hover restored
6305 processPressure(mapper, 0);
6306 processSync(mapper);
6307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6308 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6310 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6311
6312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6313 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6314 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6315 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6316
6317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6318 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6319 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6320 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6321
6322 // exit hover when pointer goes away
6323 processUp(mapper);
6324 processSync(mapper);
6325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6326 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6327 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6328 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6329}
6330
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006331TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6332 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006333 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006334 prepareButtons();
6335 prepareAxes(POSITION | PRESSURE);
6336 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6337
6338 // Touch down.
6339 processDown(mapper, 100, 200);
6340 processPressure(mapper, 1);
6341 processSync(mapper);
6342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6343 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6344
6345 // Reset the mapper. This should cancel the ongoing gesture.
6346 resetMapper(mapper, ARBITRARY_TIME);
6347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6348 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6349
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6351}
6352
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006353TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6354 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006355 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006356 prepareButtons();
6357 prepareAxes(POSITION | PRESSURE);
6358 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6359
6360 // Set the initial state for the touch pointer.
6361 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6362 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6363 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6364 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6365
6366 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006367 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6368 // does not generate any events.
6369 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006370
6371 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6372 // the recreated touch state to generate a down event.
6373 processSync(mapper);
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6375 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6376
6377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6378}
6379
lilinnan687e58f2022-07-19 16:00:50 +08006380TEST_F(SingleTouchInputMapperTest,
6381 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6382 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006383 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006384 prepareButtons();
6385 prepareAxes(POSITION);
6386 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6387 NotifyMotionArgs motionArgs;
6388
6389 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006390 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006391 processSync(mapper);
6392
6393 // We should receive a down event
6394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6395 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6396
6397 // Change display id
6398 clearViewports();
6399 prepareSecondaryDisplay(ViewportType::INTERNAL);
6400
6401 // We should receive a cancel event
6402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6403 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6404 // Then receive reset called
6405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6406}
6407
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006408TEST_F(SingleTouchInputMapperTest,
6409 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6410 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006411 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006412 prepareButtons();
6413 prepareAxes(POSITION);
6414 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6416 NotifyMotionArgs motionArgs;
6417
6418 // Start a new gesture.
6419 processDown(mapper, 100, 200);
6420 processSync(mapper);
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6422 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6423
6424 // Make the viewport inactive. This will put the device in disabled mode.
6425 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6426 viewport->isActive = false;
6427 mFakePolicy->updateViewport(*viewport);
6428 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6429
6430 // We should receive a cancel event for the ongoing gesture.
6431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6432 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6433 // Then we should be notified that the device was reset.
6434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6435
6436 // No events are generated while the viewport is inactive.
6437 processMove(mapper, 101, 201);
6438 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006439 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006440 processSync(mapper);
6441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6442
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006443 // Start a new gesture while the viewport is still inactive.
6444 processDown(mapper, 300, 400);
6445 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6446 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6447 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6448 processSync(mapper);
6449
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006450 // Make the viewport active again. The device should resume processing events.
6451 viewport->isActive = true;
6452 mFakePolicy->updateViewport(*viewport);
6453 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6454
6455 // The device is reset because it changes back to direct mode, without generating any events.
6456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6458
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006459 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006460 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6462 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006463
6464 // No more events.
6465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6467}
6468
Prabir Pradhan211ba622022-10-31 21:09:21 +00006469TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6470 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006471 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006472 prepareButtons();
6473 prepareAxes(POSITION);
6474 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6476
6477 // Press a stylus button.
6478 processKey(mapper, BTN_STYLUS, 1);
6479 processSync(mapper);
6480
6481 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6482 processDown(mapper, 100, 200);
6483 processSync(mapper);
6484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6485 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6486 WithCoords(toDisplayX(100), toDisplayY(200)),
6487 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6489 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6490 WithCoords(toDisplayX(100), toDisplayY(200)),
6491 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6492
6493 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6494 // the button has not actually been released, since there will be no pointers through which the
6495 // button state can be reported. The event is generated at the location of the pointer before
6496 // it went up.
6497 processUp(mapper);
6498 processSync(mapper);
6499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6500 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6501 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6503 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6504 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6505}
6506
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006507TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6508 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6509 prepareDisplay(ui::ROTATION_0);
6510 prepareButtons();
6511 prepareAxes(POSITION);
6512 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6514
6515 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6516}
6517
Prabir Pradhan5632d622021-09-06 07:57:20 -07006518// --- TouchDisplayProjectionTest ---
6519
6520class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6521public:
6522 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6523 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6524 // rotated equivalent of the given un-rotated physical display bounds.
Michael Wrighta9cf4192022-12-01 23:46:39 +00006525 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006526 uint32_t inverseRotationFlags;
6527 auto width = DISPLAY_WIDTH;
6528 auto height = DISPLAY_HEIGHT;
6529 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006530 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006531 inverseRotationFlags = ui::Transform::ROT_270;
6532 std::swap(width, height);
6533 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006534 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006535 inverseRotationFlags = ui::Transform::ROT_180;
6536 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006537 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006538 inverseRotationFlags = ui::Transform::ROT_90;
6539 std::swap(width, height);
6540 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006541 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006542 inverseRotationFlags = ui::Transform::ROT_0;
6543 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006544 }
6545
6546 const ui::Transform rotation(inverseRotationFlags, width, height);
6547 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6548
6549 std::optional<DisplayViewport> internalViewport =
6550 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6551 DisplayViewport& v = *internalViewport;
6552 v.displayId = DISPLAY_ID;
6553 v.orientation = orientation;
6554
6555 v.logicalLeft = 0;
6556 v.logicalTop = 0;
6557 v.logicalRight = 100;
6558 v.logicalBottom = 100;
6559
6560 v.physicalLeft = rotatedPhysicalDisplay.left;
6561 v.physicalTop = rotatedPhysicalDisplay.top;
6562 v.physicalRight = rotatedPhysicalDisplay.right;
6563 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6564
6565 v.deviceWidth = width;
6566 v.deviceHeight = height;
6567
6568 v.isActive = true;
6569 v.uniqueId = UNIQUE_ID;
6570 v.type = ViewportType::INTERNAL;
6571 mFakePolicy->updateViewport(v);
6572 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6573 }
6574
6575 void assertReceivedMove(const Point& point) {
6576 NotifyMotionArgs motionArgs;
6577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6578 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6579 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6581 1, 0, 0, 0, 0, 0, 0, 0));
6582 }
6583};
6584
6585TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6586 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006587 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006588
6589 prepareButtons();
6590 prepareAxes(POSITION);
6591 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6592
6593 NotifyMotionArgs motionArgs;
6594
6595 // Configure the DisplayViewport such that the logical display maps to a subsection of
6596 // the display panel called the physical display. Here, the physical display is bounded by the
6597 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6598 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6599 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6600 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6601
Michael Wrighta9cf4192022-12-01 23:46:39 +00006602 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006603 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6604
6605 // Touches outside the physical display should be ignored, and should not generate any
6606 // events. Ensure touches at the following points that lie outside of the physical display
6607 // area do not generate any events.
6608 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6609 processDown(mapper, toRawX(point.x), toRawY(point.y));
6610 processSync(mapper);
6611 processUp(mapper);
6612 processSync(mapper);
6613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6614 << "Unexpected event generated for touch outside physical display at point: "
6615 << point.x << ", " << point.y;
6616 }
6617 }
6618}
6619
6620TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6621 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006622 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006623
6624 prepareButtons();
6625 prepareAxes(POSITION);
6626 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6627
6628 NotifyMotionArgs motionArgs;
6629
6630 // Configure the DisplayViewport such that the logical display maps to a subsection of
6631 // the display panel called the physical display. Here, the physical display is bounded by the
6632 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6633 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6634
Michael Wrighta9cf4192022-12-01 23:46:39 +00006635 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006636 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6637
6638 // Touches that start outside the physical display should be ignored until it enters the
6639 // physical display bounds, at which point it should generate a down event. Start a touch at
6640 // the point (5, 100), which is outside the physical display bounds.
6641 static const Point kOutsidePoint{5, 100};
6642 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6643 processSync(mapper);
6644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6645
6646 // Move the touch into the physical display area. This should generate a pointer down.
6647 processMove(mapper, toRawX(11), toRawY(21));
6648 processSync(mapper);
6649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6650 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6651 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6652 ASSERT_NO_FATAL_FAILURE(
6653 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6654
6655 // Move the touch inside the physical display area. This should generate a pointer move.
6656 processMove(mapper, toRawX(69), toRawY(159));
6657 processSync(mapper);
6658 assertReceivedMove({69, 159});
6659
6660 // Move outside the physical display area. Since the pointer is already down, this should
6661 // now continue generating events.
6662 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6663 processSync(mapper);
6664 assertReceivedMove(kOutsidePoint);
6665
6666 // Release. This should generate a pointer up.
6667 processUp(mapper);
6668 processSync(mapper);
6669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6670 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6671 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6672 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6673
6674 // Ensure no more events were generated.
6675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6677 }
6678}
6679
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006680// --- ExternalStylusFusionTest ---
6681
6682class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6683public:
6684 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6685 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006686 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006687 prepareButtons();
6688 prepareAxes(POSITION);
6689 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6690
6691 mStylusState.when = ARBITRARY_TIME;
6692 mStylusState.pressure = 0.f;
6693 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
6694 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
6695 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
6696 processExternalStylusState(mapper);
6697 return mapper;
6698 }
6699
6700 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6701 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6702 for (const NotifyArgs& args : generatedArgs) {
6703 mFakeListener->notify(args);
6704 }
6705 // Loop the reader to flush the input listener queue.
6706 mReader->loopOnce();
6707 return generatedArgs;
6708 }
6709
6710protected:
6711 StylusState mStylusState{};
6712 static constexpr uint32_t EXPECTED_SOURCE =
6713 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
6714
6715 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6716 auto toolTypeSource =
6717 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6718
6719 // The first pointer is withheld.
6720 processDown(mapper, 100, 200);
6721 processSync(mapper);
6722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6723 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6724 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6725
6726 // The external stylus reports pressure. The withheld finger pointer is released as a
6727 // stylus.
6728 mStylusState.pressure = 1.f;
6729 processExternalStylusState(mapper);
6730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6731 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6732 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6733
6734 // Subsequent pointer events are not withheld.
6735 processMove(mapper, 101, 201);
6736 processSync(mapper);
6737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6738 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6739
6740 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6742 }
6743
6744 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6745 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6746
6747 // Releasing the touch pointer ends the gesture.
6748 processUp(mapper);
6749 processSync(mapper);
6750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6751 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
6752 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6753
6754 mStylusState.pressure = 0.f;
6755 processExternalStylusState(mapper);
6756 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6758 }
6759
6760 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6761 auto toolTypeSource =
6762 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER));
6763
6764 // The first pointer is withheld when an external stylus is connected,
6765 // and a timeout is requested.
6766 processDown(mapper, 100, 200);
6767 processSync(mapper);
6768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6769 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6770 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6771
6772 // If the timeout expires early, it is requested again.
6773 handleTimeout(mapper, ARBITRARY_TIME + 1);
6774 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6775 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6776
6777 // When the timeout expires, the withheld touch is released as a finger pointer.
6778 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
6779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6780 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6781
6782 // Subsequent pointer events are not withheld.
6783 processMove(mapper, 101, 201);
6784 processSync(mapper);
6785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6786 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6787 processUp(mapper);
6788 processSync(mapper);
6789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6790 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6791
6792 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6794 }
6795
6796private:
6797 InputDeviceInfo mExternalStylusDeviceInfo{};
6798};
6799
6800TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
6801 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6802 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
6803}
6804
6805TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
6806 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6807 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6808}
6809
6810TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
6811 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6812 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6813}
6814
6815// Test a successful stylus fusion gesture where the pressure is reported by the external
6816// before the touch is reported by the touchscreen.
6817TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
6818 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6819 auto toolTypeSource =
6820 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6821
6822 // The external stylus reports pressure first. It is ignored for now.
6823 mStylusState.pressure = 1.f;
6824 processExternalStylusState(mapper);
6825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6826 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6827
6828 // When the touch goes down afterwards, it is reported as a stylus pointer.
6829 processDown(mapper, 100, 200);
6830 processSync(mapper);
6831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6832 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6833 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6834
6835 processMove(mapper, 101, 201);
6836 processSync(mapper);
6837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6838 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6839 processUp(mapper);
6840 processSync(mapper);
6841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6842 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6843
6844 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6846}
6847
6848TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
6849 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6850
6851 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6852 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6853
6854 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6855 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6856 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6857 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6858}
6859
6860TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
6861 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6862 auto toolTypeSource =
6863 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6864
6865 mStylusState.pressure = 0.8f;
6866 processExternalStylusState(mapper);
6867 processDown(mapper, 100, 200);
6868 processSync(mapper);
6869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6870 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6871 WithPressure(0.8f))));
6872 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6873
6874 // The external stylus reports a pressure change. We wait for some time for a touch event.
6875 mStylusState.pressure = 0.6f;
6876 processExternalStylusState(mapper);
6877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6878 ASSERT_NO_FATAL_FAILURE(
6879 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6880
6881 // If a touch is reported within the timeout, it reports the updated pressure.
6882 processMove(mapper, 101, 201);
6883 processSync(mapper);
6884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6885 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6886 WithPressure(0.6f))));
6887 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6888
6889 // There is another pressure change.
6890 mStylusState.pressure = 0.5f;
6891 processExternalStylusState(mapper);
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6893 ASSERT_NO_FATAL_FAILURE(
6894 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6895
6896 // If a touch is not reported within the timeout, a move event is generated to report
6897 // the new pressure.
6898 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6900 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6901 WithPressure(0.5f))));
6902
6903 // If a zero pressure is reported before the touch goes up, the previous pressure value is
6904 // repeated indefinitely.
6905 mStylusState.pressure = 0.0f;
6906 processExternalStylusState(mapper);
6907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6908 ASSERT_NO_FATAL_FAILURE(
6909 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6910 processMove(mapper, 102, 202);
6911 processSync(mapper);
6912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6913 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6914 WithPressure(0.5f))));
6915 processMove(mapper, 103, 203);
6916 processSync(mapper);
6917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6918 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6919 WithPressure(0.5f))));
6920
6921 processUp(mapper);
6922 processSync(mapper);
6923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6924 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
6925 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6926
6927 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6929}
6930
6931TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
6932 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6933 auto source = WithSource(EXPECTED_SOURCE);
6934
6935 mStylusState.pressure = 1.f;
6936 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_ERASER;
6937 processExternalStylusState(mapper);
6938 processDown(mapper, 100, 200);
6939 processSync(mapper);
6940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6941 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6942 WithToolType(AMOTION_EVENT_TOOL_TYPE_ERASER))));
6943 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6944
6945 // The external stylus reports a tool change. We wait for some time for a touch event.
6946 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
6947 processExternalStylusState(mapper);
6948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6949 ASSERT_NO_FATAL_FAILURE(
6950 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6951
6952 // If a touch is reported within the timeout, it reports the updated pressure.
6953 processMove(mapper, 101, 201);
6954 processSync(mapper);
6955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6956 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6957 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6958 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6959
6960 // There is another tool type change.
6961 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
6962 processExternalStylusState(mapper);
6963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6964 ASSERT_NO_FATAL_FAILURE(
6965 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6966
6967 // If a touch is not reported within the timeout, a move event is generated to report
6968 // the new tool type.
6969 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6971 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6972 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
6973
6974 processUp(mapper);
6975 processSync(mapper);
6976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6977 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
6978 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
6979
6980 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6982}
6983
6984TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
6985 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6986 auto toolTypeSource =
6987 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6988
6989 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6990
6991 // The external stylus reports a button change. We wait for some time for a touch event.
6992 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
6993 processExternalStylusState(mapper);
6994 ASSERT_NO_FATAL_FAILURE(
6995 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6996
6997 // If a touch is reported within the timeout, it reports the updated button state.
6998 processMove(mapper, 101, 201);
6999 processSync(mapper);
7000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7001 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7002 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7004 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7005 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7006 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7007
7008 // The button is now released.
7009 mStylusState.buttons = 0;
7010 processExternalStylusState(mapper);
7011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7012 ASSERT_NO_FATAL_FAILURE(
7013 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7014
7015 // If a touch is not reported within the timeout, a move event is generated to report
7016 // the new button state.
7017 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7019 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7020 WithButtonState(0))));
7021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007022 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7023 WithButtonState(0))));
7024
7025 processUp(mapper);
7026 processSync(mapper);
7027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007028 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7029
7030 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7032}
7033
Michael Wrightd02c5b62014-02-10 15:10:22 -08007034// --- MultiTouchInputMapperTest ---
7035
7036class MultiTouchInputMapperTest : public TouchInputMapperTest {
7037protected:
7038 void prepareAxes(int axes);
7039
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007040 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7041 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7042 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7043 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7044 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7045 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7046 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7047 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7048 void processId(MultiTouchInputMapper& mapper, int32_t id);
7049 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7050 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7051 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007052 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007053 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007054 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7055 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007056};
7057
7058void MultiTouchInputMapperTest::prepareAxes(int axes) {
7059 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007060 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7061 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007062 }
7063 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007064 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7065 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007066 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007067 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7068 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007069 }
7070 }
7071 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007072 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7073 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007074 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007075 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007076 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007077 }
7078 }
7079 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007080 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7081 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007082 }
7083 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007084 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7085 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007086 }
7087 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007088 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7089 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007090 }
7091 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007092 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7093 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007094 }
7095 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007096 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7097 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007098 }
7099 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007100 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007101 }
7102}
7103
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007104void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7105 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007108}
7109
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007110void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7111 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007112 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007113}
7114
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007115void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7116 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007117 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007118}
7119
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007120void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007122}
7123
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007124void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007126}
7127
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007128void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7129 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007130 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007131}
7132
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007133void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007134 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007135}
7136
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007137void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007139}
7140
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007141void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007142 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007143}
7144
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007145void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007147}
7148
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007149void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007151}
7152
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007153void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7154 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007156}
7157
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007158void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7159 int32_t value) {
7160 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7162}
7163
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007164void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007166}
7167
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007168void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7169 nsecs_t readTime) {
7170 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007171}
7172
Michael Wrightd02c5b62014-02-10 15:10:22 -08007173TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007174 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007175 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007176 prepareAxes(POSITION);
7177 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007178 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007179
arthurhungdcef2dc2020-08-11 14:47:50 +08007180 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007181
7182 NotifyMotionArgs motionArgs;
7183
7184 // Two fingers down at once.
7185 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7186 processPosition(mapper, x1, y1);
7187 processMTSync(mapper);
7188 processPosition(mapper, x2, y2);
7189 processMTSync(mapper);
7190 processSync(mapper);
7191
7192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7193 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7194 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7195 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7196 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7197 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7198 ASSERT_EQ(0, motionArgs.flags);
7199 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7200 ASSERT_EQ(0, motionArgs.buttonState);
7201 ASSERT_EQ(0, motionArgs.edgeFlags);
7202 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7203 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7204 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7206 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7207 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7208 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7209 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7210
7211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7212 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7213 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7214 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7215 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007216 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007217 ASSERT_EQ(0, motionArgs.flags);
7218 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7219 ASSERT_EQ(0, motionArgs.buttonState);
7220 ASSERT_EQ(0, motionArgs.edgeFlags);
7221 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7222 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7223 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7224 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7225 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7226 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7227 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7229 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7230 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7231 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7232 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7233
7234 // Move.
7235 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7236 processPosition(mapper, x1, y1);
7237 processMTSync(mapper);
7238 processPosition(mapper, x2, y2);
7239 processMTSync(mapper);
7240 processSync(mapper);
7241
7242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7243 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7244 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7245 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7246 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7248 ASSERT_EQ(0, motionArgs.flags);
7249 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7250 ASSERT_EQ(0, motionArgs.buttonState);
7251 ASSERT_EQ(0, motionArgs.edgeFlags);
7252 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7253 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7255 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7256 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7258 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7260 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7261 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7262 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7263 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7264
7265 // First finger up.
7266 x2 += 15; y2 -= 20;
7267 processPosition(mapper, x2, y2);
7268 processMTSync(mapper);
7269 processSync(mapper);
7270
7271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7272 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7273 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7274 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7275 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007276 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007277 ASSERT_EQ(0, motionArgs.flags);
7278 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7279 ASSERT_EQ(0, motionArgs.buttonState);
7280 ASSERT_EQ(0, motionArgs.edgeFlags);
7281 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7282 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7284 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7285 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7287 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7289 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7290 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7291 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7292 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7293
7294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7295 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7296 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7297 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7298 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7299 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7300 ASSERT_EQ(0, motionArgs.flags);
7301 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7302 ASSERT_EQ(0, motionArgs.buttonState);
7303 ASSERT_EQ(0, motionArgs.edgeFlags);
7304 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7305 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7306 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7308 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7309 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7310 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7311 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7312
7313 // Move.
7314 x2 += 20; y2 -= 25;
7315 processPosition(mapper, x2, y2);
7316 processMTSync(mapper);
7317 processSync(mapper);
7318
7319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7320 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7321 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7322 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7323 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7324 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7325 ASSERT_EQ(0, motionArgs.flags);
7326 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7327 ASSERT_EQ(0, motionArgs.buttonState);
7328 ASSERT_EQ(0, motionArgs.edgeFlags);
7329 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7330 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7331 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7333 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7334 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7335 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7336 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7337
7338 // New finger down.
7339 int32_t x3 = 700, y3 = 300;
7340 processPosition(mapper, x2, y2);
7341 processMTSync(mapper);
7342 processPosition(mapper, x3, y3);
7343 processMTSync(mapper);
7344 processSync(mapper);
7345
7346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7347 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7348 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7349 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7350 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007351 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007352 ASSERT_EQ(0, motionArgs.flags);
7353 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7354 ASSERT_EQ(0, motionArgs.buttonState);
7355 ASSERT_EQ(0, motionArgs.edgeFlags);
7356 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7357 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7359 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7360 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7362 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7364 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7365 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7366 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7367 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7368
7369 // Second finger up.
7370 x3 += 30; y3 -= 20;
7371 processPosition(mapper, x3, y3);
7372 processMTSync(mapper);
7373 processSync(mapper);
7374
7375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7376 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7377 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7378 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7379 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007380 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007381 ASSERT_EQ(0, motionArgs.flags);
7382 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7383 ASSERT_EQ(0, motionArgs.buttonState);
7384 ASSERT_EQ(0, motionArgs.edgeFlags);
7385 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7386 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7388 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7389 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7391 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7393 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7394 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7395 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7396 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7397
7398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7399 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7400 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7401 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7402 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7404 ASSERT_EQ(0, motionArgs.flags);
7405 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7406 ASSERT_EQ(0, motionArgs.buttonState);
7407 ASSERT_EQ(0, motionArgs.edgeFlags);
7408 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7409 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7410 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7412 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7413 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7414 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7415 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7416
7417 // Last finger up.
7418 processMTSync(mapper);
7419 processSync(mapper);
7420
7421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7422 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7423 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7424 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7425 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7426 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7427 ASSERT_EQ(0, motionArgs.flags);
7428 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7429 ASSERT_EQ(0, motionArgs.buttonState);
7430 ASSERT_EQ(0, motionArgs.edgeFlags);
7431 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7432 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7433 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7435 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7436 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7437 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7438 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7439
7440 // Should not have sent any more keys or motions.
7441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7443}
7444
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007445TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7446 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007447 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007448
7449 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7450 /*fuzz*/ 0, /*resolution*/ 10);
7451 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7452 /*fuzz*/ 0, /*resolution*/ 11);
7453 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7454 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7455 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7456 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7457 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7458 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7459 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7460 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7461
7462 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7463
7464 // X and Y axes
7465 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7466 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7467 // Touch major and minor
7468 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7469 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7470 // Tool major and minor
7471 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7472 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7473}
7474
7475TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7476 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007477 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007478
7479 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7480 /*fuzz*/ 0, /*resolution*/ 10);
7481 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7482 /*fuzz*/ 0, /*resolution*/ 11);
7483
7484 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7485
7486 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7487
7488 // Touch major and minor
7489 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7490 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7491 // Tool major and minor
7492 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7493 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7494}
7495
Michael Wrightd02c5b62014-02-10 15:10:22 -08007496TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007497 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007498 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007499 prepareAxes(POSITION | ID);
7500 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007501 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007502
arthurhungdcef2dc2020-08-11 14:47:50 +08007503 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007504
7505 NotifyMotionArgs motionArgs;
7506
7507 // Two fingers down at once.
7508 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7509 processPosition(mapper, x1, y1);
7510 processId(mapper, 1);
7511 processMTSync(mapper);
7512 processPosition(mapper, x2, y2);
7513 processId(mapper, 2);
7514 processMTSync(mapper);
7515 processSync(mapper);
7516
7517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7518 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7519 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7520 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7521 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7523 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7524
7525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007526 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007527 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7528 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7529 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7530 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7531 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7532 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7533 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7535 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7536
7537 // Move.
7538 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7539 processPosition(mapper, x1, y1);
7540 processId(mapper, 1);
7541 processMTSync(mapper);
7542 processPosition(mapper, x2, y2);
7543 processId(mapper, 2);
7544 processMTSync(mapper);
7545 processSync(mapper);
7546
7547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7549 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7550 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7551 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7552 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7555 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7557 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7558
7559 // First finger up.
7560 x2 += 15; y2 -= 20;
7561 processPosition(mapper, x2, y2);
7562 processId(mapper, 2);
7563 processMTSync(mapper);
7564 processSync(mapper);
7565
7566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007567 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007568 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7569 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7571 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7574 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7576 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7577
7578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7580 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7581 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7584 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7585
7586 // Move.
7587 x2 += 20; y2 -= 25;
7588 processPosition(mapper, x2, y2);
7589 processId(mapper, 2);
7590 processMTSync(mapper);
7591 processSync(mapper);
7592
7593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7594 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7595 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7596 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7597 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7599 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7600
7601 // New finger down.
7602 int32_t x3 = 700, y3 = 300;
7603 processPosition(mapper, x2, y2);
7604 processId(mapper, 2);
7605 processMTSync(mapper);
7606 processPosition(mapper, x3, y3);
7607 processId(mapper, 3);
7608 processMTSync(mapper);
7609 processSync(mapper);
7610
7611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007612 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007613 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7614 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7615 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7616 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7617 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7619 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7621 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7622
7623 // Second finger up.
7624 x3 += 30; y3 -= 20;
7625 processPosition(mapper, x3, y3);
7626 processId(mapper, 3);
7627 processMTSync(mapper);
7628 processSync(mapper);
7629
7630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007631 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007632 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7633 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7635 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7638 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7640 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7641
7642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7643 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7644 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7645 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7646 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7648 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7649
7650 // Last finger up.
7651 processMTSync(mapper);
7652 processSync(mapper);
7653
7654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7655 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7656 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7657 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7660 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7661
7662 // Should not have sent any more keys or motions.
7663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7665}
7666
7667TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007668 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007669 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007670 prepareAxes(POSITION | ID | SLOT);
7671 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007672 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007673
arthurhungdcef2dc2020-08-11 14:47:50 +08007674 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007675
7676 NotifyMotionArgs motionArgs;
7677
7678 // Two fingers down at once.
7679 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7680 processPosition(mapper, x1, y1);
7681 processId(mapper, 1);
7682 processSlot(mapper, 1);
7683 processPosition(mapper, x2, y2);
7684 processId(mapper, 2);
7685 processSync(mapper);
7686
7687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7688 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7689 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7690 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7691 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7692 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7693 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7694
7695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007696 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007697 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7698 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7699 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7700 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7701 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7703 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7705 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7706
7707 // Move.
7708 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7709 processSlot(mapper, 0);
7710 processPosition(mapper, x1, y1);
7711 processSlot(mapper, 1);
7712 processPosition(mapper, x2, y2);
7713 processSync(mapper);
7714
7715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7717 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7718 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7720 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7721 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7723 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7725 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7726
7727 // First finger up.
7728 x2 += 15; y2 -= 20;
7729 processSlot(mapper, 0);
7730 processId(mapper, -1);
7731 processSlot(mapper, 1);
7732 processPosition(mapper, x2, y2);
7733 processSync(mapper);
7734
7735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007736 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007737 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7738 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7739 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7740 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7741 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7743 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7745 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7746
7747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7749 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7750 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7751 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7753 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7754
7755 // Move.
7756 x2 += 20; y2 -= 25;
7757 processPosition(mapper, x2, y2);
7758 processSync(mapper);
7759
7760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7761 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7762 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7763 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7764 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7766 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7767
7768 // New finger down.
7769 int32_t x3 = 700, y3 = 300;
7770 processPosition(mapper, x2, y2);
7771 processSlot(mapper, 0);
7772 processId(mapper, 3);
7773 processPosition(mapper, x3, y3);
7774 processSync(mapper);
7775
7776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007777 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007778 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7779 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7781 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7782 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7784 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7786 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7787
7788 // Second finger up.
7789 x3 += 30; y3 -= 20;
7790 processSlot(mapper, 1);
7791 processId(mapper, -1);
7792 processSlot(mapper, 0);
7793 processPosition(mapper, x3, y3);
7794 processSync(mapper);
7795
7796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007797 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007798 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7799 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7800 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7801 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7802 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7803 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7804 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7806 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7807
7808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7809 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7810 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7811 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7812 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7814 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7815
7816 // Last finger up.
7817 processId(mapper, -1);
7818 processSync(mapper);
7819
7820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7821 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7822 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7823 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7824 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7826 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7827
7828 // Should not have sent any more keys or motions.
7829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7831}
7832
7833TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007834 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007835 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007836 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007837 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007838
7839 // These calculations are based on the input device calibration documentation.
7840 int32_t rawX = 100;
7841 int32_t rawY = 200;
7842 int32_t rawTouchMajor = 7;
7843 int32_t rawTouchMinor = 6;
7844 int32_t rawToolMajor = 9;
7845 int32_t rawToolMinor = 8;
7846 int32_t rawPressure = 11;
7847 int32_t rawDistance = 0;
7848 int32_t rawOrientation = 3;
7849 int32_t id = 5;
7850
7851 float x = toDisplayX(rawX);
7852 float y = toDisplayY(rawY);
7853 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7854 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7855 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7856 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7857 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7858 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7859 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7860 float distance = float(rawDistance);
7861
7862 processPosition(mapper, rawX, rawY);
7863 processTouchMajor(mapper, rawTouchMajor);
7864 processTouchMinor(mapper, rawTouchMinor);
7865 processToolMajor(mapper, rawToolMajor);
7866 processToolMinor(mapper, rawToolMinor);
7867 processPressure(mapper, rawPressure);
7868 processOrientation(mapper, rawOrientation);
7869 processDistance(mapper, rawDistance);
7870 processId(mapper, id);
7871 processMTSync(mapper);
7872 processSync(mapper);
7873
7874 NotifyMotionArgs args;
7875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7876 ASSERT_EQ(0, args.pointerProperties[0].id);
7877 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7878 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7879 orientation, distance));
7880}
7881
7882TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007883 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007884 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007885 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7886 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007887 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007888
7889 // These calculations are based on the input device calibration documentation.
7890 int32_t rawX = 100;
7891 int32_t rawY = 200;
7892 int32_t rawTouchMajor = 140;
7893 int32_t rawTouchMinor = 120;
7894 int32_t rawToolMajor = 180;
7895 int32_t rawToolMinor = 160;
7896
7897 float x = toDisplayX(rawX);
7898 float y = toDisplayY(rawY);
7899 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7900 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7901 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7902 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7903 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7904
7905 processPosition(mapper, rawX, rawY);
7906 processTouchMajor(mapper, rawTouchMajor);
7907 processTouchMinor(mapper, rawTouchMinor);
7908 processToolMajor(mapper, rawToolMajor);
7909 processToolMinor(mapper, rawToolMinor);
7910 processMTSync(mapper);
7911 processSync(mapper);
7912
7913 NotifyMotionArgs args;
7914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7916 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7917}
7918
7919TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007920 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007921 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007922 prepareAxes(POSITION | TOUCH | TOOL);
7923 addConfigurationProperty("touch.size.calibration", "diameter");
7924 addConfigurationProperty("touch.size.scale", "10");
7925 addConfigurationProperty("touch.size.bias", "160");
7926 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007927 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007928
7929 // These calculations are based on the input device calibration documentation.
7930 // Note: We only provide a single common touch/tool value because the device is assumed
7931 // not to emit separate values for each pointer (isSummed = 1).
7932 int32_t rawX = 100;
7933 int32_t rawY = 200;
7934 int32_t rawX2 = 150;
7935 int32_t rawY2 = 250;
7936 int32_t rawTouchMajor = 5;
7937 int32_t rawToolMajor = 8;
7938
7939 float x = toDisplayX(rawX);
7940 float y = toDisplayY(rawY);
7941 float x2 = toDisplayX(rawX2);
7942 float y2 = toDisplayY(rawY2);
7943 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7944 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7945 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7946
7947 processPosition(mapper, rawX, rawY);
7948 processTouchMajor(mapper, rawTouchMajor);
7949 processToolMajor(mapper, rawToolMajor);
7950 processMTSync(mapper);
7951 processPosition(mapper, rawX2, rawY2);
7952 processTouchMajor(mapper, rawTouchMajor);
7953 processToolMajor(mapper, rawToolMajor);
7954 processMTSync(mapper);
7955 processSync(mapper);
7956
7957 NotifyMotionArgs args;
7958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7959 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7960
7961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007962 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007963 ASSERT_EQ(size_t(2), args.pointerCount);
7964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7965 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7967 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7968}
7969
7970TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007971 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007972 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007973 prepareAxes(POSITION | TOUCH | TOOL);
7974 addConfigurationProperty("touch.size.calibration", "area");
7975 addConfigurationProperty("touch.size.scale", "43");
7976 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007977 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007978
7979 // These calculations are based on the input device calibration documentation.
7980 int32_t rawX = 100;
7981 int32_t rawY = 200;
7982 int32_t rawTouchMajor = 5;
7983 int32_t rawToolMajor = 8;
7984
7985 float x = toDisplayX(rawX);
7986 float y = toDisplayY(rawY);
7987 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7988 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7989 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7990
7991 processPosition(mapper, rawX, rawY);
7992 processTouchMajor(mapper, rawTouchMajor);
7993 processToolMajor(mapper, rawToolMajor);
7994 processMTSync(mapper);
7995 processSync(mapper);
7996
7997 NotifyMotionArgs args;
7998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7999 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8000 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8001}
8002
8003TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008004 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008005 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008006 prepareAxes(POSITION | PRESSURE);
8007 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8008 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008009 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008010
Michael Wrightaa449c92017-12-13 21:21:43 +00008011 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008012 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008013 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8014 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8015 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8016
Michael Wrightd02c5b62014-02-10 15:10:22 -08008017 // These calculations are based on the input device calibration documentation.
8018 int32_t rawX = 100;
8019 int32_t rawY = 200;
8020 int32_t rawPressure = 60;
8021
8022 float x = toDisplayX(rawX);
8023 float y = toDisplayY(rawY);
8024 float pressure = float(rawPressure) * 0.01f;
8025
8026 processPosition(mapper, rawX, rawY);
8027 processPressure(mapper, rawPressure);
8028 processMTSync(mapper);
8029 processSync(mapper);
8030
8031 NotifyMotionArgs args;
8032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8034 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8035}
8036
8037TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008038 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008039 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008040 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008041 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008042
8043 NotifyMotionArgs motionArgs;
8044 NotifyKeyArgs keyArgs;
8045
8046 processId(mapper, 1);
8047 processPosition(mapper, 100, 200);
8048 processSync(mapper);
8049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8050 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8051 ASSERT_EQ(0, motionArgs.buttonState);
8052
8053 // press BTN_LEFT, release BTN_LEFT
8054 processKey(mapper, BTN_LEFT, 1);
8055 processSync(mapper);
8056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8057 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8058 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8059
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8061 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8062 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8063
Michael Wrightd02c5b62014-02-10 15:10:22 -08008064 processKey(mapper, BTN_LEFT, 0);
8065 processSync(mapper);
8066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008067 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008068 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008069
8070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008072 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008073
8074 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8075 processKey(mapper, BTN_RIGHT, 1);
8076 processKey(mapper, BTN_MIDDLE, 1);
8077 processSync(mapper);
8078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8079 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8080 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8081 motionArgs.buttonState);
8082
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8084 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8085 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8086
8087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8088 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8089 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8090 motionArgs.buttonState);
8091
Michael Wrightd02c5b62014-02-10 15:10:22 -08008092 processKey(mapper, BTN_RIGHT, 0);
8093 processSync(mapper);
8094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008095 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008096 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008097
8098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008099 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008100 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008101
8102 processKey(mapper, BTN_MIDDLE, 0);
8103 processSync(mapper);
8104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008105 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008106 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008107
8108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008109 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008110 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008111
8112 // press BTN_BACK, release BTN_BACK
8113 processKey(mapper, BTN_BACK, 1);
8114 processSync(mapper);
8115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8116 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8117 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008118
Michael Wrightd02c5b62014-02-10 15:10:22 -08008119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008120 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008121 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8122
8123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8124 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8125 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008126
8127 processKey(mapper, BTN_BACK, 0);
8128 processSync(mapper);
8129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008130 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008131 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008132
8133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008135 ASSERT_EQ(0, motionArgs.buttonState);
8136
Michael Wrightd02c5b62014-02-10 15:10:22 -08008137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8138 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8139 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8140
8141 // press BTN_SIDE, release BTN_SIDE
8142 processKey(mapper, BTN_SIDE, 1);
8143 processSync(mapper);
8144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8145 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8146 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008147
Michael Wrightd02c5b62014-02-10 15:10:22 -08008148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008150 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8151
8152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8153 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8154 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008155
8156 processKey(mapper, BTN_SIDE, 0);
8157 processSync(mapper);
8158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008159 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008160 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008161
8162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008164 ASSERT_EQ(0, motionArgs.buttonState);
8165
Michael Wrightd02c5b62014-02-10 15:10:22 -08008166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8167 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8168 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8169
8170 // press BTN_FORWARD, release BTN_FORWARD
8171 processKey(mapper, BTN_FORWARD, 1);
8172 processSync(mapper);
8173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8174 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8175 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008176
Michael Wrightd02c5b62014-02-10 15:10:22 -08008177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008179 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8180
8181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8182 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8183 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008184
8185 processKey(mapper, BTN_FORWARD, 0);
8186 processSync(mapper);
8187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008188 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008189 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008190
8191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008193 ASSERT_EQ(0, motionArgs.buttonState);
8194
Michael Wrightd02c5b62014-02-10 15:10:22 -08008195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8196 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8197 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8198
8199 // press BTN_EXTRA, release BTN_EXTRA
8200 processKey(mapper, BTN_EXTRA, 1);
8201 processSync(mapper);
8202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8203 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8204 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008205
Michael Wrightd02c5b62014-02-10 15:10:22 -08008206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008207 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008208 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8209
8210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8211 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8212 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008213
8214 processKey(mapper, BTN_EXTRA, 0);
8215 processSync(mapper);
8216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008217 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008218 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008219
8220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008222 ASSERT_EQ(0, motionArgs.buttonState);
8223
Michael Wrightd02c5b62014-02-10 15:10:22 -08008224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8225 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8226 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8227
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8229
Michael Wrightd02c5b62014-02-10 15:10:22 -08008230 // press BTN_STYLUS, release BTN_STYLUS
8231 processKey(mapper, BTN_STYLUS, 1);
8232 processSync(mapper);
8233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008235 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8236
8237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8238 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8239 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008240
8241 processKey(mapper, BTN_STYLUS, 0);
8242 processSync(mapper);
8243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008244 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008245 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008246
8247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008249 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008250
8251 // press BTN_STYLUS2, release BTN_STYLUS2
8252 processKey(mapper, BTN_STYLUS2, 1);
8253 processSync(mapper);
8254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008256 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8257
8258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8259 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8260 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008261
8262 processKey(mapper, BTN_STYLUS2, 0);
8263 processSync(mapper);
8264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008265 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008266 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008267
8268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008270 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008271
8272 // release touch
8273 processId(mapper, -1);
8274 processSync(mapper);
8275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8276 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8277 ASSERT_EQ(0, motionArgs.buttonState);
8278}
8279
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008280TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8281 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008282 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008283 prepareAxes(POSITION | ID | SLOT);
8284 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8285
8286 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8287 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8288
8289 // Touch down.
8290 processId(mapper, 1);
8291 processPosition(mapper, 100, 200);
8292 processSync(mapper);
8293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8294 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8295
8296 // Press and release button mapped to the primary stylus button.
8297 processKey(mapper, BTN_A, 1);
8298 processSync(mapper);
8299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8300 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8301 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8303 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8304 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8305
8306 processKey(mapper, BTN_A, 0);
8307 processSync(mapper);
8308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8309 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8311 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8312
8313 // Press and release the HID usage mapped to the secondary stylus button.
8314 processHidUsage(mapper, 0xabcd, 1);
8315 processSync(mapper);
8316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8317 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8318 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8320 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8321 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8322
8323 processHidUsage(mapper, 0xabcd, 0);
8324 processSync(mapper);
8325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8326 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8328 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8329
8330 // Release touch.
8331 processId(mapper, -1);
8332 processSync(mapper);
8333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8334 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8335}
8336
Michael Wrightd02c5b62014-02-10 15:10:22 -08008337TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008338 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008339 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008340 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008341 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008342
8343 NotifyMotionArgs motionArgs;
8344
8345 // default tool type is finger
8346 processId(mapper, 1);
8347 processPosition(mapper, 100, 200);
8348 processSync(mapper);
8349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8350 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8351 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8352
8353 // eraser
8354 processKey(mapper, BTN_TOOL_RUBBER, 1);
8355 processSync(mapper);
8356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8359
8360 // stylus
8361 processKey(mapper, BTN_TOOL_RUBBER, 0);
8362 processKey(mapper, BTN_TOOL_PEN, 1);
8363 processSync(mapper);
8364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8365 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8366 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8367
8368 // brush
8369 processKey(mapper, BTN_TOOL_PEN, 0);
8370 processKey(mapper, BTN_TOOL_BRUSH, 1);
8371 processSync(mapper);
8372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8373 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8374 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8375
8376 // pencil
8377 processKey(mapper, BTN_TOOL_BRUSH, 0);
8378 processKey(mapper, BTN_TOOL_PENCIL, 1);
8379 processSync(mapper);
8380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8382 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8383
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008384 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008385 processKey(mapper, BTN_TOOL_PENCIL, 0);
8386 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8387 processSync(mapper);
8388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8390 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8391
8392 // mouse
8393 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8394 processKey(mapper, BTN_TOOL_MOUSE, 1);
8395 processSync(mapper);
8396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8398 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8399
8400 // lens
8401 processKey(mapper, BTN_TOOL_MOUSE, 0);
8402 processKey(mapper, BTN_TOOL_LENS, 1);
8403 processSync(mapper);
8404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8405 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8406 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8407
8408 // double-tap
8409 processKey(mapper, BTN_TOOL_LENS, 0);
8410 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8411 processSync(mapper);
8412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8413 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8414 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8415
8416 // triple-tap
8417 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8418 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8419 processSync(mapper);
8420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8421 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8422 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8423
8424 // quad-tap
8425 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8426 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8427 processSync(mapper);
8428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8430 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8431
8432 // finger
8433 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8434 processKey(mapper, BTN_TOOL_FINGER, 1);
8435 processSync(mapper);
8436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8437 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8439
8440 // stylus trumps finger
8441 processKey(mapper, BTN_TOOL_PEN, 1);
8442 processSync(mapper);
8443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8445 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8446
8447 // eraser trumps stylus
8448 processKey(mapper, BTN_TOOL_RUBBER, 1);
8449 processSync(mapper);
8450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8452 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8453
8454 // mouse trumps eraser
8455 processKey(mapper, BTN_TOOL_MOUSE, 1);
8456 processSync(mapper);
8457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8459 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8460
8461 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8462 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8463 processSync(mapper);
8464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8465 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8466 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8467
8468 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8469 processToolType(mapper, MT_TOOL_PEN);
8470 processSync(mapper);
8471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8473 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8474
8475 // back to default tool type
8476 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8477 processKey(mapper, BTN_TOOL_MOUSE, 0);
8478 processKey(mapper, BTN_TOOL_RUBBER, 0);
8479 processKey(mapper, BTN_TOOL_PEN, 0);
8480 processKey(mapper, BTN_TOOL_FINGER, 0);
8481 processSync(mapper);
8482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8483 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8484 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8485}
8486
8487TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008488 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008489 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008490 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008491 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008492 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008493
8494 NotifyMotionArgs motionArgs;
8495
8496 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8497 processId(mapper, 1);
8498 processPosition(mapper, 100, 200);
8499 processSync(mapper);
8500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8501 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8502 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8503 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8504
8505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8506 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8508 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8509
8510 // move a little
8511 processPosition(mapper, 150, 250);
8512 processSync(mapper);
8513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8514 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8516 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8517
8518 // down when BTN_TOUCH is pressed, pressure defaults to 1
8519 processKey(mapper, BTN_TOUCH, 1);
8520 processSync(mapper);
8521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8522 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8524 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8525
8526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8527 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8529 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8530
8531 // up when BTN_TOUCH is released, hover restored
8532 processKey(mapper, BTN_TOUCH, 0);
8533 processSync(mapper);
8534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8535 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8536 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8537 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8538
8539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8540 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8542 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8543
8544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8545 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8547 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8548
8549 // exit hover when pointer goes away
8550 processId(mapper, -1);
8551 processSync(mapper);
8552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8553 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8555 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8556}
8557
8558TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008559 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008560 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008561 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008562 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008563
8564 NotifyMotionArgs motionArgs;
8565
8566 // initially hovering because pressure is 0
8567 processId(mapper, 1);
8568 processPosition(mapper, 100, 200);
8569 processPressure(mapper, 0);
8570 processSync(mapper);
8571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8572 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8574 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8575
8576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8577 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8579 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8580
8581 // move a little
8582 processPosition(mapper, 150, 250);
8583 processSync(mapper);
8584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8585 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8587 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8588
8589 // down when pressure becomes non-zero
8590 processPressure(mapper, RAW_PRESSURE_MAX);
8591 processSync(mapper);
8592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8593 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8595 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8596
8597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8598 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8600 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8601
8602 // up when pressure becomes 0, hover restored
8603 processPressure(mapper, 0);
8604 processSync(mapper);
8605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8606 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8608 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8609
8610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8611 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8613 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8614
8615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8616 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8618 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8619
8620 // exit hover when pointer goes away
8621 processId(mapper, -1);
8622 processSync(mapper);
8623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8624 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8626 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8627}
8628
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008629/**
8630 * Set the input device port <--> display port associations, and check that the
8631 * events are routed to the display that matches the display port.
8632 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8633 */
8634TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008635 const std::string usb2 = "USB2";
8636 const uint8_t hdmi1 = 0;
8637 const uint8_t hdmi2 = 1;
8638 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008639 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008640
8641 addConfigurationProperty("touch.deviceType", "touchScreen");
8642 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008643 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008644
8645 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8646 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8647
8648 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8649 // for this input device is specified, and the matching viewport is not present,
8650 // the input device should be disabled (at the mapper level).
8651
8652 // Add viewport for display 2 on hdmi2
8653 prepareSecondaryDisplay(type, hdmi2);
8654 // Send a touch event
8655 processPosition(mapper, 100, 100);
8656 processSync(mapper);
8657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8658
8659 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008660 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008661 // Send a touch event again
8662 processPosition(mapper, 100, 100);
8663 processSync(mapper);
8664
8665 NotifyMotionArgs args;
8666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8667 ASSERT_EQ(DISPLAY_ID, args.displayId);
8668}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008669
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008670TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8671 addConfigurationProperty("touch.deviceType", "touchScreen");
8672 prepareAxes(POSITION);
8673 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8674
8675 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8676
Michael Wrighta9cf4192022-12-01 23:46:39 +00008677 prepareDisplay(ui::ROTATION_0);
8678 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008679
8680 // Send a touch event
8681 processPosition(mapper, 100, 100);
8682 processSync(mapper);
8683
8684 NotifyMotionArgs args;
8685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8686 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8687}
8688
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008689TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008690 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008691 std::shared_ptr<FakePointerController> fakePointerController =
8692 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008693 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008694 fakePointerController->setPosition(100, 200);
8695 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008696 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008697
Garfield Tan888a6a42020-01-09 11:39:16 -08008698 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008699 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008700
Michael Wrighta9cf4192022-12-01 23:46:39 +00008701 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008702 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008703 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008704
Harry Cutts16a24cc2022-10-26 15:22:19 +00008705 // Check source is a touchpad that would obtain the PointerController.
8706 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008707
8708 NotifyMotionArgs motionArgs;
8709 processPosition(mapper, 100, 100);
8710 processSync(mapper);
8711
8712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8713 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8714 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8715}
8716
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008717/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008718 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8719 */
8720TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8721 addConfigurationProperty("touch.deviceType", "touchScreen");
8722 prepareAxes(POSITION);
8723 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8724
Michael Wrighta9cf4192022-12-01 23:46:39 +00008725 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008726 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8727 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8728 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8729 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8730
8731 NotifyMotionArgs args;
8732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8733 ASSERT_EQ(26, args.readTime);
8734
8735 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8736 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8737 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8738
8739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8740 ASSERT_EQ(33, args.readTime);
8741}
8742
8743/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008744 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8745 * events should not be delivered to the listener.
8746 */
8747TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8748 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008749 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008750 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8751 false /*isActive*/, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008752 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8753 prepareAxes(POSITION);
8754 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8755
8756 NotifyMotionArgs motionArgs;
8757 processPosition(mapper, 100, 100);
8758 processSync(mapper);
8759
8760 mFakeListener->assertNotifyMotionWasNotCalled();
8761}
8762
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008763/**
8764 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8765 * the touch mapper can process the events and the events can be delivered to the listener.
8766 */
8767TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8768 addConfigurationProperty("touch.deviceType", "touchScreen");
8769 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008770 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8771 false /*isActive*/, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008772 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8773 prepareAxes(POSITION);
8774 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8775
8776 NotifyMotionArgs motionArgs;
8777 processPosition(mapper, 100, 100);
8778 processSync(mapper);
8779
8780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8781 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8782}
8783
Garfield Tanc734e4f2021-01-15 20:01:39 -08008784TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8785 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008786 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008787 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8788 true /*isActive*/, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008789 std::optional<DisplayViewport> optionalDisplayViewport =
8790 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8791 ASSERT_TRUE(optionalDisplayViewport.has_value());
8792 DisplayViewport displayViewport = *optionalDisplayViewport;
8793
8794 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8795 prepareAxes(POSITION);
8796 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8797
8798 // Finger down
8799 int32_t x = 100, y = 100;
8800 processPosition(mapper, x, y);
8801 processSync(mapper);
8802
8803 NotifyMotionArgs motionArgs;
8804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8805 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8806
8807 // Deactivate display viewport
8808 displayViewport.isActive = false;
8809 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8810 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8811
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008812 // The ongoing touch should be canceled immediately
8813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8814 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8815
8816 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008817 x += 10, y += 10;
8818 processPosition(mapper, x, y);
8819 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008821
8822 // Reactivate display viewport
8823 displayViewport.isActive = true;
8824 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8825 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8826
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008827 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008828 x += 10, y += 10;
8829 processPosition(mapper, x, y);
8830 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8832 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008833}
8834
Arthur Hung7c645402019-01-25 17:45:42 +08008835TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8836 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008837 prepareAxes(POSITION | ID | SLOT);
8838 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008839 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008840
8841 // Create the second touch screen device, and enable multi fingers.
8842 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008843 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008844 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008845 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008846 std::shared_ptr<InputDevice> device2 =
8847 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008848 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008849
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008850 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8851 0 /*flat*/, 0 /*fuzz*/);
8852 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8853 0 /*flat*/, 0 /*fuzz*/);
8854 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8855 0 /*flat*/, 0 /*fuzz*/);
8856 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8857 0 /*flat*/, 0 /*fuzz*/);
8858 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8859 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8860 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008861
8862 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008863 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008864 std::list<NotifyArgs> unused =
8865 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8866 0 /*changes*/);
8867 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08008868
8869 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008870 std::shared_ptr<FakePointerController> fakePointerController =
8871 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008872 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008873
8874 // Setup policy for associated displays and show touches.
8875 const uint8_t hdmi1 = 0;
8876 const uint8_t hdmi2 = 1;
8877 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8878 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8879 mFakePolicy->setShowTouches(true);
8880
8881 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008882 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008883 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008884
8885 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008886 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8887 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8888 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008889
8890 // Two fingers down at default display.
8891 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8892 processPosition(mapper, x1, y1);
8893 processId(mapper, 1);
8894 processSlot(mapper, 1);
8895 processPosition(mapper, x2, y2);
8896 processId(mapper, 2);
8897 processSync(mapper);
8898
8899 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8900 fakePointerController->getSpots().find(DISPLAY_ID);
8901 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8902 ASSERT_EQ(size_t(2), iter->second.size());
8903
8904 // Two fingers down at second display.
8905 processPosition(mapper2, x1, y1);
8906 processId(mapper2, 1);
8907 processSlot(mapper2, 1);
8908 processPosition(mapper2, x2, y2);
8909 processId(mapper2, 2);
8910 processSync(mapper2);
8911
8912 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8913 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8914 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008915
8916 // Disable the show touches configuration and ensure the spots are cleared.
8917 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008918 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8919 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00008920
8921 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008922}
8923
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008924TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008925 prepareAxes(POSITION);
8926 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008927 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008928 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008929
8930 NotifyMotionArgs motionArgs;
8931 // Unrotated video frame
8932 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8933 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008934 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008935 processPosition(mapper, 100, 200);
8936 processSync(mapper);
8937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8938 ASSERT_EQ(frames, motionArgs.videoFrames);
8939
8940 // Subsequent touch events should not have any videoframes
8941 // This is implemented separately in FakeEventHub,
8942 // but that should match the behaviour of TouchVideoDevice.
8943 processPosition(mapper, 200, 200);
8944 processSync(mapper);
8945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8946 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8947}
8948
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008949TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008950 prepareAxes(POSITION);
8951 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008952 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008953 // Unrotated video frame
8954 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8955 NotifyMotionArgs motionArgs;
8956
8957 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008958 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008959 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8960 clearViewports();
8961 prepareDisplay(orientation);
8962 std::vector<TouchVideoFrame> frames{frame};
8963 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8964 processPosition(mapper, 100, 200);
8965 processSync(mapper);
8966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8967 ASSERT_EQ(frames, motionArgs.videoFrames);
8968 }
8969}
8970
8971TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8972 prepareAxes(POSITION);
8973 addConfigurationProperty("touch.deviceType", "touchScreen");
8974 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8975 // orientation-aware are affected by display rotation.
8976 addConfigurationProperty("touch.orientationAware", "0");
8977 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8978 // Unrotated video frame
8979 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8980 NotifyMotionArgs motionArgs;
8981
8982 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008983 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008984 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8985 clearViewports();
8986 prepareDisplay(orientation);
8987 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008988 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008989 processPosition(mapper, 100, 200);
8990 processSync(mapper);
8991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008992 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8993 // compared to the display. This is so that when the window transform (which contains the
8994 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8995 // window's coordinate space.
8996 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008997 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008998
8999 // Release finger.
9000 processSync(mapper);
9001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009002 }
9003}
9004
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009005TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009006 prepareAxes(POSITION);
9007 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009008 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009009 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9010 // so mix these.
9011 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9012 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9013 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9014 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9015 NotifyMotionArgs motionArgs;
9016
Michael Wrighta9cf4192022-12-01 23:46:39 +00009017 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009018 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009019 processPosition(mapper, 100, 200);
9020 processSync(mapper);
9021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009022 ASSERT_EQ(frames, motionArgs.videoFrames);
9023}
9024
9025TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9026 prepareAxes(POSITION);
9027 addConfigurationProperty("touch.deviceType", "touchScreen");
9028 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9029 // orientation-aware are affected by display rotation.
9030 addConfigurationProperty("touch.orientationAware", "0");
9031 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9032 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9033 // so mix these.
9034 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9035 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9036 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9037 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9038 NotifyMotionArgs motionArgs;
9039
Michael Wrighta9cf4192022-12-01 23:46:39 +00009040 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009041 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9042 processPosition(mapper, 100, 200);
9043 processSync(mapper);
9044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9045 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9046 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9047 // compared to the display. This is so that when the window transform (which contains the
9048 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9049 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009050 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009051 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009052 ASSERT_EQ(frames, motionArgs.videoFrames);
9053}
9054
Arthur Hung9da14732019-09-02 16:16:58 +08009055/**
9056 * If we had defined port associations, but the viewport is not ready, the touch device would be
9057 * expected to be disabled, and it should be enabled after the viewport has found.
9058 */
9059TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009060 constexpr uint8_t hdmi2 = 1;
9061 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009062 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009063
9064 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9065
9066 addConfigurationProperty("touch.deviceType", "touchScreen");
9067 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009068 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009069
9070 ASSERT_EQ(mDevice->isEnabled(), false);
9071
9072 // Add display on hdmi2, the device should be enabled and can receive touch event.
9073 prepareSecondaryDisplay(type, hdmi2);
9074 ASSERT_EQ(mDevice->isEnabled(), true);
9075
9076 // Send a touch event.
9077 processPosition(mapper, 100, 100);
9078 processSync(mapper);
9079
9080 NotifyMotionArgs args;
9081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9082 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9083}
9084
Arthur Hung421eb1c2020-01-16 00:09:42 +08009085TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009086 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009087 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009088 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009089 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009090
9091 NotifyMotionArgs motionArgs;
9092
9093 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9094 // finger down
9095 processId(mapper, 1);
9096 processPosition(mapper, x1, y1);
9097 processSync(mapper);
9098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9099 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9100 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9101
9102 // finger move
9103 processId(mapper, 1);
9104 processPosition(mapper, x2, y2);
9105 processSync(mapper);
9106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9108 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9109
9110 // finger up.
9111 processId(mapper, -1);
9112 processSync(mapper);
9113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9114 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9115 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9116
9117 // new finger down
9118 processId(mapper, 1);
9119 processPosition(mapper, x3, y3);
9120 processSync(mapper);
9121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9122 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9124}
9125
9126/**
arthurhungcc7f9802020-04-30 17:55:40 +08009127 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9128 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009129 */
arthurhungcc7f9802020-04-30 17:55:40 +08009130TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009131 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009132 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009133 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009134 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009135
9136 NotifyMotionArgs motionArgs;
9137
9138 // default tool type is finger
9139 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009140 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009141 processPosition(mapper, x1, y1);
9142 processSync(mapper);
9143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9144 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9145 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9146
9147 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9148 processToolType(mapper, MT_TOOL_PALM);
9149 processSync(mapper);
9150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9151 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9152
9153 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009154 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009155 processPosition(mapper, x2, y2);
9156 processSync(mapper);
9157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9158
9159 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009160 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009161 processSync(mapper);
9162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9163
9164 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009165 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009166 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009167 processPosition(mapper, x3, y3);
9168 processSync(mapper);
9169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9170 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9171 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9172}
9173
arthurhungbf89a482020-04-17 17:37:55 +08009174/**
arthurhungcc7f9802020-04-30 17:55:40 +08009175 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9176 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009177 */
arthurhungcc7f9802020-04-30 17:55:40 +08009178TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009179 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009180 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009181 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9182 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9183
9184 NotifyMotionArgs motionArgs;
9185
9186 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009187 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9188 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009189 processPosition(mapper, x1, y1);
9190 processSync(mapper);
9191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9192 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9193 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9194
9195 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009196 processSlot(mapper, SECOND_SLOT);
9197 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009198 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009199 processSync(mapper);
9200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009201 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009202 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9203
9204 // If the tool type of the first finger changes to MT_TOOL_PALM,
9205 // we expect to receive ACTION_POINTER_UP with cancel flag.
9206 processSlot(mapper, FIRST_SLOT);
9207 processId(mapper, FIRST_TRACKING_ID);
9208 processToolType(mapper, MT_TOOL_PALM);
9209 processSync(mapper);
9210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009211 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009212 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9213
9214 // The following MOVE events of second finger should be processed.
9215 processSlot(mapper, SECOND_SLOT);
9216 processId(mapper, SECOND_TRACKING_ID);
9217 processPosition(mapper, x2 + 1, y2 + 1);
9218 processSync(mapper);
9219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9221 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9222
9223 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9224 // it. Second finger receive move.
9225 processSlot(mapper, FIRST_SLOT);
9226 processId(mapper, INVALID_TRACKING_ID);
9227 processSync(mapper);
9228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9230 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9231
9232 // Second finger keeps moving.
9233 processSlot(mapper, SECOND_SLOT);
9234 processId(mapper, SECOND_TRACKING_ID);
9235 processPosition(mapper, x2 + 2, y2 + 2);
9236 processSync(mapper);
9237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9238 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9239 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9240
9241 // Second finger up.
9242 processId(mapper, INVALID_TRACKING_ID);
9243 processSync(mapper);
9244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9245 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9246 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9247}
9248
9249/**
9250 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9251 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9252 */
9253TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9254 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009255 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009256 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9257 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9258
9259 NotifyMotionArgs motionArgs;
9260
9261 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9262 // First finger down.
9263 processId(mapper, FIRST_TRACKING_ID);
9264 processPosition(mapper, x1, y1);
9265 processSync(mapper);
9266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9267 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9268 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9269
9270 // Second finger down.
9271 processSlot(mapper, SECOND_SLOT);
9272 processId(mapper, SECOND_TRACKING_ID);
9273 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009274 processSync(mapper);
9275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009276 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9278
arthurhungcc7f9802020-04-30 17:55:40 +08009279 // If the tool type of the first finger changes to MT_TOOL_PALM,
9280 // we expect to receive ACTION_POINTER_UP with cancel flag.
9281 processSlot(mapper, FIRST_SLOT);
9282 processId(mapper, FIRST_TRACKING_ID);
9283 processToolType(mapper, MT_TOOL_PALM);
9284 processSync(mapper);
9285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009286 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009287 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9288
9289 // Second finger keeps moving.
9290 processSlot(mapper, SECOND_SLOT);
9291 processId(mapper, SECOND_TRACKING_ID);
9292 processPosition(mapper, x2 + 1, y2 + 1);
9293 processSync(mapper);
9294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9295 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9296
9297 // second finger becomes palm, receive cancel due to only 1 finger is active.
9298 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009299 processToolType(mapper, MT_TOOL_PALM);
9300 processSync(mapper);
9301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9302 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9303
arthurhungcc7f9802020-04-30 17:55:40 +08009304 // third finger down.
9305 processSlot(mapper, THIRD_SLOT);
9306 processId(mapper, THIRD_TRACKING_ID);
9307 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009308 processPosition(mapper, x3, y3);
9309 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9311 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9312 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009313 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9314
9315 // third finger move
9316 processId(mapper, THIRD_TRACKING_ID);
9317 processPosition(mapper, x3 + 1, y3 + 1);
9318 processSync(mapper);
9319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9321
9322 // first finger up, third finger receive move.
9323 processSlot(mapper, FIRST_SLOT);
9324 processId(mapper, INVALID_TRACKING_ID);
9325 processSync(mapper);
9326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9328 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9329
9330 // second finger up, third finger receive move.
9331 processSlot(mapper, SECOND_SLOT);
9332 processId(mapper, INVALID_TRACKING_ID);
9333 processSync(mapper);
9334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9336 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9337
9338 // third finger up.
9339 processSlot(mapper, THIRD_SLOT);
9340 processId(mapper, INVALID_TRACKING_ID);
9341 processSync(mapper);
9342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9343 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9344 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9345}
9346
9347/**
9348 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9349 * and the active finger could still be allowed to receive the events
9350 */
9351TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9352 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009353 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009354 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9355 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9356
9357 NotifyMotionArgs motionArgs;
9358
9359 // default tool type is finger
9360 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9361 processId(mapper, FIRST_TRACKING_ID);
9362 processPosition(mapper, x1, y1);
9363 processSync(mapper);
9364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9365 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9366 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9367
9368 // Second finger down.
9369 processSlot(mapper, SECOND_SLOT);
9370 processId(mapper, SECOND_TRACKING_ID);
9371 processPosition(mapper, x2, y2);
9372 processSync(mapper);
9373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009374 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9376
9377 // If the tool type of the second finger changes to MT_TOOL_PALM,
9378 // we expect to receive ACTION_POINTER_UP with cancel flag.
9379 processId(mapper, SECOND_TRACKING_ID);
9380 processToolType(mapper, MT_TOOL_PALM);
9381 processSync(mapper);
9382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009383 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009384 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9385
9386 // The following MOVE event should be processed.
9387 processSlot(mapper, FIRST_SLOT);
9388 processId(mapper, FIRST_TRACKING_ID);
9389 processPosition(mapper, x1 + 1, y1 + 1);
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 up.
9396 processSlot(mapper, SECOND_SLOT);
9397 processId(mapper, INVALID_TRACKING_ID);
9398 processSync(mapper);
9399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9401
9402 // first finger keep moving
9403 processSlot(mapper, FIRST_SLOT);
9404 processId(mapper, FIRST_TRACKING_ID);
9405 processPosition(mapper, x1 + 2, y1 + 2);
9406 processSync(mapper);
9407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9409
9410 // first finger up.
9411 processId(mapper, INVALID_TRACKING_ID);
9412 processSync(mapper);
9413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9414 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9415 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009416}
9417
Arthur Hung9ad18942021-06-19 02:04:46 +00009418/**
9419 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9420 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9421 * cause slot be valid again.
9422 */
9423TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9424 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009425 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009426 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9427 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9428
9429 NotifyMotionArgs motionArgs;
9430
9431 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9432 // First finger down.
9433 processId(mapper, FIRST_TRACKING_ID);
9434 processPosition(mapper, x1, y1);
9435 processPressure(mapper, RAW_PRESSURE_MAX);
9436 processSync(mapper);
9437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9438 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9439 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9440
9441 // First finger move.
9442 processId(mapper, FIRST_TRACKING_ID);
9443 processPosition(mapper, x1 + 1, y1 + 1);
9444 processPressure(mapper, RAW_PRESSURE_MAX);
9445 processSync(mapper);
9446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9448 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9449
9450 // Second finger down.
9451 processSlot(mapper, SECOND_SLOT);
9452 processId(mapper, SECOND_TRACKING_ID);
9453 processPosition(mapper, x2, y2);
9454 processPressure(mapper, RAW_PRESSURE_MAX);
9455 processSync(mapper);
9456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009457 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009458 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9459
9460 // second finger up with some unexpected data.
9461 processSlot(mapper, SECOND_SLOT);
9462 processId(mapper, INVALID_TRACKING_ID);
9463 processPosition(mapper, x2, y2);
9464 processSync(mapper);
9465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009466 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009467 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9468
9469 // first finger up with some unexpected data.
9470 processSlot(mapper, FIRST_SLOT);
9471 processId(mapper, INVALID_TRACKING_ID);
9472 processPosition(mapper, x2, y2);
9473 processPressure(mapper, RAW_PRESSURE_MAX);
9474 processSync(mapper);
9475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9476 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9477 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9478}
9479
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009480TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9481 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009482 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009483 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9484 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9485
9486 // First finger down.
9487 processId(mapper, FIRST_TRACKING_ID);
9488 processPosition(mapper, 100, 200);
9489 processPressure(mapper, RAW_PRESSURE_MAX);
9490 processSync(mapper);
9491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9492 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9493
9494 // Second finger down.
9495 processSlot(mapper, SECOND_SLOT);
9496 processId(mapper, SECOND_TRACKING_ID);
9497 processPosition(mapper, 300, 400);
9498 processPressure(mapper, RAW_PRESSURE_MAX);
9499 processSync(mapper);
9500 ASSERT_NO_FATAL_FAILURE(
9501 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9502
9503 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009504 // preserved. Resetting should cancel the ongoing gesture.
9505 resetMapper(mapper, ARBITRARY_TIME);
9506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9507 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009508
9509 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9510 // the existing touch state to generate a down event.
9511 processPosition(mapper, 301, 302);
9512 processSync(mapper);
9513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9514 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9516 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9517
9518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9519}
9520
9521TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9522 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009523 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009524 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9525 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9526
9527 // First finger touches down and releases.
9528 processId(mapper, FIRST_TRACKING_ID);
9529 processPosition(mapper, 100, 200);
9530 processPressure(mapper, RAW_PRESSURE_MAX);
9531 processSync(mapper);
9532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9533 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9534 processId(mapper, INVALID_TRACKING_ID);
9535 processSync(mapper);
9536 ASSERT_NO_FATAL_FAILURE(
9537 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9538
9539 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9540 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009541 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9543
9544 // Send an empty sync frame. Since there are no pointers, no events are generated.
9545 processSync(mapper);
9546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9547}
9548
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009549TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009550 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009551 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009552 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
9553 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009555
9556 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9557 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9558 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9559 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9560 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9561
9562 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009563 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009564 processId(mapper, FIRST_TRACKING_ID);
9565 processToolType(mapper, MT_TOOL_PEN);
9566 processPosition(mapper, 100, 200);
9567 processPressure(mapper, RAW_PRESSURE_MAX);
9568 processSync(mapper);
9569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9570 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9571 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9572 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
9573
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009574 // Now that we know the device supports styluses, ensure that the device is re-configured with
9575 // the stylus source.
9576 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9577 {
9578 const auto& devices = mReader->getInputDevices();
9579 auto deviceInfo =
9580 std::find_if(devices.begin(), devices.end(),
9581 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9582 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9583 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9584 }
9585
9586 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9588
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009589 processId(mapper, INVALID_TRACKING_ID);
9590 processSync(mapper);
9591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9592 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9593 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9594 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009595}
9596
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009597// --- MultiTouchInputMapperTest_ExternalDevice ---
9598
9599class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9600protected:
Chris Yea52ade12020-08-27 16:49:20 -07009601 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009602};
9603
9604/**
9605 * Expect fallback to internal viewport if device is external and external viewport is not present.
9606 */
9607TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9608 prepareAxes(POSITION);
9609 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009610 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009611 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9612
9613 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9614
9615 NotifyMotionArgs motionArgs;
9616
9617 // Expect the event to be sent to the internal viewport,
9618 // because an external viewport is not present.
9619 processPosition(mapper, 100, 100);
9620 processSync(mapper);
9621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9622 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9623
9624 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009625 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009626 processPosition(mapper, 100, 100);
9627 processSync(mapper);
9628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9629 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9630}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009631
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009632TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9633 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9634 std::shared_ptr<FakePointerController> fakePointerController =
9635 std::make_shared<FakePointerController>();
9636 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9637 fakePointerController->setPosition(0, 0);
9638 fakePointerController->setButtonState(0);
9639
9640 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009641 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009642 prepareAxes(POSITION | ID | SLOT);
9643 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9644 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9645 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009646 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009647 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9648
9649 // captured touchpad should be a touchpad source
9650 NotifyDeviceResetArgs resetArgs;
9651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9652 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9653
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009654 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009655
9656 const InputDeviceInfo::MotionRange* relRangeX =
9657 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9658 ASSERT_NE(relRangeX, nullptr);
9659 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9660 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9661 const InputDeviceInfo::MotionRange* relRangeY =
9662 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9663 ASSERT_NE(relRangeY, nullptr);
9664 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9665 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9666
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009667 // run captured pointer tests - note that this is unscaled, so input listener events should be
9668 // identical to what the hardware sends (accounting for any
9669 // calibration).
9670 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009671 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009672 processId(mapper, 1);
9673 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9674 processKey(mapper, BTN_TOUCH, 1);
9675 processSync(mapper);
9676
9677 // expect coord[0] to contain initial location of touch 0
9678 NotifyMotionArgs args;
9679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9680 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9681 ASSERT_EQ(1U, args.pointerCount);
9682 ASSERT_EQ(0, args.pointerProperties[0].id);
9683 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9684 ASSERT_NO_FATAL_FAILURE(
9685 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9686
9687 // FINGER 1 DOWN
9688 processSlot(mapper, 1);
9689 processId(mapper, 2);
9690 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9691 processSync(mapper);
9692
9693 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009695 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009696 ASSERT_EQ(2U, args.pointerCount);
9697 ASSERT_EQ(0, args.pointerProperties[0].id);
9698 ASSERT_EQ(1, args.pointerProperties[1].id);
9699 ASSERT_NO_FATAL_FAILURE(
9700 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9701 ASSERT_NO_FATAL_FAILURE(
9702 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9703
9704 // FINGER 1 MOVE
9705 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9706 processSync(mapper);
9707
9708 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9709 // from move
9710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9711 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9712 ASSERT_NO_FATAL_FAILURE(
9713 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9714 ASSERT_NO_FATAL_FAILURE(
9715 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9716
9717 // FINGER 0 MOVE
9718 processSlot(mapper, 0);
9719 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9720 processSync(mapper);
9721
9722 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9725 ASSERT_NO_FATAL_FAILURE(
9726 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9727 ASSERT_NO_FATAL_FAILURE(
9728 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9729
9730 // BUTTON DOWN
9731 processKey(mapper, BTN_LEFT, 1);
9732 processSync(mapper);
9733
9734 // touchinputmapper design sends a move before button press
9735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9736 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9738 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9739
9740 // BUTTON UP
9741 processKey(mapper, BTN_LEFT, 0);
9742 processSync(mapper);
9743
9744 // touchinputmapper design sends a move after button release
9745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9746 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9749
9750 // FINGER 0 UP
9751 processId(mapper, -1);
9752 processSync(mapper);
9753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9754 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9755
9756 // FINGER 1 MOVE
9757 processSlot(mapper, 1);
9758 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9759 processSync(mapper);
9760
9761 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9763 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9764 ASSERT_EQ(1U, args.pointerCount);
9765 ASSERT_EQ(1, args.pointerProperties[0].id);
9766 ASSERT_NO_FATAL_FAILURE(
9767 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9768
9769 // FINGER 1 UP
9770 processId(mapper, -1);
9771 processKey(mapper, BTN_TOUCH, 0);
9772 processSync(mapper);
9773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9774 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9775
Harry Cutts16a24cc2022-10-26 15:22:19 +00009776 // A non captured touchpad should have a mouse and touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009777 mFakePolicy->setPointerCapture(false);
9778 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Harry Cutts16a24cc2022-10-26 15:22:19 +00009780 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009781}
9782
9783TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9784 std::shared_ptr<FakePointerController> fakePointerController =
9785 std::make_shared<FakePointerController>();
9786 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9787 fakePointerController->setPosition(0, 0);
9788 fakePointerController->setButtonState(0);
9789
9790 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009791 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009792 prepareAxes(POSITION | ID | SLOT);
9793 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9794 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009795 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009796 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9797 // run uncaptured pointer tests - pushes out generic events
9798 // FINGER 0 DOWN
9799 processId(mapper, 3);
9800 processPosition(mapper, 100, 100);
9801 processKey(mapper, BTN_TOUCH, 1);
9802 processSync(mapper);
9803
9804 // start at (100,100), cursor should be at (0,0) * scale
9805 NotifyMotionArgs args;
9806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9807 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9808 ASSERT_NO_FATAL_FAILURE(
9809 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9810
9811 // FINGER 0 MOVE
9812 processPosition(mapper, 200, 200);
9813 processSync(mapper);
9814
9815 // compute scaling to help with touch position checking
9816 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9817 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9818 float scale =
9819 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9820
9821 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9823 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9825 0, 0, 0, 0, 0, 0, 0));
9826}
9827
9828TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9829 std::shared_ptr<FakePointerController> fakePointerController =
9830 std::make_shared<FakePointerController>();
9831
Michael Wrighta9cf4192022-12-01 23:46:39 +00009832 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009833 prepareAxes(POSITION | ID | SLOT);
9834 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009835 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009836 mFakePolicy->setPointerCapture(false);
9837 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9838
Harry Cutts16a24cc2022-10-26 15:22:19 +00009839 // An uncaptured touchpad should be a pointer device, with additional touchpad source.
9840 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009841
Harry Cutts16a24cc2022-10-26 15:22:19 +00009842 // A captured touchpad should just have a touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009843 mFakePolicy->setPointerCapture(true);
9844 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9845 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9846}
9847
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009848// --- BluetoothMultiTouchInputMapperTest ---
9849
9850class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
9851protected:
9852 void SetUp() override {
9853 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
9854 }
9855};
9856
9857TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
9858 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009859 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009860 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9861 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9862
9863 nsecs_t kernelEventTime = ARBITRARY_TIME;
9864 nsecs_t expectedEventTime = ARBITRARY_TIME;
9865 // Touch down.
9866 processId(mapper, FIRST_TRACKING_ID);
9867 processPosition(mapper, 100, 200);
9868 processPressure(mapper, RAW_PRESSURE_MAX);
9869 processSync(mapper, ARBITRARY_TIME);
9870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9871 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
9872
9873 // Process several events that come in quick succession, according to their timestamps.
9874 for (int i = 0; i < 3; i++) {
9875 constexpr static nsecs_t delta = ms2ns(1);
9876 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
9877 kernelEventTime += delta;
9878 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
9879
9880 processPosition(mapper, 101 + i, 201 + i);
9881 processSync(mapper, kernelEventTime);
9882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9883 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9884 WithEventTime(expectedEventTime))));
9885 }
9886
9887 // Release the touch.
9888 processId(mapper, INVALID_TRACKING_ID);
9889 processPressure(mapper, RAW_PRESSURE_MIN);
9890 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
9891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9892 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9893 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
9894}
9895
9896// --- MultiTouchPointerModeTest ---
9897
HQ Liue6983c72022-04-19 22:14:56 +00009898class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9899protected:
9900 float mPointerMovementScale;
9901 float mPointerXZoomScale;
9902 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9903 addConfigurationProperty("touch.deviceType", "pointer");
9904 std::shared_ptr<FakePointerController> fakePointerController =
9905 std::make_shared<FakePointerController>();
9906 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9907 fakePointerController->setPosition(0, 0);
9908 fakePointerController->setButtonState(0);
Michael Wrighta9cf4192022-12-01 23:46:39 +00009909 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +00009910
9911 prepareAxes(POSITION);
9912 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9913 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9914 // needs to be disabled, and the pointer gesture needs to be enabled.
9915 mFakePolicy->setPointerCapture(false);
9916 mFakePolicy->setPointerGestureEnabled(true);
9917 mFakePolicy->setPointerController(fakePointerController);
9918
9919 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9920 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9921 mPointerMovementScale =
9922 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9923 mPointerXZoomScale =
9924 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9925 }
9926
9927 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9928 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9929 /*flat*/ 0,
9930 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9931 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9932 /*flat*/ 0,
9933 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9934 }
9935};
9936
9937/**
9938 * Two fingers down on a pointer mode touch pad. The width
9939 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9940 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9941 * be greater than the both value to be freeform gesture, so that after two
9942 * fingers start to move downwards, the gesture should be swipe.
9943 */
9944TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9945 // The min freeform gesture width is 25units/mm x 30mm = 750
9946 // which is greater than fraction of the diagnal length of the touchpad (349).
9947 // Thus, MaxSwipWidth is 750.
9948 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9949 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9950 NotifyMotionArgs motionArgs;
9951
9952 // Two fingers down at once.
9953 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9954 // Pointer's initial position is used the [0,0] coordinate.
9955 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9956
9957 processId(mapper, FIRST_TRACKING_ID);
9958 processPosition(mapper, x1, y1);
9959 processMTSync(mapper);
9960 processId(mapper, SECOND_TRACKING_ID);
9961 processPosition(mapper, x2, y2);
9962 processMTSync(mapper);
9963 processSync(mapper);
9964
9965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9966 ASSERT_EQ(1U, motionArgs.pointerCount);
9967 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9968 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009969 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009970 ASSERT_NO_FATAL_FAILURE(
9971 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9972
9973 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9974 // that there should be 1 pointer.
9975 int32_t movingDistance = 200;
9976 y1 += movingDistance;
9977 y2 += movingDistance;
9978
9979 processId(mapper, FIRST_TRACKING_ID);
9980 processPosition(mapper, x1, y1);
9981 processMTSync(mapper);
9982 processId(mapper, SECOND_TRACKING_ID);
9983 processPosition(mapper, x2, y2);
9984 processMTSync(mapper);
9985 processSync(mapper);
9986
9987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9988 ASSERT_EQ(1U, motionArgs.pointerCount);
9989 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9990 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009991 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009992 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9993 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9994 0, 0, 0, 0));
9995}
9996
9997/**
9998 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9999 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10000 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10001 * value to be freeform gesture, so that after two fingers start to move downwards,
10002 * the gesture should be swipe.
10003 */
10004TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10005 // The min freeform gesture width is 5units/mm x 30mm = 150
10006 // which is greater than fraction of the diagnal length of the touchpad (349).
10007 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
10008 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
10009 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10010 NotifyMotionArgs motionArgs;
10011
10012 // Two fingers down at once.
10013 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10014 // Pointer's initial position is used the [0,0] coordinate.
10015 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10016
10017 processId(mapper, FIRST_TRACKING_ID);
10018 processPosition(mapper, x1, y1);
10019 processMTSync(mapper);
10020 processId(mapper, SECOND_TRACKING_ID);
10021 processPosition(mapper, x2, y2);
10022 processMTSync(mapper);
10023 processSync(mapper);
10024
10025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10026 ASSERT_EQ(1U, motionArgs.pointerCount);
10027 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10028 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010029 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010030 ASSERT_NO_FATAL_FAILURE(
10031 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10032
10033 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10034 // and there should be 1 pointer.
10035 int32_t movingDistance = 200;
10036 y1 += movingDistance;
10037 y2 += movingDistance;
10038
10039 processId(mapper, FIRST_TRACKING_ID);
10040 processPosition(mapper, x1, y1);
10041 processMTSync(mapper);
10042 processId(mapper, SECOND_TRACKING_ID);
10043 processPosition(mapper, x2, y2);
10044 processMTSync(mapper);
10045 processSync(mapper);
10046
10047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10048 ASSERT_EQ(1U, motionArgs.pointerCount);
10049 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10050 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010051 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010052 // New coordinate is the scaled relative coordinate from the initial coordinate.
10053 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10054 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10055 0, 0, 0, 0));
10056}
10057
10058/**
10059 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10060 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10061 * freeform gestures after two fingers start to move downwards.
10062 */
10063TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
10064 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10065 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10066
10067 NotifyMotionArgs motionArgs;
10068
10069 // Two fingers down at once. Wider than the max swipe width.
10070 // The gesture is expected to be PRESS, then transformed to FREEFORM
10071 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10072
10073 processId(mapper, FIRST_TRACKING_ID);
10074 processPosition(mapper, x1, y1);
10075 processMTSync(mapper);
10076 processId(mapper, SECOND_TRACKING_ID);
10077 processPosition(mapper, x2, y2);
10078 processMTSync(mapper);
10079 processSync(mapper);
10080
10081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10082 ASSERT_EQ(1U, motionArgs.pointerCount);
10083 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10084 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010085 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010086 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10087 ASSERT_NO_FATAL_FAILURE(
10088 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10089
10090 int32_t movingDistance = 200;
10091
10092 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10093 // then two down events for two pointers.
10094 y1 += movingDistance;
10095 y2 += movingDistance;
10096
10097 processId(mapper, FIRST_TRACKING_ID);
10098 processPosition(mapper, x1, y1);
10099 processMTSync(mapper);
10100 processId(mapper, SECOND_TRACKING_ID);
10101 processPosition(mapper, x2, y2);
10102 processMTSync(mapper);
10103 processSync(mapper);
10104
10105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10106 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10107 ASSERT_EQ(1U, motionArgs.pointerCount);
10108 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10110 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10111 ASSERT_EQ(1U, motionArgs.pointerCount);
10112 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10114 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010115 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010116 ASSERT_EQ(2U, motionArgs.pointerCount);
10117 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10118 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010119 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010120 // Two pointers' scaled relative coordinates from their initial centroid.
10121 // Initial y coordinates are 0 as y1 and y2 have the same value.
10122 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10123 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10124 // When pointers move, the new coordinates equal to the initial coordinates plus
10125 // scaled moving distance.
10126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10127 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10128 0, 0, 0, 0));
10129 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10130 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10131 0, 0, 0, 0));
10132
10133 // Move two fingers down again, expect one MOVE motion event.
10134 y1 += movingDistance;
10135 y2 += movingDistance;
10136
10137 processId(mapper, FIRST_TRACKING_ID);
10138 processPosition(mapper, x1, y1);
10139 processMTSync(mapper);
10140 processId(mapper, SECOND_TRACKING_ID);
10141 processPosition(mapper, x2, y2);
10142 processMTSync(mapper);
10143 processSync(mapper);
10144
10145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10146 ASSERT_EQ(2U, motionArgs.pointerCount);
10147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10148 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010149 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010150 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10151 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10152 0, 0, 0, 0, 0));
10153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10154 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10155 0, 0, 0, 0, 0));
10156}
10157
Harry Cutts39b7ca22022-10-05 15:55:48 +000010158TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
10159 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10160 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10161 NotifyMotionArgs motionArgs;
10162
10163 // Place two fingers down.
10164 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10165
10166 processId(mapper, FIRST_TRACKING_ID);
10167 processPosition(mapper, x1, y1);
10168 processMTSync(mapper);
10169 processId(mapper, SECOND_TRACKING_ID);
10170 processPosition(mapper, x2, y2);
10171 processMTSync(mapper);
10172 processSync(mapper);
10173
10174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10175 ASSERT_EQ(1U, motionArgs.pointerCount);
10176 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10177 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10178 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10179 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10180
10181 // Move the two fingers down and to the left.
10182 int32_t movingDistance = 200;
10183 x1 -= movingDistance;
10184 y1 += movingDistance;
10185 x2 -= movingDistance;
10186 y2 += movingDistance;
10187
10188 processId(mapper, FIRST_TRACKING_ID);
10189 processPosition(mapper, x1, y1);
10190 processMTSync(mapper);
10191 processId(mapper, SECOND_TRACKING_ID);
10192 processPosition(mapper, x2, y2);
10193 processMTSync(mapper);
10194 processSync(mapper);
10195
10196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10197 ASSERT_EQ(1U, motionArgs.pointerCount);
10198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10199 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10200 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10201 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10202}
10203
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010204TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
10205 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10206 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10207 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10209
10210 // Start a stylus gesture.
10211 processKey(mapper, BTN_TOOL_PEN, 1);
10212 processId(mapper, FIRST_TRACKING_ID);
10213 processPosition(mapper, 100, 200);
10214 processSync(mapper);
10215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10216 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10217 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10218 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10219 // TODO(b/257078296): Pointer mode generates extra event.
10220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10221 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10222 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10223 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10225
10226 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10227 // gesture should be disabled.
10228 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10229 viewport->isActive = false;
10230 mFakePolicy->updateViewport(*viewport);
10231 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10233 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10234 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10235 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10236 // TODO(b/257078296): Pointer mode generates extra event.
10237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10238 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10239 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10240 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10242}
10243
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010244// --- JoystickInputMapperTest ---
10245
10246class JoystickInputMapperTest : public InputMapperTest {
10247protected:
10248 static const int32_t RAW_X_MIN;
10249 static const int32_t RAW_X_MAX;
10250 static const int32_t RAW_Y_MIN;
10251 static const int32_t RAW_Y_MAX;
10252
10253 void SetUp() override {
10254 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10255 }
10256 void prepareAxes() {
10257 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10258 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10259 }
10260
10261 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10262 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10263 }
10264
10265 void processSync(JoystickInputMapper& mapper) {
10266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10267 }
10268
Michael Wrighta9cf4192022-12-01 23:46:39 +000010269 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010270 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10271 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10272 NO_PORT, ViewportType::VIRTUAL);
10273 }
10274};
10275
10276const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10277const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10278const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10279const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10280
10281TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10282 prepareAxes();
10283 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10284
10285 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10286
Michael Wrighta9cf4192022-12-01 23:46:39 +000010287 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010288
10289 // Send an axis event
10290 processAxis(mapper, ABS_X, 100);
10291 processSync(mapper);
10292
10293 NotifyMotionArgs args;
10294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10295 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10296
10297 // Send another axis event
10298 processAxis(mapper, ABS_Y, 100);
10299 processSync(mapper);
10300
10301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10302 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10303}
10304
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010305// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010306
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010307class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010308protected:
10309 static const char* DEVICE_NAME;
10310 static const char* DEVICE_LOCATION;
10311 static const int32_t DEVICE_ID;
10312 static const int32_t DEVICE_GENERATION;
10313 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010314 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010315 static const int32_t EVENTHUB_ID;
10316
10317 std::shared_ptr<FakeEventHub> mFakeEventHub;
10318 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010319 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010320 std::unique_ptr<InstrumentedInputReader> mReader;
10321 std::shared_ptr<InputDevice> mDevice;
10322
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010323 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010324 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010325 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010326 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010327 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010328 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010329 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10330 }
10331
10332 void SetUp() override { SetUp(DEVICE_CLASSES); }
10333
10334 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010335 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010336 mFakePolicy.clear();
10337 }
10338
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010339 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010340 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10341 mReader->requestRefreshConfiguration(changes);
10342 mReader->loopOnce();
10343 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010344 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010345 }
10346
10347 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10348 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010349 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010350 InputDeviceIdentifier identifier;
10351 identifier.name = name;
10352 identifier.location = location;
10353 std::shared_ptr<InputDevice> device =
10354 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10355 identifier);
10356 mReader->pushNextDevice(device);
10357 mFakeEventHub->addDevice(eventHubId, name, classes);
10358 mReader->loopOnce();
10359 return device;
10360 }
10361
10362 template <class T, typename... Args>
10363 T& addControllerAndConfigure(Args... args) {
10364 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10365
10366 return controller;
10367 }
10368};
10369
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010370const char* PeripheralControllerTest::DEVICE_NAME = "device";
10371const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10372const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10373const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10374const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010375const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10376 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010377const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010378
10379// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010380class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010381protected:
10382 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010383 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010384 }
10385};
10386
10387TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010388 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010389
Harry Cuttsa5b71292022-11-28 12:56:17 +000010390 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10391 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10392 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010393}
10394
10395TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010396 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010397
Harry Cuttsa5b71292022-11-28 12:56:17 +000010398 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10399 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10400 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010401}
10402
10403// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010404class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010405protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010406 void SetUp() override {
10407 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10408 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010409};
10410
Chris Ye85758332021-05-16 23:05:17 -070010411TEST_F(LightControllerTest, MonoLight) {
10412 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010413 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010414 .maxBrightness = 255,
10415 .flags = InputLightClass::BRIGHTNESS,
10416 .path = ""};
10417 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010418
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010419 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010420 InputDeviceInfo info;
10421 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010422 std::vector<InputDeviceLightInfo> lights = info.getLights();
10423 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010424 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10425 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10426
10427 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10428 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10429}
10430
10431TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10432 RawLightInfo infoMono = {.id = 1,
10433 .name = "mono_keyboard_backlight",
10434 .maxBrightness = 255,
10435 .flags = InputLightClass::BRIGHTNESS |
10436 InputLightClass::KEYBOARD_BACKLIGHT,
10437 .path = ""};
10438 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10439
10440 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10441 InputDeviceInfo info;
10442 controller.populateDeviceInfo(&info);
10443 std::vector<InputDeviceLightInfo> lights = info.getLights();
10444 ASSERT_EQ(1U, lights.size());
10445 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10446 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010447
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010448 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10449 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010450}
10451
10452TEST_F(LightControllerTest, RGBLight) {
10453 RawLightInfo infoRed = {.id = 1,
10454 .name = "red",
10455 .maxBrightness = 255,
10456 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10457 .path = ""};
10458 RawLightInfo infoGreen = {.id = 2,
10459 .name = "green",
10460 .maxBrightness = 255,
10461 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10462 .path = ""};
10463 RawLightInfo infoBlue = {.id = 3,
10464 .name = "blue",
10465 .maxBrightness = 255,
10466 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10467 .path = ""};
10468 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10469 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10470 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10471
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010472 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010473 InputDeviceInfo info;
10474 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010475 std::vector<InputDeviceLightInfo> lights = info.getLights();
10476 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010477 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10478 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10479 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10480
10481 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10482 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10483}
10484
10485TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10486 RawLightInfo infoRed = {.id = 1,
10487 .name = "red_keyboard_backlight",
10488 .maxBrightness = 255,
10489 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10490 InputLightClass::KEYBOARD_BACKLIGHT,
10491 .path = ""};
10492 RawLightInfo infoGreen = {.id = 2,
10493 .name = "green_keyboard_backlight",
10494 .maxBrightness = 255,
10495 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10496 InputLightClass::KEYBOARD_BACKLIGHT,
10497 .path = ""};
10498 RawLightInfo infoBlue = {.id = 3,
10499 .name = "blue_keyboard_backlight",
10500 .maxBrightness = 255,
10501 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10502 InputLightClass::KEYBOARD_BACKLIGHT,
10503 .path = ""};
10504 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10505 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10506 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10507
10508 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10509 InputDeviceInfo info;
10510 controller.populateDeviceInfo(&info);
10511 std::vector<InputDeviceLightInfo> lights = info.getLights();
10512 ASSERT_EQ(1U, lights.size());
10513 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10514 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10515 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10516
10517 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10518 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10519}
10520
10521TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10522 RawLightInfo infoRed = {.id = 1,
10523 .name = "red",
10524 .maxBrightness = 255,
10525 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10526 .path = ""};
10527 RawLightInfo infoGreen = {.id = 2,
10528 .name = "green",
10529 .maxBrightness = 255,
10530 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10531 .path = ""};
10532 RawLightInfo infoBlue = {.id = 3,
10533 .name = "blue",
10534 .maxBrightness = 255,
10535 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10536 .path = ""};
10537 RawLightInfo infoGlobal = {.id = 3,
10538 .name = "global_keyboard_backlight",
10539 .maxBrightness = 255,
10540 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10541 InputLightClass::KEYBOARD_BACKLIGHT,
10542 .path = ""};
10543 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10544 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10545 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10546 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10547
10548 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10549 InputDeviceInfo info;
10550 controller.populateDeviceInfo(&info);
10551 std::vector<InputDeviceLightInfo> lights = info.getLights();
10552 ASSERT_EQ(1U, lights.size());
10553 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10554 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10555 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010556
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010557 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10558 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010559}
10560
10561TEST_F(LightControllerTest, MultiColorRGBLight) {
10562 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010563 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010564 .maxBrightness = 255,
10565 .flags = InputLightClass::BRIGHTNESS |
10566 InputLightClass::MULTI_INTENSITY |
10567 InputLightClass::MULTI_INDEX,
10568 .path = ""};
10569
10570 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10571
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010572 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010573 InputDeviceInfo info;
10574 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010575 std::vector<InputDeviceLightInfo> lights = info.getLights();
10576 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010577 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10578 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10579 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10580
10581 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10582 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10583}
10584
10585TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10586 RawLightInfo infoColor = {.id = 1,
10587 .name = "multi_color_keyboard_backlight",
10588 .maxBrightness = 255,
10589 .flags = InputLightClass::BRIGHTNESS |
10590 InputLightClass::MULTI_INTENSITY |
10591 InputLightClass::MULTI_INDEX |
10592 InputLightClass::KEYBOARD_BACKLIGHT,
10593 .path = ""};
10594
10595 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10596
10597 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10598 InputDeviceInfo info;
10599 controller.populateDeviceInfo(&info);
10600 std::vector<InputDeviceLightInfo> lights = info.getLights();
10601 ASSERT_EQ(1U, lights.size());
10602 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10603 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10604 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010605
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010606 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10607 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010608}
10609
10610TEST_F(LightControllerTest, PlayerIdLight) {
10611 RawLightInfo info1 = {.id = 1,
10612 .name = "player1",
10613 .maxBrightness = 255,
10614 .flags = InputLightClass::BRIGHTNESS,
10615 .path = ""};
10616 RawLightInfo info2 = {.id = 2,
10617 .name = "player2",
10618 .maxBrightness = 255,
10619 .flags = InputLightClass::BRIGHTNESS,
10620 .path = ""};
10621 RawLightInfo info3 = {.id = 3,
10622 .name = "player3",
10623 .maxBrightness = 255,
10624 .flags = InputLightClass::BRIGHTNESS,
10625 .path = ""};
10626 RawLightInfo info4 = {.id = 4,
10627 .name = "player4",
10628 .maxBrightness = 255,
10629 .flags = InputLightClass::BRIGHTNESS,
10630 .path = ""};
10631 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10632 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10633 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10634 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10635
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010636 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010637 InputDeviceInfo info;
10638 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010639 std::vector<InputDeviceLightInfo> lights = info.getLights();
10640 ASSERT_EQ(1U, lights.size());
10641 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010642 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10643 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010644
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010645 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10646 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10647 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010648}
10649
Michael Wrightd02c5b62014-02-10 15:10:22 -080010650} // namespace android